Created
May 20, 2022 10:14
-
-
Save ctreffs/a6cd7e2208bcfc359fa9dab0d38232ce to your computer and use it in GitHub Desktop.
AppUpdate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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