Slide 1

Slide 1 text

Flux for Android shaunkawano

Slide 2

Slide 2 text

What is Flux?

Slide 3

Slide 3 text

•Introduced by Facebook(@F8 in 2014) •Originally for building client-side web applications •Data flows in one direction Flux

Slide 4

Slide 4 text

Unidirectional Data Flow https://github.com/facebook/flux/tree/master/examples/flux-concepts#overview “The most important concept is that data flows in one direction.”

Slide 5

Slide 5 text

Flow of Data Action View Dispatcher Store http://facebook.github.io/flux/docs/in-depth-overview.html#structure-and-data-flow

Slide 6

Slide 6 text

Flow of Data Action View Dispatcher Store Action Creator

Slide 7

Slide 7 text

Simple

Slide 8

Slide 8 text

Flow of Data Action View Dispatcher Store Action Creator

Slide 9

Slide 9 text

Action View Dispatcher Store Action Creator

Slide 10

Slide 10 text

Action View Dispatcher Store Action Creator • User Interface • Render based on Store’s state • Store subscriber • Has no states

Slide 11

Slide 11 text

Action View Dispatcher Store Action Creator • Helper • Creates Actions • Passes Actions to Dispatcher • Has no states

Slide 12

Slide 12 text

Action View Dispatcher Store Action Creator • Container • Created by ActionCreator • Dispatched by Dispatcher • Delivered to Store

Slide 13

Slide 13 text

Action View Dispatcher Store Action Creator • Central Hub for all of the data flows • Dispatches Actions to Stores • Has no states

Slide 14

Slide 14 text

Action View Dispatcher Store Action Creator • State manager • Changes its states and data only by receiving Actions • Has states / internal logic

Slide 15

Slide 15 text

Store The ONLY state manager

Slide 16

Slide 16 text

Why? Action View Dispatcher Store Action Creator

Slide 17

Slide 17 text

DRY

Slide 18

Slide 18 text

Construct Flux Dataflow Action View Dispatcher Store Action Creator

Slide 19

Slide 19 text

image: https://material.io/guidelines/components/lists.html#lists-specs

Slide 20

Slide 20 text

Libraries in this Example •RxJava •Dagger •Data Binding •greenrobot/EventBus •uber/AutoDispose •trello/navi

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

View View

Slide 23

Slide 23 text

Activity Fragment Views(e.g. RecyclerView) Custom View View

Slide 24

Slide 24 text

onCreate(savedInstanceState: Bundle?) View

Slide 25

Slide 25 text

class UserListActivity : BaseActivity() { private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) }

Slide 26

Slide 26 text

class UserListActivity : BaseActivity() { private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) }

Slide 27

Slide 27 text

… …

Slide 28

Slide 28 text

class UserListActivity : BaseActivity() { private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) }

Slide 29

Slide 29 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) }

Slide 30

Slide 30 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var userListStore: UserListStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } 1. Initialize View 2. Subscribe State 3. Call ActionCreator’s functions View 3 Steps in Activity / Fragment

Slide 31

Slide 31 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var userListStore: UserListStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } 1. Initialize View View

Slide 32

Slide 32 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Initialize View binding.fab.setOnClickListener { doSomething() } binding.userList.adapter = userListAdapter binding.userList.layoutManager = LinearLayoutManager(this) }

Slide 33

Slide 33 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Initialize View binding.fab.setOnClickListener { doSomething() } binding.userList.adapter = userListAdapter binding.userList.layoutManager = LinearLayoutManager(this) } View 2. Subscribe State

Slide 34

Slide 34 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Initialize View binding.fab.setOnClickListener { doSomething() } binding.userList.adapter = userListAdapter binding.userList.layoutManager = LinearLayoutManager(this) } View 2. Subscribe “Loading” State

Slide 35

Slide 35 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Initialize View binding.fab.setOnClickListener { doSomething() } binding.userList.adapter = userListAdapter binding.userList.layoutManager = LinearLayoutManager(this) // Subscribe State loadingStore.loadingState.autoDisposable(scope) .subscribe { binding.isLoading = it } }

Slide 36

Slide 36 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Initialize View binding.fab.setOnClickListener { doSomething() } binding.userList.adapter = userListAdapter binding.userList.layoutManager = LinearLayoutManager(this) // Subscribe State loadingStore.loadingState.autoDisposable(scope) .subscribe { binding.isLoading = it } }

Slide 37

Slide 37 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // initView() binding.fab.setOnClickListener { doSomething() } binding.userList.adapter = userListAdapter binding.userList.layoutManager = LinearLayoutManager(this) // subscribeState() loadingStore.loadingState.autoDisposable(scope) .subscribe { binding.isLoading = it } }

Slide 38

Slide 38 text

class UserListActivity : Activity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initView() subscribeState() } …

Slide 39

Slide 39 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initView() subscribeState() } … View 3. Call ActionCreators’ functions

Slide 40

Slide 40 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initView() subscribeState() // Create & Dispatch Action actionCreator.showUserList() } …

Slide 41

Slide 41 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initView() subscribeState() // Create & Dispatch Action actionCreator.showUserList() } … View

Slide 42

Slide 42 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initView() subscribeState() // Create & Dispatch Action actionCreator.showUserList() } … Action View Dispatcher Store Action Creator

Slide 43

Slide 43 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initView() subscribeState() // Create & Dispatch Action actionCreator.showUserList() } … Action View Dispatcher Store Action Creator

Slide 44

Slide 44 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initView() subscribeState() // Create & Dispatch Action actionCreator.showUserList() } … Action Creator

Slide 45

Slide 45 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Action Creator Action View Dispatcher Store Action Creator

Slide 46

Slide 46 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Action Creator Action View Dispatcher Store Action Creator

Slide 47

Slide 47 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Action Creator

Slide 48

Slide 48 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Action Creator

Slide 49

Slide 49 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Action Creator

Slide 50

Slide 50 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Action Creator

Slide 51

Slide 51 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } LoadingAction Action

Slide 52

Slide 52 text

Action @PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } }

Slide 53

Slide 53 text

Action sealed class LoadingAction(activity: Activity) { val activityKey: String = activity.componentName.className class Idle(activity: Activity) : LoadingAction(activity) class Loading(activity: Activity) : LoadingAction(activity) }

Slide 54

Slide 54 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } UserListAction Action

Slide 55

Slide 55 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Action

Slide 56

Slide 56 text

Action sealed class UserListAction { class ShowUserList(val userList: UserList) : UserListAction() class Error(val error: Throwable) : UserListAction() }

Slide 57

Slide 57 text

Action sealed class UserListAction { class ShowUserList(val userList: UserList) : UserListAction() class Error(val error: Throwable) : UserListAction() } Action View Dispatcher Store Action Creator

Slide 58

Slide 58 text

Action sealed class UserListAction { class ShowUserList(val userList: UserList) : UserListAction() class Error(val error: Throwable) : UserListAction() } Action View Dispatcher Store Action Creator

Slide 59

Slide 59 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Action Creator

Slide 60

Slide 60 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Dispatcher

Slide 61

Slide 61 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Dispatcher

Slide 62

Slide 62 text

Dispatcher class Dispatcher { private val eventBus = EventBus.builder() .addIndex(DispatcherIndex()) .throwSubscriberException(BuildConfig.DEBUG) .build() fun dispatch(event: Any) { eventBus.post(event) } fun register(observer: Any) { eventBus.register(observer) } fun unregister(observer: Any) { eventBus.unregister(observer) }

Slide 63

Slide 63 text

class Dispatcher { private val eventBus = EventBus.builder() .addIndex(DispatcherIndex()) .throwSubscriberException(BuildConfig.DEBUG) .build() fun dispatch(event: Any) { eventBus.post(event) } fun register(observer: Any) { eventBus.register(observer) } fun unregister(observer: Any) { eventBus.unregister(observer) } Dispatcher For Broadcasting Actions: • RxJava • EventBus … and more?

Slide 64

Slide 64 text

Dispatcher class Dispatcher { private val eventBus = EventBus.builder() .addIndex(DispatcherIndex()) .throwSubscriberException(BuildConfig.DEBUG) .build() fun dispatch(event: Any) { eventBus.post(event) } fun register(observer: Any) { eventBus.register(observer) } fun unregister(observer: Any) { eventBus.unregister(observer) } Action View Dispatcher Store Action Creator

Slide 65

Slide 65 text

Dispatcher class Dispatcher { private val eventBus = EventBus.builder() .addIndex(DispatcherIndex()) .throwSubscriberException(BuildConfig.DEBUG) .build() fun dispatch(event: Any) { eventBus.post(event) } fun register(observer: Any) { eventBus.register(observer) } fun unregister(observer: Any) { eventBus.unregister(observer) } Action View Dispatcher Store Action Creator

Slide 66

Slide 66 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Dispatcher

Slide 67

Slide 67 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Dispatch LoadingAction Dispatcher

Slide 68

Slide 68 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Dispatcher

Slide 69

Slide 69 text

@PerActivity class LoadingStore @Inject constructor(private val activityKey: String, dispatcher: Dispatcher, navi: NaviComponent) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { … } Store

Slide 70

Slide 70 text

@PerActivity class LoadingStore @Inject constructor(private val activityKey: String, dispatcher: Dispatcher, navi: NaviComponent) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { … } Store

Slide 71

Slide 71 text

@PerActivity class LoadingStore @Inject constructor(private val activityKey: String, dispatcher: Dispatcher, navi: NaviComponent) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { … } Store

Slide 72

Slide 72 text

@PerActivity class LoadingStore @Inject constructor(private val activityKey: String, dispatcher: Dispatcher, navi: NaviComponent) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { … } Store For Observing Lifecycle Events: • AAC: Lifecycle • RxLifecycle: Navi … and more? Store

Slide 73

Slide 73 text

@PerActivity class LoadingStore @Inject constructor(private val activityKey: String, dispatcher: Dispatcher, navi: NaviComponent) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { … } Store

Slide 74

Slide 74 text

@PerActivity class LoadingStore @Inject constructor(private val activityKey: String, dispatcher: Dispatcher, navi: NaviComponent) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { … } Store

Slide 75

Slide 75 text

@PerActivity class LoadingStore @Inject constructor(private val activityKey: String, dispatcher: Dispatcher, navi: NaviComponent) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { … } Store

Slide 76

Slide 76 text

@PerActivity class LoadingStore @Inject constructor( … ) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { … } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { if (action.activityKey == activityKey) { when (action) { is LoadingAction.Loading -> { loadingSubject.onNext(true) } is LoadingAction.Idle -> { loadingSubject.onNext(false) } … Store

Slide 77

Slide 77 text

@PerActivity class LoadingStore @Inject constructor( … ) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { … } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { if (action.activityKey == activityKey) { when (action) { is LoadingAction.Loading -> { loadingSubject.onNext(true) } is LoadingAction.Idle -> { loadingSubject.onNext(false) } … Store

Slide 78

Slide 78 text

@PerActivity class LoadingStore @Inject constructor( … ) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { … } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { if (action.activityKey == activityKey) { when (action) { is LoadingAction.Loading -> { loadingSubject.onNext(true) } is LoadingAction.Idle -> { loadingSubject.onNext(false) } … Store

Slide 79

Slide 79 text

@PerActivity class LoadingStore @Inject constructor( … ) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { … } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { if (action.activityKey == activityKey) { when (action) { is LoadingAction.Loading -> { loadingSubject.onNext(true) } is LoadingAction.Idle -> { loadingSubject.onNext(false) } … Store

Slide 80

Slide 80 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Dispatch UserListAction Dispatcher

Slide 81

Slide 81 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Dispatcher

Slide 82

Slide 82 text

@PerActivity class UserListActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi, private val activity: Activity) { fun showUserList() { dispatcher.dispatch(LoadingAction.Loading(activity)) api.fetchUserList().subscribeBy( onSuccess= { userList -> dispatcher.dispatch(UserListAction.ShowUserList(userList)) dispatcher.dispatch(LoadingAction.Idle(activity)) }, onError = { e -> dispatcher.dispatch(UserListAction.Error(e)) dispatcher.dispatch(LoadingAction.Idle(activity)) }) } } Dispatcher

Slide 83

Slide 83 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserListAction) { when (action) { is UserListAction.ShowUserList -> { userList.addAll(action.userList.data) } is UserListAction.Error -> { … } } …

Slide 84

Slide 84 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserListAction) { when (action) { is UserListAction.ShowUserList -> { userList.addAll(action.userList.data) } is UserListAction.Error -> { … } } …

Slide 85

Slide 85 text

@PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList … fun addOnListChangedCallback(callback: ObservableList.OnListChangedCallback>) { userList.addOnListChangedCallback(callback) } fun removeOnListChangedCallback(callback: ObservableList.OnListChangedCallback>) { userList.removeOnListChangedCallback(callback) } Store

Slide 86

Slide 86 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserListAction) { when (action) { is UserListAction.ShowUserList -> { userList.addAll(action.userList.data) } is UserListAction.Error -> { … } } …

Slide 87

Slide 87 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserListAction) { when (action) { is UserListAction.ShowUserList -> { userList.addAll(action.userList.data) } is UserListAction.Error -> { … } } …

Slide 88

Slide 88 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserListAction) { when (action) { is UserListAction.ShowUserList -> { userList.addAll(action.userList.data) } is UserListAction.Error -> { … } } …

Slide 89

Slide 89 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val listSubject = PublishSubject.create>() val listState: Observable> = listSubject.hide() init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserListAction) { when (action) { is UserListAction.ShowUserList -> { listSubject.onNext(action.userList.data) } } } } Action View Dispatcher Store Action Creator

Slide 90

Slide 90 text

@PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val listSubject = PublishSubject.create>() val listState: Observable> = listSubject.hide() init { Streaming Loading State Change Using RxJava Store

Slide 91

Slide 91 text

@PerActivity class LoadingStore @Inject constructor( … ) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { … } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { if (action.activityKey == activityKey) { when (action) { is LoadingAction.Loading -> { loadingSubject.onNext(true) } is LoadingAction.Idle -> { loadingSubject.onNext(false) } … Store

Slide 92

Slide 92 text

@PerActivity class LoadingStore @Inject constructor( … ) { private val loadingSubject = PublishSubject.create() val loadingState: Observable = loadingSubject.hide() init { … } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { if (action.activityKey == activityKey) { when (action) { is LoadingAction.Loading -> { loadingSubject.onNext(true) } is LoadingAction.Idle -> { loadingSubject.onNext(false) } … Store

Slide 93

Slide 93 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // initView() binding.fab.setOnClickListener { doSomething() } binding.userList.adapter = userListAdapter binding.userList.layoutManager = LinearLayoutManager(this) // subscribeState() loadingStore.loadingState.autoDisposable(scope) .subscribe { binding.isLoading = it } } View

Slide 94

Slide 94 text

class UserListActivity : BaseActivity() { @Inject lateinit var actionCreator: UserListActionCreator @Inject lateinit var loadingStore: LoadingStore @Inject lateinit var userListAdapter: UserListAdapter private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_list) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // initView() binding.fab.setOnClickListener { doSomething() } binding.userList.adapter = userListAdapter binding.userList.layoutManager = LinearLayoutManager(this) // subscribeState() loadingStore.loadingState.autoDisposable(scope) .subscribe { binding.isLoading = it } } View

Slide 95

Slide 95 text

… …

Slide 96

Slide 96 text

… …

Slide 97

Slide 97 text

@PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val listSubject = PublishSubject.create>() val listState: Observable> = listSubject.hide() init { Streaming UserList Change Using Data Binding Store

Slide 98

Slide 98 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { navi.addListener(Event.CREATE, { dispatcher.register(this)}) navi.addListener(Event.DESTROY, { dispatcher.unregister(this)}) } @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserListAction) { when (action) { is UserListAction.ShowUserList -> { userList.addAll(action.userList.data) } is UserListAction.Error -> { … } } …

Slide 99

Slide 99 text

@PerActivity class UserListAdapter @Inject constructor( private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { private val listChangeCallback = … init { navi.addListener(Event.CREATE, { store.addOnListChangedCallback(listChangeCallback) }) navi.addListener(Event.DESTROY, { store.removeOnListChangedCallback(listChangeCallback) }) } … View

Slide 100

Slide 100 text

@PerActivity class UserListAdapter @Inject constructor( private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { private val listChangeCallback = … init { navi.addListener(Event.CREATE, { store.addOnListChangedCallback(listChangeCallback) }) navi.addListener(Event.DESTROY, { store.removeOnListChangedCallback(listChangeCallback) }) } … View

Slide 101

Slide 101 text

@PerActivity class UserListAdapter @Inject constructor( private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { private val listChangeCallback = … init { navi.addListener(Event.CREATE, { store.addOnListChangedCallback(listChangeCallback) }) navi.addListener(Event.DESTROY, { store.removeOnListChangedCallback(listChangeCallback) }) } … View

Slide 102

Slide 102 text

@PerActivity class UserListAdapter @Inject constructor( private val store: UserListStore navi: NaviComponent): RecyclerView.Adapter() { private val listChangeCallback = … init { … } override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.binding.executePendingBindings() } override fun getItemCount() = store.userListSize … View

Slide 103

Slide 103 text

Constructed Flux Dataflow Action View Dispatcher Store Action Creator

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

‘Favorite’? User Detail?

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

‘Favorite’? User Detail?

Slide 114

Slide 114 text

@PerActivity class UserListAdapter @Inject constructor( private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { init { … } override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.binding.executePendingBindings() } … View

Slide 115

Slide 115 text

@PerActivity class UserListAdapter @Inject constructor( private val userActionCreator: UserActionCreator, private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { init { … } override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.favIcon.setOnClickListener { userActionCreator.favorite(user) } holder.binding.executePendingBindings() } … View

Slide 116

Slide 116 text

@PerActivity class UserListAdapter @Inject constructor( private val userActionCreator: UserActionCreator, private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { init { … } override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.favIcon.setOnClickListener { userActionCreator.favorite(user) } holder.binding.executePendingBindings() } … View

Slide 117

Slide 117 text

@PerActivity class UserListAdapter @Inject constructor( private val userActionCreator: UserActionCreator, private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { init { … } override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.favIcon.setOnClickListener { userActionCreator.favorite(user) } holder.binding.executePendingBindings() } … View

Slide 118

Slide 118 text

@PerActivity class UserListAdapter @Inject constructor( private val userActionCreator: UserActionCreator, private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { init { … } override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.favIcon.setOnClickListener { userActionCreator.favorite(user) } holder.binding.executePendingBindings() } … View

Slide 119

Slide 119 text

@PerActivity class UserActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi) { fun favorite(user: User) { api.favorite(user.id).subscribeBy( onComplete = { dispatcher.dispatch(UserAction.Favorite(user.id)) }, … } } Action Creator

Slide 120

Slide 120 text

@PerActivity class UserActionCreator @Inject constructor(private val dispatcher: Dispatcher, private val api: SomeApi) { fun favorite(user: User) { api.favorite(user.id).subscribeBy( onComplete = { dispatcher.dispatch(UserAction.Favorite(user.id)) }, … } } Action Creator

Slide 121

Slide 121 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { … } … @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserAction) { when (action) { is UserAction.Favorite -> { userList.find { it.id == action.targetUserId }?.let { // Update or replace with new user } } } …

Slide 122

Slide 122 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { … } … @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserAction) { when (action) { is UserAction.Favorite -> { userList.find { it.id == action.targetUserId }?.let { // Update or replace with new user } } } …

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

‘Favorite’? User Detail?

Slide 126

Slide 126 text

@PerActivity class UserListAdapter @Inject constructor( private val userActionCreator: UserActionCreator, private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { init { … } override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.favIcon.setOnClickListener { userActionCreator.favorite(user) } holder.binding.executePendingBindings() } … View

Slide 127

Slide 127 text

@PerActivity class UserListAdapter @Inject constructor( private val activityActionCreator: ActivityActionCreator, private val userActionCreator: UserActionCreator, private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { … override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.favIcon.setOnClickListener { userActionCreator.favorite(user) } holder.binding.root.setOnClickListener { activityActionCreator.startUserDetailActivity(user) } holder.binding.executePendingBindings() } … View

Slide 128

Slide 128 text

@PerActivity class UserListAdapter @Inject constructor( private val activityActionCreator: ActivityActionCreator, private val userActionCreator: UserActionCreator, private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { … override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.favIcon.setOnClickListener { userActionCreator.favorite(user) } holder.binding.root.setOnClickListener { activityActionCreator.startUserDetailActivity(user) } holder.binding.executePendingBindings() } … View

Slide 129

Slide 129 text

@PerActivity class UserListAdapter @Inject constructor( private val activityActionCreator: ActivityActionCreator, private val userActionCreator: UserActionCreator, private val store: UserListStore, navi: NaviComponent): RecyclerView.Adapter() { … override fun onBindViewHolder( … ) { … holder.binding.user = store.getUserAt(position) holder.favIcon.setOnClickListener { userActionCreator.favorite(user) } holder.binding.root.setOnClickListener { activityActionCreator.startUserDetailActivity(user) } holder.binding.executePendingBindings() } … View

Slide 130

Slide 130 text

@PerActivity class ActivityActionCreator @Inject constructor(private val activity: Activity) { fun startUserDetailActivity(user: User) { val intent = UserDetailActivity.createIntent(activity, user) activity.startActivity(intent) } fun start…Activity ( … ) { … } fun start…Activity ( … ) { … } } Action Creator

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

No content

Slide 133

Slide 133 text

class UserDetailActivity : BaseActivity() { @Inject lateinit var actionCreator: UserActionCreator @Inject lateinit var loadingStore: LoadingStore private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_detail) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initView() subscribeState() } … View

Slide 134

Slide 134 text

class UserDetailActivity : BaseActivity() { @Inject lateinit var actionCreator: UserActionCreator @Inject lateinit var loadingStore: LoadingStore private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_detail) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initView() subscribeState() } … View

Slide 135

Slide 135 text

class UserDetailActivity : BaseActivity() { @Inject lateinit var actionCreator: UserActionCreator @Inject lateinit var loadingStore: LoadingStore private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_detail) } override fun onCreate(savedInstanceState: Bundle?) { initView() … } private fun initView() { binding.fab.setOnClickListener { userActionCreator.favorite(user) } } … View

Slide 136

Slide 136 text

class UserDetailActivity : BaseActivity() { @Inject lateinit var actionCreator: UserActionCreator @Inject lateinit var loadingStore: LoadingStore private val binding by lazy { DataBindingUtil.setContentView( this, R.layout.activity_user_detail) } override fun onCreate(savedInstanceState: Bundle?) { initView() … } private fun initView() { binding.fab.setOnClickListener { userActionCreator.favorite(user) } } … View

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

No content

Slide 140

Slide 140 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { … } … @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserAction) { when (action) { is UserAction.Favorite -> { userList.find { it.id == action.targetUserId }?.let { // Update or replace with new user } } } …

Slide 141

Slide 141 text

Store @PerActivity class UserListStore @Inject constructor(dispatcher: Dispatcher, navi: NaviComponent) { private val userList = ObservableArrayList init { … } … @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserAction) { when (action) { is UserAction.Favorite -> { userList.find { it.id == action.targetUserId }?.let { // Update or replace with new user } } } …

Slide 142

Slide 142 text

DRY

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

Code Consistency

Slide 145

Slide 145 text

1. Initialize View 2. Subscribe State 3. Call ActionCreators’ functions Steps in Activity / Fragment View

Slide 146

Slide 146 text

1. initView() 2. subscibeState() 3. actionCreator.doSomething() Steps in Activity / Fragment View

Slide 147

Slide 147 text

1. (Create & Dispatch LoadingAction) 2. Create & Dispatch Success / Error Action 3. (Create & Dispatch Idle Action) Steps in ActionCreator Action Creator

Slide 148

Slide 148 text

The logic inside Store becomes the intuitive specification

Slide 149

Slide 149 text

• Kotlin sealed classes & “when” keyword • Actions with descriptive names

Slide 150

Slide 150 text

@Subscribe(threadMode = ThreadMode.MAIN) fun on(action: LoadingAction) { if (action.activityKey == activityKey) { when (action) { is LoadingAction.Loading -> { loadingSubject.onNext(true) } is LoadingAction.Idle -> { loadingSubject.onNext(false) } … Store When given action is Loading, then the next loading state is “true”, if the action is Idle, then “false”.

Slide 151

Slide 151 text

Store @Subscribe(threadMode = ThreadMode.MAIN) fun on(action: UserAction) { when (action) { is UserAction.Favorite -> { userList.find { it.id == action.targetUserId }?.let { // Update or replace with new user } } } … When given action is User’s Favorite Action, then find target user from user list and update as favorited (if there is).

Slide 152

Slide 152 text

Slide 153

Slide 153 text

Dispatcher

Slide 154

Slide 154 text

Dispatcher class Dispatcher { private val eventBus = EventBus.builder() .addIndex(DispatcherIndex()) .throwSubscriberException(BuildConfig.DEBUG) .build() fun dispatch(event: Any) { eventBus.post(event) } fun register(observer: Any) { eventBus.register(observer) } fun unregister(observer: Any) { eventBus.unregister(observer) } Action View Dispatcher Store Action Creator

Slide 155

Slide 155 text

Dispatcher class Dispatcher { private val eventBus = EventBus.builder() .addIndex(DispatcherIndex()) .throwSubscriberException(BuildConfig.DEBUG) .build() fun dispatch(event: Any) { eventBus.post(event) } fun register(observer: Any) { eventBus.register(observer) } fun unregister(observer: Any) { eventBus.unregister(observer) } Action View Dispatcher Store Action Creator

Slide 156

Slide 156 text

Store Action Creator

Slide 157

Slide 157 text

Store The ONLY state manager

Slide 158

Slide 158 text

Tips

Slide 159

Slide 159 text

Tips • DataBinding • ✅ Construct feature-based • Talk / Ask to team members!

Slide 160

Slide 160 text

Thank you!