Fix state machine crashes & background tasks (#343)

* Fix `UIApplication.shared` after moving to SwiftUI app

* Do not autoplay videos on background

* Move app state changes into the app coordinator

* Add application background task, move into the suspended state more accurately

* Add changelog

* Fix most of the linter errors

* Strip suspended state from state machine

* Fix build

* Clear audio session warning

* Update AppCoordinator.swift

* Update AppCoordinator.swift

* Swift format
This commit is contained in:
ismailgulek
2022-11-28 18:42:49 +03:00
committed by GitHub
parent bd530333df
commit cb5db22b7f
10 changed files with 131 additions and 97 deletions
@@ -36,7 +36,6 @@ class UserSessionFlowCoordinator: CoordinatorProtocol {
self.bugReportService = bugReportService
setupStateMachine()
startObservingApplicationState()
}
func start() {
@@ -84,11 +83,6 @@ class UserSessionFlowCoordinator: CoordinatorProtocol {
case (.feedbackScreen, .dismissedFeedbackScreen, .homeScreen):
break
case (_, .resignActive, .suspended):
self.pause()
case (_, .becomeActive, _):
self.resume()
default:
fatalError("Unknown transition: \(context)")
}
@@ -98,17 +92,6 @@ class UserSessionFlowCoordinator: CoordinatorProtocol {
fatalError("Failed transition with context: \(context)")
}
}
private func startObservingApplicationState() {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillResignActive),
name: UIApplication.willResignActiveNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil)
}
private func presentHomeScreen() {
userSession.clientProxy.startSync()
@@ -247,24 +230,4 @@ class UserSessionFlowCoordinator: CoordinatorProtocol {
self?.stateMachine.processEvent(.dismissedFeedbackScreen)
}
}
// MARK: - Application State
private func pause() {
userSession.clientProxy.stopSync()
}
private func resume() {
userSession.clientProxy.startSync()
}
@objc
private func applicationWillResignActive() {
stateMachine.processEvent(.resignActive)
}
@objc
private func applicationDidBecomeActive() {
stateMachine.processEvent(.becomeActive)
}
}
@@ -38,9 +38,6 @@ class UserSessionFlowCoordinatorStateMachine {
/// Showing the settings screen
case settingsScreen
/// Application has been suspended
case suspended
}
/// Events that can be triggered on the AppCoordinator state machine
@@ -68,22 +65,15 @@ class UserSessionFlowCoordinatorStateMachine {
case showSessionVerificationScreen
/// Session verification has finished
case dismissedSessionVerificationScreen
/// Application goes into inactive state
case resignActive
/// Application goes into active state
case becomeActive
}
private let stateMachine: StateMachine<State, Event>
private var stateBeforeSuspension: State?
init() {
stateMachine = StateMachine(state: .initial)
configure()
}
// swiftlint:disable:next cyclomatic_complexity
private func configure() {
stateMachine.addRoutes(event: .start, transitions: [.initial => .homeScreen])
@@ -109,18 +99,6 @@ class UserSessionFlowCoordinatorStateMachine {
case (.dismissedSessionVerificationScreen, .sessionVerificationScreen):
return .homeScreen
case (.resignActive, _):
self.stateBeforeSuspension = fromState
return .suspended
case (.becomeActive, _):
// Cannot become active if not previously suspended
// Happens when the app is backgrounded before the session is setup
guard let previousState = self.stateBeforeSuspension else {
return self.stateMachine.state
}
return previousState
default:
return nil
}