Skip to content

Instantly share code, notes, and snippets.

@ctreffs
Created May 20, 2022 10:14
Show Gist options
  • Save ctreffs/a6cd7e2208bcfc359fa9dab0d38232ce to your computer and use it in GitHub Desktop.
Save ctreffs/a6cd7e2208bcfc359fa9dab0d38232ce to your computer and use it in GitHub Desktop.
AppUpdate
protocol State {
var manager: AppUpdateManager { get }
init(manager: AppUpdateManager)
}
protocol UpdateProcessControlling {
func startUpdate()
func postponeUpdate()
}
public class AppUpdateManager {
var currentState: State
init() {
currentState = UpToDate(manager: self)
}
internal func transition<S>(to: S.Type) {
currentState = S(manager: self)
}
public func startUpdate() {
(currentState as? UpdateProcessControlling)?.startUpdate()
}
}
// MARK: - UpToDate
struct UpToDate: State, UpdateProcessControlling {
let manager: AppUpdateManager
init(manager: AppUpdateManager) {
self.manager = manager
}
func startUpdate() {
// execute update logic etc.
manager.transition(to: Updating.self)
}
func postponeUpdate() {
// execute postponed logic etc.
manager.transition(to: Postponed.self)
}
}
// MARK: - Updating
struct Updating: State {
let manager: AppUpdateManager
init(manager: AppUpdateManager) {
self.manager = manager
}
}
// MARK: - Postponed
struct Postponed: State {
let manager: AppUpdateManager
init(manager: AppUpdateManager) {
self.manager = manager
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment