From aa2b07df7d5ea0296227e2302ccc41b71a00af40 Mon Sep 17 00:00:00 2001 From: Peter <> Date: Tue, 21 May 2019 15:50:40 +0200 Subject: [PATCH] Support groups in contact list --- TelegramUI/ComposeControllerNode.swift | 2 +- TelegramUI/ContactListNode.swift | 47 +++++++++++++++++-- .../ContactMultiselectionControllerNode.swift | 4 +- .../ContactSelectionControllerNode.swift | 2 +- TelegramUI/ContactsControllerNode.swift | 2 +- TelegramUI/PeerSelectionControllerNode.swift | 2 +- 6 files changed, 49 insertions(+), 10 deletions(-) diff --git a/TelegramUI/ComposeControllerNode.swift b/TelegramUI/ComposeControllerNode.swift index bf11fdfcb8..06b51d4c82 100644 --- a/TelegramUI/ComposeControllerNode.swift +++ b/TelegramUI/ComposeControllerNode.swift @@ -44,7 +44,7 @@ final class ComposeControllerNode: ASDisplayNode { ContactListAdditionalOption(title: self.presentationData.strings.Compose_NewChannel, icon: .generic(UIImage(bundleImageName: "Contact List/CreateChannelActionIcon")!), action: { openCreateNewChannelImpl?() }) - ])), displayPermissionPlaceholder: false) + ], includeChatList: false)), displayPermissionPlaceholder: false) super.init() diff --git a/TelegramUI/ContactListNode.swift b/TelegramUI/ContactListNode.swift index 4bc6aafaff..2d28e94329 100644 --- a/TelegramUI/ContactListNode.swift +++ b/TelegramUI/ContactListNode.swift @@ -501,7 +501,7 @@ private func contactListNodeEntries(accountPeer: Peer?, peers: [ContactListPeer] for i in 0 ..< options.count { entries.append(.option(i, options[i], commonHeader, theme, strings)) } - case let .natural(options): + case let .natural(options, _): let sortedPeers = peers.sorted(by: { lhs, rhs in let result = lhs.indexName.isLessThan(other: rhs.indexName, ordering: sortOrder) if result == .orderedSame { @@ -698,7 +698,7 @@ public struct ContactListAdditionalOption: Equatable { enum ContactListPresentation { case orderedByPresence(options: [ContactListAdditionalOption]) - case natural(options: [ContactListAdditionalOption]) + case natural(options: [ContactListAdditionalOption], includeChatList: Bool) case search(signal: Signal, searchChatList: Bool, searchDeviceContacts: Bool, searchGroups: Bool) var sortOrder: ContactsSortOrder? { @@ -937,8 +937,10 @@ final class ContactListNode: ASDisplayNode { transition = presentation |> mapToSignal { presentation in var generateSections = false - if case .natural = presentation { + var includeChatList = false + if case let .natural(natural) = presentation { generateSections = true + includeChatList = natural.includeChatList } if case let .search(query, searchChatList, searchDeviceContacts, searchGroups) = presentation { @@ -1122,10 +1124,45 @@ final class ContactListNode: ASDisplayNode { } } } else { - return (combineLatest(self.contactPeersViewPromise.get(), selectionStateSignal, themeAndStringsPromise.get(), contactsAuthorization.get(), contactsWarningSuppressed.get()) - |> mapToQueue { view, selectionState, themeAndStrings, authorizationStatus, warningSuppressed -> Signal in + let chatListSignal: Signal<[(Peer, Int32)], NoError> + if includeChatList { + chatListSignal = self.context.account.viewTracker.tailChatListView(groupId: .root, count: 100) + |> take(1) + |> mapToSignal { view, _ -> Signal<[(Peer, Int32)], NoError> in + return context.account.postbox.transaction { transaction -> [(Peer, Int32)] in + var peers: [(Peer, Int32)] = [] + for entry in view.entries { + switch entry { + case let .MessageEntry(messageEntry): + if let peer = messageEntry.5.peer { + if peer is TelegramGroup { + peers.append((peer, 0)) + } else if let channel = peer as? TelegramChannel, case .group = channel.info { + var memberCount: Int32 = 0 + if let cachedData = transaction.getPeerCachedData(peerId: peer.id) as? CachedChannelData { + memberCount = cachedData.participantsSummary.memberCount ?? 0 + } + peers.append((peer, memberCount)) + } + } + default: + break + } + } + return peers + } + } + } else { + chatListSignal = .single([]) + } + + return (combineLatest(self.contactPeersViewPromise.get(), chatListSignal, selectionStateSignal, themeAndStringsPromise.get(), contactsAuthorization.get(), contactsWarningSuppressed.get()) + |> mapToQueue { view, chatListPeers, selectionState, themeAndStrings, authorizationStatus, warningSuppressed -> Signal in let signal = deferred { () -> Signal in var peers = view.peers.map({ ContactListPeer.peer(peer: $0, isGlobal: false, participantCount: nil) }) + for (peer, memberCount) in chatListPeers { + peers.append(.peer(peer: peer, isGlobal: false, participantCount: memberCount)) + } var existingPeerIds = Set() var disabledPeerIds = Set() for filter in filters { diff --git a/TelegramUI/ContactMultiselectionControllerNode.swift b/TelegramUI/ContactMultiselectionControllerNode.swift index 9dbf2c5d71..574b235686 100644 --- a/TelegramUI/ContactMultiselectionControllerNode.swift +++ b/TelegramUI/ContactMultiselectionControllerNode.swift @@ -49,8 +49,10 @@ final class ContactMultiselectionControllerNode: ASDisplayNode { self.presentationData = context.sharedContext.currentPresentationData.with { $0 } let placeholder: String + var includeChatList = false switch mode { case let .peerSelection(_, searchGroups): + includeChatList = searchGroups if searchGroups { placeholder = self.presentationData.strings.Contacts_SearchUsersAndGroupsLabel } else { @@ -60,7 +62,7 @@ final class ContactMultiselectionControllerNode: ASDisplayNode { placeholder = self.presentationData.strings.Compose_TokenListPlaceholder } - self.contactListNode = ContactListNode(context: context, presentation: .single(.natural(options: options)), filters: filters, selectionState: ContactListNodeGroupSelectionState()) + self.contactListNode = ContactListNode(context: context, presentation: .single(.natural(options: options, includeChatList: includeChatList)), filters: filters, selectionState: ContactListNodeGroupSelectionState()) self.tokenListNode = EditableTokenListNode(theme: EditableTokenListNodeTheme(backgroundColor: self.presentationData.theme.rootController.navigationBar.backgroundColor, separatorColor: self.presentationData.theme.rootController.navigationBar.separatorColor, placeholderTextColor: self.presentationData.theme.list.itemPlaceholderTextColor, primaryTextColor: self.presentationData.theme.list.itemPrimaryTextColor, selectedTextColor: self.presentationData.theme.list.itemAccentColor, keyboardColor: self.presentationData.theme.chatList.searchBarKeyboardColor), placeholder: placeholder) super.init() diff --git a/TelegramUI/ContactSelectionControllerNode.swift b/TelegramUI/ContactSelectionControllerNode.swift index 4ef41b649e..e50bb5a218 100644 --- a/TelegramUI/ContactSelectionControllerNode.swift +++ b/TelegramUI/ContactSelectionControllerNode.swift @@ -39,7 +39,7 @@ final class ContactSelectionControllerNode: ASDisplayNode { self.presentationData = context.sharedContext.currentPresentationData.with { $0 } self.displayDeviceContacts = displayDeviceContacts - self.contactListNode = ContactListNode(context: context, presentation: .single(.natural(options: options))) + self.contactListNode = ContactListNode(context: context, presentation: .single(.natural(options: options, includeChatList: false))) self.dimNode = ASDisplayNode() diff --git a/TelegramUI/ContactsControllerNode.swift b/TelegramUI/ContactsControllerNode.swift index 57cb2d8972..4f292bd0b7 100644 --- a/TelegramUI/ContactsControllerNode.swift +++ b/TelegramUI/ContactsControllerNode.swift @@ -38,7 +38,7 @@ final class ContactsControllerNode: ASDisplayNode { case .presence: return .orderedByPresence(options: options) case .natural: - return .natural(options: options) + return .natural(options: options, includeChatList: false) } } diff --git a/TelegramUI/PeerSelectionControllerNode.swift b/TelegramUI/PeerSelectionControllerNode.swift index c537e36661..525b9f22da 100644 --- a/TelegramUI/PeerSelectionControllerNode.swift +++ b/TelegramUI/PeerSelectionControllerNode.swift @@ -326,7 +326,7 @@ final class PeerSelectionControllerNode: ASDisplayNode { self.recursivelyEnsureDisplaySynchronously(true) contactListNode.enableUpdates = true } else { - let contactListNode = ContactListNode(context: context, presentation: .single(.natural(options: []))) + let contactListNode = ContactListNode(context: context, presentation: .single(.natural(options: [], includeChatList: false))) self.contactListNode = contactListNode contactListNode.enableUpdates = true contactListNode.activateSearch = { [weak self] in