Skip to content

Instantly share code, notes, and snippets.

@mattmook
mattmook / Orbit-PostListViewModel.kt
Last active July 14, 2021 10:55
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
@mattmook
mattmook / ReduxKotlin-subscribe.kt
Created July 14, 2021 10:50
The state of MVI on Android - ReduxKotlin - subscribe
lifecycleScope.launchWhenCreated {
suspendCancellableCoroutine { continuation ->
val unsubscribe = viewModel.store.subscribe {
render(viewModel.store.state)
}
continuation.invokeOnCancellation {
unsubscribe()
}
}
@mattmook
mattmook / ReduxKotlin-PostDetailsViewModel.kt
Created July 14, 2021 10:49
The state of MVI on Android - ReduxKotlin - PostDetailsViewModel
class PostDetailsViewModel(
private val postRepository: PostRepository,
private val postOverview: PostOverview
) : ViewModel() {
private val detailsMiddleware = middleware<PostDetailState> { store, next, action ->
when (action) {
is ActionTypes.INIT -> store.dispatch(loadDetails(postOverview.id))
else -> next(action)
}
@mattmook
mattmook / Uniflow-actions.kt
Created July 14, 2021 10:47
The state of MVI on Android - Uniflow - actions
class PostListViewModel : AndroidDataFlow(defaultState = PostListState()) {
init {
action1()
action2()
}
private fun action1() = actionOn<PostListState> { state ->
delay(5000)
@mattmook
mattmook / Uniflow-onStates.kt
Created July 14, 2021 10:46
The state of MVI on Android - Uniflow - onStates
onStates(viewModel) {
reduce(adapter, it as PostListState)
}
onEvents(viewModel) {
sideEffect(it as NavigationEvent)
}
@mattmook
mattmook / Uniflow-PostListViewModel.kt
Created July 14, 2021 10:45
The state of MVI on Android - Uniflow - PostListViewModel
class PostListViewModel(
savedStateHandle: SavedStateHandle,
private val postRepository: PostRepository
) : AndroidDataFlow(defaultState = PostListState(), savedStateHandle) {
init {
actionOn<PostListState> {
if (it.overviews.isEmpty()) {
loadOverviews()
}
@mattmook
mattmook / Roxie-NoteListViewModel.kt
Created July 14, 2021 10:44
The state of MVI on Android - Roxie - NoteListViewModel
class NoteListViewModel(
initialState: State?,
private val loadNoteListUseCase: GetNoteListUseCase
) : CoroutineViewModel<Action, State>() {
override val initialState = initialState ?: State(isIdle = true)
private val reducer: Reducer<State, Change> = { state, change ->
when (change) {
is Change.Loading -> state.copy(
isIdle = false,
isLoading = true,
@mattmook
mattmook / Mavericks-PostListViewModel.kt
Created July 14, 2021 10:43
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()
@mattmook
mattmook / MVIKotlin-onViewCreated.kt
Created July 14, 2021 10:40
The state of MVI on Android - MVIKotlin - onViewCreated
fun onViewCreated(view: PostListView) {
binder?.stop()
binder = bind(lifecycle, BinderLifecycleMode.CREATE_DESTROY) {
store.states bindTo view
store.labels bindTo view::sideEffect
}
}
@mattmook
mattmook / MVIKotlin-PostListStoreFactory.kt
Last active July 14, 2021 10:39
The state of MVI on Android - MVIKotlin - PostListStoreFactory
internal class PostListStoreFactory(
private val storeFactory: StoreFactory,
private val postRepository: PostRepository,
val stateKeeper: StateKeeper<PostListState>
) {
fun create() = object : Store<PostListIntent, PostListState, NavigationEvent> by storeFactory.create(
name = "PostListStore",
initialState = stateKeeper.consume() ?: PostListState(),
bootstrapper = SimpleBootstrapper(Unit),