-
Notifications
You must be signed in to change notification settings - Fork 873
/
Copy pathAccountUtils.swift
53 lines (48 loc) · 2.35 KB
/
AccountUtils.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// AccountUtils.swift
// Telegram
//
// Created by Mikhail Filimonov on 02.12.2019.
// Copyright © 2019 Telegram. All rights reserved.
//
import Cocoa
import SwiftSignalKit
import Postbox
import TelegramCore
import InAppSettings
let maximumNumberOfAccounts = 3
func activeAccountsAndPeers(context: AccountContext, includePrimary: Bool = false) -> Signal<((Account, Peer)?, [(Account, Peer, Int32)]), NoError> {
let sharedContext = context.sharedContext
return context.sharedContext.activeAccounts
|> mapToSignal { primary, activeAccounts, _ -> Signal<((Account, Peer)?, [(Account, Peer, Int32)]), NoError> in
var accounts: [Signal<(Account, Peer, Int32)?, NoError>] = []
func accountWithPeer(_ account: Account) -> Signal<(Account, Peer, Int32)?, NoError> {
return combineLatest(account.postbox.peerView(id: account.peerId), renderedTotalUnreadCount(accountManager: sharedContext.accountManager, postbox: account.postbox))
|> map { view, totalUnreadCount -> (Peer?, Int32) in
return (view.peers[view.peerId], totalUnreadCount.0)
}
|> distinctUntilChanged { lhs, rhs in
return arePeersEqual(lhs.0, rhs.0) && lhs.1 == rhs.1
}
|> map { peer, totalUnreadCount -> (Account, Peer, Int32)? in
if let peer = peer {
return (account, peer, totalUnreadCount)
} else {
return nil
}
}
}
for (_, account, _) in activeAccounts {
accounts.append(accountWithPeer(account))
}
return combineLatest(accounts)
|> map { accounts -> ((Account, Peer)?, [(Account, Peer, Int32)]) in
var primaryRecord: (Account, Peer)?
if let first = accounts.filter({ $0?.0.id == primary?.id }).first, let (account, peer, _) = first {
primaryRecord = (account, peer)
}
let accountRecords: [(Account, Peer, Int32)] = (includePrimary ? accounts : accounts.filter({ $0?.0.id != primary?.id })).compactMap({ $0 })
return (primaryRecord, accountRecords)
}
}
}