Created
July 14, 2021 10:43
-
-
Save mattmook/93338680e2caee92ea44379f9cd7782c to your computer and use it in GitHub Desktop.
The state of MVI on Android - Mavericks - PostListViewModel
This file contains hidden or 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 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