Skip to content

Instantly share code, notes, and snippets.

@mattmook
Last active July 14, 2021 10:55
Show Gist options
  • Save mattmook/db6e2268b995d69d7a0c1540597dcd17 to your computer and use it in GitHub Desktop.
Save mattmook/db6e2268b995d69d7a0c1540597dcd17 to your computer and use it in GitHub Desktop.
The state of MVI on Android - Orbit - PostListViewModel
class PostListViewModel(
savedStateHandle: SavedStateHandle,
private val postRepository: PostRepository
) : ViewModel(), ContainerHost<PostListState, NavigationEvent> {
override val container = container<PostListState, NavigationEvent>(
initialState = PostListState(),
savedStateHandle = savedStateHandle
) { state ->
// Executed on creation
// state is either initialState or provided by savedStateHandle
if (state.overviews.isEmpty()) {
loadOverviews()
}
}
private fun loadOverviews() = intent {
val overviews = postRepository.getOverviews()
reduce {
state.copy(overviews = overviews)
}
}
fun onPostClicked(post: PostOverview) = intent {
postSideEffect(OpenPostNavigationEvent(post))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment