This file contains 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
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) | |
} |
This file contains 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
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() { |
This file contains 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
// In your view model | |
private val _eventChannel = Channel<Event>(Channel.BUFFERED) | |
val events = _eventChannel.receiveAsFlow() |
This file contains 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
// In your view/fragment | |
viewLifecycleOwner.lifecycleScope.launch { | |
viewModel.viewState | |
.flowWithLifecycle(viewLifecycleOwner.lifecycle, Lifecycle.State.STARTED) | |
.collect { | |
// do something with the UI updates | |
} | |
} |
This file contains 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
class MyViewMode: ViewModel() { | |
data class ViewState( | |
val someUIProperty: String = "", | |
val someOtherUIProperty: Int = 1, | |
) | |
private val _viewState = MutableStateFlow<ViewState>(ViewState()) | |
val viewState = _viewState.asStateFlow() | |
} |
This file contains 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
viewModel.events | |
.observeWithLifecycle(fragment = this, minActiveState = Lifecycle.State.RESUMED) { | |
// do things | |
} | |
viewModel.events | |
.observeWithLifecycle(lifecycleOwner = viewLifecycleOwner, minActiveState = Lifecycle.State.RESUMED) { | |
// do things | |
} |
This file contains 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
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, |
This file contains 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
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) |
This file contains 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
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) | |
} |
This file contains 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
class MainFragment : Fragment(R.layout.main_fragment) { | |
companion object { | |
fun newInstance() = MainFragment() | |
} | |
private lateinit var viewModel: MainViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { |
NewerOlder