From b657c44db7d7b1b7823ad71622df41f90b943242 Mon Sep 17 00:00:00 2001 From: Tomas Strba Date: Fri, 29 Mar 2024 16:26:51 +0100 Subject: [PATCH] Edge case: unexpected app termination --- DuckDuckGo/Application/AppDelegate.swift | 2 ++ .../Application/BurnOnQuitHandler.swift | 33 ++++++++++++++++--- .../Utilities/UserDefaultsWrapper.swift | 1 + 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/DuckDuckGo/Application/AppDelegate.swift b/DuckDuckGo/Application/AppDelegate.swift index 5c7e0627cd..8e1f260661 100644 --- a/DuckDuckGo/Application/AppDelegate.swift +++ b/DuckDuckGo/Application/AppDelegate.swift @@ -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) { diff --git a/DuckDuckGo/Application/BurnOnQuitHandler.swift b/DuckDuckGo/Application/BurnOnQuitHandler.swift index f403b7b9ad..fbe10d7cf7 100644 --- a/DuckDuckGo/Application/BurnOnQuitHandler.swift +++ b/DuckDuckGo/Application/BurnOnQuitHandler.swift @@ -17,6 +17,7 @@ // import Foundation +import Combine final class BurnOnQuitHandler { @@ -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 } @@ -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() + } + } diff --git a/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift b/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift index 37311af671..11732df24d 100644 --- a/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift +++ b/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift @@ -78,6 +78,7 @@ public struct UserDefaultsWrapper { 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"