Skip to content

Commit

Permalink
Handle new cases in state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
jaceklyp committed Dec 13, 2024
1 parent 7fee87d commit d6edc0b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 52 deletions.
4 changes: 0 additions & 4 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -2876,7 +2875,6 @@
CB2A7EF028410DF700885F67 /* PixelEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PixelEvent.swift; sourceTree = "<group>"; };
CB2A7EF3285383B300885F67 /* AppLastCompiledRulesStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppLastCompiledRulesStore.swift; sourceTree = "<group>"; };
CB2C47822AF6D55800AEDCD9 /* nb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nb; path = nb.lproj/InfoPlist.strings; sourceTree = "<group>"; };
CB3C78902D08483F00A7E4ED /* InactiveBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InactiveBackground.swift; sourceTree = "<group>"; };
CB4448752AF6D51D001F93F7 /* hr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = hr; path = hr.lproj/InfoPlist.strings; sourceTree = "<group>"; };
CB48D3312B90CE9F00631D8B /* PageRefreshStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PageRefreshStore.swift; sourceTree = "<group>"; };
CB4FA44D2C78AACE00A16F5A /* SpecialErrorPageUserScript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpecialErrorPageUserScript.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5610,7 +5608,6 @@
CBAD0EFC2CFE1D48006267B8 /* Active.swift */,
CBAD0EFE2CFE1D4E006267B8 /* Inactive.swift */,
CBAD0F002CFE1D54006267B8 /* Background.swift */,
CB3C78902D08483F00A7E4ED /* InactiveBackground.swift */,
);
path = AppStates;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
44 changes: 3 additions & 41 deletions DuckDuckGo/AppLifecycle/AppStateTransitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension Launched {
shortcutItemToHandle = shortcutItem
return self
case .backgrounding:
return InactiveBackground()
return Background(stateContext: makeStateContext())
case .launching, .suspending:
return handleUnexpectedEvent(event)
}
Expand Down Expand Up @@ -101,53 +101,15 @@ extension Background {
urlToOpen = url
return self
case .backgrounding:
return DoubleBackground()
run()
return self
case .launching, .suspending, .handleShortcutItem:
return handleUnexpectedEvent(event)
}
}

}

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 {
Expand Down
21 changes: 14 additions & 7 deletions DuckDuckGo/AppLifecycle/AppStates/Background.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,7 +66,7 @@ struct Background: AppState {
autofillLoginSession.endSession()

suspendSync(syncService: syncService)
syncDataProviders.bookmarksAdapter.cancelFaviconsFetching(stateContext.application)
syncDataProviders.bookmarksAdapter.cancelFaviconsFetching(application)
privacyProDataReporter.saveApplicationLastSessionEnded()

resetAppStartTime()
Expand All @@ -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)
Expand Down Expand Up @@ -108,7 +119,3 @@ extension Background {
}

}

struct DoubleBackground: AppState {

}

0 comments on commit d6edc0b

Please sign in to comment.