diff --git a/DuckDuckGo/AutoClear.swift b/DuckDuckGo/AutoClear.swift index 5f507f809a..a90b5f6278 100644 --- a/DuckDuckGo/AutoClear.swift +++ b/DuckDuckGo/AutoClear.swift @@ -27,7 +27,9 @@ protocol AutoClearWorker { func forgetData() async func forgetData(applicationState: DataStoreWarmup.ApplicationState) async func forgetTabs() - func clearDataFinished(_: AutoClear) + + func willStartClearing(_: AutoClear) + func autoClearDidFinishClearing(_: AutoClear, isLaunching: Bool) } class AutoClear { @@ -50,6 +52,8 @@ class AutoClear { func clearDataIfEnabled(launching: Bool = false, applicationState: DataStoreWarmup.ApplicationState = .unknown) async { guard let settings = AutoClearSettingsModel(settings: appSettings) else { return } + worker.willStartClearing(self) + if settings.action.contains(.clearTabs) { worker.forgetTabs() } @@ -58,9 +62,7 @@ class AutoClear { await worker.forgetData(applicationState: applicationState) } - if !launching { - worker.clearDataFinished(self) - } + worker.autoClearDidFinishClearing(self, isLaunching: launching) } /// Note: function is parametrised because of tests. diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index 9591586fac..40c3cd0d15 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -90,8 +90,9 @@ class MainViewController: UIViewController { let previewsSource: TabPreviewsSource let appSettings: AppSettings private var launchTabObserver: LaunchTabNotification.Observer? - - var doRefreshAfterClear = true + + var autoClearInProgress = false + var autoClearShouldRefreshUIAfterClear = true let bookmarksDatabase: CoreDataDatabase private weak var bookmarksDatabaseCleaner: BookmarkDatabaseCleaner? @@ -835,7 +836,9 @@ class MainViewController: UIViewController { selectTab(existing) return } else if reuseExisting, let existing = tabManager.firstHomeTab() { - doRefreshAfterClear = false + if autoClearInProgress { + autoClearShouldRefreshUIAfterClear = false + } tabManager.selectTab(existing) loadUrl(url, fromExternalLink: fromExternalLink) } else { @@ -2403,7 +2406,7 @@ extension MainViewController: GestureToolbarButtonDelegate { } extension MainViewController: AutoClearWorker { - + func clearNavigationStack() { dismissOmniBar() @@ -2421,10 +2424,6 @@ extension MainViewController: AutoClearWorker { } func refreshUIAfterClear() { - guard doRefreshAfterClear else { - doRefreshAfterClear = true - return - } showBars() attachHomeScreen() tabsBarController?.refresh(tabsModel: tabManager.model) @@ -2433,8 +2432,18 @@ extension MainViewController: AutoClearWorker { } @MainActor - func clearDataFinished(_: AutoClear) { - refreshUIAfterClear() + func willStartClearing(_: AutoClear) { + autoClearInProgress = true + } + + @MainActor + func autoClearDidFinishClearing(_: AutoClear, isLaunching: Bool) { + if autoClearShouldRefreshUIAfterClear && isLaunching == false { + refreshUIAfterClear() + } + + autoClearInProgress = false + autoClearShouldRefreshUIAfterClear = true } @MainActor diff --git a/DuckDuckGoTests/AutoClearTests.swift b/DuckDuckGoTests/AutoClearTests.swift index 1cc8383bf9..7600916684 100644 --- a/DuckDuckGoTests/AutoClearTests.swift +++ b/DuckDuckGoTests/AutoClearTests.swift @@ -47,7 +47,11 @@ class AutoClearTests: XCTestCase { forgetTabsInvocationCount += 1 } - func clearDataFinished(_: AutoClear) { + func willStartClearing(_: DuckDuckGo.AutoClear) { + + } + + func autoClearDidFinishClearing(_: DuckDuckGo.AutoClear, isLaunching: Bool) { clearDataFinishedInvocationCount += 1 } }