Last active
July 14, 2021 10:55
-
-
Save mattmook/db6e2268b995d69d7a0c1540597dcd17 to your computer and use it in GitHub Desktop.
The state of MVI on Android - Orbit - 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( | |
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