-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically clear data upon quitting (#2600)
Task/Issue URL: https://app.asana.com/0/1177771139624306/1205062321200340/f **Description**: Adding Burn on Quit feature for macOS. Upon standard exit, data are cleared and the fire animation is presented. If macOS is restarting, the app delays the restart until all data have been cleared. In the event of an unexpected termination, such as a crash or force quit, data clearing is handled at the next startup.
- Loading branch information
1 parent
92592bb
commit 36aa51f
Showing
22 changed files
with
1,362 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// | ||
// AutoClearHandler.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
final class AutoClearHandler { | ||
|
||
private let preferences: DataClearingPreferences | ||
private let fireViewModel: FireViewModel | ||
private let stateRestorationManager: AppStateRestorationManager | ||
|
||
init(preferences: DataClearingPreferences, | ||
fireViewModel: FireViewModel, | ||
stateRestorationManager: AppStateRestorationManager) { | ||
self.preferences = preferences | ||
self.fireViewModel = fireViewModel | ||
self.stateRestorationManager = stateRestorationManager | ||
} | ||
|
||
@MainActor | ||
func handleAppLaunch() { | ||
burnOnStartIfNeeded() | ||
restoreTabsIfNeeded() | ||
resetTheCorrectTerminationFlag() | ||
} | ||
|
||
var onAutoClearCompleted: (() -> Void)? | ||
|
||
@MainActor | ||
func handleAppTermination() -> NSApplication.TerminateReply? { | ||
guard preferences.isAutoClearEnabled else { return nil } | ||
|
||
if preferences.isWarnBeforeClearingEnabled { | ||
switch confirmAutoClear() { | ||
case .alertFirstButtonReturn: | ||
// Clear and Quit | ||
performAutoClear() | ||
return .terminateLater | ||
case .alertSecondButtonReturn: | ||
// Quit without Clearing Data | ||
appTerminationHandledCorrectly = true | ||
restoreTabsOnStartup = true | ||
return .terminateNow | ||
default: | ||
// Cancel | ||
return .terminateCancel | ||
} | ||
} | ||
|
||
performAutoClear() | ||
return .terminateLater | ||
} | ||
|
||
func resetTheCorrectTerminationFlag() { | ||
appTerminationHandledCorrectly = false | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func confirmAutoClear() -> NSApplication.ModalResponse { | ||
let alert = NSAlert.autoClearAlert() | ||
let response = alert.runModal() | ||
return response | ||
} | ||
|
||
@MainActor | ||
private func performAutoClear() { | ||
fireViewModel.fire.burnAll { [weak self] in | ||
self?.appTerminationHandledCorrectly = true | ||
self?.onAutoClearCompleted?() | ||
} | ||
} | ||
|
||
// MARK: - Burn On Start | ||
// Burning on quit wasn't successful | ||
|
||
@UserDefaultsWrapper(key: .appTerminationHandledCorrectly, defaultValue: false) | ||
private var appTerminationHandledCorrectly: Bool | ||
|
||
@MainActor | ||
@discardableResult | ||
func burnOnStartIfNeeded() -> Bool { | ||
let shouldBurnOnStart = preferences.isAutoClearEnabled && !appTerminationHandledCorrectly | ||
guard shouldBurnOnStart else { return false } | ||
|
||
fireViewModel.fire.burnAll() | ||
return true | ||
} | ||
|
||
// MARK: - Burn without Clearing Data | ||
|
||
@UserDefaultsWrapper(key: .restoreTabsOnStartup, defaultValue: false) | ||
private var restoreTabsOnStartup: Bool | ||
|
||
@MainActor | ||
@discardableResult | ||
func restoreTabsIfNeeded() -> Bool { | ||
let isAutoClearEnabled = preferences.isAutoClearEnabled | ||
let restoreTabsOnStartup = restoreTabsOnStartup | ||
self.restoreTabsOnStartup = false | ||
if isAutoClearEnabled && restoreTabsOnStartup { | ||
stateRestorationManager.restoreLastSessionState(interactive: false) | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
} |
Binary file removed
BIN
-42.4 KB
DuckDuckGo/Assets.xcassets/Images/BurnAlert.imageset/Burn-Original-large.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
DuckDuckGo/Assets.xcassets/Images/BurnAlert.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.