-
-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathAccountListViewModel.swift
More file actions
156 lines (132 loc) · 5.49 KB
/
AccountListViewModel.swift
File metadata and controls
156 lines (132 loc) · 5.49 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// AccountListViewModel.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-9-13.
//
import UIKit
import Combine
import CoreData
import CoreDataStack
import MastodonSDK
import MastodonMeta
import MastodonCore
import MastodonUI
@MainActor
final class AccountListViewModel: NSObject {
var disposeBag = Set<AnyCancellable>()
// input
let authenticationBox: MastodonAuthenticationBox
// output
@Published var items: [Item] = []
var diffableDataSource: UITableViewDiffableDataSource<Section, Item>!
init(authenticationBox: MastodonAuthenticationBox) {
self.authenticationBox = authenticationBox
super.init()
// end init
AuthenticationServiceProvider.shared.$mastodonAuthenticationBoxes
.receive(on: DispatchQueue.main)
.sink { [weak self] authentications in
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
let authenticationItems: [Item] = authentications.map {
Item.authentication(record: $0.authentication)
}
snapshot.appendItems(authenticationItems, toSection: .main)
snapshot.appendItems([.addAccount], toSection: .main)
if authentications.count > 1 {
snapshot.appendItems([.logoutOfAllAccounts], toSection: .main)
}
diffableDataSource.apply(snapshot, animatingDifferences: false)
}
.store(in: &disposeBag)
}
}
extension AccountListViewModel {
enum Section: Hashable {
case main
}
enum Item: Hashable {
case authentication(record: MastodonAuthentication)
case addAccount
case logoutOfAllAccounts
}
func setupDiffableDataSource(tableView: UITableView) {
diffableDataSource = UITableViewDiffableDataSource(tableView: tableView) { tableView, indexPath, item in
switch item {
case .authentication(let record):
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AccountListTableViewCell.self), for: indexPath) as? AccountListTableViewCell else {
assertionFailure("unexpected cell dequeued")
return nil
}
if let activeAuthentication = AuthenticationServiceProvider.shared.currentActiveUser.value
{
AccountListViewModel.configure(
cell: cell,
authentication: record,
activeAuthentication: activeAuthentication.authentication
)
}
return cell
case .addAccount:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AddAccountTableViewCell.self), for: indexPath) as? AddAccountTableViewCell else {
assertionFailure("unexpected cell dequeued")
return nil
}
return cell
case .logoutOfAllAccounts:
guard let cell = tableView.dequeueReusableCell(withIdentifier: LogoutOfAllAccountsCell.reuseIdentifier, for: indexPath) as? LogoutOfAllAccountsCell else {
assertionFailure("unexpected cell dequeued")
return nil
}
return cell
}
}
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
diffableDataSource?.apply(snapshot)
}
static func configure(
cell: AccountListTableViewCell,
authentication: MastodonAuthentication,
activeAuthentication: MastodonAuthentication
) {
guard let account = authentication.cachedAccount() else { return }
// avatar
cell.avatarButton.avatarImageView.configure(with: account.avatarImageURL())
// name
do {
let content = MastodonContent(content: account.displayNameWithFallback, emojis: account.emojis.asDictionary)
let metaContent = try MastodonMetaContent.convert(document: content)
cell.nameLabel.configure(content: metaContent)
} catch {
assertionFailure()
cell.nameLabel.configure(content: PlaintextMetaContent(string: account.displayNameWithFallback))
}
// username
let usernameMetaContent = PlaintextMetaContent(string: "@" + account.acctWithDomain)
cell.usernameLabel.configure(content: usernameMetaContent)
// badge
let accessToken = authentication.userAccessToken
let count = UserDefaults.shared.getNotificationCountWithAccessToken(accessToken: accessToken)
cell.badgeButton.setBadge(number: count)
// checkmark
let isActive = activeAuthentication.userID == authentication.userID
cell.tintColor = .label
cell.checkmarkImageView.isHidden = !isActive
if isActive {
cell.accessibilityTraits.insert(.selected)
} else {
cell.accessibilityTraits.remove(.selected)
}
cell.accessibilityLabel = [
cell.nameLabel.text,
cell.usernameLabel.text,
cell.badgeButton.accessibilityLabel
]
.compactMap { $0 }
.joined(separator: ", ")
}
}