From f094395c67d71b4ecd6539cdc45e9239b3528f9f Mon Sep 17 00:00:00 2001 From: Pavel Gurov Date: Sun, 23 Jun 2019 19:03:43 +0300 Subject: [PATCH] Add highlighting of cell in chat list when returning to screen --- TelegramUI/ChatListController.swift | 33 ++++++++++++++++++++--------- TelegramUI/ChatListItem.swift | 13 ++++++++++++ TelegramUI/ChatListNode.swift | 21 ++++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/TelegramUI/ChatListController.swift b/TelegramUI/ChatListController.swift index 2b09e5c3..d5be10eb 100644 --- a/TelegramUI/ChatListController.swift +++ b/TelegramUI/ChatListController.swift @@ -682,9 +682,7 @@ public class ChatListController: TelegramController, KeyShortcutResponder, UIVie if let layout = strongSelf.validLayout, case .regular = layout.metrics.widthClass { scrollToEndIfExists = true } - navigateToChatController(navigationController: navigationController, context: strongSelf.context, chatLocation: .peer(peerId), scrollToEndIfExists: scrollToEndIfExists, animated: animated, completion: { [weak self] in - self?.chatListDisplayNode.chatListNode.clearHighlightAnimated(true) - }) + navigateToChatController(navigationController: navigationController, context: strongSelf.context, chatLocation: .peer(peerId), scrollToEndIfExists: scrollToEndIfExists, animated: animated) } } } @@ -694,7 +692,6 @@ public class ChatListController: TelegramController, KeyShortcutResponder, UIVie if let navigationController = strongSelf.navigationController as? NavigationController { let chatListController = ChatListController(context: strongSelf.context, groupId: groupId, controlsHistoryPreload: false) navigationController.pushViewController(chatListController) - strongSelf.chatListDisplayNode.chatListNode.clearHighlightAnimated(true) } } } @@ -1070,8 +1067,17 @@ public class ChatListController: TelegramController, KeyShortcutResponder, UIVie self.dismissSearchOnDisappear = false self.deactivateSearch(animated: false) } - - self.chatListDisplayNode.chatListNode.clearHighlightAnimated(true) + } + + override public func navigationAlongsideTransition(type: NavigationTransition) -> ((CGFloat) -> ())? { + switch type { + case .Pop: + return { [weak self] progress in + self?.chatListDisplayNode.chatListNode.updateHiglightPercent(1 - progress) + } + case .Push: + return nil + } } override public func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) { @@ -1091,10 +1097,17 @@ public class ChatListController: TelegramController, KeyShortcutResponder, UIVie override public func navigationStackConfigurationUpdated(next: [ViewController]) { super.navigationStackConfigurationUpdated(next: next) - - let chatLocation = (next.first as? ChatController)?.chatLocation - - self.chatListDisplayNode.chatListNode.updateSelectedChatLocation(chatLocation, progress: 1.0, transition: .immediate) + + if UIDevice.current.userInterfaceIdiom != .phone { + let chatLocation = (navigationController?.viewControllers.last as? ChatController)?.chatLocation + chatListDisplayNode.chatListNode.clearHighlightAnimated(false) + chatListDisplayNode.chatListNode.updateSelectedChatLocation(chatLocation, progress: 1.0, transition: .immediate) + } else { + if let chatLocation = (navigationController?.viewControllers.last as? ChatController)?.chatLocation { + chatListDisplayNode.chatListNode.clearHighlightAnimated(false) + chatListDisplayNode.chatListNode.updateSelectedChatLocation(chatLocation, progress: 1.0, transition: .immediate) + } + } } @objc func editPressed() { diff --git a/TelegramUI/ChatListItem.swift b/TelegramUI/ChatListItem.swift index 815fdc66..03700e03 100644 --- a/TelegramUI/ChatListItem.swift +++ b/TelegramUI/ChatListItem.swift @@ -502,6 +502,17 @@ class ChatListItemNode: ItemListRevealOptionsItemNode { self.updateIsHighlighted(transition: (animated && !highlighted) ? .animated(duration: 0.3, curve: .easeInOut) : .immediate) } + override func setHighlightedPercent(_ percent: CGFloat) -> Bool { + guard isHighlighted else { return false } + + if percent == 0 { + setHighlighted(false, at: .zero, animated: true) + } else { + self.highlightedBackgroundNode.alpha = 2 * (1 - 1 / (1 + percent)) + } + return isHighlighted + } + func updateIsHighlighted(transition: ContainedViewLayoutTransition) { var reallyHighlighted = self.isHighlighted if let item = self.item { @@ -513,6 +524,7 @@ class ChatListItemNode: ItemListRevealOptionsItemNode { } if reallyHighlighted { + self.isHighlighted = true if self.highlightedBackgroundNode.supernode == nil { self.insertSubnode(self.highlightedBackgroundNode, aboveSubnode: self.separatorNode) self.highlightedBackgroundNode.alpha = 0.0 @@ -524,6 +536,7 @@ class ChatListItemNode: ItemListRevealOptionsItemNode { self.onlineNode.setImage(PresentationResourcesChatList.recentStatusOnlineIcon(item.presentationData.theme, state: .highlighted)) } } else { + self.isHighlighted = false if self.highlightedBackgroundNode.supernode != nil { transition.updateAlpha(layer: self.highlightedBackgroundNode.layer, alpha: 0.0, completion: { [weak self] completed in if let strongSelf = self { diff --git a/TelegramUI/ChatListNode.swift b/TelegramUI/ChatListNode.swift index 6f66b9a3..f9d7c355 100644 --- a/TelegramUI/ChatListNode.swift +++ b/TelegramUI/ChatListNode.swift @@ -1419,11 +1419,32 @@ final class ChatListNode: ListView { self.forEachItemNode { itemNode in if let itemNode = itemNode as? ChatListItemNode { + _ = itemNode.setHighlightedPercent(0) itemNode.updateIsHighlighted(transition: transition) } } } + private var nodeCurrentlyBeingAnimated: ChatListItemNode? + override public func updateHiglightPercent(_ percent: CGFloat) { + if percent == 0, let interaction = self.interaction { + interaction.highlightedChatLocation = nil + } + if percent == 0 || percent == 1 { + nodeCurrentlyBeingAnimated = nil + } + + if let currentlyAnimatedNode = nodeCurrentlyBeingAnimated { + _ = currentlyAnimatedNode.setHighlightedPercent(percent) + } else { + self.forEachItemNode { itemNode in + if let itemNode = itemNode as? ChatListItemNode, itemNode.setHighlightedPercent(percent) { + nodeCurrentlyBeingAnimated = itemNode + } + } + } + } + private func currentlyVisibleLatestChatListIndex() -> ChatListIndex? { guard let chatListView = (self.opaqueTransactionState as? ChatListOpaqueTransactionState)?.chatListView else { return nil