From d6edc0b881e81e8623180dac0bfc8ec0c4259400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20=C5=81yp?= Date: Fri, 13 Dec 2024 16:58:15 +0100 Subject: [PATCH] Handle new cases in state machine --- DuckDuckGo.xcodeproj/project.pbxproj | 4 -- .../AppLifecycle/AppStateTransitions.swift | 44 ++----------------- .../AppLifecycle/AppStates/Background.swift | 21 ++++++--- 3 files changed, 17 insertions(+), 52 deletions(-) diff --git a/DuckDuckGo.xcodeproj/project.pbxproj b/DuckDuckGo.xcodeproj/project.pbxproj index d42edfcf6a..d96996c8b7 100644 --- a/DuckDuckGo.xcodeproj/project.pbxproj +++ b/DuckDuckGo.xcodeproj/project.pbxproj @@ -981,7 +981,6 @@ CB3C788D2D06D3A700A7E4ED /* AppStateTransitions.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBAD0F072CFE27D5006267B8 /* AppStateTransitions.swift */; }; CB3C788E2D06D3A700A7E4ED /* Init.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBAD0EF82CFE1D35006267B8 /* Init.swift */; }; CB3C788F2D06D3A700A7E4ED /* Inactive.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBAD0EFE2CFE1D4E006267B8 /* Inactive.swift */; }; - CB3C78912D08484800A7E4ED /* InactiveBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB3C78902D08483F00A7E4ED /* InactiveBackground.swift */; }; CB48D3332B90CE9F00631D8B /* PageRefreshStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB48D3312B90CE9F00631D8B /* PageRefreshStore.swift */; }; CB4FA44E2C78AACE00A16F5A /* SpecialErrorPageUserScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB4FA44D2C78AACE00A16F5A /* SpecialErrorPageUserScript.swift */; }; CB5516D0286500290079B175 /* TrackerRadarIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85519124247468580010FDD0 /* TrackerRadarIntegrationTests.swift */; }; @@ -2876,7 +2875,6 @@ CB2A7EF028410DF700885F67 /* PixelEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PixelEvent.swift; sourceTree = ""; }; CB2A7EF3285383B300885F67 /* AppLastCompiledRulesStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppLastCompiledRulesStore.swift; sourceTree = ""; }; CB2C47822AF6D55800AEDCD9 /* nb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nb; path = nb.lproj/InfoPlist.strings; sourceTree = ""; }; - CB3C78902D08483F00A7E4ED /* InactiveBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InactiveBackground.swift; sourceTree = ""; }; CB4448752AF6D51D001F93F7 /* hr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = hr; path = hr.lproj/InfoPlist.strings; sourceTree = ""; }; CB48D3312B90CE9F00631D8B /* PageRefreshStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PageRefreshStore.swift; sourceTree = ""; }; CB4FA44D2C78AACE00A16F5A /* SpecialErrorPageUserScript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpecialErrorPageUserScript.swift; sourceTree = ""; }; @@ -5610,7 +5608,6 @@ CBAD0EFC2CFE1D48006267B8 /* Active.swift */, CBAD0EFE2CFE1D4E006267B8 /* Inactive.swift */, CBAD0F002CFE1D54006267B8 /* Background.swift */, - CB3C78902D08483F00A7E4ED /* InactiveBackground.swift */, ); path = AppStates; sourceTree = ""; @@ -8247,7 +8244,6 @@ 1DEAADF42BA47B5300E25A97 /* WebTrackingProtectionView.swift in Sources */, F15D43201E706CC500BF2CDC /* AutocompleteViewController.swift in Sources */, BD862E092B30F63E0073E2EE /* VPNMetadataCollector.swift in Sources */, - CB3C78912D08484800A7E4ED /* InactiveBackground.swift in Sources */, D6E83C682B23B6A3006C8AFB /* FontSettings.swift in Sources */, 7BF78E022CA2CC3E0026A1FC /* TipKitAppEventHandling.swift in Sources */, 1DEAADF62BA4809400E25A97 /* CookiePopUpProtectionView.swift in Sources */, diff --git a/DuckDuckGo/AppLifecycle/AppStateTransitions.swift b/DuckDuckGo/AppLifecycle/AppStateTransitions.swift index 3c2d4040d3..e9e1f213ad 100644 --- a/DuckDuckGo/AppLifecycle/AppStateTransitions.swift +++ b/DuckDuckGo/AppLifecycle/AppStateTransitions.swift @@ -46,7 +46,7 @@ extension Launched { shortcutItemToHandle = shortcutItem return self case .backgrounding: - return InactiveBackground() + return Background(stateContext: makeStateContext()) case .launching, .suspending: return handleUnexpectedEvent(event) } @@ -101,7 +101,8 @@ extension Background { urlToOpen = url return self case .backgrounding: - return DoubleBackground() + run() + return self case .launching, .suspending, .handleShortcutItem: return handleUnexpectedEvent(event) } @@ -109,45 +110,6 @@ extension Background { } -extension DoubleBackground { - - func apply(event: AppEvent) -> any AppState { - // report event so we know what events can be called at this moment, but do not let SM be stuck in this state just not to be flooded with these events - handleUnexpectedEvent(event) - - //todo: to be removed -// switch event { -// case .activating(let application): -// return Active(application: application) -// case .suspending(let application): -// return Inactive(application: application) -// case .launching, .backgrounding, .openURL: -// return self -// } - - } - -} - -extension InactiveBackground { - - func apply(event: AppEvent) -> any AppState { - // report event so we know what events can be called at this moment, but do not let SM be stuck in this state just not to be flooded with these events - handleUnexpectedEvent(event) - - //todo: to be removed -// switch event { -// case .activating(let application): -// return Active(application: application) -// case .suspending(let application): -// return Inactive(application: application) -// case .launching, .backgrounding, .openURL: -// return self -// } - } - -} - extension AppEvent { var rawValue: String { diff --git a/DuckDuckGo/AppLifecycle/AppStates/Background.swift b/DuckDuckGo/AppLifecycle/AppStates/Background.swift index da5f4f1571..4e74e59c3e 100644 --- a/DuckDuckGo/AppLifecycle/AppStates/Background.swift +++ b/DuckDuckGo/AppLifecycle/AppStates/Background.swift @@ -35,8 +35,19 @@ struct Background: AppState { application = stateContext.application appDependencies = stateContext.appDependencies urlToOpen = stateContext.urlToOpen - syncDidFinishCancellable = appDependencies.syncDidFinishCancellable + run() + } + + init(stateContext: Launched.StateContext) { + application = stateContext.application + appDependencies = stateContext.appDependencies + urlToOpen = stateContext.urlToOpen + + run() + } + + mutating func run() { let autoClear = appDependencies.autoClear let privacyStore = appDependencies.privacyStore let privacyProDataReporter = appDependencies.privacyProDataReporter @@ -55,7 +66,7 @@ struct Background: AppState { autofillLoginSession.endSession() suspendSync(syncService: syncService) - syncDataProviders.bookmarksAdapter.cancelFaviconsFetching(stateContext.application) + syncDataProviders.bookmarksAdapter.cancelFaviconsFetching(application) privacyProDataReporter.saveApplicationLastSessionEnded() resetAppStartTime() @@ -70,7 +81,7 @@ struct Background: AppState { Logger.sync.debug("Forcing background task completion") UIApplication.shared.endBackgroundTask(taskID) } - syncDidFinishCancellable?.cancel() + appDependencies.syncDidFinishCancellable?.cancel() syncDidFinishCancellable = syncService.isSyncInProgressPublisher.filter { !$0 } .prefix(1) .receive(on: DispatchQueue.main) @@ -108,7 +119,3 @@ extension Background { } } - -struct DoubleBackground: AppState { - -}