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

236 lines
9.8 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
import MastodonAsset
2022-10-08 07:43:06 +02:00
import MastodonCore
import MastodonLocalization
2021-09-22 13:08:09 +02:00
final class SidebarViewModel {
var disposeBag = Set<AnyCancellable>()
// input
let context: AppContext
let authContext: AuthContext?
@Published private var isSidebarDataSourceReady = false
@Published private var isAvatarButtonDataReady = false
2022-05-06 08:29:34 +02:00
@Published var currentTab: MainTabBarController.Tab = .home
2021-09-22 13:08:09 +02:00
// output
var diffableDataSource: UICollectionViewDiffableDataSource<Section, Item>?
2021-10-28 13:17:41 +02:00
var secondaryDiffableDataSource: UICollectionViewDiffableDataSource<Section, Item>?
@Published private(set) var isReadyForWizardAvatarButton = false
2021-10-28 13:17:41 +02:00
init(context: AppContext, authContext: AuthContext?) {
2021-09-22 13:08:09 +02:00
self.context = context
self.authContext = authContext
Publishers.CombineLatest(
$isSidebarDataSourceReady,
$isAvatarButtonDataReady
)
.map { $0 && $1 }
.assign(to: &$isReadyForWizardAvatarButton)
self.isAvatarButtonDataReady = authContext != nil
2021-09-22 13:08:09 +02:00
}
}
extension SidebarViewModel {
enum Section: Int, Hashable, CaseIterable {
2021-10-28 13:17:41 +02:00
case main
case secondary
2021-09-22 13:08:09 +02:00
}
enum Item: Hashable {
case tab(MainTabBarController.Tab)
2021-10-28 13:17:41 +02:00
case setting
case compose
2021-09-22 13:08:09 +02:00
}
}
extension SidebarViewModel {
func setupDiffableDataSource(
2021-10-28 13:17:41 +02:00
collectionView: UICollectionView,
secondaryCollectionView: UICollectionView
2021-09-22 13:08:09 +02:00
) {
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 user = self.authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: self.context.managedObjectContext)?.user
return user?.avatarImageURL()
2021-09-24 13:58:50 +02:00
default:
return nil
}
}()
cell.item = SidebarListContentView.Item(
2022-05-06 08:29:34 +02:00
isActive: false,
2021-10-28 13:17:41 +02:00
title: item.title,
2022-05-06 08:29:34 +02:00
image: item.image,
activeImage: item.selectedImage,
2021-10-28 13:17:41 +02:00
imageURL: imageURL
2021-09-24 13:58:50 +02:00
)
cell.setNeedsUpdateConfiguration()
cell.isAccessibilityElement = true
cell.accessibilityLabel = item.title
2022-05-06 08:29:34 +02:00
self.$currentTab
.receive(on: DispatchQueue.main)
.sink { [weak cell] currentTab in
guard let cell = cell else { return }
cell.item?.isActive = currentTab == item
cell.setNeedsUpdateConfiguration()
}
.store(in: &cell.disposeBag)
switch item {
case .notification:
Publishers.CombineLatest(
2022-05-06 08:29:34 +02:00
self.context.notificationService.unreadNotificationCountDidUpdate,
self.$currentTab
)
.receive(on: DispatchQueue.main)
.sink { [weak cell] authentication, currentTab in
guard let cell = cell else { return }
let hasUnreadPushNotification: Bool = {
guard let accessToken = self.authContext?.mastodonAuthenticationBox.userAuthorization.accessToken else { return false }
let count = UserDefaults.shared.getNotificationCountWithAccessToken(accessToken: accessToken)
return count > 0
}()
2022-05-06 08:29:34 +02:00
let image: UIImage = {
if currentTab == .notification {
return hasUnreadPushNotification ? Asset.ObjectsAndTools.bellBadgeFill.image.withRenderingMode(.alwaysTemplate) : Asset.ObjectsAndTools.bellFill.image.withRenderingMode(.alwaysTemplate)
} else {
return hasUnreadPushNotification ? Asset.ObjectsAndTools.bellBadge.image.withRenderingMode(.alwaysTemplate) : Asset.ObjectsAndTools.bell.image.withRenderingMode(.alwaysTemplate)
}
}()
cell.item?.image = image
cell.item?.activeImage = image
cell.setNeedsUpdateConfiguration()
}
.store(in: &cell.disposeBag)
case .me:
guard let user = self.authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: self.context.managedObjectContext)?.user else { return }
let currentUserDisplayName = user.displayNameWithFallback
cell.accessibilityHint = L10n.Scene.AccountList.tabBarHint(currentUserDisplayName)
default:
break
}
}
2021-10-28 13:17:41 +02:00
let cellRegistration = UICollectionView.CellRegistration<SidebarListCollectionViewCell, SidebarListContentView.Item> { [weak self] cell, indexPath, item in
2022-05-06 08:29:34 +02:00
guard let _ = self else { return }
2021-10-28 13:17:41 +02:00
cell.item = item
cell.setNeedsUpdateConfiguration()
cell.isAccessibilityElement = true
cell.accessibilityLabel = item.title
2021-09-22 13:08:09 +02:00
}
2021-10-28 13:17:41 +02:00
// header
let headerRegistration = UICollectionView.SupplementaryRegistration<SidebarListHeaderView>(elementKind: UICollectionView.elementKindSectionHeader) { supplementaryView, elementKind, indexPath in
// do nothing
2021-09-22 13:08:09 +02:00
}
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)
2021-10-28 13:17:41 +02:00
case .setting:
let item = SidebarListContentView.Item(
2022-05-06 08:29:34 +02:00
isActive: false,
2021-10-28 13:17:41 +02:00
title: L10n.Common.Controls.Actions.settings,
2022-05-06 08:29:34 +02:00
image: Asset.ObjectsAndTools.gear.image.withRenderingMode(.alwaysTemplate),
activeImage: Asset.ObjectsAndTools.gear.image.withRenderingMode(.alwaysTemplate),
2021-10-28 13:17:41 +02:00
imageURL: nil
)
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
case .compose:
let item = SidebarListContentView.Item(
2022-05-06 08:29:34 +02:00
isActive: false,
2021-11-10 11:00:16 +01:00
title: L10n.Common.Controls.Actions.compose,
2022-05-06 08:29:34 +02:00
image: Asset.ObjectsAndTools.squareAndPencil.image.withRenderingMode(.alwaysTemplate),
activeImage: Asset.ObjectsAndTools.squareAndPencil.image.withRenderingMode(.alwaysTemplate),
2021-10-28 13:17:41 +02:00
imageURL: nil
)
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
}
}
_diffableDataSource.supplementaryViewProvider = { collectionView, elementKind, indexPath in
switch elementKind {
case UICollectionView.elementKindSectionHeader:
return collectionView.dequeueConfiguredReusableSupplementary(using: headerRegistration, for: indexPath)
default:
assertionFailure()
return UICollectionReusableView()
2021-09-22 13:08:09 +02:00
}
}
diffableDataSource = _diffableDataSource
2021-09-22 13:08:09 +02:00
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
2021-10-28 13:17:41 +02:00
snapshot.appendSections([.main])
2021-09-22 13:08:09 +02:00
2021-10-28 13:17:41 +02:00
var sectionSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
let items: [Item] = [
.tab(.home),
.tab(.search),
.tab(.notification),
.tab(.me),
.setting,
]
sectionSnapshot.append(items, to: nil)
// animatingDifferences must to be `true`
// otherwise the UI layout will infinity loop
_diffableDataSource.apply(sectionSnapshot, to: .main, animatingDifferences: true) { [weak self] in
guard let self = self else { return }
self.isSidebarDataSourceReady = true
}
2021-10-28 13:17:41 +02:00
// secondary
let _secondaryDiffableDataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: secondaryCollectionView) { collectionView, indexPath, item in
guard case .compose = item else {
assertionFailure()
return UICollectionViewCell()
2021-09-22 13:08:09 +02:00
}
2021-10-28 13:17:41 +02:00
let item = SidebarListContentView.Item(
2022-05-06 08:29:34 +02:00
isActive: false,
2021-11-10 10:58:37 +01:00
title: L10n.Common.Controls.Actions.compose,
2022-05-06 08:29:34 +02:00
image: Asset.ObjectsAndTools.squareAndPencil.image.withRenderingMode(.alwaysTemplate),
activeImage: Asset.ObjectsAndTools.squareAndPencil.image.withRenderingMode(.alwaysTemplate),
2021-10-28 13:17:41 +02:00
imageURL: nil
)
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
2021-09-22 13:08:09 +02:00
}
2022-05-06 08:29:34 +02:00
2021-10-28 13:17:41 +02:00
secondaryDiffableDataSource = _secondaryDiffableDataSource
2021-09-22 13:08:09 +02:00
2021-10-28 13:17:41 +02:00
var secondarySnapshot = NSDiffableDataSourceSnapshot<Section, Item>()
secondarySnapshot.appendSections([.secondary])
2021-10-28 13:17:41 +02:00
var secondarySectionSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
let secondarySectionItems: [Item] = [
.compose,
]
secondarySectionSnapshot.append(secondarySectionItems, to: nil)
_secondaryDiffableDataSource.apply(secondarySectionSnapshot, to: .secondary)
2021-09-22 13:08:09 +02:00
}
}