Skip to content

Commit

Permalink
Do not refresh UI only if AutoClear was triggered (#2924)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/856498667320406/1207474102025833/f
Tech Design URL:
CC:

Description:

Make sure that we correctly refresh the UI once we forget data. Right now there is a case when we do not refresh UI despite correctly clearing data.
  • Loading branch information
bwaresiak authored Jun 6, 2024
1 parent d1fdf44 commit 6c4a928
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
10 changes: 6 additions & 4 deletions DuckDuckGo/AutoClear.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
Expand All @@ -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.
Expand Down
29 changes: 19 additions & 10 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2403,7 +2406,7 @@ extension MainViewController: GestureToolbarButtonDelegate {
}

extension MainViewController: AutoClearWorker {

func clearNavigationStack() {
dismissOmniBar()

Expand All @@ -2421,10 +2424,6 @@ extension MainViewController: AutoClearWorker {
}

func refreshUIAfterClear() {
guard doRefreshAfterClear else {
doRefreshAfterClear = true
return
}
showBars()
attachHomeScreen()
tabsBarController?.refresh(tabsModel: tabManager.model)
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion DuckDuckGoTests/AutoClearTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ class AutoClearTests: XCTestCase {
forgetTabsInvocationCount += 1
}

func clearDataFinished(_: AutoClear) {
func willStartClearing(_: DuckDuckGo.AutoClear) {

}

func autoClearDidFinishClearing(_: DuckDuckGo.AutoClear, isLaunching: Bool) {
clearDataFinishedInvocationCount += 1
}
}
Expand Down

0 comments on commit 6c4a928

Please sign in to comment.