mastodon-app-ufficiale-ipho.../Mastodon/Scene/MainTab/MainTabBarController.swift

218 lines
9.2 KiB
Swift
Raw Normal View History

2021-01-27 07:50:13 +01:00
//
// MainTabBarController.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-1-27.
//
import os.log
import UIKit
import Combine
import SafariServices
class MainTabBarController: UITabBarController {
var disposeBag = Set<AnyCancellable>()
weak var context: AppContext!
weak var coordinator: SceneCoordinator!
enum Tab: Int, CaseIterable {
case home
2021-02-23 09:45:00 +01:00
case search
case notification
case me
2021-01-27 07:50:13 +01:00
var title: String {
switch self {
case .home: return L10n.Common.Controls.Tabs.home
case .search: return L10n.Common.Controls.Tabs.search
case .notification: return L10n.Common.Controls.Tabs.notification
case .me: return L10n.Common.Controls.Tabs.profile
2021-01-27 07:50:13 +01:00
}
}
var image: UIImage {
switch self {
2021-02-23 09:45:00 +01:00
case .home: return UIImage(systemName: "house.fill")!
case .search: return UIImage(systemName: "magnifyingglass")!
case .notification: return UIImage(systemName: "bell.fill")!
case .me: return UIImage(systemName: "person.fill")!
2021-01-27 07:50:13 +01:00
}
}
func viewController(context: AppContext, coordinator: SceneCoordinator) -> UIViewController {
let viewController: UIViewController
switch self {
case .home:
2021-02-07 07:42:50 +01:00
let _viewController = HomeTimelineViewController()
2021-01-27 07:50:13 +01:00
_viewController.context = context
_viewController.coordinator = coordinator
viewController = _viewController
2021-02-23 09:45:00 +01:00
case .search:
let _viewController = SearchViewController()
_viewController.context = context
_viewController.coordinator = coordinator
viewController = _viewController
case .notification:
let _viewController = NotificationViewController()
_viewController.context = context
_viewController.coordinator = coordinator
viewController = _viewController
case .me:
let _viewController = ProfileViewController()
2021-01-28 09:10:30 +01:00
_viewController.context = context
_viewController.coordinator = coordinator
2021-04-01 08:39:15 +02:00
_viewController.viewModel = MeProfileViewModel(context: context)
2021-01-28 09:10:30 +01:00
viewController = _viewController
2021-01-27 07:50:13 +01:00
}
viewController.title = self.title
2021-04-01 08:39:15 +02:00
return AdaptiveStatusBarStyleNavigationController(rootViewController: viewController)
2021-01-27 07:50:13 +01:00
}
}
init(context: AppContext, coordinator: SceneCoordinator) {
self.context = context
self.coordinator = coordinator
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension MainTabBarController {
2021-04-01 08:39:15 +02:00
open override var childForStatusBarStyle: UIViewController? {
return selectedViewController
}
2021-01-27 07:50:13 +01:00
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
let tabs = Tab.allCases
let viewControllers: [UIViewController] = tabs.map { tab in
let viewController = tab.viewController(context: context, coordinator: coordinator)
viewController.tabBarItem.title = "" // set text to empty string for image only style (SDK failed to layout when set to nil)
viewController.tabBarItem.image = tab.image
viewController.tabBarItem.accessibilityLabel = tab.title
2021-01-27 07:50:13 +01:00
return viewController
}
setViewControllers(viewControllers, animated: false)
selectedIndex = 0
// TODO: custom accent color
2021-04-01 08:39:15 +02:00
// let tabBarAppearance = UITabBarAppearance()
// tabBarAppearance.configureWithDefaultBackground()
// tabBar.standardAppearance = tabBarAppearance
2021-02-03 09:01:08 +01:00
context.apiService.error
.receive(on: DispatchQueue.main)
.sink { [weak self] error in
guard let self = self, let coordinator = self.coordinator else { return }
switch error {
case .implicit:
break
case .explicit:
let alertController = UIAlertController(for: error, title: nil, preferredStyle: .alert)
2021-02-03 09:01:08 +01:00
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
coordinator.present(
scene: .alertController(alertController: alertController),
from: nil,
transition: .alertController(animated: true, completion: nil)
)
}
}
.store(in: &disposeBag)
// handle post failure
context.statusPublishService
.latestPublishingComposeViewModel
.receive(on: DispatchQueue.main)
.sink { [weak self] composeViewModel in
guard let self = self else { return }
guard let composeViewModel = composeViewModel else { return }
guard let currentState = composeViewModel.publishStateMachine.currentState else { return }
guard currentState is ComposeViewModel.PublishState.Fail else { return }
let alertController = UIAlertController(title: L10n.Common.Alerts.PublishPostFailure.title, message: L10n.Common.Alerts.PublishPostFailure.message, preferredStyle: .alert)
let discardAction = UIAlertAction(title: L10n.Common.Controls.Actions.discard, style: .destructive) { [weak self, weak composeViewModel] _ in
guard let self = self else { return }
guard let composeViewModel = composeViewModel else { return }
self.context.statusPublishService.remove(composeViewModel: composeViewModel)
}
alertController.addAction(discardAction)
let retryAction = UIAlertAction(title: L10n.Common.Controls.Actions.tryAgain, style: .default) { [weak composeViewModel] _ in
guard let composeViewModel = composeViewModel else { return }
composeViewModel.publishStateMachine.enter(ComposeViewModel.PublishState.Publishing.self)
}
alertController.addAction(retryAction)
self.present(alertController, animated: true, completion: nil)
}
.store(in: &disposeBag)
2021-01-27 07:50:13 +01:00
2021-04-27 11:27:03 +02:00
// handle push notification. toggle entry when finish fetch latest notification
context.notificationService.hasUnreadPushNotification
.receive(on: DispatchQueue.main)
.sink { [weak self] hasUnreadPushNotification in
guard let self = self else { return }
guard let notificationViewController = self.notificationViewController else { return }
let image = hasUnreadPushNotification ? UIImage(systemName: "bell.badge.fill")! : UIImage(systemName: "bell.fill")!
notificationViewController.tabBarItem.image = image
notificationViewController.navigationController?.tabBarItem.image = image
}
.store(in: &disposeBag)
context.notificationService.requestRevealNotificationPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] notificationID in
guard let self = self else { return }
self.coordinator.switchToTabBar(tab: .notification)
let threadViewModel = RemoteThreadViewModel(context: self.context, notificationID: notificationID)
self.coordinator.present(scene: .thread(viewModel: threadViewModel), from: nil, transition: .show)
}
.store(in: &disposeBag)
2021-01-27 07:50:13 +01:00
}
}
2021-04-27 11:27:03 +02:00
extension MainTabBarController {
var notificationViewController: NotificationViewController? {
return viewController(of: NotificationViewController.self)
}
}
2021-05-19 09:15:19 +02:00
extension MainTabBarController {
override var keyCommands: [UIKeyCommand]? {
var commands: [UIKeyCommand] = []
for (i, tab) in Tab.allCases.enumerated() {
let title = L10n.Common.Controls.Tabs.Keyboard.switchToTab(tab.title)
let input = String(i + 1)
let command = UIKeyCommand(title: title, image: nil, action: #selector(MainTabBarController.switchToTab(_:)), input: input, modifierFlags: .control, propertyList: tab.rawValue, alternates: [], discoverabilityTitle: nil, attributes: [], state: .off)
commands.append(command)
}
return commands
}
@objc private func switchToTab(_ sender: UIKeyCommand) {
guard let rawValue = sender.propertyList as? Int,
let tab = Tab(rawValue: rawValue) else { return }
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: %s", ((#file as NSString).lastPathComponent), #line, #function, tab.title)
guard let index = Tab.allCases.firstIndex(of: tab) else { return }
selectedIndex = index
}
}