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?) { |
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
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
class FlowObserver<T> ( | |
lifecycleOwner: LifecycleOwner, | |
private val flow: Flow<T>, | |
private val collector: suspend (T) -> Unit | |
) { | |
private var job: Job? = null | |
init { | |
lifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { |
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 MainViewModel : ViewModel() { | |
sealed class Event { | |
object NavigateToSettings: Event() | |
data class ShowSnackBar(val text: String): Event() | |
data class ShowToast(val text: String): Event() | |
} | |
private val eventChannel = Channel<Event>(Channel.BUFFERED) | |
val eventsFlow = 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
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 | |
} |
NewerOlder