mastodon-ios/Mastodon/Scene/MainTab/MainTabBarController.swift

118 lines
3.9 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-01-28 09:10:30 +01:00
case publicTimeline
2021-01-27 07:50:13 +01:00
var title: String {
switch self {
case .home: return "Home"
case .publicTimeline : return "Public"
2021-01-27 07:50:13 +01:00
}
}
var image: UIImage {
switch self {
case .home: return UIImage(systemName: "house")!
2021-01-28 09:10:30 +01:00
case .publicTimeline: return UIImage(systemName: "flame")!
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-01-28 09:10:30 +01:00
case .publicTimeline:
let _viewController = PublicTimelineViewController()
_viewController.viewModel = PublicTimelineViewModel(context: context)
_viewController.context = context
_viewController.coordinator = coordinator
viewController = _viewController
2021-01-27 07:50:13 +01:00
}
viewController.title = self.title
return UINavigationController(rootViewController: viewController)
}
}
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 {
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
return viewController
}
setViewControllers(viewControllers, animated: false)
selectedIndex = 0
// TODO: custom accent color
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(error, preferredStyle: .alert)
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)
2021-01-27 07:50:13 +01:00
#if DEBUG
// selectedIndex = 1
#endif
}
}