2020-10-28 17:57:34 -07:00
|
|
|
//
|
|
|
|
|
// TabBarViewController.swift
|
|
|
|
|
// App
|
|
|
|
|
//
|
|
|
|
|
// Created by James Magahern on 10/28/20.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
|
|
class TabBarViewController: UIViewController, TabBarViewDataSource, TabBarViewDelegate
|
|
|
|
|
{
|
|
|
|
|
let tabBarView = TabBarView()
|
|
|
|
|
|
|
|
|
|
private var tabController: TabController
|
|
|
|
|
|
|
|
|
|
private var tabObserver: AnyCancellable?
|
|
|
|
|
private var activeTabIndexObserver: AnyCancellable?
|
|
|
|
|
|
|
|
|
|
override func loadView() {
|
|
|
|
|
self.view = tabBarView
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(tabController: TabController) {
|
|
|
|
|
self.tabController = tabController
|
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
|
|
|
|
|
|
tabBarView.dataSource = self
|
|
|
|
|
tabBarView.delegate = self
|
|
|
|
|
tabBarView.reloadTabs()
|
|
|
|
|
|
2021-03-30 15:39:24 -07:00
|
|
|
tabObserver = tabController.$tabs.sink(receiveValue: { [unowned self] (newTabs: [Tab]) in
|
2022-02-21 16:01:01 -08:00
|
|
|
DispatchQueue.main.async { self.tabBarView.reloadTabs() }
|
2020-10-28 17:57:34 -07:00
|
|
|
})
|
|
|
|
|
|
2021-03-30 15:39:24 -07:00
|
|
|
activeTabIndexObserver = tabController.$activeTabIndex.sink(receiveValue: { [unowned self] (activeIndex: Int) in
|
2020-10-28 17:57:34 -07:00
|
|
|
tabBarView.activateTab(atIndex: activeIndex)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: TabBarViewDelegate
|
|
|
|
|
func tabBarView(_ tabBarView: TabBarView, didClickToActivateTabAtIndex index: Int) {
|
|
|
|
|
tabController.activeTabIndex = index
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func tabBarView(_ tabBarView: TabBarView, didClickToCloseTabAtIndex index: Int) {
|
|
|
|
|
let tab = tabController.tabs[index]
|
|
|
|
|
tabController.closeTab(tab)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: TabBarViewDataSource
|
|
|
|
|
func numberOfTabs(forTabBarView: TabBarView) -> Int {
|
|
|
|
|
return tabController.tabs.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func tabBarView(_ tabBarView: TabBarView, titleForTabAtIndex index: Int) -> String {
|
|
|
|
|
let defaultTitle = "New Tab"
|
|
|
|
|
|
|
|
|
|
if let title = tabController.tabs[index].title, title.count > 0 {
|
|
|
|
|
return title
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return defaultTitle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func tabBarView(_ tabBarView: TabBarView, imageForTabAtIndex index: Int) -> UIImage? {
|
|
|
|
|
tabController.tabs[index].favicon
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 18:13:38 -08:00
|
|
|
func tabBarView(_ tabBarView: TabBarView, uniqueIdentifierForTabAtIndex index: Int) -> UUID {
|
|
|
|
|
tabController.tabs[index].identifier
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 17:57:34 -07:00
|
|
|
}
|