Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix screentime to not freeze and to set the URL for the back-button g…
Browse files Browse the repository at this point in the history
…litch
  • Loading branch information
Brandon-T committed Jan 4, 2024
1 parent e7eb916 commit 799a50b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
11 changes: 2 additions & 9 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1250,18 +1250,10 @@ public class BrowserViewController: UIViewController {
make.leading.trailing.equalTo(self.view)
}

if let screenTimeViewController = screenTimeViewController {
if screenTimeViewController.parent == nil {
addChild(screenTimeViewController)
screenTimeViewController.didMove(toParent: self)
webViewContainer.addSubview(screenTimeViewController.view)
}

if let screenTimeViewController = screenTimeViewController, screenTimeViewController.parent != nil {
screenTimeViewController.view.snp.remakeConstraints {
$0.edges.equalTo(webViewContainer)
}

webViewContainer.bringSubviewToFront(screenTimeViewController.view)
}

webViewContainer.snp.remakeConstraints { make in
Expand Down Expand Up @@ -1948,6 +1940,7 @@ public class BrowserViewController: UIViewController {
}

updateInContentHomePanel(url as URL)
updateScreenTimeUrl(url)
updatePlaylistURLBar(tab: tab, state: tab.playlistItemState, item: tab.playlistItem)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ extension BrowserViewController {
/// There is also one hack required: STWebpageController breaks if you pass scheme other than http or https,
/// it will not block anything for the rest of its lifecycle. Our internal urls have to be bridged to an empty https url.
func updateScreenTimeUrl(_ url: URL?) {
guard let screenTimeViewController = screenTimeViewController else {
return
}

guard let url = url, (url.scheme == "http" || url.scheme == "https") else {
screenTimeViewController?.view.removeFromSuperview()
screenTimeViewController?.willMove(toParent: nil)
screenTimeViewController?.removeFromParent()
// This is signficantly better than removing the view controller from the screen!
// If we use `nil` instead, STViewController goes into a broken state PERMANENTLY until the app is restarted
// The URL cannot be nil, and it cannot be anything other than http(s) otherwise it will break
// for the duration of the app.
// Chromium solves this issue by not setting the URL, but instead removing it from the view entirely.
// But setting the URL to an empty URL works too.
screenTimeViewController.url = NSURL() as URL
return
}

screenTimeViewController?.url = url
screenTimeViewController.url = url
}

func recordScreenTimeUsage(for tab: Tab) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ extension BrowserViewController: TabManagerDelegate {
webView.snp.remakeConstraints { make in
make.left.right.top.bottom.equalTo(self.webViewContainer)
}

// Add ScreenTime above the WebView
if let screenTimeViewController = screenTimeViewController {
if screenTimeViewController.parent == nil {
addChild(screenTimeViewController)
screenTimeViewController.didMove(toParent: self)
}

webViewContainer.addSubview(screenTimeViewController.view)

screenTimeViewController.view.snp.remakeConstraints {
$0.edges.equalTo(webViewContainer)
}
}

// This is a terrible workaround for a bad iOS 12 bug where PDF
// content disappears any time the view controller changes (i.e.
Expand Down Expand Up @@ -137,6 +151,7 @@ extension BrowserViewController: TabManagerDelegate {
topToolbar.updateReaderModeState(ReaderModeState.unavailable)
}

updateScreenTimeUrl(tabManager.selectedTab?.url)
updateInContentHomePanel(selected?.url as URL?)

notificationsPresenter.removeNotification(with: WalletNotification.Constant.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ extension BrowserViewController: TopToolbarDelegate {
func topToolbarDidLeaveOverlayMode(_ topToolbar: TopToolbarView) {
hideSearchController()
hideFavoritesController()
updateScreenTimeUrl(tabManager.selectedTab?.url)
updateInContentHomePanel(tabManager.selectedTab?.url as URL?)
updateTabsBarVisibility()
if isUsingBottomBar {
Expand Down

0 comments on commit 799a50b

Please sign in to comment.