Skip to content

Commit

Permalink
use a dedicated container view in the coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy committed Jan 12, 2024
1 parent 657b50a commit 9e68c9e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
33 changes: 26 additions & 7 deletions DuckDuckGo/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class MainViewFactory {
extension MainViewFactory {

private func createViews() {
createWebViewContainer()
createLogoBackground()
createContentContainer()
createSuggestionTrayContainer()
Expand All @@ -64,13 +65,19 @@ extension MainViewFactory {
createProgressView()
createToolbar()
}

final class WebViewContainerView: UIView { }
private func createWebViewContainer() {
coordinator.webViewContainer = WebViewContainerView()
superview.addSubview(coordinator.webViewContainer)
}

private func createProgressView() {
coordinator.progress = ProgressView()
superview.addSubview(coordinator.progress)
}

class NavigationBarContainer: UIView { }
final class NavigationBarContainer: UIView { }
private func createNavigationBarContainer() {
coordinator.omniBar = OmniBar.loadFromXib()
coordinator.omniBar.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -79,31 +86,31 @@ extension MainViewFactory {
superview.addSubview(coordinator.navigationBarContainer)
}

class NotificationBarContainer: UIView { }
final class NotificationBarContainer: UIView { }
private func createNotificationBarContainer() {
coordinator.notificationBarContainer = NotificationBarContainer()
superview.addSubview(coordinator.notificationBarContainer)
}

class ContentContainer: UIView { }
final class ContentContainer: UIView { }
private func createContentContainer() {
coordinator.contentContainer = ContentContainer()
superview.addSubview(coordinator.contentContainer)
}

class StatusBackgroundView: UIView { }
final class StatusBackgroundView: UIView { }
private func createStatusBackground() {
coordinator.statusBackground = StatusBackgroundView()
superview.addSubview(coordinator.statusBackground)
}

class TabBarContainer: UIView { }
final class TabBarContainer: UIView { }
private func createTabBarContainer() {
coordinator.tabBarContainer = TabBarContainer()
superview.addSubview(coordinator.tabBarContainer)
}

class SuggestionTrayContainer: UIView { }
final class SuggestionTrayContainer: UIView { }
private func createSuggestionTrayContainer() {
coordinator.suggestionTrayContainer = SuggestionTrayContainer()
coordinator.suggestionTrayContainer.isHidden = true
Expand Down Expand Up @@ -136,7 +143,7 @@ extension MainViewFactory {
], animated: true)
}

class LogoBackgroundView: UIView { }
final class LogoBackgroundView: UIView { }
private func createLogoBackground() {
coordinator.logoContainer = LogoBackgroundView()
coordinator.logo = UIImageView(image: UIImage(named: "Logo"))
Expand All @@ -156,6 +163,7 @@ extension MainViewFactory {
extension MainViewFactory {

private func constrainViews() {
constrainWebViewContainer()
constrainLogoBackground()
constrainContentContainer()
constrainSuggestionTrayContainer()
Expand All @@ -166,6 +174,16 @@ extension MainViewFactory {
constrainProgress()
constrainToolbar()
}

private func constrainWebViewContainer() {
let webViewContainer = coordinator.webViewContainer!
NSLayoutConstraint.activate([
webViewContainer.constrainView(superview, by: .width),
webViewContainer.constrainView(superview, by: .height),
webViewContainer.constrainView(superview, by: .centerX),
webViewContainer.constrainView(superview, by: .centerY),
])
}

private func constrainProgress() {
let progress = coordinator.progress!
Expand Down Expand Up @@ -333,6 +351,7 @@ class MainViewCoordinator {
var toolbarFireButton: UIBarButtonItem!
var toolbarForwardButton: UIBarButtonItem!
var toolbarTabSwitcherButton: UIBarButtonItem!
var webViewContainer: UIView!

let constraints = Constraints()

Expand Down
21 changes: 10 additions & 11 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ class MainViewController: UIViewController {
guard let tab = tabManager.current(createIfNeeded: true) else {
fatalError("Unable to create tab")
}
addToView(tab: tab)
addToWebViewContainer(tab: tab)
refreshControls()
} else {
attachHomeScreen()
Expand Down Expand Up @@ -704,7 +704,7 @@ class MainViewController: UIViewController {
controller.chromeDelegate = self
controller.delegate = self

addToView(controller: controller)
addToContentContainer(controller: controller)

refreshControls()
syncService.scheduler.requestSyncImmediately()
Expand Down Expand Up @@ -853,7 +853,7 @@ class MainViewController: UIViewController {
private func addTab(url: URL?, inheritedAttribution: AdClickAttributionLogic.State?) {
let tab = tabManager.add(url: url, inheritedAttribution: inheritedAttribution)
dismissOmniBar()
addToView(tab: tab)
addToWebViewContainer(tab: tab)
}

func select(tabAt index: Int) {
Expand All @@ -867,7 +867,7 @@ class MainViewController: UIViewController {
if tab.link == nil {
attachHomeScreen()
} else {
addToView(tab: tab)
addToWebViewContainer(tab: tab)
refreshControls()
}
tabsBarController?.refresh(tabsModel: tabManager.model, scrollToSelected: true)
Expand All @@ -876,17 +876,16 @@ class MainViewController: UIViewController {
}
}

private func addToView(tab: TabViewController) {
private func addToWebViewContainer(tab: TabViewController) {
removeHomeScreen()
updateFindInPage()
currentTab?.progressWorker.progressBar = nil
currentTab?.chromeDelegate = nil
currentTab?.webView.scrollView.contentInsetAdjustmentBehavior = .never

// addToView(controller: tab)

addChild(tab)
tab.willMove(toParent: self)
view.insertSubview(tab.view, at: 0)
viewCoordinator.webViewContainer.subviews.forEach { $0.removeFromSuperview() }
viewCoordinator.webViewContainer.addSubview(tab.view)
tab.view.frame = self.view.frame
tab.didMove(toParent: self)

Expand All @@ -898,7 +897,7 @@ class MainViewController: UIViewController {
tab.chromeDelegate = self
}

private func addToView(controller: UIViewController) {
private func addToContentContainer(controller: UIViewController) {
viewCoordinator.contentContainer.isHidden = false
addChild(controller)
viewCoordinator.contentContainer.subviews.forEach { $0.removeFromSuperview() }
Expand Down Expand Up @@ -1733,7 +1732,7 @@ extension MainViewController: TabDelegate {
guard self.tabManager.model.tabs.contains(newTab.tabModel) else { return }

self.dismissOmniBar()
self.addToView(tab: newTab)
self.addToWebViewContainer(tab: newTab)
self.refreshOmniBar()
}

Expand Down

0 comments on commit 9e68c9e

Please sign in to comment.