Skip to content

Commit

Permalink
Edge case: unexpected app termination
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasstrba committed Mar 29, 2024
1 parent 2198747 commit b657c44
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions DuckDuckGo/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, FileDownloadManagerDel
#endif

burnOnQuitHandler = BurnOnQuitHandler(preferences: .shared, fireCoordinator: FireCoordinator())
burnOnQuitHandler.burnOnStartIfNeeded()
burnOnQuitHandler.resetTheFlag()
}

func applicationDidBecomeActive(_ notification: Notification) {
Expand Down
33 changes: 28 additions & 5 deletions DuckDuckGo/Application/BurnOnQuitHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//

import Foundation
import Combine

final class BurnOnQuitHandler {

Expand All @@ -29,6 +30,8 @@ final class BurnOnQuitHandler {
private let preferences: DataClearingPreferences
private let fireCoordinator: FireCoordinator

// MARK: - Burn On Quit

var shouldBurnOnQuit: Bool {
return preferences.isBurnDataOnQuitEnabled
}
Expand All @@ -38,13 +41,33 @@ final class BurnOnQuitHandler {

@MainActor
func burnOnQuit() {
//TODO: Refactor from static
//TODO: Without opening a new window
guard shouldBurnOnQuit else { return }
// TODO: Refactor from static
FireCoordinator.fireViewModel.fire.burnAll { [weak self] in
DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
self?.onBurnOnQuitCompleted?()
}
self?.burnPerformedSuccessfullyOnQuit = true
self?.onBurnOnQuitCompleted?()
}
}

// MARK: - Burn On Start
// In case the burn on quit wasn't successfull

@UserDefaultsWrapper(key: .burnPerformedSuccessfullyOnQuit, defaultValue: false)
private var burnPerformedSuccessfullyOnQuit: Bool

var shouldBurnOnStart: Bool {
return !burnPerformedSuccessfullyOnQuit
}

func resetTheFlag() {
burnPerformedSuccessfullyOnQuit = false
}

@MainActor
func burnOnStartIfNeeded() {
guard preferences.isBurnDataOnQuitEnabled, shouldBurnOnStart else { return }

FireCoordinator.fireViewModel.fire.burnAll()
}

}
1 change: 1 addition & 0 deletions DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public struct UserDefaultsWrapper<T> {
case lastCrashReportCheckDate = "last.crash.report.check.date"

case fireInfoPresentedOnce = "fire.info.presented.once"
case burnPerformedSuccessfullyOnQuit = "burn.performed.successfully.on.quit"

case restorePreviousSession = "preferences.startup.restore-previous-session"
case launchToCustomHomePage = "preferences.startup.launch-to-custom-home-page"
Expand Down

0 comments on commit b657c44

Please sign in to comment.