From a1d82879912130d091bc32267d51fd509ccc8059 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Wed, 15 Jul 2020 17:11:07 +0300 Subject: [PATCH] Video avatar fixes --- .../ChatItemGalleryFooterContentNode.swift | 4 +-- .../ChatVideoGalleryItemScrubberView.swift | 27 ++++++++++++++++--- .../Sources/GalleryControllerNode.swift | 9 ++++--- .../Sources/GalleryFooterContentNode.swift | 2 +- .../GalleryUI/Sources/GalleryFooterNode.swift | 8 +++--- .../TelegramUI/Sources/ChatController.swift | 13 ++++++--- .../Sources/PeerInfo/PeerInfoHeaderNode.swift | 8 +++--- 7 files changed, 50 insertions(+), 21 deletions(-) diff --git a/submodules/GalleryUI/Sources/ChatItemGalleryFooterContentNode.swift b/submodules/GalleryUI/Sources/ChatItemGalleryFooterContentNode.swift index 576abff2ec..e72d01226a 100644 --- a/submodules/GalleryUI/Sources/ChatItemGalleryFooterContentNode.swift +++ b/submodules/GalleryUI/Sources/ChatItemGalleryFooterContentNode.swift @@ -250,10 +250,10 @@ final class ChatItemGalleryFooterContentNode: GalleryFooterContentNode, UIScroll } } - override func setVisibilityAlpha(_ alpha: CGFloat) { + override func setVisibilityAlpha(_ alpha: CGFloat, animated: Bool) { self.visibilityAlpha = alpha self.contentNode.alpha = alpha - self.scrubberView?.setCollapsed(alpha < 1.0 ? true : false, animated: true) + self.scrubberView?.setCollapsed(alpha < 1.0 ? true : false, animated: animated) } init(context: AccountContext, presentationData: PresentationData, present: @escaping (ViewController, Any?) -> Void = { _, _ in }) { diff --git a/submodules/GalleryUI/Sources/ChatVideoGalleryItemScrubberView.swift b/submodules/GalleryUI/Sources/ChatVideoGalleryItemScrubberView.swift index c2c25e09e7..4bcc2091d9 100644 --- a/submodules/GalleryUI/Sources/ChatVideoGalleryItemScrubberView.swift +++ b/submodules/GalleryUI/Sources/ChatVideoGalleryItemScrubberView.swift @@ -120,13 +120,32 @@ final class ChatVideoGalleryItemScrubberView: UIView { self.fetchStatusDisposable.dispose() } + var collapsed: Bool = false func setCollapsed(_ collapsed: Bool, animated: Bool) { - let alpha: CGFloat = collapsed ? 0.0 : 1.0 + guard self.collapsed != collapsed else { + return + } - self.scrubberNode.setCollapsed(collapsed, animated: animated) + self.collapsed = collapsed + + let alpha: CGFloat = collapsed ? 0.0 : 1.0 self.leftTimestampNode.alpha = alpha self.rightTimestampNode.alpha = alpha self.infoNode.alpha = alpha + self.updateScrubberVisibility(animated: animated) + } + + private func updateScrubberVisibility(animated: Bool) { + var collapsed = self.collapsed + var alpha: CGFloat = 1.0 + if let playbackStatus = self.playbackStatus, playbackStatus.duration <= 30.0 { + } else { + alpha = self.collapsed ? 0.0 : 1.0 + collapsed = false + } + self.scrubberNode.setCollapsed(collapsed, animated: animated) + let transition: ContainedViewLayoutTransition = animated ? .animated(duration: 0.3, curve: .linear) : .immediate + transition.updateAlpha(node: self.scrubberNode, alpha: alpha) } func setStatusSignal(_ status: Signal?) { @@ -232,7 +251,7 @@ final class ChatVideoGalleryItemScrubberView: UIView { self.containerLayout = (size, leftInset, rightInset) let scrubberHeight: CGFloat = 14.0 - let scrubberInset: CGFloat + var scrubberInset: CGFloat let leftTimestampOffset: CGFloat let rightTimestampOffset: CGFloat let infoOffset: CGFloat @@ -257,7 +276,7 @@ final class ChatVideoGalleryItemScrubberView: UIView { let infoSize = self.infoNode.measure(infoConstrainedSize) self.infoNode.bounds = CGRect(origin: CGPoint(), size: infoSize) transition.updatePosition(node: self.infoNode, position: CGPoint(x: size.width / 2.0, y: infoOffset + infoSize.height / 2.0)) - self.infoNode.alpha = size.width < size.height ? 1.0 : 0.0 + self.infoNode.alpha = size.width < size.height && !self.collapsed ? 1.0 : 0.0 self.scrubberNode.frame = CGRect(origin: CGPoint(x: scrubberInset, y: 6.0), size: CGSize(width: size.width - leftInset - rightInset - scrubberInset * 2.0, height: scrubberHeight)) } diff --git a/submodules/GalleryUI/Sources/GalleryControllerNode.swift b/submodules/GalleryUI/Sources/GalleryControllerNode.swift index 481fbe325f..a689f8ba4a 100644 --- a/submodules/GalleryUI/Sources/GalleryControllerNode.swift +++ b/submodules/GalleryUI/Sources/GalleryControllerNode.swift @@ -253,7 +253,7 @@ open class GalleryControllerNode: ASDisplayNode, UIScrollViewDelegate, UIGesture if displayThumbnailPanel { thumbnailPanelHeight = 52.0 } - let thumbnailsFrame = CGRect(origin: CGPoint(x: 0.0, y: layout.size.height - 40.0 - panelHeight + 4.0 - layout.intrinsicInsets.bottom + (self.areControlsHidden ? 54.0 : 0.0)), size: CGSize(width: layout.size.width, height: panelHeight - 4.0)) + let thumbnailsFrame = CGRect(origin: CGPoint(x: 0.0, y: layout.size.height - 40.0 - panelHeight + 4.0 - layout.intrinsicInsets.bottom + (self.areControlsHidden ? 106.0 : 0.0)), size: CGSize(width: layout.size.width, height: panelHeight - 4.0)) transition.updateFrame(node: currentThumbnailContainerNode, frame: thumbnailsFrame) currentThumbnailContainerNode.updateLayout(size: thumbnailsFrame.size, transition: transition) @@ -279,6 +279,9 @@ open class GalleryControllerNode: ASDisplayNode, UIScrollViewDelegate, UIGesture } open func setControlsHidden(_ hidden: Bool, animated: Bool) { + guard self.areControlsHidden != hidden else { + return + } self.areControlsHidden = hidden self.controlsVisibilityChanged?(!hidden) if animated { @@ -286,7 +289,7 @@ open class GalleryControllerNode: ASDisplayNode, UIScrollViewDelegate, UIGesture let alpha: CGFloat = self.areControlsHidden ? 0.0 : 1.0 self.navigationBar?.alpha = alpha self.statusBar?.updateAlpha(alpha, transition: .animated(duration: 0.3, curve: .easeInOut)) - self.footerNode.setVisibilityAlpha(alpha) + self.footerNode.setVisibilityAlpha(alpha, animated: animated) self.updateThumbnailContainerNodeAlpha(.immediate) }) @@ -297,7 +300,7 @@ open class GalleryControllerNode: ASDisplayNode, UIScrollViewDelegate, UIGesture let alpha: CGFloat = self.areControlsHidden ? 0.0 : 1.0 self.navigationBar?.alpha = alpha self.statusBar?.updateAlpha(alpha, transition: .immediate) - self.footerNode.setVisibilityAlpha(alpha) + self.footerNode.setVisibilityAlpha(alpha, animated: animated) self.updateThumbnailContainerNodeAlpha(.immediate) if let (navigationBarHeight, layout) = self.containerLayout { diff --git a/submodules/GalleryUI/Sources/GalleryFooterContentNode.swift b/submodules/GalleryUI/Sources/GalleryFooterContentNode.swift index 0c85e4e792..d1c25e9205 100644 --- a/submodules/GalleryUI/Sources/GalleryFooterContentNode.swift +++ b/submodules/GalleryUI/Sources/GalleryFooterContentNode.swift @@ -21,7 +21,7 @@ open class GalleryFooterContentNode: ASDisplayNode { public var controllerInteraction: GalleryControllerInteraction? var visibilityAlpha: CGFloat = 1.0 - open func setVisibilityAlpha(_ alpha: CGFloat) { + open func setVisibilityAlpha(_ alpha: CGFloat, animated: Bool) { self.visibilityAlpha = alpha self.alpha = alpha } diff --git a/submodules/GalleryUI/Sources/GalleryFooterNode.swift b/submodules/GalleryUI/Sources/GalleryFooterNode.swift index b407ee98fd..57ef587554 100644 --- a/submodules/GalleryUI/Sources/GalleryFooterNode.swift +++ b/submodules/GalleryUI/Sources/GalleryFooterNode.swift @@ -24,10 +24,10 @@ public final class GalleryFooterNode: ASDisplayNode { } private var visibilityAlpha: CGFloat = 1.0 - public func setVisibilityAlpha(_ alpha: CGFloat) { + public func setVisibilityAlpha(_ alpha: CGFloat, animated: Bool) { self.visibilityAlpha = alpha self.backgroundNode.alpha = alpha - self.currentFooterContentNode?.setVisibilityAlpha(alpha) + self.currentFooterContentNode?.setVisibilityAlpha(alpha, animated: true) self.currentOverlayContentNode?.setVisibilityAlpha(alpha) } @@ -43,7 +43,7 @@ public final class GalleryFooterNode: ASDisplayNode { } self.currentFooterContentNode = footerContentNode if let footerContentNode = footerContentNode { - footerContentNode.setVisibilityAlpha(self.visibilityAlpha) + footerContentNode.setVisibilityAlpha(self.visibilityAlpha, animated: transition.isAnimated) footerContentNode.controllerInteraction = self.controllerInteraction footerContentNode.requestLayout = { [weak self] transition in if let strongSelf = self, let (currentLayout, currentThumbnailPanelHeight, isHidden) = strongSelf.currentLayout { @@ -67,7 +67,7 @@ public final class GalleryFooterNode: ASDisplayNode { } var backgroundHeight: CGFloat = 0.0 - let verticalOffset: CGFloat = isHidden ? (layout.size.width > layout.size.height ? 44.0 : 54.0) : 0.0 + let verticalOffset: CGFloat = isHidden ? (layout.size.width > layout.size.height ? 44.0 : (thumbnailPanelHeight > 0.0 ? 106.0 : 54.0)) : 0.0 if let footerContentNode = self.currentFooterContentNode { backgroundHeight = footerContentNode.updateLayout(size: layout.size, metrics: layout.metrics, leftInset: layout.safeInsets.left, rightInset: layout.safeInsets.right, bottomInset: cleanInsets.bottom, contentInset: thumbnailPanelHeight, transition: transition) transition.updateFrame(node: footerContentNode, frame: CGRect(origin: CGPoint(x: 0.0, y: layout.size.height - backgroundHeight + verticalOffset), size: CGSize(width: layout.size.width, height: backgroundHeight))) diff --git a/submodules/TelegramUI/Sources/ChatController.swift b/submodules/TelegramUI/Sources/ChatController.swift index 53ce27b7c9..364109e5e5 100644 --- a/submodules/TelegramUI/Sources/ChatController.swift +++ b/submodules/TelegramUI/Sources/ChatController.swift @@ -5069,11 +5069,16 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G self.keepPeerInfoScreenDataHotDisposable.set(keepPeerInfoScreenDataHot(context: context, peerId: peerId).start()) self.preloadAvatarDisposable.set((peerInfoProfilePhotosWithCache(context: context, peerId: peerId) |> mapToSignal { result -> Signal in - if let video = result.first?.videoRepresentations.first { - let duration: Double = (video.representation.startTimestamp ?? 0.0) + 4.0 - return preloadVideoResource(postbox: context.account.postbox, resourceReference: video.reference, duration: duration) + var signals: [Signal] = [.complete()] + for i in 0 ..< min(5, result.count) { + if let video = result[i].videoRepresentations.first { + let duration: Double = (video.representation.startTimestamp ?? 0.0) + (i == 0 ? 4.0 : 2.0) + signals.append(preloadVideoResource(postbox: context.account.postbox, resourceReference: video.reference, duration: duration)) + } + } + return combineLatest(signals) |> mapToSignal { _ in + return .never() } - return .complete() }).start()) } } diff --git a/submodules/TelegramUI/Sources/PeerInfo/PeerInfoHeaderNode.swift b/submodules/TelegramUI/Sources/PeerInfo/PeerInfoHeaderNode.swift index 4ff9d63478..bfdd3db426 100644 --- a/submodules/TelegramUI/Sources/PeerInfo/PeerInfoHeaderNode.swift +++ b/submodules/TelegramUI/Sources/PeerInfo/PeerInfoHeaderNode.swift @@ -1002,7 +1002,8 @@ final class PeerInfoAvatarListContainerNode: ASDisplayNode { var additiveTransitionOffset: CGFloat = 0.0 var itemsAdded = false if self.currentIndex >= 0 && self.currentIndex < self.items.count { - for i in max(0, self.currentIndex - 1) ... min(self.currentIndex + 1, self.items.count - 1) { + let preloadSpan: Int = 2 + for i in max(0, self.currentIndex - preloadSpan) ... min(self.currentIndex + preloadSpan, self.items.count - 1) { validIds.append(self.items[i].id) var itemNode: PeerInfoAvatarListItemNode? var wasAdded = false @@ -2576,8 +2577,9 @@ final class PeerInfoHeaderNode: ASDisplayNode { func initiateAvatarExpansion(gallery: Bool) { if self.isAvatarExpanded || gallery { - if let currentEntry = self.avatarListNode.listContainerNode.currentEntry { - self.requestAvatarExpansion?(true, self.avatarListNode.listContainerNode.galleryEntries, self.avatarListNode.listContainerNode.currentEntry, self.avatarTransitionArguments(entry: currentEntry)) + if let currentEntry = self.avatarListNode.listContainerNode.currentEntry, let firstEntry = self.avatarListNode.listContainerNode.galleryEntries.first { + let entry = gallery ? firstEntry : currentEntry + self.requestAvatarExpansion?(true, self.avatarListNode.listContainerNode.galleryEntries, entry, self.avatarTransitionArguments(entry: currentEntry)) } } else if let entry = self.avatarListNode.listContainerNode.galleryEntries.first { let _ = self.avatarListNode.avatarContainerNode.avatarNode