Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
* Keeps track of current and prior application state.
* @param dataStore storage
class StateMachine(private val dataStore: DataStore) {
* Storage implementation for our state machine.
* @param currentState the current state of the application
* @param lastKnownError the last error that occurred
* @param lastActionPerformed the last action the user performed
@DataStore.Key(key = "StateMachine")
private data class StateStorage(
var currentState: Int,
var lastKnownError: String?,
var lastActionPerformed: String?
// Restore our storage from DataStore or create a new instance if one hasn't been created
private val stateStorage: StateStorage = dataStore.restore(,
GsonUtils::deserialize) ?: StateStorage(0, null, null)
private fun saveChanges() {
// Call this any time a change is made to a variable within StateStorage, GsonUtils::serialize)
// Add methods to access/update data in stateStorage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.