Skip to content

Instantly share code, notes, and snippets.

@mattmook
Created July 14, 2021 10:43
Show Gist options
  • Save mattmook/93338680e2caee92ea44379f9cd7782c to your computer and use it in GitHub Desktop.
Save mattmook/93338680e2caee92ea44379f9cd7782c to your computer and use it in GitHub Desktop.
The state of MVI on Android - Mavericks - PostListViewModel
class PostListViewModel @AssistedInject constructor(
@Assisted initialState: PostListState,
private val postRepository: PostRepository
) : MavericksViewModel<PostListState>(initialState) {
private val _sideEffect =
Channel<NavigationEvent>(Channel.BUFFERED)
val sideEffect: Flow<NavigationEvent> =
_sideEffect.receiveAsFlow()
init {
loadOverviews()
}
private fun loadOverviews() = withState { state ->
suspend {
postRepository.getOverviews()
}.execute { async ->
async()?.let { copy(overviews = it) } ?: state
}
}
@Suppress("UNUSED_PARAMETER")
fun onPostClicked(post: PostOverview) {
_sideEffect.sendBlocking(OpenPostNavigationEvent(post))
}
@AssistedFactory
interface Factory : AssistedViewModelFactory<PostListViewModel, PostListState> {
override fun create(state: PostListState): PostListViewModel
}
companion object : MavericksViewModelFactory<PostListViewModel, PostListState> by hiltMavericksViewModelFactory()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment