import Foundation import UIKit import SwiftSignalKit import Postbox import TelegramCore import AccountContext enum ChatListSelectionReadOption: Equatable { case all(enabled: Bool) case selective(enabled: Bool) } struct ChatListSelectionOptions: Equatable { let read: ChatListSelectionReadOption let delete: Bool } func chatListSelectionOptions(context: AccountContext, peerIds: Set, filterId: Int32?) -> Signal { if peerIds.isEmpty { if let filterId = filterId { return chatListFilterItems(context: context) |> map { filterItems -> ChatListSelectionOptions in for (filter, unreadCount, _) in filterItems.1 { if filter.id == filterId { return ChatListSelectionOptions(read: .all(enabled: unreadCount != 0), delete: false) } } return ChatListSelectionOptions(read: .all(enabled: false), delete: false) } |> distinctUntilChanged } else { return context.engine.data.subscribe(TelegramEngine.EngineData.Item.Messages.TotalReadCounters()) |> map { readCounters -> ChatListSelectionOptions in var hasUnread = false if readCounters.count(for: .filtered, in: .chats, with: .all) != 0 { hasUnread = true } return ChatListSelectionOptions(read: .all(enabled: hasUnread), delete: false) } |> distinctUntilChanged } } else { return context.engine.data.subscribe(EngineDataList( peerIds.map(TelegramEngine.EngineData.Item.Messages.PeerReadCounters.init) )) |> map { readCounters -> ChatListSelectionOptions in var hasUnread = false for counters in readCounters { if counters.isUnread { hasUnread = true break } } return ChatListSelectionOptions(read: .selective(enabled: hasUnread), delete: true) } |> distinctUntilChanged } } func forumSelectionOptions(context: AccountContext, peerId: PeerId, threadIds: Set, canDelete: Bool) -> Signal { let threadIdsArray = Array(threadIds) var threadSignals: [Signal] = [] for threadId in threadIdsArray { threadSignals.append( context.engine.data.get(TelegramEngine.EngineData.Item.Peer.ThreadData(id: peerId, threadId: threadId)) ) } return combineLatest(threadSignals) |> map { threadDatas -> ChatListSelectionOptions in if threadIds.isEmpty { return ChatListSelectionOptions(read: .selective(enabled: false), delete: false) } else { var hasUnread = false for thread in threadDatas { guard let thread = thread else { continue } if thread.incomingUnreadCount > 0 { hasUnread = true break } } return ChatListSelectionOptions(read: .selective(enabled: hasUnread), delete: canDelete) } } }