Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Scene/Root/ContentSplitViewController....

93 lines
3.6 KiB
Swift
Raw Normal View History

2021-10-28 13:17:41 +02:00
//
// ContentSplitViewController.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-10-28.
//
import os.log
import UIKit
import Combine
import CoreDataStack
final class ContentSplitViewController: UIViewController, NeedsDependency {
var disposeBag = Set<AnyCancellable>()
static let sidebarWidth: CGFloat = 89
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
private(set) lazy var sidebarViewController: SidebarViewController = {
let sidebarViewController = SidebarViewController()
sidebarViewController.context = context
sidebarViewController.coordinator = coordinator
sidebarViewController.viewModel = SidebarViewModel(context: context)
sidebarViewController.delegate = self
return sidebarViewController
}()
@Published var currentSupplementaryTab: MainTabBarController.Tab = .home
private(set) lazy var mainTabBarController: MainTabBarController = {
let mainTabBarController = MainTabBarController(context: context, coordinator: coordinator)
return mainTabBarController
}()
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension ContentSplitViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(true, animated: false)
addChild(sidebarViewController)
sidebarViewController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(sidebarViewController.view)
sidebarViewController.didMove(toParent: self)
NSLayoutConstraint.activate([
sidebarViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
sidebarViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
sidebarViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
sidebarViewController.view.widthAnchor.constraint(equalToConstant: ContentSplitViewController.sidebarWidth),
])
addChild(mainTabBarController)
mainTabBarController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(mainTabBarController.view)
sidebarViewController.didMove(toParent: self)
NSLayoutConstraint.activate([
mainTabBarController.view.topAnchor.constraint(equalTo: view.topAnchor),
mainTabBarController.view.leadingAnchor.constraint(equalTo: sidebarViewController.view.trailingAnchor),
mainTabBarController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
mainTabBarController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
$currentSupplementaryTab
.removeDuplicates()
.sink(receiveValue: { [weak self] tab in
guard let self = self else { return }
self.mainTabBarController.selectedIndex = tab.rawValue
self.mainTabBarController.currentTab.value = tab
})
.store(in: &disposeBag)
}
}
// MARK: - SidebarViewControllerDelegate
extension ContentSplitViewController: SidebarViewControllerDelegate {
func sidebarViewController(_ sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab) {
guard let _ = MainTabBarController.Tab.allCases.firstIndex(of: tab) else {
assertionFailure()
return
}
currentSupplementaryTab = tab
}
}