2021-09-13 13:14:26 +02:00
|
|
|
//
|
|
|
|
// AccountListViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-9-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-09-15 14:18:19 +02:00
|
|
|
import MastodonSDK
|
2021-09-14 12:21:15 +02:00
|
|
|
import MastodonMeta
|
2021-09-13 13:14:26 +02:00
|
|
|
|
|
|
|
final class AccountListViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
|
|
|
|
|
|
|
// output
|
|
|
|
let authentications = CurrentValueSubject<[Item], Never>([])
|
2021-09-16 06:23:47 +02:00
|
|
|
let activeMastodonUserObjectID = CurrentValueSubject<NSManagedObjectID?, Never>(nil)
|
|
|
|
let dataSourceDidUpdate = PassthroughSubject<Void, Never>()
|
2021-09-13 13:14:26 +02:00
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<Section, Item>!
|
|
|
|
|
|
|
|
init(context: AppContext) {
|
|
|
|
self.context = context
|
|
|
|
|
2021-09-15 14:18:19 +02:00
|
|
|
Publishers.CombineLatest(
|
|
|
|
context.authenticationService.mastodonAuthentications,
|
|
|
|
context.authenticationService.activeMastodonAuthentication
|
|
|
|
)
|
|
|
|
.sink { [weak self] authentications, activeAuthentication in
|
|
|
|
guard let self = self else { return }
|
|
|
|
var items: [Item] = []
|
2021-09-16 06:23:47 +02:00
|
|
|
var activeMastodonUserObjectID: NSManagedObjectID?
|
2021-09-15 14:18:19 +02:00
|
|
|
for authentication in authentications {
|
|
|
|
let item = Item.authentication(objectID: authentication.objectID)
|
|
|
|
items.append(item)
|
|
|
|
if authentication === activeAuthentication {
|
2021-09-16 06:23:47 +02:00
|
|
|
activeMastodonUserObjectID = authentication.user.objectID
|
2021-09-13 13:14:26 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-15 14:18:19 +02:00
|
|
|
self.authentications.value = items
|
2021-09-16 06:23:47 +02:00
|
|
|
self.activeMastodonUserObjectID.value = activeMastodonUserObjectID
|
2021-09-15 14:18:19 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-09-13 13:14:26 +02:00
|
|
|
|
|
|
|
authentications
|
|
|
|
.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])
|
|
|
|
snapshot.appendItems(authentications, toSection: .main)
|
2021-09-14 12:21:15 +02:00
|
|
|
snapshot.appendItems([.addAccount], toSection: .main)
|
2021-09-13 13:14:26 +02:00
|
|
|
|
2021-09-16 06:23:47 +02:00
|
|
|
diffableDataSource.apply(snapshot) {
|
|
|
|
self.dataSourceDidUpdate.send()
|
|
|
|
}
|
2021-09-13 13:14:26 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AccountListViewModel {
|
|
|
|
enum Section: Hashable {
|
|
|
|
case main
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Item: Hashable {
|
|
|
|
case authentication(objectID: NSManagedObjectID)
|
2021-09-14 12:21:15 +02:00
|
|
|
case addAccount
|
2021-09-13 13:14:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupDiffableDataSource(
|
|
|
|
tableView: UITableView,
|
|
|
|
managedObjectContext: NSManagedObjectContext
|
|
|
|
) {
|
|
|
|
diffableDataSource = UITableViewDiffableDataSource(tableView: tableView) { tableView, indexPath, item in
|
|
|
|
switch item {
|
|
|
|
case .authentication(let objectID):
|
|
|
|
let authentication = managedObjectContext.object(with: objectID) as! MastodonAuthentication
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AccountListTableViewCell.self), for: indexPath) as! AccountListTableViewCell
|
2021-09-15 14:18:19 +02:00
|
|
|
AccountListViewModel.configure(
|
|
|
|
cell: cell,
|
2021-09-16 10:30:21 +02:00
|
|
|
authentication: authentication,
|
2021-09-16 06:23:47 +02:00
|
|
|
activeMastodonUserObjectID: self.activeMastodonUserObjectID.eraseToAnyPublisher()
|
2021-09-15 14:18:19 +02:00
|
|
|
)
|
2021-09-14 12:21:15 +02:00
|
|
|
return cell
|
|
|
|
case .addAccount:
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AddAccountTableViewCell.self), for: indexPath) as! AddAccountTableViewCell
|
2021-09-13 13:14:26 +02:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
2021-09-14 12:21:15 +02:00
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
snapshot.appendSections([.main])
|
|
|
|
diffableDataSource?.apply(snapshot)
|
2021-09-13 13:14:26 +02:00
|
|
|
}
|
|
|
|
|
2021-09-14 12:21:15 +02:00
|
|
|
static func configure(
|
|
|
|
cell: AccountListTableViewCell,
|
2021-09-16 10:30:21 +02:00
|
|
|
authentication: MastodonAuthentication,
|
2021-09-16 06:23:47 +02:00
|
|
|
activeMastodonUserObjectID: AnyPublisher<NSManagedObjectID?, Never>
|
2021-09-14 12:21:15 +02:00
|
|
|
) {
|
2021-09-16 10:30:21 +02:00
|
|
|
let user = authentication.user
|
|
|
|
|
2021-09-14 12:21:15 +02:00
|
|
|
// avatar
|
2022-01-27 14:23:39 +01:00
|
|
|
cell.avatarButton.avatarImageView.configure(
|
|
|
|
configuration: .init(url: user.avatarImageURL())
|
|
|
|
)
|
2021-09-14 12:21:15 +02:00
|
|
|
|
|
|
|
// name
|
|
|
|
do {
|
2022-01-27 14:23:39 +01:00
|
|
|
let content = MastodonContent(content: user.displayNameWithFallback, emojis: user.emojis.asDictionary)
|
2021-09-14 12:21:15 +02:00
|
|
|
let metaContent = try MastodonMetaContent.convert(document: content)
|
|
|
|
cell.nameLabel.configure(content: metaContent)
|
|
|
|
} catch {
|
|
|
|
assertionFailure()
|
|
|
|
cell.nameLabel.configure(content: PlaintextMetaContent(string: user.displayNameWithFallback))
|
|
|
|
}
|
|
|
|
|
|
|
|
// username
|
2021-09-15 14:18:19 +02:00
|
|
|
let usernameMetaContent = PlaintextMetaContent(string: "@" + user.acctWithDomain)
|
|
|
|
cell.usernameLabel.configure(content: usernameMetaContent)
|
|
|
|
|
2021-09-16 10:30:21 +02:00
|
|
|
// badge
|
|
|
|
let accessToken = authentication.userAccessToken
|
|
|
|
let count = UserDefaults.shared.getNotificationCountWithAccessToken(accessToken: accessToken)
|
|
|
|
cell.badgeButton.setBadge(number: count)
|
|
|
|
|
2021-09-15 14:18:19 +02:00
|
|
|
// checkmark
|
2021-09-16 06:23:47 +02:00
|
|
|
activeMastodonUserObjectID
|
2021-09-15 14:18:19 +02:00
|
|
|
.receive(on: DispatchQueue.main)
|
2021-09-16 06:23:47 +02:00
|
|
|
.sink { objectID in
|
|
|
|
let isCurrentUser = user.objectID == objectID
|
2021-09-15 14:18:19 +02:00
|
|
|
cell.tintColor = .label
|
2021-09-16 10:30:21 +02:00
|
|
|
cell.checkmarkImageView.isHidden = !isCurrentUser
|
|
|
|
if isCurrentUser {
|
|
|
|
cell.accessibilityTraits.insert(.selected)
|
|
|
|
} else {
|
|
|
|
cell.accessibilityTraits.remove(.selected)
|
|
|
|
}
|
2021-09-15 14:18:19 +02:00
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-09-16 10:30:21 +02:00
|
|
|
|
|
|
|
cell.accessibilityLabel = [
|
|
|
|
cell.nameLabel.text,
|
|
|
|
cell.usernameLabel.text,
|
|
|
|
cell.badgeButton.accessibilityLabel
|
|
|
|
]
|
|
|
|
.compactMap { $0 }
|
|
|
|
.joined(separator: " ")
|
2021-09-14 12:21:15 +02:00
|
|
|
}
|
|
|
|
}
|