Tab Bar: Adds tab bar view/view controller
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
// Created by James Magahern on 7/21/20.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import UIKit
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
@@ -18,6 +19,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
var webView: WKWebView { tab.webView }
|
||||
|
||||
private let tabController = TabController()
|
||||
private let tabBarViewController: TabBarViewController
|
||||
private let toolbarController = ToolbarViewController()
|
||||
private let findOnPageController = FindOnPageViewController()
|
||||
|
||||
@@ -32,6 +34,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
private var loadingObservation: NSKeyValueObservation?
|
||||
private var backButtonObservation: NSKeyValueObservation?
|
||||
private var forwardButtonObservation: NSKeyValueObservation?
|
||||
private var activeTabObservation: AnyCancellable?
|
||||
|
||||
private var loadError: Error?
|
||||
|
||||
@@ -39,10 +42,12 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
|
||||
init() {
|
||||
self.tab = tabController.tabs.first!
|
||||
self.tabBarViewController = TabBarViewController(tabController: tabController)
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
|
||||
addChild(toolbarController)
|
||||
addChild(findOnPageController)
|
||||
addChild(tabBarViewController)
|
||||
|
||||
didChangeTab(tab)
|
||||
}
|
||||
@@ -52,6 +57,7 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
override func loadView() {
|
||||
browserView.toolbarView = toolbarController.toolbarView
|
||||
browserView.findOnPageView = findOnPageController.findOnPageView
|
||||
browserView.tabBarView = tabBarViewController.tabBarView
|
||||
|
||||
// Refresh button
|
||||
toolbarController.urlBar.refreshButton.addAction(UIAction(handler: { [unowned self] action in
|
||||
@@ -215,6 +221,16 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
browserView.setFindOnPageVisible(false, animated: true)
|
||||
}), for: .touchUpInside)
|
||||
|
||||
// Tab controller
|
||||
activeTabObservation = tabController.$activeTabIndex
|
||||
.receive(on: RunLoop.main)
|
||||
.sink(receiveValue: { [unowned self] (activeTab: Int) in
|
||||
let tab = tabController.tabs[activeTab]
|
||||
if self.tab != tab {
|
||||
self.tab = tab
|
||||
}
|
||||
})
|
||||
|
||||
self.view = browserView
|
||||
}
|
||||
|
||||
@@ -236,9 +252,19 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
} else {
|
||||
toolbarController.urlBar.textField.text = ""
|
||||
}
|
||||
|
||||
// Figure out which tab this corresponds to
|
||||
let tab = tabController.tabs.first { $0.webView == webView }
|
||||
if let tab = tab, let tabIndex = tabController.tabs.firstIndex(of: tab) {
|
||||
tabBarViewController.tabBarView.reloadTab(atIndex: tabIndex)
|
||||
}
|
||||
}
|
||||
|
||||
private func didChangeTab(_ tab: Tab) {
|
||||
if let activeIndex = tabController.tabs.firstIndex(of: tab) {
|
||||
tabController.activeTabIndex = activeIndex
|
||||
}
|
||||
|
||||
tab.delegate = self
|
||||
|
||||
let webView = tab.webView
|
||||
@@ -253,6 +279,9 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, WKUIDelegat
|
||||
// Autocomplete view
|
||||
browserView.autocompleteView = autocompleteViewController.view
|
||||
|
||||
// Show tab bar view?
|
||||
browserView.tabBarViewVisible = tabController.tabs.count > 1
|
||||
|
||||
// Load progress
|
||||
updateLoadProgress(forWebView: webView)
|
||||
loadingObservation = webView.observe(\.estimatedProgress) { [unowned self] (webView, observedChange) in
|
||||
|
||||
Reference in New Issue
Block a user