Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
StateMachine
/**
* Keeps track of current and prior application state.
*
* @param dataStore storage
*/
@Singleton
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(StateStorage::class.java,
GsonUtils::deserialize) ?: StateStorage(0, null, null)
private fun saveChanges() {
// Call this any time a change is made to a variable within StateStorage
dataStore.save(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.