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 StoreRepository { | |
| Observable<Lce<List<Store>> getStoreEventStream() { | |
| return userBundleManager.switchMap { bundleEvent -> | |
| if (bundleEvent.isLoading()) { | |
| return Observable.just(Lce.loading()) | |
| } else if (bundleEvent.hasError()) { | |
| return Observable.just(Lce.error(bundleEvent.getError())) | |
| } else { | |
| // Stores can come from cached source or network | |
| return storeListEventStream(bundleEvent.data.getZipCode()) |
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
| typealias Reducer<T> = (T) -> T |
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 CommentFormModel(val commentService: CommentService) { | |
| fun formState( | |
| textChangedEvents: Observable<TextChangedEvent>, | |
| submitEvents: Observable<SubmitCommentEvent> | |
| ) : Observable<CommentFormData> { | |
| // Explicitly declaring the type, for clarity sake | |
| val requestStateEvents: Observable<Lce<Comment>> = submitEvents | |
| .switchMap { event -> commentService.submit(event.comment) } |
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
| // User changes text entry | |
| data class TextChangedEvent(val newText: String) | |
| // User clicks create | |
| data class CreateTodoEvent(val text: String) |
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
| data class TodoFormState( | |
| val enteredText: String, | |
| // After user clicks create, we send a post request. | |
| val createTodoRequest: CreateTodoRequestState?) |
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 reduce(event: TextChangedEvent, state: CommentFormData): CommentFormData { | |
| // Let's check comment validity | |
| val isValid = event.enteredText.length > 5 | |
| // Update the current state with the changes | |
| return state.copy(comment = event.enteredText, isCommentValid = isValid) | |
| } |
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 dataStream( | |
| textChangedEvents: Flowable<TextChangedEvent>, | |
| submitCommentEvents: Flowable<SubmitCommentEvent> | |
| ): Flowable<CommentFormData> { | |
| // We need to start with something | |
| val initialFormState = CommentFormData(comment = "", isCommentValid = false, submitRequest = null) | |
| // RxJava scan operator takes initial state and a reduce function | |
| // This operator manages the state for us, where it will keep track | |
| // of the latest state and provide it for next incoming event | |
| return textChangedEvents.scan(initialFormState) { event, currentState -> |
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
| // User can submit the comment | |
| data class SubmitCommentEvent(val comment: String) |
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
| interface CommentRepo { | |
| fun submitComment(comment: String): Observable<Lce<Comment>> | |
| } |
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
| sealed class UserEvent { | |
| data class TextChangedEvent(...): UserEvent() | |
| data class SubmitCommentRequestEvent(...): UserEvent() | |
| } |