Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android における Model-View-Intent アーキテクチャ

Android における Model-View-Intent アーキテクチャ

Video: https://youtu.be/MIV7Mi9zko8

Android上の開発は非同期の扱いを避ける事はできないです。ネットワーク、フレームワーク、ユーザの操作などから非同期処理が発生してます。油断してしまうとアプリが複雑化してメンテナンスが難しくなります。皆様はネットワークからレスポンスを待っている途中、ユーザが画面をローテーションしても問題ないですか?並行してユーザがいろんな操作しても大丈夫でしょうか?

Model-View-Intent アーキテクチャは非同期処理が発生する前提で考えられたため、すべてがストリームとして扱ってデータの流れを一方通行にするかつ不変オブジェクトを使うのが MVI アーキテクチャの方針です。マルチスレッディングやAndroidのライフサイクルの対応から生じる問題がアーキテクチャによって解決されるおかげでアプリのロジックに集中できるようになり、コードが書きやすく、今後の保守も楽になります!

皆様が MVI アーキテクチャの強みを理解し自分で実現できるようにする目標で Kotlin と RxJava を使って並行処理、画面ローテーション、スナックバーを含んだ画面をどうやって作っていくかを語るつもりです!

Benoît Quenaudon

February 08, 2018
Tweet

More Decks by Benoît Quenaudon

Other Decks in Programming

Transcript

  1. sealed class TasksIntent { object InitialIntent : TasksIntent() object RefreshIntent

    : TasksIntent() data class ActivateTaskIntent(val task: Task) : TasksIntent() data class CompleteTaskIntent(val task: Task) : TasksIntent() }@
  2. sealed class TasksIntent { object InitialIntent : TasksIntent() object RefreshIntent

    : TasksIntent() data class ActivateTaskIntent(val task: Task) : TasksIntent() data class CompleteTaskIntent(val task: Task) : TasksIntent() object ClearCompletedTasksIntent : TasksIntent() }@
  3. sealed class TasksIntent { object InitialIntent : TasksIntent() object RefreshIntent

    : TasksIntent() data class ActivateTaskIntent(val task: Task) : TasksIntent() data class CompleteTaskIntent(val task: Task) : TasksIntent() object ClearCompletedTasksIntent : TasksIntent() data class ChangeFilterIntent(val filterType: TasksFilterType) : TasksIntent() }@
  4. sealed class TasksIntent { object InitialIntent : TasksIntent() object RefreshIntent

    : TasksIntent() data class ActivateTaskIntent(val task: Task) : TasksIntent() data class CompleteTaskIntent(val task: Task) : TasksIntent() object ClearCompletedTasksIntent : TasksIntent() data class ChangeFilterIntent(val filterType: TasksFilterType) : TasksIntent() }@
  5. class TasksFragment fun intents(): Observable<TasksIntent> { return initialIntent() }@a private

    fun initialIntent(): Observable<InitialIntent> { return Observable.just(InitialIntent) } }@
  6. class TasksFragment fun_intents():_Observable<TasksIntent> { return Observable.merge(initialIntent(), refreshIntent()) }@a private fun

    refreshIntent(): Observable<RefreshIntent> { return RxSwipeRefreshLayout.refreshes(swipeRefreshLayout) .map { RefreshIntent } }@ }@
  7. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> is RefreshIntent -> is ActivateTaskIntent -> is CompleteTaskIntent -> is ClearCompletedTasksIntent -> is ChangeFilterIntent -> }@
  8. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> is RefreshIntent -> is ActivateTaskIntent -> is CompleteTaskIntent -> is ClearCompletedTasksIntent -> is ChangeFilterIntent -> }@
  9. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> is RefreshIntent -> is ActivateTaskIntent -> is CompleteTaskIntent -> is ClearCompletedTasksIntent -> is ChangeFilterIntent -> }@
  10. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> LoadAndFilterTasksAction(TasksFilterType.ALL_TASKS) is RefreshIntent -> is ActivateTaskIntent -> is CompleteTaskIntent -> is ClearCompletedTasksIntent -> is ChangeFilterIntent -> }@
  11. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> LoadAndFilterTasksAction(TasksFilterType.ALL_TASKS) is RefreshIntent -> LoadTasksAction is ActivateTaskIntent -> is CompleteTaskIntent -> is ClearCompletedTasksIntent -> is ChangeFilterIntent -> }@
  12. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> LoadAndFilterTasksAction(TasksFilterType.ALL_TASKS) is RefreshIntent -> LoadTasksAction is ActivateTaskIntent -> ActivateTaskAction(intent.task) is CompleteTaskIntent -> is ClearCompletedTasksIntent -> is ChangeFilterIntent -> }@
  13. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> LoadAndFilterTasksAction(TasksFilterType.ALL_TASKS) is RefreshIntent -> LoadTasksAction is ActivateTaskIntent -> ActivateTaskAction(intent.task) is CompleteTaskIntent -> CompleteTaskAction(intent.task) is ClearCompletedTasksIntent -> is ChangeFilterIntent -> }@
  14. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> LoadAndFilterTasksAction(TasksFilterType.ALL_TASKS) is RefreshIntent -> LoadTasksAction is ActivateTaskIntent -> ActivateTaskAction(intent.task) is CompleteTaskIntent -> CompleteTaskAction(intent.task) is ClearCompletedTasksIntent -> ClearCompletedTasksAction is ChangeFilterIntent -> }@
  15. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> LoadAndFilterTasksAction(TasksFilterType.ALL_TASKS) is RefreshIntent -> LoadTasksAction is ActivateTaskIntent -> ActivateTaskAction(intent.task) is CompleteTaskIntent -> CompleteTaskAction(intent.task) is ClearCompletedTasksIntent -> ClearCompletedTasksAction is ChangeFilterIntent -> LoadAndFilterTasksAction(intent.filterType) }@
  16. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> LoadAndFilterTasksAction(TasksFilterType.ALL_TASKS) is RefreshIntent -> LoadTasksAction is ActivateTaskIntent -> ActivateTaskAction(intent.task) is CompleteTaskIntent -> CompleteTaskAction(intent.task) is ClearCompletedTasksIntent -> ClearCompletedTasksAction is ChangeFilterIntent -> LoadAndFilterTasksAction(intent.filterType) }@
  17. private TasksAction actionFromIntent(MviIntent intent) { if (intent instanceof InitialIntent) {

    return LoadTasks.loadAndFilter(true, TasksFilterType.ALL_TASKS); } if (intent instanceof ChangeFilterIntent) { return LoadTasks.loadAndFilter(false, ((ChangeFilterIntent) intent).filterType()); } if (intent instanceof RefreshIntent) { return LoadTasks.load(((RefreshIntent) intent).forceUpdate()); } if (intent instanceof ActivateTaskIntent) { return ActivateTaskAction.create(((ActivateTaskIntent) intent).task()); } if (intent instanceof CompleteTaskIntent) { return CompleteTaskAction.create(((CompleteTaskIntent) intent).task()); } if (intent instanceof ClearCompletedTasksIntent) { return ClearCompletedTasksAction.create(); } throw new IllegalArgumentException("do not know how to treat this intent " + intent); }
  18. private TasksAction actionFromIntent(MviIntent intent) { if (intent instanceof InitialIntent) {

    return LoadTasks.loadAndFilter(true, TasksFilterType.ALL_TASKS); } if (intent instanceof ChangeFilterIntent) { return LoadTasks.loadAndFilter(false, ((ChangeFilterIntent) intent).filterType()); } if (intent instanceof RefreshIntent) { return LoadTasks.load(((RefreshIntent) intent).forceUpdate()); } if (intent instanceof ActivateTaskIntent) { return ActivateTaskAction.create(((ActivateTaskIntent) intent).task()); } if (intent instanceof CompleteTaskIntent) { return CompleteTaskAction.create(((CompleteTaskIntent) intent).task()); } if (intent instanceof ClearCompletedTasksIntent) { return ClearCompletedTasksAction.create(); } throw new IllegalArgumentException("do not know how to treat this intent " + intent); }
  19. private TasksAction actionFromIntent(MviIntent intent) { if (intent instanceof InitialIntent) {

    return LoadTasks.loadAndFilter(true, TasksFilterType.ALL_TASKS); } if (intent instanceof ChangeFilterIntent) { return LoadTasks.loadAndFilter(false, ((ChangeFilterIntent) intent).filterType()); } if (intent instanceof RefreshIntent) { return LoadTasks.load(((RefreshIntent) intent).forceUpdate()); } if (intent instanceof ActivateTaskIntent) { return ActivateTaskAction.create(((ActivateTaskIntent) intent).task()); } if (intent instanceof CompleteTaskIntent) { return CompleteTaskAction.create(((CompleteTaskIntent) intent).task()); } if (intent instanceof ClearCompletedTasksIntent) { return ClearCompletedTasksAction.create(); } throw new IllegalArgumentException("do not know how to treat this intent " + intent); }
  20. private TasksAction actionFromIntent(MviIntent intent) { if (intent instanceof InitialIntent) {

    return LoadTasks.loadAndFilter(true, TasksFilterType.ALL_TASKS); } if (intent instanceof ChangeFilterIntent) { return LoadTasks.loadAndFilter(false, ((ChangeFilterIntent) intent).filterType()); } if (intent instanceof RefreshIntent) { return LoadTasks.load(((RefreshIntent) intent).forceUpdate()); } if (intent instanceof ActivateTaskIntent) { return ActivateTaskAction.create(((ActivateTaskIntent) intent).task()); } if (intent instanceof CompleteTaskIntent) { return CompleteTaskAction.create(((CompleteTaskIntent) intent).task()); } if (intent instanceof ClearCompletedTasksIntent) { return ClearCompletedTasksAction.create(); } throw new IllegalArgumentException("do not know how to treat this intent " + intent); }
  21. fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent

    -> LoadAndFilterTasksAction(TasksFilterType.ALL_TASKS) is RefreshIntent -> LoadTasksAction is ActivateTaskIntent -> ActivateTaskAction(intent.task) is CompleteTaskIntent -> CompleteTaskAction(intent.task) is ClearCompletedTasksIntent -> ClearCompletedTasksAction is ChangeFilterIntent -> LoadAndFilterTasksAction(intent.filterType) }@
  22. sealed class TasksAction { data class LoadAndFilterTasksAction(val filterType: TasksFilterType) :

    TasksAction() object LoadTasksAction : TasksAction() data class ActivateTaskAction(val task: Task) : TasksAction() data class CompleteTaskAction(val task: Task) : TasksAction() object ClearCompletedTasksAction : TasksAction() }@
  23. Action Load and Filter Tasks Logic Clear Completed Task Logic

    Complete Task Logic Activate Task Logic Load Tasks Logic Result
  24. var actionProcessor: ObservableTransformer<TasksAction, TasksResult> = ObservableTransformer { actions: Observable<TasksAction> ->

    actions.publish { shared -> Observable.merge( shared.ofType(LoadTasksAction::class.java).compose(loadTasksProcessor) )a }b }c
  25. var actionProcessor: ObservableTransformer<TasksAction, TasksResult> = ObservableTransformer { actions: Observable<TasksAction> ->

    actions.publish { shared -> Observable.merge( shared.ofType(LoadTasksAction::class.java).compose(loadTasksProcessor), shared.ofType(LoadAndFilterTasksAction::class.java).compose(loadAndFilterTasksProcessor) )a }b }c
  26. var actionProcessor: ObservableTransformer<TasksAction, TasksResult> = ObservableTransformer { actions: Observable<TasksAction> ->

    actions.publish { shared -> Observable.merge( shared.ofType(LoadTasksAction::class.java).compose(loadTasksProcessor), shared.ofType(LoadAndFilterTasksAction::class.java).compose(loadAndFilterTasksProcessor), shared.ofType(ActivateTaskAction::class.java).compose(activateTaskProcessor) )a }b }c
  27. var actionProcessor: ObservableTransformer<TasksAction, TasksResult> = ObservableTransformer { actions: Observable<TasksAction> ->

    actions.publish { shared -> Observable.merge( shared.ofType(LoadTasksAction::class.java).compose(loadTasksProcessor), shared.ofType(LoadAndFilterTasksAction::class.java).compose(loadAndFilterTasksProcessor), shared.ofType(ActivateTaskAction::class.java).compose(activateTaskProcessor), shared.ofType(ClearCompletedTasksAction::class.java).compose(clearCompletedTasksProcessor) )a }b }c
  28. var actionProcessor: ObservableTransformer<TasksAction, TasksResult> = ObservableTransformer { actions: Observable<TasksAction> ->

    actions.publish { shared -> Observable.merge( shared.ofType(LoadTasksAction::class.java).compose(loadTasksProcessor), shared.ofType(LoadAndFilterTasksAction::class.java).compose(loadAndFilterTasksProcessor), shared.ofType(ActivateTaskAction::class.java).compose(activateTaskProcessor), shared.ofType(ClearCompletedTasksAction::class.java).compose(clearCompletedTasksProcessor), shared.ofType(CompleteTaskAction::class.java).compose(completeTaskProcessor) )a }b }c
  29. var actionProcessor: ObservableTransformer<TasksAction, TasksResult> = ObservableTransformer { actions: Observable<TasksAction> ->

    actions.publish { shared -> Observable.merge( shared.ofType(LoadTasksAction::class.java).compose(loadTasksProcessor), shared.ofType(LoadAndFilterTasksAction::class.java).compose(loadAndFilterTasksProcessor), shared.ofType(ActivateTaskAction::class.java).compose(activateTaskProcessor), shared.ofType(ClearCompletedTasksAction::class.java).compose(clearCompletedTasksProcessor), shared.ofType(CompleteTaskAction::class.java).compose(completeTaskProcessor) )a }b }c
  30. val loadTasksProcessor = ObservableTransformer { actions: Observable<LoadTasksAction> -> actions.switchMap {

    tasksRepository.getTasks() // Observable<List<Tasks>> .startWith(LoadTasksResult.InFlight) }@ }@
  31. val loadTasksProcessor = ObservableTransformer { actions: Observable<LoadTasksAction> -> actions.switchMap {

    tasksRepository.getTasks() // Observable<List<Tasks>> .startWith(LoadTasksResult.InFlight) .map { tasks -> LoadTasksResult.Success(tasks) } }@ }@
  32. val loadTasksProcessor = ObservableTransformer { actions: Observable<LoadTasksAction> -> actions.switchMap {

    tasksRepository.getTasks() // Observable<List<Tasks>> .startWith(LoadTasksResult.InFlight) .map { tasks -> LoadTasksResult.Success(tasks) } .onErrorReturn { t -> loadTasksResult.Failure(t) } }@ }@
  33. val loadTasksProcessor = ObservableTransformer { actions: Observable<LoadTasksAction> -> actions.switchMap {

    tasksRepository.getTasks() // Observable<List<Tasks>> .startWith(LoadTasksResult.InFlight) .map { tasks -> LoadTasksResult.Success(tasks) } .onErrorReturn { t -> loadTasksResult.Failure(t) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) }@ }@
  34. val loadTasksProcessor = ObservableTransformer { actions: Observable<LoadTasksAction> -> actions.switchMap {

    tasksRepository.getTasks() // Observable<List<Tasks>> .startWith(LoadTasksResult.InFlight) .map { tasks -> LoadTasksResult.Success(tasks) } .onErrorReturn { t -> loadTasksResult.Failure(t) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) }@ }@
  35. val loadTasksProcessor = ObservableTransformer { actions: Observable<LoadTasksAction> -> actions.switchMap {

    tasksRepository.getTasks() // Observable<List<Tasks>> .map { tasks -> LoadTasksResult.Success(tasks) } .onErrorReturn { t -> loadTasksResult.Failure(t) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .startWith(LoadTasksResult.InFlight) }@ }@
  36. var actionProcessor: ObservableTransformer<TasksAction, TasksResult> = ObservableTransformer { actions: Observable<TasksAction> ->

    actions.publish { shared -> Observable.merge( shared.ofType(LoadTasksAction::class.java).compose(loadTasksProcessor), shared.ofType(LoadAndFilterTasksAction::class.java).compose(loadAndFilterTasksProcessor), shared.ofType(ActivateTaskAction::class.java).compose(activateTaskProcessor), shared.ofType(ClearCompletedTasksAction::class.java).compose(clearCompletedTasksProcessor), shared.ofType(CompleteTaskAction::class.java).compose(completeTaskProcessor) )a }b }c
  37. intents // Observable<TasksIntent> .map { intent -> actionFromIntent(intent) } //

    Observable<TasksAction> .compose(actionProcessor) // Observable<TasksResult>
  38. data class TasksViewState(z val isLoading: Boolean, val tasksFilterType: TasksFilterType, val

    tasks: List<Task>, val error: Throwable?, val taskComplete: Boolean, val taskActivated: Boolean, val completedTasksCleared: Boolean )@
  39. data class TasksViewState(z val isLoading: Boolean, val tasksFilterType: TasksFilterType, val

    tasks: List<Task>, val error: Throwable?, val taskComplete: Boolean, val taskActivated: Boolean, val completedTasksCleared: Boolean )@
  40. data class TasksViewState(z val isLoading: Boolean, val tasksFilterType: TasksFilterType, val

    tasks: List<Task>, val error: Throwable?, val taskComplete: Boolean, val taskActivated: Boolean, val completedTasksCleared: Boolean )@{ companion object Factory { fun default() = TasksViewState( isLoading = false, tasksFilterType = ALL_TASKS, tasks = emptyList(), error = null, taskComplete = false, taskActivated = false, completedTasksCleared = false) } }
  41. intents // Observable<TasksIntent> .map { intent -> actionFromIntent(intent) } //

    Observable<TasksAction> .compose(actionProcessor) // Observable<TasksResult> .scan(TasksViewState.default(), reducer) // Observable<TasksViewState>
  42. val reducer = BiFunction<TasksViewState, TasksResult, TasksViewState> { previousState: TasksViewState, result:

    TasksResult -> when (result)_{ is LoadTasksResult -> /***/ is LoadAndFilterTasksResult -> /***/ is CompleteTaskResult -> /***/ is ActivateTaskResult -> /***/ is ClearCompletedTasksResult -> /***/ }d }e
  43. val reducer = BiFunction<TasksViewState, TasksResult, TasksViewState> { previousState: TasksViewState, result:

    TasksResult -> when (result)_{ is LoadTasksResult -> { when (result) { is LoadTasksResult.InFlight -> /***/ is LoadTasksResult.Failure -> /***/ is LoadTasksResult.Success -> /***/ }b }c is LoadAndFilterTasksResult -> /***/ is CompleteTaskResult -> /***/ is ActivateTaskResult -> /***/ is ClearCompletedTasksResult -> /***/ }d }e
  44. val reducer = BiFunction<TasksViewState, TasksResult, TasksViewState> { previousState: TasksViewState, result:

    TasksResult -> when (result)_{ is LoadTasksResult -> { when (result) { is LoadTasksResult.InFlight -> previousState.copy(isLoading = true) is LoadTasksResult.Failure -> /***/ is LoadTasksResult.Success -> /***/ }b }c is LoadAndFilterTasksResult -> /***/ is CompleteTaskResult -> /***/ is ActivateTaskResult -> /***/ is ClearCompletedTasksResult -> /***/ }d }e
  45. val reducer = BiFunction<TasksViewState, TasksResult, TasksViewState> { previousState: TasksViewState, result:

    TasksResult -> when (result)_{ is LoadTasksResult -> { when (result) { is LoadTasksResult.InFlight -> previousState.copy(isLoading = true) is LoadTasksResult.Failure -> previousState.copy(isLoading = false, error = result.error) is LoadTasksResult.Success -> /***/ }b }c is LoadAndFilterTasksResult -> /***/ is CompleteTaskResult -> /***/ is ActivateTaskResult -> /***/ is ClearCompletedTasksResult -> /***/ }d }e
  46. val reducer = BiFunction<TasksViewState, TasksResult, TasksViewState> { previousState: TasksViewState, result:

    TasksResult -> when (result)_{ is LoadTasksResult -> { when (result) { is LoadTasksResult.InFlight -> previousState.copy(isLoading = true) is LoadTasksResult.Failure -> previousState.copy(isLoading = false, error = result.error) is LoadTasksResult.Success -> { previousState.copy(isLoading = false, tasks = result.tasks) }a }b }c is LoadAndFilterTasksResult -> /***/ is CompleteTaskResult -> /***/ is ActivateTaskResult -> /***/ is ClearCompletedTasksResult -> /***/ }d }e
  47. val reducer = BiFunction<TasksViewState, TasksResult, TasksViewState> { previousState: TasksViewState, result:

    TasksResult -> when (result)_{ is LoadTasksResult -> { when (result) { is LoadTasksResult.InFlight -> previousState.copy(isLoading = true) is LoadTasksResult.Failure -> previousState.copy(isLoading = false, error = result.error) is LoadTasksResult.Success -> { previousState.copy(isLoading = false, tasks = result.tasks) }a }b }c is LoadAndFilterTasksResult -> /***/ is CompleteTaskResult -> /***/ is ActivateTaskResult -> /***/ is ClearCompletedTasksResult -> /***/ }d }e
  48. intents // Observable<TasksIntent> .map { intent -> actionFromIntent(intent) } //

    Observable<TasksAction> .compose(actionProcessor) // Observable<TasksResult> .scan(TasksViewState.default(), reducer) // Observable<TasksViewState>
  49. intents // Observable<TasksIntent> .map { intent -> actionFromIntent(intent) } //

    Observable<TasksAction> .compose(actionProcessor) // Observable<TasksResult> .scan(TasksViewState.default(), reducer) // Observable<TasksViewState> .subscribe { state -> render(state) }
  50. fun render(state: TasksViewState) { swipeRefreshLayout.isRefreshing = state.isLoading if (state.error !=

    null) { showLoadingTasksError() return }a if (state.taskActivated) { showMessage(getString(R.string.task_marked_active)) }b if (state.taskComplete) { showMessage(getString(R.string.task_marked_complete)) }c if (state.completedTasksCleared) { showMessage(getString(R.string.completed_tasks_cleared)) }d }u
  51. if (state.taskActivated) { showMessage(getString(R.string.task_marked_active)) }b if (state.taskComplete) { showMessage(getString(R.string.task_marked_complete)) }c

    if (state.completedTasksCleared) { showMessage(getString(R.string.completed_tasks_cleared)) }d if (state.tasks.isEmpty()) { when (state.tasksFilterType) {z ACTIVE_TASKS -> showNoActiveTasks() COMPLETED_TASKS -> showNoCompletedTasks() ALL_TASKS -> showNoTasks() }e }_ }u
  52. showMessage(getString(R.string.completed_tasks_cleared)) }d if (state.tasks.isEmpty()) { when (state.tasksFilterType) {z ACTIVE_TASKS ->

    showNoActiveTasks() COMPLETED_TASKS -> showNoCompletedTasks() ALL_TASKS -> showNoTasks() }e }_else { listAdapter.replaceData(state.tasks) tasksView.visibility = View.VISIBLE noTasksView.visibility = View.GONE when (state.tasksFilterType) { ACTIVE_TASKS -> showActiveFilterLabel() COMPLETED_TASKS -> showCompletedFilterLabel() ALL_TASKS -> showAllFilterLabel() }g }h }u
  53. fun render(state: TasksViewState) { swipeRefreshLayout.isRefreshing = state.isLoading if (state.error !=

    null) { showLoadingTasksError() return }a if (state.taskActivated) { showMessage(getString(R.string.task_marked_active)) }b if (state.taskComplete) { showMessage(getString(R.string.task_marked_complete)) }c if (state.completedTasksCleared) { showMessage(getString(R.string.completed_tasks_cleared)) }d if (state.tasks.isEmpty()) { when (state.tasksFilterType) { ACTIVE_TASKS -> showNoActiveTasks() COMPLETED_TASKS -> showNoCompletedTasks() ALL_TASKS -> showNoTasks() }e }_else { listAdapter.replaceData(state.tasks) tasksView.visibility = View.VISIBLE noTasksView.visibility = View.GONE when (state.tasksFilterType) { ACTIVE_TASKS -> showActiveFilterLabel() COMPLETED_TASKS -> showCompletedFilterLabel() ALL_TASKS -> showAllFilterLabel() }g }h }u
  54. USER USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action

    State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY
  55. interface TasksUi { fun render(state: TasksViewState) fun intents(): Observable<TasksIntent> }@

    interface TasksViewModel { fun processIntents(intents: Observable<TasksIntent>) fun states(): Observable<TasksViewState> }@
  56. interface TasksUi { fun render(state: TasksViewState) fun intents(): Observable<TasksIntent> }@

    interface TasksViewModel { fun processIntents(intents: Observable<TasksIntent>) fun states(): Observable<TasksViewState> }@
  57. interface TasksUi { fun render(state: TasksViewState) fun intents(): Observable<TasksIntent> }@

    interface TasksViewModel { fun processIntents(intents: Observable<TasksIntent>) fun states(): Observable<TasksViewState> }@
  58. USER USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action

    State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY C B A
  59. INTENTS RENDER VIEW MODEL Intent Result Action State INTENT INTERPRETOR

    REDUCER PROCESSOR REPOSITORY B C A USER USER INTERFACE B C A
  60. USER USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action

    State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY A
  61. USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action State

    INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY
  62. class TasksViewModel : ViewModel() { private val intentsSubject = PublishSubject.create<TasksIntent>()

    fun processIntents(intents: Observable<TasksIntent>) { intents.subscribe(intentsSubject) }b fun states(): Observable<TasksViewState> { }c }e
  63. class TasksViewModel : ViewModel() { private val intentsSubject = PublishSubject.create<TasksIntent()

    val states: Observable<TasksViewState> = compose() fun processIntents(intents: Observable<TasksIntent>) { intents.subscribe(intentsSubject) }b private fun compose(): Observable<TasksViewState> { }c }e
  64. class TasksViewModel : ViewModel() { private val intentsSubject = PublishSubject.create<TasksIntent>()

    val states: Observable<TasksViewState> = compose() fun processIntents(intents: Observable<TasksIntent>) { intents.subscribe(intentsSubject) }b private fun compose(): Observable<TasksViewState> { return intentsSubject .map { intent -> actionFromIntent(intent) } .compose(actionProcessor) .scan(TasksViewState.default(), reducer) }c }e
  65. class TasksViewModel : ViewModel() { private val intentsSubject = PublishSubject.create<TasksIntent>()

    val states: Observable<TasksViewState> = compose() fun processIntents(intents: Observable<TasksIntent>) { intents.subscribe(intentsSubject) }b private fun compose(): Observable<TasksViewState> { return intentsSubject .map { intent -> actionFromIntent(intent) } .compose(actionProcessor) .scan(TasksViewState.default(), reducer) .replay(1) .autoConnect(0) }c }e
  66. class TasksViewModel : ViewModel() { private val intentsSubject = PublishSubject.create<TasksIntent>()

    val states: Observable<TasksViewState> = compose() fun processIntents(intents: Observable<TasksIntent>) { intents.subscribe(intentsSubject) }b private fun compose(): Observable<TasksViewState> { return intentsSubject .map { intent -> actionFromIntent(intent) } .compose(actionProcessor) .scan(TasksViewState.default(), reducer) .replay(1) .autoConnect(0) }c }e
  67. class TasksFragment : Fragment() { private val viewModel: TasksViewModel by

    lazy(NONE) { ViewModelProviders.of(this).get(TasksViewModel::class.java) }a fun intents(): Observable<TasksIntent> { /***/ } fun render(state: TasksViewState) { /***/ } }d
  68. class TasksFragment : Fragment() { private val viewModel: TasksViewModel by

    lazy(NONE) { ViewModelProviders.of(this).get(TasksViewModel::class.java) }a fun intents(): Observable<TasksIntent> { /***/ } fun render(state: TasksViewState) { /***/ } override fun onStart() { viewModel.states().subscribe(this::render) }b }d
  69. class TasksFragment : Fragment() { private val viewModel: TasksViewModel by

    lazy(NONE) { ViewModelProviders.of(this).get(TasksViewModel::class.java) }a private val disposables = CompositeDisposable()a fun intents(): Observable<TasksIntent> { /***/ } fun render(state: TasksViewState) { /***/ } override fun onStart() { disposables.add(viewModel.states().subscribe(this::render)) }b }d
  70. class TasksFragment : Fragment() { private val viewModel: TasksViewModel by

    lazy(NONE) { ViewModelProviders.of(this).get(TasksViewModel::class.java) }a private val disposables = CompositeDisposable() fun intents(): Observable<TasksIntent> { /***/ } fun render(state: TasksViewState) { /***/ } override fun onStart() { disposables.add(viewModel.states().subscribe(this::render)) viewModel.processIntents(intents()) }b }d
  71. class TasksFragment : Fragment() { private val viewModel: TasksViewModel by

    lazy(NONE) { ViewModelProviders.of(this).get(TasksViewModel::class.java) }a private val disposables = CompositeDisposable() fun intents(): Observable<TasksIntent> { /***/ } fun render(state: TasksViewState) { /***/ } override fun onStart() { disposables.add(viewModel.states().subscribe(this::render)) viewModel.processIntents(intents()) }b override fun onDestroy() { disposables.dispose() super.onDestroy() }c }d
  72. class TasksFragment : Fragment() { private val viewModel: TasksViewModel by

    lazy(NONE) { ViewModelProviders.of(this).get(TasksViewModel::class.java) }a private val disposables = CompositeDisposable() fun intents(): Observable<TasksIntent> { /***/ } fun render(state: TasksViewState) { /***/ } override fun onStart() { disposables.add(viewModel.states().subscribe(this::render)) viewModel.processIntents(intents()) }b override fun onDestroy() { disposables.dispose() super.onDestroy() }c }d
  73. class TasksFragment : Fragment() { private val viewModel: TasksViewModel by

    lazy(NONE) { ViewModelProviders.of(this).get(TasksViewModel::class.java) }a private val disposables = CompositeDisposable() fun intents(): Observable<TasksIntent> { /***/ } fun render(state: TasksViewState) { /***/ } override fun onStart() { disposables.add(viewModel.states().subscribe(this::render)) viewModel.processIntents(intents()) }b override fun onDestroy() { disposables.dispose() super.onDestroy() }c }d
  74. class TasksFragment fun intents(): Observable<TasksIntent> { return Observable.merge(initialIntent(), refreshIntent(), completeTaskIntent(),

    activateTaskIntent(), clearCompletedTaskIntent(), changeFilterIntent()) }@ }@ fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent -> LoadAndFilterTasksAction /***/ }@
  75. private fun compose(): Observable<TasksViewState> { return intentsSubject .map { this.actionFromIntent(it)

    } .compose(actionProcessorHolder.actionProcessor) .scan(TasksViewState.idle(), reducer) .replay(1) .autoConnect(0) }a
  76. private fun compose(): Observable<TasksViewState> { return intentsSubject .compose(intentFilter) .map {

    this.actionFromIntent(it) } .compose(actionProcessorHolder.actionProcessor) .scan(TasksViewState.idle(), reducer) .replay(1) .autoConnect(0) }a
  77. private fun compose(): Observable<TasksViewState> { return intentsSubject .compose(intentFilter) .map {

    this.actionFromIntent(it) } .compose(actionProcessorHolder.actionProcessor) .scan(TasksViewState.idle(), reducer) .replay(1) .autoConnect(0) }a private val intentFilter: ObservableTransformer<TasksIntent, TasksIntent> get() = ObservableTransformer { intents -> }z
  78. private fun compose(): Observable<TasksViewState> { return intentsSubject .compose(intentFilter) .map {

    this.actionFromIntent(it) } .compose(actionProcessorHolder.actionProcessor) .scan(TasksViewState.idle(), reducer) .replay(1) .autoConnect(0) }a private val intentFilter: ObservableTransformer<TasksIntent, TasksIntent> get() = ObservableTransformer { intents -> intents.publish { shared -> Observable.merge()w }o }z
  79. private fun compose(): Observable<TasksViewState> { return intentsSubject .compose(intentFilter) .map {

    this.actionFromIntent(it) } .compose(actionProcessorHolder.actionProcessor) .scan(TasksViewState.idle(), reducer) .replay(1) .autoConnect(0) }a private val intentFilter: ObservableTransformer<TasksIntent, TasksIntent> get() = ObservableTransformer { intents -> intents.publish { shared -> Observable.merge( shared.ofType(InitialIntent::class.java).take(1) )w }o }z
  80. private fun compose(): Observable<TasksViewState> { return intentsSubject .compose(intentFilter) .map {

    this.actionFromIntent(it) } .compose(actionProcessorHolder.actionProcessor) .scan(TasksViewState.idle(), reducer) .replay(1) .autoConnect(0) }a private val intentFilter: ObservableTransformer<TasksIntent, TasksIntent> get() = ObservableTransformer { intents -> intents.publish { shared -> Observable.merge( shared.ofType(InitialIntent::class.java).take(1), shared.notOfType(InitialIntent::class.java) )w }o }z
  81. private fun compose(): Observable<TasksViewState> { return intentsSubject .compose(intentFilter) .map {

    this.actionFromIntent(it) } .compose(actionProcessorHolder.actionProcessor) .scan(TasksViewState.idle(), reducer) .replay(1) .autoConnect(0) }a private val intentFilter: ObservableTransformer<TasksIntent, TasksIntent> get() = ObservableTransformer { intents -> intents.publish { shared -> Observable.merge( shared.ofType(InitialIntent::class.java).take(1), shared.notOfType(InitialIntent::class.java) )w }o }z
  82. USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action State

    INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY
  83. USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action State

    INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY
  84. USER INTERFACE VIEW MODEL SUBJECT TEST OBSERVER Intent Result Action

    State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY
  85. USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action State

    INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY
  86. USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action State

    INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY
  87. USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action State

    INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY
  88. Action Load and Filter Tasks Logic Clear Completed Task Logic

    Complete Task Logic Activate Task Logic Load Tasks Logic Result PROCESSOR
  89. Load and Filter Tasks Logic Clear Completed Task Logic Complete

    Task Logic Activate Task Logic Load Tasks Logic SUBJECT TEST OBSERVER SUBJECT SUBJECT SUBJECT SUBJECT TEST OBSERVER TEST OBSERVER TEST OBSERVER TEST OBSERVER