mastodon-ios/Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift

344 lines
15 KiB
Swift
Raw Normal View History

2021-09-22 13:08:09 +02:00
//
// SidebarViewModel.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-9-22.
//
import UIKit
import Combine
import CoreData
import CoreDataStack
2021-09-24 13:58:50 +02:00
import Meta
import MastodonMeta
2021-09-22 13:08:09 +02:00
final class SidebarViewModel {
var disposeBag = Set<AnyCancellable>()
// input
let context: AppContext
let searchHistoryFetchedResultController: SearchHistoryFetchedResultController
2021-09-22 13:08:09 +02:00
// output
var diffableDataSource: UICollectionViewDiffableDataSource<Section, Item>?
let activeMastodonAuthenticationObjectID = CurrentValueSubject<NSManagedObjectID?, Never>(nil)
2021-09-22 13:08:09 +02:00
init(context: AppContext) {
self.context = context
self.searchHistoryFetchedResultController = SearchHistoryFetchedResultController(managedObjectContext: context.managedObjectContext)
context.authenticationService.activeMastodonAuthentication
.sink { [weak self] authentication in
guard let self = self else { return }
// bind search history
self.searchHistoryFetchedResultController.domain.value = authentication?.domain
self.searchHistoryFetchedResultController.userID.value = authentication?.userID
// bind objectID
self.activeMastodonAuthenticationObjectID.value = authentication?.objectID
}
.store(in: &disposeBag)
try? searchHistoryFetchedResultController.fetchedResultsController.performFetch()
2021-09-22 13:08:09 +02:00
}
}
extension SidebarViewModel {
enum Section: Int, Hashable, CaseIterable {
2021-09-22 13:08:09 +02:00
case tab
case account
}
enum Item: Hashable {
case tab(MainTabBarController.Tab)
case searchHistory(SearchHistoryViewModel)
2021-09-22 13:08:09 +02:00
case header(HeaderViewModel)
case account(AccountViewModel)
case addAccount
}
struct SearchHistoryViewModel: Hashable {
let searchHistoryObjectID: NSManagedObjectID
}
2021-09-22 13:08:09 +02:00
struct HeaderViewModel: Hashable {
let title: String
}
struct AccountViewModel: Hashable {
let authenticationObjectID: NSManagedObjectID
}
struct AddAccountViewModel: Hashable {
let id = UUID()
}
}
extension SidebarViewModel {
func setupDiffableDataSource(
collectionView: UICollectionView
) {
let tabCellRegistration = UICollectionView.CellRegistration<SidebarListCollectionViewCell, MainTabBarController.Tab> { [weak self] cell, indexPath, item in
guard let self = self else { return }
2021-09-24 13:58:50 +02:00
let imageURL: URL? = {
switch item {
case .me:
let authentication = self.context.authenticationService.activeMastodonAuthentication.value
return authentication?.user.avatarImageURL()
default:
return nil
}
}()
let headline: MetaContent = {
switch item {
case .me:
return PlaintextMetaContent(string: item.title)
// TODO:
// return PlaintextMetaContent(string: "Myself")
default:
return PlaintextMetaContent(string: item.title)
}
}()
let needsOutlineDisclosure = item == .search
2021-09-24 13:58:50 +02:00
cell.item = SidebarListContentView.Item(
image: item.sidebarImage,
imageURL: imageURL,
headline: headline,
subheadline: nil,
needsOutlineDisclosure: needsOutlineDisclosure
2021-09-24 13:58:50 +02:00
)
cell.setNeedsUpdateConfiguration()
switch item {
case .notification:
Publishers.CombineLatest(
self.context.authenticationService.activeMastodonAuthentication,
self.context.notificationService.unreadNotificationCountDidUpdate
)
.receive(on: DispatchQueue.main)
.sink { [weak cell] authentication, _ in
guard let cell = cell else { return }
let hasUnreadPushNotification: Bool = authentication.flatMap { authentication in
let count = UserDefaults.shared.getNotificationCountWithAccessToken(accessToken: authentication.userAccessToken)
return count > 0
} ?? false
let image = hasUnreadPushNotification ? UIImage(systemName: "bell.badge")! : UIImage(systemName: "bell")!
cell._contentView?.imageView.image = image
}
.store(in: &cell.disposeBag)
default:
break
}
}
let searchHistoryCellRegistration = UICollectionView.CellRegistration<SidebarListCollectionViewCell, SearchHistoryViewModel> { [weak self] cell, indexPath, item in
guard let self = self else { return }
let managedObjectContext = self.searchHistoryFetchedResultController.fetchedResultsController.managedObjectContext
guard let searchHistory = try? managedObjectContext.existingObject(with: item.searchHistoryObjectID) as? SearchHistory else { return }
if let account = searchHistory.account {
let headline: MetaContent = {
do {
let content = MastodonContent(content: account.displayNameWithFallback, emojis: account.emojiMeta)
return try MastodonMetaContent.convert(document: content)
} catch {
return PlaintextMetaContent(string: account.displayNameWithFallback)
}
}()
cell.item = SidebarListContentView.Item(
image: .placeholder(color: .systemFill),
imageURL: account.avatarImageURL(),
headline: headline,
subheadline: PlaintextMetaContent(string: "@" + account.acctWithDomain),
needsOutlineDisclosure: false
)
} else if let hashtag = searchHistory.hashtag {
let image = UIImage(systemName: "number.square.fill")!.withRenderingMode(.alwaysTemplate)
let headline = PlaintextMetaContent(string: "#" + hashtag.name)
cell.item = SidebarListContentView.Item(
image: image,
imageURL: nil,
headline: headline,
subheadline: nil,
needsOutlineDisclosure: false
)
} else {
assertionFailure()
}
cell.setNeedsUpdateConfiguration()
2021-09-22 13:08:09 +02:00
}
let headerRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, HeaderViewModel> { (cell, indexPath, item) in
2021-09-24 13:58:50 +02:00
var content = UIListContentConfiguration.sidebarHeader()
2021-09-22 13:08:09 +02:00
content.text = item.title
cell.contentConfiguration = content
cell.accessories = [.outlineDisclosure()]
}
let accountRegistration = UICollectionView.CellRegistration<SidebarListCollectionViewCell, AccountViewModel> { [weak self] (cell, indexPath, item) in
guard let self = self else { return }
// accounts maybe already sign-out
// check isDeleted before using
guard let authentication = try? AppContext.shared.managedObjectContext.existingObject(with: item.authenticationObjectID) as? MastodonAuthentication,
!authentication.isDeleted else {
return
}
2021-09-24 13:58:50 +02:00
let user = authentication.user
let imageURL = user.avatarImageURL()
let headline: MetaContent = {
do {
let content = MastodonContent(content: user.displayNameWithFallback, emojis: user.emojiMeta)
return try MastodonMetaContent.convert(document: content)
} catch {
return PlaintextMetaContent(string: user.displayNameWithFallback)
}
}()
cell.item = SidebarListContentView.Item(
image: .placeholder(color: .systemFill),
imageURL: imageURL,
headline: headline,
subheadline: PlaintextMetaContent(string: "@" + user.acctWithDomain),
needsOutlineDisclosure: false
2021-09-24 13:58:50 +02:00
)
cell.setNeedsUpdateConfiguration()
// FIXME: use notification, not timer
let accessToken = authentication.userAccessToken
AppContext.shared.timestampUpdatePublisher
.map { _ in UserDefaults.shared.getNotificationCountWithAccessToken(accessToken: accessToken) }
.removeDuplicates()
.receive(on: DispatchQueue.main)
.sink { [weak cell] count in
guard let cell = cell else { return }
cell._contentView?.badgeButton.setBadge(number: count)
}
.store(in: &cell.disposeBag)
let authenticationObjectID = item.authenticationObjectID
self.activeMastodonAuthenticationObjectID
.receive(on: DispatchQueue.main)
.sink { [weak cell] objectID in
guard let cell = cell else { return }
cell._contentView?.checkmarkImageView.isHidden = authenticationObjectID != objectID
}
.store(in: &cell.disposeBag)
2021-09-22 13:08:09 +02:00
}
let addAccountRegistration = UICollectionView.CellRegistration<SidebarAddAccountCollectionViewCell, AddAccountViewModel> { (cell, indexPath, item) in
var content = UIListContentConfiguration.sidebarCell()
2021-09-22 13:08:09 +02:00
content.text = L10n.Scene.AccountList.addAccount
content.image = UIImage(systemName: "plus.square.fill")!
2021-09-22 13:08:09 +02:00
cell.contentConfiguration = content
cell.accessories = []
}
let _diffableDataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) { collectionView, indexPath, item in
2021-09-22 13:08:09 +02:00
switch item {
case .tab(let tab):
2021-09-24 13:58:50 +02:00
return collectionView.dequeueConfiguredReusableCell(using: tabCellRegistration, for: indexPath, item: tab)
case .searchHistory(let viewModel):
return collectionView.dequeueConfiguredReusableCell(using: searchHistoryCellRegistration, for: indexPath, item: viewModel)
2021-09-22 13:08:09 +02:00
case .header(let viewModel):
return collectionView.dequeueConfiguredReusableCell(using: headerRegistration, for: indexPath, item: viewModel)
case .account(let viewModel):
return collectionView.dequeueConfiguredReusableCell(using: accountRegistration, for: indexPath, item: viewModel)
case .addAccount:
return collectionView.dequeueConfiguredReusableCell(using: addAccountRegistration, for: indexPath, item: AddAccountViewModel())
}
}
diffableDataSource = _diffableDataSource
2021-09-22 13:08:09 +02:00
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections(Section.allCases)
_diffableDataSource.apply(snapshot)
2021-09-22 13:08:09 +02:00
for section in Section.allCases {
switch section {
case .tab:
var sectionSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
let items: [Item] = [
.tab(.home),
.tab(.search),
.tab(.notification),
.tab(.me),
]
sectionSnapshot.append(items, to: nil)
_diffableDataSource.apply(sectionSnapshot, to: section)
2021-09-22 13:08:09 +02:00
case .account:
var sectionSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
let headerItem = Item.header(HeaderViewModel(title: "Accounts"))
sectionSnapshot.append([headerItem], to: nil)
sectionSnapshot.append([], to: headerItem)
sectionSnapshot.append([.addAccount], to: headerItem)
sectionSnapshot.expand([headerItem])
_diffableDataSource.apply(sectionSnapshot, to: section)
2021-09-22 13:08:09 +02:00
}
}
// update .search tab
searchHistoryFetchedResultController.objectIDs
.removeDuplicates()
.receive(on: DispatchQueue.main)
.sink { [weak self] objectIDs in
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
// update .search tab
var sectionSnapshot = diffableDataSource.snapshot(for: .tab)
// remove children
let searchHistorySnapshot = sectionSnapshot.snapshot(of: .tab(.search))
sectionSnapshot.delete(searchHistorySnapshot.items)
// append children
let managedObjectContext = self.searchHistoryFetchedResultController.fetchedResultsController.managedObjectContext
let items: [Item] = objectIDs.compactMap { objectID -> Item? in
guard let searchHistory = try? managedObjectContext.existingObject(with: objectID) as? SearchHistory else { return nil }
guard searchHistory.account != nil || searchHistory.hashtag != nil else { return nil }
let viewModel = SearchHistoryViewModel(searchHistoryObjectID: objectID)
return Item.searchHistory(viewModel)
}
sectionSnapshot.append(Array(items.prefix(5)), to: .tab(.search))
sectionSnapshot.expand([.tab(.search)])
// apply snapshot
diffableDataSource.apply(sectionSnapshot, to: .tab, animatingDifferences: false)
}
.store(in: &disposeBag)
// update .me tab and .account section
2021-09-22 13:08:09 +02:00
context.authenticationService.mastodonAuthentications
.receive(on: DispatchQueue.main)
.sink { [weak self] authentications in
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
2021-09-24 13:58:50 +02:00
// tab
var snapshot = diffableDataSource.snapshot()
2021-09-24 13:58:50 +02:00
snapshot.reloadItems([.tab(.me)])
diffableDataSource.apply(snapshot)
2021-09-24 13:58:50 +02:00
// account
var accountSectionSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
2021-09-22 13:08:09 +02:00
let headerItem = Item.header(HeaderViewModel(title: "Accounts"))
2021-09-24 13:58:50 +02:00
accountSectionSnapshot.append([headerItem], to: nil)
let accountItems = authentications.map { authentication in
2021-09-22 13:08:09 +02:00
Item.account(AccountViewModel(authenticationObjectID: authentication.objectID))
}
2021-09-24 13:58:50 +02:00
accountSectionSnapshot.append(accountItems, to: headerItem)
accountSectionSnapshot.append([.addAccount], to: headerItem)
accountSectionSnapshot.expand([headerItem])
diffableDataSource.apply(accountSectionSnapshot, to: .account)
2021-09-22 13:08:09 +02:00
}
.store(in: &disposeBag)
}
}