Created
July 14, 2021 10:45
-
-
Save mattmook/1347840acddd762e8e2497443cd02aab to your computer and use it in GitHub Desktop.
The state of MVI on Android - Uniflow - 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 | |
) : AndroidDataFlow(defaultState = PostListState(), savedStateHandle) { | |
init { | |
actionOn<PostListState> { | |
if (it.overviews.isEmpty()) { | |
loadOverviews() | |
} | |
} | |
} | |
private fun loadOverviews() = actionOn<PostListState> { state -> | |
val overviews = postRepository.getOverviews() | |
setState { | |
state.copy(overviews = overviews) | |
} | |
} | |
fun onPostClicked(post: PostOverview) = action { | |
sendEvent(OpenPostNavigationEvent(post)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment