Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.02 KB | None | 0 0
  1. func chatListFilterItems(context: AccountContext) -> Signal<(Int, [(ChatListFilter, Int)]), NoError> {
  2.     let preferencesKey: PostboxViewKey = .preferences(keys: [PreferencesKeys.chatListFilters])
  3.     return context.account.postbox.combinedView(keys: [preferencesKey])
  4.     |> map { combinedView -> [ChatListFilter] in
  5.         if let filtersState = (combinedView.views[preferencesKey] as? PreferencesView)?.values[PreferencesKeys.chatListFilters] as? ChatListFiltersState {
  6.             return filtersState.filters
  7.         } else {
  8.             return []
  9.         }
  10.     }
  11.     |> distinctUntilChanged
  12.     |> mapToSignal { filters -> Signal<(Int, [(ChatListFilter, Int)]), NoError> in
  13.         var unreadCountItems: [UnreadMessageCountsItem] = []
  14.         unreadCountItems.append(.total(nil))
  15.         var additionalPeerIds = Set<PeerId>()
  16.         for filter in filters {
  17.             additionalPeerIds.formUnion(filter.includePeers)
  18.         }
  19.         if !additionalPeerIds.isEmpty {
  20.             for peerId in additionalPeerIds {
  21.                 unreadCountItems.append(.peer(peerId))
  22.             }
  23.         }
  24.         let unreadKey: PostboxViewKey = .unreadCounts(items: unreadCountItems)
  25.         var keys: [PostboxViewKey] = []
  26.         keys.append(unreadKey)
  27.         for peerId in additionalPeerIds {
  28.             keys.append(.basicPeer(peerId))
  29.         }
  30.        
  31.         return context.account.postbox.combinedView(keys: keys)
  32.         |> map { view -> (Int, [(ChatListFilter, Int)]) in
  33.             guard let unreadCounts = view.views[unreadKey] as? UnreadMessageCountsView else {
  34.                 return (0, [])
  35.             }
  36.            
  37.             var result: [(ChatListFilter, Int)] = []
  38.            
  39.             var peerTagAndCount: [PeerId: (PeerSummaryCounterTags, Int)] = [:]
  40.            
  41.             var totalState: ChatListTotalUnreadState?
  42.             for entry in unreadCounts.entries {
  43.                 switch entry {
  44.                 case let .total(_, totalStateValue):
  45.                     totalState = totalStateValue
  46.                 case let .peer(peerId, state):
  47.                     if let state = state, state.isUnread {
  48.                         if let peerView = view.views[.basicPeer(peerId)] as? BasicPeerView, let peer = peerView.peer {
  49.                             let tag = context.account.postbox.seedConfiguration.peerSummaryCounterTags(peer)
  50.                             if let notificationSettings = peerView.notificationSettings as? TelegramPeerNotificationSettings, case .muted = notificationSettings.muteState {
  51.                                 peerTagAndCount[peerId] = (tag, 0)
  52.                             } else {
  53.                                 var peerCount = Int(state.count)
  54.                                 if state.isUnread {
  55.                                     peerCount = max(1, peerCount)
  56.                                 }
  57.                                 peerTagAndCount[peerId] = (tag, peerCount)
  58.                             }
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.            
  64.             var totalUnreadChatCount = 0
  65.             if let totalState = totalState {
  66.                 for (_, counters) in totalState.filteredCounters {
  67.                     totalUnreadChatCount += Int(counters.chatCount)
  68.                 }
  69.             }
  70.            
  71.             var shouldUpdateLayout = false
  72.             for filter in filters {
  73.                 var tags: [PeerSummaryCounterTags] = []
  74.                 if filter.categories.contains(.privateChats) {
  75.                     tags.append(.privateChat)
  76.                 }
  77.                 if filter.categories.contains(.secretChats) {
  78.                     tags.append(.secretChat)
  79.                 }
  80.                 if filter.categories.contains(.privateGroups) {
  81.                     tags.append(.privateGroup)
  82.                 }
  83.                 if filter.categories.contains(.bots) {
  84.                     tags.append(.bot)
  85.                 }
  86.                 if filter.categories.contains(.publicGroups) {
  87.                     tags.append(.publicGroup)
  88.                 }
  89.                 if filter.categories.contains(.channels) {
  90.                     tags.append(.channel)
  91.                 }
  92.                    
  93.                 var count = 0
  94.                 if let totalState = totalState {
  95.                     for tag in tags {
  96.                         if let value = totalState.filteredCounters[tag] {
  97.                             count += Int(value.chatCount)
  98.                         }
  99.                     }
  100.                 }
  101.                 for peerId in filter.includePeers {
  102.                     if let (tag, peerCount) = peerTagAndCount[peerId] {
  103.                         if !tags.contains(tag) {
  104.                             if peerCount != 0 {
  105.                                 count += 1
  106.                             }
  107.                         }
  108.                     }
  109.                 }
  110.                 result.append((filter, count))
  111.             }
  112.            
  113.             return (totalUnreadChatCount, result)
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement