Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift

243 lines
10 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
import MastodonLocalization
2021-09-22 13:08:09 +02:00
final class SidebarViewModel {
var disposeBag = Set<AnyCancellable>()
// input
let context: AppContext
@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
let activeMastodonAuthenticationObjectID = CurrentValueSubject<NSManagedObjectID?, Never>(nil)
2021-09-22 13:08:09 +02:00
init(context: AppContext) {
self.context = context
Publishers.CombineLatest(
$isSidebarDataSourceReady,
$isAvatarButtonDataReady
)
.map { $0 && $1 }
.assign(to: &$isReadyForWizardAvatarButton)
context.authenticationService.activeMastodonAuthentication
.sink { [weak self] authentication in
guard let self = self else { return }
// bind objectID
self.activeMastodonAuthenticationObjectID.value = authentication?.objectID
self.isAvatarButtonDataReady = authentication != nil
}
.store(in: &disposeBag)
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 authentication = self.context.authenticationService.activeMastodonAuthentication.value
return authentication?.user.avatarImageURL()
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:
2022-05-06 08:29:34 +02:00
Publishers.CombineLatest3(
self.context.authenticationService.activeMastodonAuthentication,
2022-05-06 08:29:34 +02:00
self.context.notificationService.unreadNotificationCountDidUpdate,
self.$currentTab
)
.receive(on: DispatchQueue.main)
2022-05-06 08:29:34 +02:00
.sink { [weak cell] authentication, _, currentTab 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
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 authentication = self.context.authenticationService.activeMastodonAuthentication.value else { break }
2022-05-06 08:29:34 +02:00
let currentUserDisplayName = authentication.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
}
}