Home telegram-peerInfoPaneContainerNode
Post
Cancel

telegram-peerInfoPaneContainerNode

PeerInfoPaneContainerNode

class PeerInfoPaneWrapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// the associated key
let key: PeerInfoPaneKey
// the wrapped node
let node: PeerInfoPaneNode
var isAnimatingOut: Bool = false

// applied layout info
private var appliedParams: (CGSize, CGFloat, CGFloat, CGFloat, CGFloat, Bool, CGFloat, PresentationData)?

init(key: PeerInfoPaneKey, node: PeerInfoPaneNode) {
    self.key = key
    self.node = node
}

// update layout
func update(size: CGSize, topInset: CGFloat, sideInset: CGFloat, bottomInset: CGFloat, visibleHeight: CGFloat, isScrollingLockedAtTop: Bool, expandProgress: CGFloat, presentationData: PresentationData, synchronous: Bool, transition: ContainedViewLayoutTransition) {
    if let (currentSize, currentTopInset, currentSideInset, currentBottomInset, _, currentIsScrollingLockedAtTop, currentExpandProgress, currentPresentationData) = self.appliedParams {
        if currentSize == size && currentTopInset == topInset, currentSideInset == sideInset && currentBottomInset == bottomInset, currentIsScrollingLockedAtTop == isScrollingLockedAtTop && currentExpandProgress == expandProgress && currentPresentationData === presentationData {
            return
        }
    }
    self.appliedParams = (size, topInset, sideInset, bottomInset, visibleHeight, isScrollingLockedAtTop, expandProgress, presentationData)
    self.node.update(size: size, topInset: topInset, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, isScrollingLockedAtTop: isScrollingLockedAtTop, expandProgress: expandProgress, presentationData: presentationData, synchronous: synchronous, transition: transition)
}

class PeerInfoPendingPane

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
let pane: PeerInfoPaneWrapper
private var disposable: Disposable?
var isReady: Bool = false

init(
    context: AccountContext,
    updatedPresentationData: (initial: PresentationData, signal: Signal<PresentationData, NoError>)?,
    chatControllerInteraction: ChatControllerInteraction,
    data: PeerInfoScreenData,
    openPeerContextAction: @escaping (Peer, ASDisplayNode, ContextGesture?) -> Void,
    openAddMemberAction: @escaping () -> Void,
    requestPerformPeerMemberAction: @escaping (PeerInfoMember, PeerMembersListAction) -> Void,
    peerId: PeerId,
    key: PeerInfoPaneKey,
    hasBecomeReady: @escaping (PeerInfoPaneKey) -> Void,
    parentController: ViewController?,
    openMediaCalendar: @escaping () -> Void,
    paneDidScroll: @escaping () -> Void
) {
    let captureProtected = data.peer?.isCopyProtectionEnabled ?? false
    let paneNode: PeerInfoPaneNode
    switch key {
    case .media:
        let visualPaneNode = PeerInfoVisualMediaPaneNode(context: context, chatControllerInteraction: chatControllerInteraction, peerId: peerId, contentType: .photoOrVideo, captureProtected: captureProtected)
        paneNode = visualPaneNode
        visualPaneNode.openCurrentDate = {
            openMediaCalendar()
        }
        visualPaneNode.paneDidScroll = {
            paneDidScroll()
        }
    case .files:
        let visualPaneNode = PeerInfoVisualMediaPaneNode(context: context, chatControllerInteraction: chatControllerInteraction, peerId: peerId, contentType: .files, captureProtected: captureProtected)
        paneNode = visualPaneNode
        //paneNode = PeerInfoListPaneNode(context: context, updatedPresentationData: updatedPresentationData, chatControllerInteraction: chatControllerInteraction, peerId: peerId, tagMask: .file)
    case .links:
        paneNode = PeerInfoListPaneNode(context: context, updatedPresentationData: updatedPresentationData, chatControllerInteraction: chatControllerInteraction, peerId: peerId, tagMask: .webPage)
    case .voice:
        let visualPaneNode = PeerInfoVisualMediaPaneNode(context: context, chatControllerInteraction: chatControllerInteraction, peerId: peerId, contentType: .voiceAndVideoMessages, captureProtected: captureProtected)
        paneNode = visualPaneNode
        //paneNode = PeerInfoListPaneNode(context: context, updatedPresentationData: updatedPresentationData, chatControllerInteraction: chatControllerInteraction, peerId: peerId, tagMask: .voiceOrInstantVideo)
    case .music:
        let visualPaneNode = PeerInfoVisualMediaPaneNode(context: context, chatControllerInteraction: chatControllerInteraction, peerId: peerId, contentType: .music, captureProtected: captureProtected)
        paneNode = visualPaneNode
        //paneNode = PeerInfoListPaneNode(context: context, updatedPresentationData: updatedPresentationData, chatControllerInteraction: chatControllerInteraction, peerId: peerId, tagMask: .music)
    case .gifs:
      // paneNode = PeerInfoGifPaneNode(context: context, chatControllerInteraction: chatControllerInteraction, peerId: peerId, contentType: .gifs)
        let visualPaneNode = PeerInfoGifPaneNode(context: context, chatControllerInteraction: chatControllerInteraction, peerId: peerId, contentType: .gifs)
        paneNode = visualPaneNode
    case .groupsInCommon:
      // paneNode = PeerInfoGroupsInCommonPaneNode(context: context, peerId: peerId, chatControllerInteraction: chatControllerInteraction, openPeerContextAction: openPeerContextAction, groupsInCommonContext: data.groupsInCommon!)
        paneNode = PeerInfoGroupsInCommonPaneNode(context: context, peerId: peerId, chatControllerInteraction: chatControllerInteraction, openPeerContextAction: openPeerContextAction, groupsInCommonContext: data.groupsInCommon!)
    case .members:
        if case let .longList(membersContext) = data.members {
          /*
          paneNode = PeerInfoMembersPaneNode(context: context, peerId: peerId, membersContext: membersContext, addMemberAction: {
                openAddMemberAction()
            }, action: { member, action in
                requestPerformPeerMemberAction(member, action)
            })
          */ 
            paneNode = PeerInfoMembersPaneNode(context: context, peerId: peerId, membersContext: membersContext, addMemberAction: {
                openAddMemberAction()
            }, action: { member, action in
                requestPerformPeerMemberAction(member, action)
            })
        } else {
          // assert failure
            preconditionFailure()
        }
    }
    paneNode.parentController = parentController
    self.pane = PeerInfoPaneWrapper(key: key, node: paneNode)
    self.disposable = (paneNode.isReady
    |> take(1)
    |> deliverOnMainQueue).start(next: { [weak self] _ in
        self?.isReady = true
        hasBecomeReady(key)
    })
}	

class PeerInfoPaneContainerNode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
private let context: AccountContext
private let peerId: PeerId
private let isMediaOnly: Bool

weak var parentController: ViewController?
private let coveringBackgroundNode: NavigationBackgroundNode
private let separatorNode: ASDisplayNode
// tabs container node
private let tabsContainerNode: PeerInfoPaneTabsContainerNode
// tabs separator node
private let tabsSeparatorNode: ASDisplayNode

let isReady = Promise<Bool>()
var didSetIsReady = false

// current params
private var currentParams: (size: CGSize, sideInset: CGFloat, bottomInset: CGFloat, visibleHeight: CGFloat, expansionFraction: CGFloat, presentationData: PresentationData, data: PeerInfoScreenData?)?

// current pane key
private(set) var currentPaneKey: PeerInfoPaneKey?

// pending switch to pane key
var pendingSwitchToPaneKey: PeerInfoPaneKey?

// expand on switch
var expandOnSwitch = false

// current pane
var currentPane: PeerInfoPaneWrapper? {
    if let currentPaneKey = self.currentPaneKey {
        return self.currentPanes[currentPaneKey]
    } else {
        return nil
    }
}

// current pane status
private let currentPaneStatusPromise = Promise<PeerInfoStatusData?>(nil)
var currentPaneStatus: Signal<PeerInfoStatusData?, NoError> {
    return self.currentPaneStatusPromise.get()
}

// current panels
private var currentPanes: [PeerInfoPaneKey: PeerInfoPaneWrapper] = [:]

// pending panes
private var pendingPanes: [PeerInfoPaneKey: PeerInfoPendingPane] = [:]

// should fade in
private var shouldFadeIn = false

// transiton fraction
private var transitionFraction: CGFloat = 0.0

// selection pane node
var selectionPanelNode: PeerInfoSelectionPanelNode?

var chatControllerInteraction: ChatControllerInteraction?
// open peer context action
var openPeerContextAction: ((Peer, ASDisplayNode, ContextGesture?) -> Void)?

// open add members action
var openAddMemberAction: (() -> Void)?

// request perform peer member action
var requestPerformPeerMemberAction: ((PeerInfoMember, PeerMembersListAction) -> Void)?

// current pane updated
var currentPaneUpdated: ((Bool) -> Void)?

// request expand tabs
var requestExpandTabs: (() -> Bool)?

// open media calendar
var openMediaCalendar: (() -> Void)?

// pane did scroll
var paneDidScroll: (() -> Void)?

// current available panes
private var currentAvailablePanes: [PeerInfoPaneKey]?

// update presentationData
private let updatedPresentationData: (initial: PresentationData, signal: Signal<PresentationData, NoError>)?

init(context: AccountContext, updatedPresentationData: (initial: PresentationData, signal: Signal<PresentationData, NoError>)?, peerId: PeerId, isMediaOnly: Bool) {
    self.context = context
    self.updatedPresentationData = updatedPresentationData
    self.peerId = peerId
    self.isMediaOnly = isMediaOnly

    self.separatorNode = ASDisplayNode()
    self.separatorNode.isLayerBacked = true

    self.coveringBackgroundNode = NavigationBackgroundNode(color: .clear)
    self.coveringBackgroundNode.isUserInteractionEnabled = false

    self.tabsContainerNode = PeerInfoPaneTabsContainerNode()

    self.tabsSeparatorNode = ASDisplayNode()
    self.tabsSeparatorNode.isLayerBacked = true

    super.init()

//        self.addSubnode(self.separatorNode)
    self.addSubnode(self.coveringBackgroundNode)
    self.addSubnode(self.tabsContainerNode)
    self.addSubnode(self.tabsSeparatorNode)

  // request select pane
    self.tabsContainerNode.requestSelectPane = { [weak self] key in
        guard let strongSelf = self else {
            return
        }
        if strongSelf.currentPaneKey == key {
            if let requestExpandTabs = strongSelf.requestExpandTabs, requestExpandTabs() {
            } else {
                let _ = strongSelf.currentPane?.node.scrollToTop()
            }
            return
        }
        if strongSelf.currentPanes[key] != nil {
            strongSelf.currentPaneKey = key


            if let (size, sideInset, bottomInset, visibleHeight, expansionFraction, presentationData, data) = strongSelf.currentParams {
                strongSelf.update(size: size, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, expansionFraction: expansionFraction, presentationData: presentationData, data: data, transition: .animated(duration: 0.4, curve: .spring))

                strongSelf.currentPaneUpdated?(true)

                strongSelf.currentPaneStatusPromise.set(strongSelf.currentPane?.node.status ?? .single(nil))
            }
        } else if strongSelf.pendingSwitchToPaneKey != key {
            strongSelf.pendingSwitchToPaneKey = key
            strongSelf.expandOnSwitch = true

            if let (size, sideInset, bottomInset, visibleHeight, expansionFraction, presentationData, data) = strongSelf.currentParams {
                strongSelf.update(size: size, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, expansionFraction: expansionFraction, presentationData: presentationData, data: data, transition: .animated(duration: 0.4, curve: .spring))
            }
        }
    }
}

override func didLoad() {
    super.didLoad()

  // pan recognizer
    let panRecognizer = InteractiveTransitionGestureRecognizer(target: self, action: #selector(self.panGesture(_:)), allowedDirections: { [weak self] point in
        guard let strongSelf = self, let currentPaneKey = strongSelf.currentPaneKey, let availablePanes = strongSelf.currentParams?.data?.availablePanes, let index = availablePanes.firstIndex(of: currentPaneKey) else {
            return []
        }
        if strongSelf.tabsContainerNode.bounds.contains(strongSelf.view.convert(point, to: strongSelf.tabsContainerNode.view)) {
            return []
        }
        if index == 0 {
            return .left
        }
        return [.left, .right]
    })
    panRecognizer.delegate = self
    panRecognizer.delaysTouchesBegan = false
    panRecognizer.cancelsTouchesInView = true
    self.view.addGestureRecognizer(panRecognizer)
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return false
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if let _ = otherGestureRecognizer as? InteractiveTransitionGestureRecognizer {
        return false
    }
    if let _ = otherGestureRecognizer as? UIPanGestureRecognizer {
        return true
    }
    return false
}

@objc private func panGesture(_ recognizer: UIPanGestureRecognizer) {
    switch recognizer.state {
    case .began:
        func cancelContextGestures(view: UIView) {
            if let gestureRecognizers = view.gestureRecognizers {
                for gesture in gestureRecognizers {
                    if let gesture = gesture as? ContextGesture {
                        gesture.cancel()
                    }
                }
            }
            for subview in view.subviews {
                cancelContextGestures(view: subview)
            }
        }

        cancelContextGestures(view: self.view)
    case .changed:
        if let (size, sideInset, bottomInset, visibleHeight, expansionFraction, presentationData, data) = self.currentParams, let availablePanes = data?.availablePanes, availablePanes.count > 1, let currentPaneKey = self.currentPaneKey, let currentIndex = availablePanes.firstIndex(of: currentPaneKey) {
            let translation = recognizer.translation(in: self.view)
            var transitionFraction = translation.x / size.width
            if currentIndex <= 0 {
                transitionFraction = min(0.0, transitionFraction)
            }
            if currentIndex >= availablePanes.count - 1 {
                transitionFraction = max(0.0, transitionFraction)
            }
            self.transitionFraction = transitionFraction
            self.update(size: size, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, expansionFraction: expansionFraction, presentationData: presentationData, data: data, transition: .immediate)
        }
    case .cancelled, .ended:
        if let (size, sideInset, bottomInset, visibleHeight, expansionFraction, presentationData, data) = self.currentParams, let availablePanes = data?.availablePanes, availablePanes.count > 1, let currentPaneKey = self.currentPaneKey, let currentIndex = availablePanes.firstIndex(of: currentPaneKey) {
            let translation = recognizer.translation(in: self.view)
            let velocity = recognizer.velocity(in: self.view)
            var directionIsToRight: Bool?
            if abs(velocity.x) > 10.0 {
                directionIsToRight = velocity.x < 0.0
            } else {
                if abs(translation.x) > size.width / 2.0 {
                    directionIsToRight = translation.x > size.width / 2.0
                }
            }
            if let directionIsToRight = directionIsToRight {
                var updatedIndex = currentIndex
                if directionIsToRight {
                    updatedIndex = min(updatedIndex + 1, availablePanes.count - 1)
                } else {
                    updatedIndex = max(updatedIndex - 1, 0)
                }
                let switchToKey = availablePanes[updatedIndex]
                if switchToKey != self.currentPaneKey && self.currentPanes[switchToKey] != nil{
                    self.currentPaneKey = switchToKey
                }
            }
            self.transitionFraction = 0.0
            self.update(size: size, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, expansionFraction: expansionFraction, presentationData: presentationData, data: data, transition: .animated(duration: 0.35, curve: .spring))
            self.currentPaneUpdated?(false)

            self.currentPaneStatusPromise.set(self.currentPane?.node.status ?? .single(nil))
        }
    default:
        break
    }
}

// scroll to top
func scrollToTop() -> Bool {
    if let currentPane = self.currentPane {
        return currentPane.node.scrollToTop()
    } else {
        return false
    }
}

// find loaded message
func findLoadedMessage(id: MessageId) -> Message? {
    return self.currentPane?.node.findLoadedMessage(id: id)
}

// update media hidden
func updateHiddenMedia() {
    self.currentPane?.node.updateHiddenMedia()
}

// transition node for gallery
func transitionNodeForGallery(messageId: MessageId, media: Media) -> (ASDisplayNode, CGRect, () -> (UIView?, UIView?))? {
    return self.currentPane?.node.transitionNodeForGallery(messageId: messageId, media: media)
}

// update selected message ids
func updateSelectedMessageIds(_ selectedMessageIds: Set<MessageId>?, animated: Bool) {
    for (_, pane) in self.currentPanes {
        pane.node.updateSelectedMessages(animated: animated)
    }
    for (_, pane) in self.pendingPanes {
        pane.pane.node.updateSelectedMessages(animated: animated)
    }
}

// update data & layout
func update(size: CGSize, sideInset: CGFloat, bottomInset: CGFloat, visibleHeight: CGFloat, expansionFraction: CGFloat, presentationData: PresentationData, data: PeerInfoScreenData?, transition: ContainedViewLayoutTransition) {
    let previousAvailablePanes = self.currentAvailablePanes
    let availablePanes = data?.availablePanes ?? []
    self.currentAvailablePanes = data?.availablePanes

    let previousPaneKeys = Set<PeerInfoPaneKey>(self.currentPanes.keys)

    let previousCurrentPaneKey = self.currentPaneKey
    var updateCurrentPaneStatus = false

    if let currentPaneKey = self.currentPaneKey, !availablePanes.contains(currentPaneKey) {
        var nextCandidatePaneKey: PeerInfoPaneKey?
        if let previousAvailablePanes = previousAvailablePanes, let index = previousAvailablePanes.firstIndex(of: currentPaneKey), index != 0 {
            for i in (0 ... index - 1).reversed() {
                if availablePanes.contains(previousAvailablePanes[i]) {
                    nextCandidatePaneKey = previousAvailablePanes[i]
                }
            }
        }
        if nextCandidatePaneKey == nil {
            nextCandidatePaneKey = availablePanes.first
        }

        if let nextCandidatePaneKey = nextCandidatePaneKey {
            self.pendingSwitchToPaneKey = nextCandidatePaneKey
        } else {
            self.currentPaneKey = nil
            self.pendingSwitchToPaneKey = nil
        }
    } else if self.currentPaneKey == nil {
        self.pendingSwitchToPaneKey = availablePanes.first
    }

    let currentIndex: Int?
    if let currentPaneKey = self.currentPaneKey {
        currentIndex = availablePanes.firstIndex(of: currentPaneKey)
    } else {
        currentIndex = nil
    }

    self.currentParams = (size, sideInset, bottomInset, visibleHeight, expansionFraction, presentationData, data)

    transition.updateAlpha(node: self.coveringBackgroundNode, alpha: expansionFraction)

    self.backgroundColor = presentationData.theme.list.itemBlocksBackgroundColor
    self.coveringBackgroundNode.updateColor(color: presentationData.theme.rootController.navigationBar.opaqueBackgroundColor, transition: .immediate)
    self.separatorNode.backgroundColor = presentationData.theme.list.itemBlocksSeparatorColor
    self.tabsSeparatorNode.backgroundColor = presentationData.theme.list.itemBlocksSeparatorColor

    let isScrollingLockedAtTop = expansionFraction < 1.0 - CGFloat.ulpOfOne

    let tabsHeight: CGFloat = 48.0

    let paneFrame = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: CGSize(width: size.width, height: size.height))

  // prepare pane references
  // visiblePaneIndices
  // requiredPendingKeys
    var visiblePaneIndices: [Int] = []
    var requiredPendingKeys: [PeerInfoPaneKey] = []
    if let currentIndex = currentIndex {
        if currentIndex != 0 {
            visiblePaneIndices.append(currentIndex - 1)
        }
        visiblePaneIndices.append(currentIndex)
        if currentIndex != availablePanes.count - 1 {
            visiblePaneIndices.append(currentIndex + 1)
        }

        for index in visiblePaneIndices {
            let key = availablePanes[index]
            if self.currentPanes[key] == nil && self.pendingPanes[key] == nil {
                requiredPendingKeys.append(key)
            }
        }
    }
    if let pendingSwitchToPaneKey = self.pendingSwitchToPaneKey {
        if self.currentPanes[pendingSwitchToPaneKey] == nil && self.pendingPanes[pendingSwitchToPaneKey] == nil {
            if !requiredPendingKeys.contains(pendingSwitchToPaneKey) {
              // requiredPendingKeys.append(pendingSwitchToPaneKey)
                requiredPendingKeys.append(pendingSwitchToPaneKey)
            }
        }
    }

  // prepare panes
    for key in requiredPendingKeys {
        if self.pendingPanes[key] == nil {
            var leftScope = false
          // create pane
            let pane = PeerInfoPendingPane(
                context: self.context,
                updatedPresentationData: self.updatedPresentationData,
                chatControllerInteraction: self.chatControllerInteraction!,
                data: data!,
                openPeerContextAction: { [weak self] peer, node, gesture in
                    self?.openPeerContextAction?(peer, node, gesture)
                },
                openAddMemberAction: { [weak self] in
                    self?.openAddMemberAction?()
                },
                requestPerformPeerMemberAction: { [weak self] member, action in
                    self?.requestPerformPeerMemberAction?(member, action)
                },
                peerId: self.peerId,
                key: key,
                hasBecomeReady: { [weak self] key in
                    let apply: () -> Void = {
                        guard let strongSelf = self else {
                            return
                        }
                        if let (size, sideInset, bottomInset, visibleHeight, expansionFraction, presentationData, data) = strongSelf.currentParams {
                            var transition: ContainedViewLayoutTransition = .immediate
                            if strongSelf.pendingSwitchToPaneKey == key && strongSelf.currentPaneKey != nil {
                                transition = .animated(duration: 0.4, curve: .spring)
                            }
                            strongSelf.update(size: size, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, expansionFraction: expansionFraction, presentationData: presentationData, data: data, transition: transition)
                        }
                    }
                    if leftScope {
                        apply()
                    }
                },
                parentController: self.parentController,
                openMediaCalendar: { [weak self] in
                    self?.openMediaCalendar?()
                },
                paneDidScroll: { [weak self] in
                    self?.paneDidScroll?()
                }
            )
            self.pendingPanes[key] = pane
            pane.pane.node.frame = paneFrame
            pane.pane.update(size: paneFrame.size, topInset: tabsHeight, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, isScrollingLockedAtTop: isScrollingLockedAtTop, expandProgress: expansionFraction, presentationData: presentationData, synchronous: true, transition: .immediate)
            let paneNode = pane.pane.node
            pane.pane.node.tabBarOffsetUpdated = { [weak self, weak paneNode] transition in
                guard let strongSelf = self, let paneNode = paneNode, let currentPane = strongSelf.currentPane, paneNode === currentPane.node else {
                    return
                }
                if let (size, sideInset, bottomInset, visibleHeight, expansionFraction, presentationData, data) = strongSelf.currentParams {
                    strongSelf.update(size: size, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, expansionFraction: expansionFraction, presentationData: presentationData, data: data, transition: transition)
                }
            }
            leftScope = true
        }
    }

    for (key, pane) in self.pendingPanes {
        pane.pane.node.frame = paneFrame
        pane.pane.update(size: paneFrame.size, topInset: tabsHeight, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, isScrollingLockedAtTop: isScrollingLockedAtTop, expandProgress: expansionFraction, presentationData: presentationData, synchronous: self.currentPaneKey == nil, transition: .immediate)

        if pane.isReady {
            self.pendingPanes.removeValue(forKey: key)
            self.currentPanes[key] = pane.pane
        }
    }

    var paneDefaultTransition = transition
    var previousPaneKey: PeerInfoPaneKey?
    var paneSwitchAnimationOffset: CGFloat = 0.0

    var updatedCurrentIndex = currentIndex
    if let pendingSwitchToPaneKey = self.pendingSwitchToPaneKey, let _ = self.currentPanes[pendingSwitchToPaneKey] {
        self.pendingSwitchToPaneKey = nil
        previousPaneKey = self.currentPaneKey
        self.currentPaneKey = pendingSwitchToPaneKey
        updateCurrentPaneStatus = true
        updatedCurrentIndex = availablePanes.firstIndex(of: pendingSwitchToPaneKey)
        if let previousPaneKey = previousPaneKey, let previousIndex = availablePanes.firstIndex(of: previousPaneKey), let updatedCurrentIndex = updatedCurrentIndex {
            if updatedCurrentIndex < previousIndex {
                paneSwitchAnimationOffset = -size.width
            } else {
                paneSwitchAnimationOffset = size.width
            }
        }

        paneDefaultTransition = .immediate
    }

    if let _ = data {
        if let previousAvailablePanes = previousAvailablePanes, previousAvailablePanes.isEmpty, !availablePanes.isEmpty {
            self.shouldFadeIn = true
        }

        let currentPaneKeys = Set<PeerInfoPaneKey>(self.currentPanes.keys)
        if previousPaneKeys.isEmpty && !currentPaneKeys.isEmpty && self.shouldFadeIn {
            self.shouldFadeIn = false
            self.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.3)
        }
    }

    for (key, pane) in self.currentPanes {
        if let index = availablePanes.firstIndex(of: key), let updatedCurrentIndex = updatedCurrentIndex {
            var paneWasAdded = false
            if pane.node.supernode == nil {
                self.insertSubnode(pane.node, belowSubnode: self.coveringBackgroundNode)
                paneWasAdded = true
            }
            let indexOffset = CGFloat(index - updatedCurrentIndex)

            let paneTransition: ContainedViewLayoutTransition = paneWasAdded ? .immediate : paneDefaultTransition
            let adjustedFrame = paneFrame.offsetBy(dx: size.width * self.transitionFraction + indexOffset * size.width, dy: 0.0)

            let paneCompletion: () -> Void = { [weak self, weak pane] in
                guard let strongSelf = self, let pane = pane else {
                    return
                }
                pane.isAnimatingOut = false
                if let (_, _, _, _, _, _, data) = strongSelf.currentParams {
                    if let availablePanes = data?.availablePanes, let currentPaneKey = strongSelf.currentPaneKey, let currentIndex = availablePanes.firstIndex(of: currentPaneKey), let paneIndex = availablePanes.firstIndex(of: key), abs(paneIndex - currentIndex) <= 1 {
                    } else {
                        if let pane = strongSelf.currentPanes.removeValue(forKey: key) {
                            //print("remove \(key)")
                            pane.node.removeFromSupernode()
                        }
                    }
                }
            }
            if let previousPaneKey = previousPaneKey, key == previousPaneKey {
                pane.node.frame = adjustedFrame
                let isAnimatingOut = pane.isAnimatingOut
                pane.isAnimatingOut = true
                transition.animateFrame(node: pane.node, from: paneFrame, to: paneFrame.offsetBy(dx: -paneSwitchAnimationOffset, dy: 0.0), completion: isAnimatingOut ? nil : { _ in
                    paneCompletion()
                })
            } else if let _ = previousPaneKey, key == self.currentPaneKey {
                pane.node.frame = adjustedFrame
                let isAnimatingOut = pane.isAnimatingOut
                pane.isAnimatingOut = true
                transition.animatePositionAdditive(node: pane.node, offset: CGPoint(x: paneSwitchAnimationOffset, y: 0.0), completion: isAnimatingOut ? nil : {
                    paneCompletion()
                })
            } else {
                let isAnimatingOut = pane.isAnimatingOut
                pane.isAnimatingOut = true
                paneTransition.updateFrame(node: pane.node, frame: adjustedFrame, completion: isAnimatingOut ? nil :  { _ in
                    paneCompletion()
                })
            }
            pane.update(size: paneFrame.size, topInset: tabsHeight, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, isScrollingLockedAtTop: isScrollingLockedAtTop, expandProgress: expansionFraction, presentationData: presentationData, synchronous: paneWasAdded, transition: paneTransition)
        }
    }

    var tabsOffset: CGFloat = 0.0
    if let currentPane = self.currentPane {
        tabsOffset = currentPane.node.tabBarOffset
    }
    tabsOffset = max(0.0, min(tabsHeight, tabsOffset))
    if isScrollingLockedAtTop || self.isMediaOnly {
        tabsOffset = 0.0
    }
    var tabsAlpha = 1.0 - tabsOffset / tabsHeight
    tabsAlpha *= tabsAlpha
    transition.updateFrame(node: self.tabsContainerNode, frame: CGRect(origin: CGPoint(x: 0.0, y: -tabsOffset), size: CGSize(width: size.width, height: tabsHeight)))
    transition.updateAlpha(node: self.tabsContainerNode, alpha: tabsAlpha)

    transition.updateFrame(node: self.separatorNode, frame: CGRect(origin: CGPoint(x: 0.0, y: -UIScreenPixel - tabsOffset), size: CGSize(width: size.width, height: UIScreenPixel)))
    transition.updateFrame(node: self.coveringBackgroundNode, frame: CGRect(origin: CGPoint(x: 0.0, y: -UIScreenPixel - tabsOffset), size: CGSize(width: size.width, height: tabsHeight + UIScreenPixel)))
    self.coveringBackgroundNode.update(size: self.coveringBackgroundNode.bounds.size, transition: transition)

    transition.updateFrame(node: self.tabsSeparatorNode, frame: CGRect(origin: CGPoint(x: 0.0, y: tabsHeight - tabsOffset), size: CGSize(width: size.width, height: UIScreenPixel)))

    self.tabsContainerNode.update(size: CGSize(width: size.width, height: tabsHeight), presentationData: presentationData, paneList: availablePanes.map { key in
        let title: String
        switch key {
        case .media:
            title = presentationData.strings.PeerInfo_PaneMedia
        case .files:
            title = presentationData.strings.PeerInfo_PaneFiles
        case .links:
            title = presentationData.strings.PeerInfo_PaneLinks
        case .voice:
            title = presentationData.strings.PeerInfo_PaneVoiceAndVideo
        case .gifs:
            title = presentationData.strings.PeerInfo_PaneGifs
        case .music:
            title = presentationData.strings.PeerInfo_PaneAudio
        case .groupsInCommon:
            title = presentationData.strings.PeerInfo_PaneGroups
        case .members:
            title = presentationData.strings.PeerInfo_PaneMembers
        }
        return PeerInfoPaneSpecifier(key: key, title: title)
    }, selectedPane: self.currentPaneKey, transitionFraction: self.transitionFraction, transition: transition)

    for (_, pane) in self.pendingPanes {
        let paneTransition: ContainedViewLayoutTransition = .immediate
        paneTransition.updateFrame(node: pane.pane.node, frame: paneFrame)
        pane.pane.update(size: paneFrame.size, topInset: tabsHeight, sideInset: sideInset, bottomInset: bottomInset, visibleHeight: visibleHeight, isScrollingLockedAtTop: isScrollingLockedAtTop, expandProgress: expansionFraction, presentationData: presentationData, synchronous: true, transition: paneTransition)
    }
    if !self.didSetIsReady && data != nil {
        if let currentPaneKey = self.currentPaneKey, let currentPane = self.currentPanes[currentPaneKey] {
            self.didSetIsReady = true
            self.isReady.set(currentPane.node.isReady)
        } else if self.pendingSwitchToPaneKey == nil {
            self.didSetIsReady = true
            self.isReady.set(.single(true))
        }
    }
    if let previousCurrentPaneKey = previousCurrentPaneKey, self.currentPaneKey != previousCurrentPaneKey {
        self.currentPaneUpdated?(self.expandOnSwitch)
        self.expandOnSwitch = false
    }
    if updateCurrentPaneStatus {
        self.currentPaneStatusPromise.set(self.currentPane?.node.status ?? .single(nil))
    }
}
This post is licensed under CC BY 4.0 by the author.