Skip to content

Instantly share code, notes, and snippets.

View fergusonm's full-sized avatar

Michael Ferguson fergusonm

View GitHub Profile
@fergusonm
fergusonm / gist:743ec5cdc8bc2881d6b0706aa4d57f2a
Created December 30, 2021 16:17
Multiplatform view model with a backing android VM
class MyMultiPlatformViewModel @Inject constructor(
private val scope: CoroutineScope,
private val stateHelper: SavedStateHelper,
) : CoroutineScope by scope, AutoCloseable {
private var screenId: String by stateHelper.savedState("screenId", default = "")
fun doAThing() {}
override fun close() {
@fergusonm
fergusonm / gist:adb8ddd1b699988b69970cccf435dc24
Created January 22, 2022 18:46
MutableStateflow that saves to saveStateHandle
class Thing(
private val savedStateHandle: SavedStateHandle,
private val key: String,
) {
private val someExistingStateFlow = MutableStateFlow<String>(savedStateHandle[key] ?: "default Initial")
private val myStateFlow = someExistingStateFlow.saveWith(savedStateHandle = savedStateHandle, key = key)
private val myNewStateFlow = SaveableMutableStateFlow<String>(savedStateHandle = savedStateHandle, key = key)
}