Slide 1

Slide 1 text

Android ʹ͓͚Δ ʊModel-View-Intentʊ ΞʔΩςΫνϟ Benoît Quenaudon @oldergod

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

“What if the user was a function?” by Andre Staltz

Slide 4

Slide 4 text

“What if the user was a function?” by Andre Staltz

Slide 5

Slide 5 text

user()

Slide 6

Slide 6 text

intent(user())

Slide 7

Slide 7 text

model(intent(user()))

Slide 8

Slide 8 text

view(model(intent(user())))

Slide 9

Slide 9 text

user(view(model(intent(user()))))

Slide 10

Slide 10 text

view(model(intent()))

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

USER

Slide 13

Slide 13 text

sealed class TasksIntent { }@

Slide 14

Slide 14 text

sealed class TasksIntent { object InitialIntent : TasksIntent() }@

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

sealed class TasksIntent { object InitialIntent : TasksIntent() object RefreshIntent : TasksIntent() }@

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

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() }@

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

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() }@

Slide 23

Slide 23 text

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() }@

Slide 24

Slide 24 text

class TasksFragment fun intents(): Observable { }@ }@

Slide 25

Slide 25 text

class TasksFragment fun intents(): Observable { return initialIntent() }@a private fun initialIntent(): Observable { return Observable.just(InitialIntent) } }@

Slide 26

Slide 26 text

class TasksFragment fun_intents():_Observable { return Observable.merge(initialIntent(), refreshIntent()) }@a private fun refreshIntent(): Observable { return RxSwipeRefreshLayout.refreshes(swipeRefreshLayout) .map { RefreshIntent } }@ }@

Slide 27

Slide 27 text

class TasksFragment fun_intents():_Observable { return Observable.merge(initialIntent(), refreshIntent(), completeTaskIntent(), activateTaskIntent(), clearCompletedTaskIntent(), changeFilterIntent()) }@ }@

Slide 28

Slide 28 text

USER INTENTS Intent

Slide 29

Slide 29 text

intents // Observable

Slide 30

Slide 30 text

intents // Observable .map { intent -> actionFromIntent(intent) } // Observable

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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 -> }@

Slide 37

Slide 37 text

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 -> }@

Slide 38

Slide 38 text

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 -> }@

Slide 39

Slide 39 text

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) }@

Slide 40

Slide 40 text

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) }@

Slide 41

Slide 41 text

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); }

Slide 42

Slide 42 text

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); }

Slide 43

Slide 43 text

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); }

Slide 44

Slide 44 text

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); }

Slide 45

Slide 45 text

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) }@

Slide 46

Slide 46 text

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() }@

Slide 47

Slide 47 text

USER INTENTS Intent Action INTENT INTERPRETOR

Slide 48

Slide 48 text

Action Load and Filter Tasks Logic Clear Completed Task Logic Complete Task Logic Activate Task Logic Load Tasks Logic Result

Slide 49

Slide 49 text

var actionProcessor: ObservableTransformer = ObservableTransformer { actions: Observable -> }c

Slide 50

Slide 50 text

var actionProcessor: ObservableTransformer = ObservableTransformer { actions: Observable -> }c

Slide 51

Slide 51 text

var actionProcessor: ObservableTransformer = ObservableTransformer { actions: Observable -> actions.publish { shared -> }b }c

Slide 52

Slide 52 text

var actionProcessor: ObservableTransformer = ObservableTransformer { actions: Observable -> actions.publish { shared -> Observable.merge()a }b }c

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

var actionProcessor: ObservableTransformer = ObservableTransformer { actions: Observable -> 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

Slide 56

Slide 56 text

var actionProcessor: ObservableTransformer = ObservableTransformer { actions: Observable -> 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

Slide 57

Slide 57 text

var actionProcessor: ObservableTransformer = ObservableTransformer { actions: Observable -> 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

Slide 58

Slide 58 text

var actionProcessor: ObservableTransformer = ObservableTransformer { actions: Observable -> 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

Slide 59

Slide 59 text

val loadTasksProcessor = ObservableTransformer { actions: Observable -> actions.switchMap { tasksRepository.getTasks() // Observable> }@ }@

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

var actionProcessor: ObservableTransformer = ObservableTransformer { actions: Observable -> 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

Slide 67

Slide 67 text

intents // Observable .map { intent -> actionFromIntent(intent) } // Observable .compose(actionProcessor) // Observable

Slide 68

Slide 68 text

USER INTENTS Intent Result Action INTENT INTERPRETOR PROCESSOR REPOSITORY

Slide 69

Slide 69 text

θϩ͔Βը໘Λ࡞Δʹ͸ʁ

Slide 70

Slide 70 text

data class TasksViewState(z )@

Slide 71

Slide 71 text

data class TasksViewState(z val isLoading: Boolean )@

Slide 72

Slide 72 text

data class TasksViewState(z val isLoading: Boolean, val tasksFilterType: TasksFilterType )@

Slide 73

Slide 73 text

data class TasksViewState(z val isLoading: Boolean, val tasksFilterType: TasksFilterType, val tasks: List )@

Slide 74

Slide 74 text

data class TasksViewState(z val isLoading: Boolean, val tasksFilterType: TasksFilterType, val tasks: List, val error: Throwable? )@

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

data class TasksViewState(z val isLoading: Boolean, val tasksFilterType: TasksFilterType, val tasks: List, 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) } }

Slide 78

Slide 78 text

New State Result Default State

Slide 79

Slide 79 text

New State REDUCER Result Default State

Slide 80

Slide 80 text

New State REDUCER Result Previous State

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

val reducer = BiFunction { previousState: TasksViewState, result: TasksResult -> }e

Slide 83

Slide 83 text

val reducer = BiFunction { previousState: TasksViewState, result: TasksResult -> }e

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

val reducer = BiFunction { 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

Slide 86

Slide 86 text

val reducer = BiFunction { 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

Slide 87

Slide 87 text

val reducer = BiFunction { 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

Slide 88

Slide 88 text

val reducer = BiFunction { 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

Slide 89

Slide 89 text

val reducer = BiFunction { 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

Slide 90

Slide 90 text

USER INTENTS Intent Result Action State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

intents // Observable .map { intent -> actionFromIntent(intent) } // Observable .compose(actionProcessor) // Observable .scan(TasksViewState.default(), reducer) // Observable .subscribe { state -> render(state) }

Slide 93

Slide 93 text

fun render(state: TasksViewState) { }u

Slide 94

Slide 94 text

fun render(state: TasksViewState) { swipeRefreshLayout.isRefreshing = state.isLoading }u

Slide 95

Slide 95 text

fun render(state: TasksViewState) { swipeRefreshLayout.isRefreshing = state.isLoading if (state.error != null) { showLoadingTasksError() return }a }u

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

USER INTENTS RENDER Intent Result Action State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY

Slide 101

Slide 101 text

effects sidS Side effects

Slide 102

Slide 102 text

USER INTENTS RENDER Intent Result Action State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY

Slide 103

Slide 103 text

USER INTENTS RENDER Intent Result Action State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY

Slide 104

Slide 104 text

USER INTENTS RENDER Intent Result Action State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY

Slide 105

Slide 105 text

USER Intent Result Action State INTENT INTERPRETOR REDUCER REPOSITORY INTENTS RENDER PROCESSOR

Slide 106

Slide 106 text

USER INTENTS RENDER Intent Result Action State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY Unidirectional data flow

Slide 107

Slide 107 text

USER INTENTS RENDER Intent Result Action State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY

Slide 108

Slide 108 text

USER USER INTERFACE INTENTS RENDER Intent Result Action State INTENT INTERPRETOR REDUCER PROCESSOR REPOSITORY

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

interface TasksUi { fun render(state: TasksViewState) fun intents(): Observable }@ interface TasksViewModel { fun processIntents(intents: Observable) fun states(): Observable }@

Slide 111

Slide 111 text

interface TasksUi { fun render(state: TasksViewState) fun intents(): Observable }@ interface TasksViewModel { fun processIntents(intents: Observable) fun states(): Observable }@

Slide 112

Slide 112 text

interface TasksUi { fun render(state: TasksViewState) fun intents(): Observable }@ interface TasksViewModel { fun processIntents(intents: Observable) fun states(): Observable }@

Slide 113

Slide 113 text

฼͔Βͷ͓ి࿩Ͱ͢

Slide 114

Slide 114 text

User: initialIntent() App: render() User: activateTask(1) User: activateTask(2) User: refresh() Mum: Android: you.onStop()

Slide 115

Slide 115 text

User: initialIntent() App: render() User: activateTask(1) User: activateTask(2) User: refresh() Mum: Android: you.onStop()

Slide 116

Slide 116 text

User: initialIntent() App: render() User: activateTask(1) User: activateTask(2) User: refresh() Mum: Android: you.onStop()

Slide 117

Slide 117 text

User: initialIntent() App: render() User: activateTask(1) User: activateTask(2) User: refresh() Mum: Android: app.onStop()

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

Config change ΤϒϦ΢ΣΞ

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

USER INTERFACE VIEW MODEL SOURCE SUBSCRIBER

Slide 125

Slide 125 text

USER INTERFACE VIEW MODEL SOURCE SUBSCRIBER

Slide 126

Slide 126 text

USER INTERFACE VIEW MODEL SOURCE SUBSCRIBER SUBJECT

Slide 127

Slide 127 text

USER INTERFACE VIEW MODEL SOURCE SUBSCRIBER SUBJECT

Slide 128

Slide 128 text

USER INTERFACE VIEW MODEL SOURCE SUBSCRIBER SUBJECT

Slide 129

Slide 129 text

USER INTERFACE VIEW MODEL SOURCE SUBSCRIBER SUBJECT .autoConnect(0)

Slide 130

Slide 130 text

USER INTERFACE VIEW MODEL SOURCE SUBSCRIBER .autoConnect(0) SUBJECT

Slide 131

Slide 131 text

VIEW MODEL .autoConnect(0) SUBJECT

Slide 132

Slide 132 text

USER INTERFACE VIEW MODEL SOURCE SUBSCRIBER .autoConnect(0) SUBJECT

Slide 133

Slide 133 text

USER INTERFACE VIEW MODEL SOURCE SUBSCRIBER .replay(1) .autoConnect(0) SUBJECT

Slide 134

Slide 134 text

class TasksViewModel : ViewModel() { fun processIntents(intents: Observable) { }b fun states(): Observable { }c }e

Slide 135

Slide 135 text

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

Slide 136

Slide 136 text

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

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

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

Slide 139

Slide 139 text

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

Slide 140

Slide 140 text

class TasksFragment : Fragment() { fun intents(): Observable { /***/ } fun render(state: TasksViewState) { /***/ } }d

Slide 141

Slide 141 text

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

Slide 142

Slide 142 text

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

Slide 143

Slide 143 text

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 { /***/ } fun render(state: TasksViewState) { /***/ } override fun onStart() { disposables.add(viewModel.states().subscribe(this::render)) }b }d

Slide 144

Slide 144 text

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 { /***/ } fun render(state: TasksViewState) { /***/ } override fun onStart() { disposables.add(viewModel.states().subscribe(this::render)) viewModel.processIntents(intents()) }b }d

Slide 145

Slide 145 text

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 { /***/ } 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

Slide 146

Slide 146 text

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 { /***/ } 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

Slide 147

Slide 147 text

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 { /***/ } 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

Slide 148

Slide 148 text

class TasksFragment fun intents(): Observable { return Observable.merge(initialIntent(), refreshIntent(), completeTaskIntent(), activateTaskIntent(), clearCompletedTaskIntent(), changeFilterIntent()) }@ }@

Slide 149

Slide 149 text

class TasksFragment fun intents(): Observable { return Observable.merge(initialIntent(), refreshIntent(), completeTaskIntent(), activateTaskIntent(), clearCompletedTaskIntent(), changeFilterIntent()) }@ }@ fun actionFromIntent(intent: TasksIntent): TasksAction = when (intent) { is InitialIntent -> LoadAndFilterTasksAction /***/ }@

Slide 150

Slide 150 text

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

Slide 151

Slide 151 text

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

Slide 152

Slide 152 text

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

Slide 153

Slide 153 text

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

Slide 154

Slide 154 text

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

Slide 155

Slide 155 text

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

Slide 156

Slide 156 text

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

Slide 157

Slide 157 text

Ͷ͐஌ͬͯΔʁςετʂ

Slide 158

Slide 158 text

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

Slide 159

Slide 159 text

USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action State SUBJECT PROCESSOR REPOSITORY

Slide 160

Slide 160 text

USER INTERFACE VIEW MODEL INTENTS RENDER Intent Result Action State TEST OBSERVER PROCESSOR REPOSITORY

Slide 161

Slide 161 text

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

Slide 162

Slide 162 text

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

Slide 163

Slide 163 text

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

Slide 164

Slide 164 text

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

Slide 165

Slide 165 text

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

Slide 166

Slide 166 text

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

Slide 167

Slide 167 text

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

Slide 168

Slide 168 text

No content

Slide 169

Slide 169 text

Fin Benoît Quenaudon @oldergod