mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 11:58:32 +00:00
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:
@@ -53,6 +53,7 @@ class UIKitBackgroundTask: BackgroundTaskProtocol {
|
||||
// attempt to start
|
||||
identifier = application.beginBackgroundTask(withName: name) { [weak self] in
|
||||
guard let self else { return }
|
||||
self.stop()
|
||||
self.expirationHandler?(self)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,13 +19,17 @@ import UIKit
|
||||
|
||||
/// /// UIKitBackgroundTaskService is a concrete implementation of BackgroundTaskServiceProtocol using a given `ApplicationProtocol` instance.
|
||||
class UIKitBackgroundTaskService: BackgroundTaskServiceProtocol {
|
||||
private let application: ApplicationProtocol?
|
||||
private let applicationBlock: () -> ApplicationProtocol?
|
||||
private var reusableTasks: WeakDictionary<String, UIKitBackgroundTask> = WeakDictionary()
|
||||
|
||||
private var application: ApplicationProtocol? {
|
||||
applicationBlock()
|
||||
}
|
||||
|
||||
/// Initializer
|
||||
/// - Parameter application: application instance to use. Defaults to `UIApplication.extensionSafeShared`.
|
||||
init(withApplication application: ApplicationProtocol? = UIApplication.extensionSafeShared) {
|
||||
self.application = application
|
||||
/// - Parameter applicationBlock: block returning the application instance to use. Defaults to a block returning `UIApplication.extensionSafeShared`.
|
||||
init(withApplicationBlock applicationBlock: @escaping () -> ApplicationProtocol? = { UIApplication.extensionSafeShared }) {
|
||||
self.applicationBlock = applicationBlock
|
||||
}
|
||||
|
||||
func startBackgroundTask(withName name: String,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user