Skip to content

Instantly share code, notes, and snippets.

View fergusonm's full-sized avatar

Michael Ferguson fergusonm

View GitHub Profile
@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)
}
@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() {
// In your view model
private val _eventChannel = Channel<Event>(Channel.BUFFERED)
val events = _eventChannel.receiveAsFlow()
@fergusonm
fergusonm / gist:8d202e5e8ba57d85fe4c77c708600e91
Last active December 22, 2021 15:56
View observing view state updates
// In your view/fragment
viewLifecycleOwner.lifecycleScope.launch {
viewModel.viewState
.flowWithLifecycle(viewLifecycleOwner.lifecycle, Lifecycle.State.STARTED)
.collect {
// do something with the UI updates
}
}
@fergusonm
fergusonm / gist:5ef6d0793d9419b2a3f6ff9d048a871e
Created December 22, 2021 15:26
View model UI stateflow
class MyViewMode: ViewModel() {
data class ViewState(
val someUIProperty: String = "",
val someOtherUIProperty: Int = 1,
)
private val _viewState = MutableStateFlow<ViewState>(ViewState())
val viewState = _viewState.asStateFlow()
}
viewModel.events
.observeWithLifecycle(fragment = this, minActiveState = Lifecycle.State.RESUMED) {
// do things
}
viewModel.events
.observeWithLifecycle(lifecycleOwner = viewLifecycleOwner, minActiveState = Lifecycle.State.RESUMED) {
// do things
}
@fergusonm
fergusonm / gist:b8cb267a1319b25e11c0d73613f956c4
Created October 21, 2021 21:06
Flow with lifecycle extension functions
inline fun <reified T> Flow<T>.observeWithLifecycle(
lifecycleOwner: LifecycleOwner,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
noinline action: suspend (T) -> Unit
): Job = lifecycleOwner.lifecycleScope.launch {
flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState).collect(action)
}
inline fun <reified T> Flow<T>.observeWithLifecycle(
fragment: Fragment,
@fergusonm
fergusonm / flowWithLifecycle
Created October 21, 2021 21:02
Flow with lifecycle example
viewModel.events
.onEach {
// can get cancelled when the lifecycle state falls below min
}
.flowWithLifecycle(lifecycle = viewLifecycleOwner.lifecycle, minActiveState = Lifecycle.State.STARTED)
.onEach {
// Do things
}
.launchIn(viewLifecycleOwner.lifecycleScope)
@fergusonm
fergusonm / collectIn.kt
Created March 29, 2021 14:48
Compact flowWithLifecycle
fun <T> Flow<T>.collectIn(
lifecycleOwner: LifecycleOwner,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
action: suspend (T) -> Unit
): Job = lifecycleOwner.lifecycleScope.launch {
flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState).collect(action)
}
@fergusonm
fergusonm / gist:88a728eb543c7f6727a7cc473671befc
Last active October 28, 2023 20:01
launchWhenX dropping events
class MainFragment : Fragment(R.layout.main_fragment) {
companion object {
fun newInstance() = MainFragment()
}
private lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {