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 |
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
| lifecycleScope.launchWhenCreated { | |
| suspendCancellableCoroutine { continuation -> | |
| val unsubscribe = viewModel.store.subscribe { | |
| render(viewModel.store.state) | |
| } | |
| continuation.invokeOnCancellation { | |
| unsubscribe() | |
| } | |
| } |
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 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) | |
| } |
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 : AndroidDataFlow(defaultState = PostListState()) { | |
| init { | |
| action1() | |
| action2() | |
| } | |
| private fun action1() = actionOn<PostListState> { state -> | |
| delay(5000) |
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
| onStates(viewModel) { | |
| reduce(adapter, it as PostListState) | |
| } | |
| onEvents(viewModel) { | |
| sideEffect(it as NavigationEvent) | |
| } |
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() | |
| } |
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 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, |
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() |
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
| fun onViewCreated(view: PostListView) { | |
| binder?.stop() | |
| binder = bind(lifecycle, BinderLifecycleMode.CREATE_DESTROY) { | |
| store.states bindTo view | |
| store.labels bindTo view::sideEffect | |
| } | |
| } |
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
| 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), |