diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController.swift b/Sources/Brave/Frontend/Browser/BrowserViewController.swift index 4e1af53b003..19473ce92b2 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController.swift @@ -380,8 +380,7 @@ public class BrowserViewController: UIViewController { } if Preferences.Privacy.screenTimeEnabled.value { - // Enable once fixed, ref #8566 - //screenTimeViewController = STWebpageController() + screenTimeViewController = STWebpageController() } } @@ -1251,11 +1250,7 @@ public class BrowserViewController: UIViewController { make.leading.trailing.equalTo(self.view) } - if let screenTimeViewController = screenTimeViewController { - webViewContainer.addSubview(screenTimeViewController.view) - addChild(screenTimeViewController) - screenTimeViewController.didMove(toParent: self) - + if let screenTimeViewController = screenTimeViewController, screenTimeViewController.parent != nil { screenTimeViewController.view.snp.remakeConstraints { $0.edges.equalTo(webViewContainer) } @@ -1945,6 +1940,7 @@ public class BrowserViewController: UIViewController { } updateInContentHomePanel(url as URL) + updateScreenTimeUrl(url) updatePlaylistURLBar(tab: tab, state: tab.playlistItemState, item: tab.playlistItem) } } @@ -3321,13 +3317,13 @@ extension BrowserViewController: PreferencesObserver { recordAdsUsageType() case Preferences.Privacy.screenTimeEnabled.key: if Preferences.Privacy.screenTimeEnabled.value { - // Enable once fixed, ref #8566 - //screenTimeViewController = STWebpageController() + screenTimeViewController = STWebpageController() if let tab = tabManager.selectedTab { recordScreenTimeUsage(for: tab) } } else { screenTimeViewController?.view.removeFromSuperview() + screenTimeViewController?.willMove(toParent: nil) screenTimeViewController?.removeFromParent() screenTimeViewController?.suppressUsageRecording = true screenTimeViewController = nil diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ScreenTime.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ScreenTime.swift index 47ad2473e7d..40507c4b0d9 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ScreenTime.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ScreenTime.swift @@ -12,12 +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?.url = URL(string: "https://about:blank") + // 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) { diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+TabManagerDelegate.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+TabManagerDelegate.swift index fcf66a7f6c4..73d2657894b 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+TabManagerDelegate.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+TabManagerDelegate.swift @@ -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. @@ -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) diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+ToolbarDelegate.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+ToolbarDelegate.swift index 6e1c1614378..9dacdbc77bd 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+ToolbarDelegate.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+ToolbarDelegate.swift @@ -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 { diff --git a/Sources/Brave/Frontend/ClientPreferences.swift b/Sources/Brave/Frontend/ClientPreferences.swift index 6e312b7386b..fff74b178f1 100644 --- a/Sources/Brave/Frontend/ClientPreferences.swift +++ b/Sources/Brave/Frontend/ClientPreferences.swift @@ -133,7 +133,7 @@ extension Preferences { /// The toggles states for clear private data screen static let clearPrivateDataToggles = Option<[Bool]>(key: "privacy.clear-data-toggles", default: []) /// Enables the Apple's Screen Time feature. - public static let screenTimeEnabled = Option(key: "privacy.screentime", default: true) + public static let screenTimeEnabled = Option(key: "privacy.screentime-toggle", default: AppConstants.buildChannel != .release) } final public class NewTabPage { diff --git a/Sources/Brave/Frontend/Settings/Features/ShieldsPrivacy/OtherPrivacySettingsSectionView.swift b/Sources/Brave/Frontend/Settings/Features/ShieldsPrivacy/OtherPrivacySettingsSectionView.swift index 2d065895078..201ae5e93fa 100644 --- a/Sources/Brave/Frontend/Settings/Features/ShieldsPrivacy/OtherPrivacySettingsSectionView.swift +++ b/Sources/Brave/Frontend/Settings/Features/ShieldsPrivacy/OtherPrivacySettingsSectionView.swift @@ -69,14 +69,11 @@ struct OtherPrivacySettingsSectionView: View { subtitle: String.localizedStringWithFormat(Strings.googleSafeBrowsingUsingWebKitDescription, URL.brave.safeBrowsingHelp.absoluteString), option: Preferences.Shields.googleSafeBrowsing ) - // Enable once fixed, ref #8566 - /* OptionToggleView( title: Strings.screenTimeSetting, subtitle: String.localizedStringWithFormat(Strings.screenTimeSettingDescription, URL.brave.screenTimeHelp.absoluteString), option: Preferences.Privacy.screenTimeEnabled ) - */ ShieldToggleView( title: Strings.P3A.settingTitle, subtitle: Strings.P3A.settingSubtitle,