Unidirectional Data Flow on Android

Unidirectional Data Flow on Android

The introduction of Android Architecture Components made things are easier for the less experienced developers. However, In order to build robust, testable and scalable applications we need more. That's where the Unidirectional Data Flow architecture comes in, borrowed from Flex and Redux on the web.

The approach is based on the idea of an immutable state that represents the state of our app. All the components are decoupled from each other, and they work together taking advantage of Kotlin's language features. We solve the asynchrony problem using Coroutines, specifically Channels and Actors.

Do you want to know more? This talk will guide you through our implementation of this paradigm, with clear steps and a detailed explanation that will help you understand its value and spark your curiosity!

820df515de752bffa0ce2644a7927186?s=128

David González

April 05, 2019
Tweet

Transcript

  1. Unidirectional Data Flow on Android David González @dggonzalez malmstein@gmail.com Droidcon

    Italy 2019
  2. Credit when credit is due… Fernando Cejas Cesar Valiente Fabio

    Collini
  3. View ViewModel Store Reducer Action State send interpret dispatch notify

  4. View ViewModel Action

  5. sealed class OrdersViewAction { object NavigateToChatScreen : OrdersViewAction() data class

    SelectAddress(val orderId: String) : OrdersViewAction() data class CancelOrder(val orderId: String) : OrdersViewAction() data class TrackShipment(val url: String) : OrdersViewAction() }
  6. open class MainViewModel : ViewModel() { val store: Store =

    Store() fun observe(owner: LifecycleOwner, stateObserver: (ViewState) -> Unit) = store.stateLiveData.observe(owner, Observer { observer(it!!) }) fun interpret(action: ViewAction) override fun onCleared() { store.cancel() } }
  7. open class MainViewModel : ViewModel() { val store: Store =

    Store() fun observe(owner: LifecycleOwner,stateObserver: (ViewState) -> Unit) = store.stateLiveData.observe(owner, Observer { observer(it!!) }) fun interpret(action: ViewAction) override fun onCleared() { store.cancel() } }
  8. open class MainViewModel : ViewModel() { val store: Store =

    Store() fun observe(owner: LifecycleOwner, stateObserver: (ViewState) -> Unit) = store.stateLiveData.observe(owner, Observer { observer(it!!) }) fun interpret(action: ViewAction) override fun onCleared() { store.cancel() } }
  9. val store: Store = Store() fun observe(owner: LifecycleOwner, stateObserver: (ViewState)

    -> Unit) = store.stateLiveData.observe(owner, Observer { observer(it!!) }) fun interpret(action: ViewAction) override fun onCleared() { store.cancel() } }
  10. store.stateLiveData.observe(owner, Observer { observer(it!!) }) fun interpret(action: ViewAction) override fun

    onCleared() { store.cancel() } }
  11. ViewModel Store Reducer Action State

  12. class Store(initialState: ViewState = ViewState.Idle){ val stateLiveData = MutableLiveData<ViewState>() .apply

    { value = initialState } @MainThread fun dispatchState(state: ViewState) { stateLiveData.value = state } }
  13. class Store(initialState: ViewState = ViewState.Idle){ val stateLiveData = MutableLiveData<ViewState>() .apply

    { value = initialState } @MainThread fun dispatchState(state: ViewState) { stateLiveData.value = state } }
  14. class Store(initialState: ViewState = ViewState.Idle){ val stateLiveData = MutableLiveData<ViewState>() .apply

    { value = initialState } @MainThread fun dispatchState(state: ViewState) { stateLiveData.value = state } }
  15. class Store(initialState: ViewState = ViewState.Idle) : CoroutineScope { private val

    job = Job() fun cancel() = job.cancel() override val coroutineContext: CoroutineContext = job + Dispatchers.IO private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) -> State<ViewState>) { launch { val action = f(state()) withContext(Dispatchers.Main) { dispatchState(action(state())) }
  16. class Store(initialState: ViewState = ViewState.Idle) : CoroutineScope { private val

    job = Job() fun cancel() = job.cancel() override val coroutineContext: CoroutineContext = job + Dispatchers.IO private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) -> State<ViewState>) { launch { val action = f(state()) withContext(Dispatchers.Main) { dispatchState(action(state())) }
  17. class Store(initialState: ViewState = ViewState.Idle) : CoroutineScope { private val

    job = Job() fun cancel() = job.cancel() override val coroutineContext: CoroutineContext = job + Dispatchers.IO private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) -> State<ViewState>) { launch { val action = f(state()) withContext(Dispatchers.Main) { dispatchState(action(state())) }
  18. class Store(initialState: ViewState = ViewState.Idle) : CoroutineScope { private val

    job = Job() fun cancel() = job.cancel() override val coroutineContext: CoroutineContext = job + Dispatchers.IO private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) -> State<ViewState>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) } }
  19. private val job = Job() fun cancel() = job.cancel() override

    val coroutineContext: CoroutineContext = job + Dispatchers.IO private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) -> State<ViewState>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } }
  20. fun cancel() = job.cancel() override val coroutineContext: CoroutineContext = job

    + Dispatchers.IO private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) -> State<ViewState>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } }
  21. private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) ->

    State<ViewState>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } }
  22. private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) ->

    State<ViewState>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } }
  23. class PitchReducer @Inject constructor(val testimonialsRepo: TestimonialsRepository, val pricingRepo: PricingRepository){ operator

    fun invoke() = loadPitch() private fun loadPitch(): State<Pitch> { val testimonials = testimonialsRepo.all() val pricing = pricingRepo.current() return State { Pitch(testimonials, pricing) } } }
  24. class PitchReducer @Inject constructor(val testimonialsRepo: TestimonialsRepository, val pricingRepo: PricingRepository){ operator

    fun invoke() = loadPitch() private fun loadPitch(): State<Pitch> { val testimonials = testimonialsRepo.all() val pricing = pricingRepo.current() return State { Pitch(testimonials, pricing) } } }
  25. class PitchReducer @Inject constructor(val testimonialsRepo: TestimonialsRepository, val pricingRepo: PricingRepository){ operator

    fun invoke() = loadPitch() private fun loadPitch(): State<Pitch> { val testimonials = testimonialsRepo.all() val pricing = pricingRepo.current() return State { Pitch(testimonials, pricing) } } }
  26. class PitchReducer @Inject constructor(val testimonialsRepo: TestimonialsRepository, val pricingRepo: PricingRepository){ operator

    fun invoke() = loadPitch() private fun loadPitch(): State<Pitch> { val testimonials = testimonialsRepo.all() val pricing = pricingRepo.current() return State { Pitch(testimonials, pricing) } } }
  27. private fun initViewModel() { viewModel=ViewModelProviders.of(this,vmf)[…] viewModel.observe(this, ::renderViewState) } override fun

    renderViewState(viewState: ViewState) { when (viewState) { is Pitch -> renderPitch(viewState) } } PitchActivity
  28. class PitchViewModel @Inject constructor(private val reducer: PitchReducer): MainViewModel(){ fun interpret(action:

    ViewAction) { return when (action) { is LoadPitch -> store.dispatchState(reducer()) else -> throw IncompatibleActionException() } } PitchViewModel
  29. class PitchViewModel @Inject constructor(private val reducer: PitchReducer): MainViewModel(){ fun interpret(action:

    ViewAction) { return when (action) { is LoadPitch -> store.dispatchState(reducer()) else -> throw IncompatibleActionException() } } PitchViewModel
  30. class PitchViewModel @Inject constructor(private val reducer: PitchReducer): MainViewModel(){ fun interpret(action:

    ViewAction) { return when (action) { is LoadPitch -> store.dispatchState(reducer()) else -> throw IncompatibleActionException() } } PitchViewModel
  31. class PitchViewModel @Inject constructor(private val reducer: PitchReducer): MainViewModel(){ fun interpret(action:

    ViewAction) { return when (action) { is LoadPitch -> store.dispatchState(reducer()) else -> throw IncompatibleActionException() } } PitchViewModel
  32. class PitchViewModel @Inject constructor(private val reducer: PitchReducer): MainViewModel(){ fun interpret(action:

    ViewAction) { return when (action) { is LoadPitch -> store.dispatchState(reducer()) else -> throw IncompatibleActionException() } } PitchViewModel
  33. Problems going forward How do you deal with a more

    complicated ViewState? How do you deal with an error? How do you deal with multiple errors that must be displayed in screen?
  34. data class Address(val order: Order = Order.empty(), val wrongCity: Boolean,

    val wrongCountry: Boolean, val wrongAddress: Boolean, val isLoading: Boolean, val isCompleted: Boolean, val exception: Exception) : ViewState() } ViewState
  35. private fun render(viewState: Address) { if (viewState.isCompleted){ finish() else {

    progress.isVisible = viewState.isLoading viewState.exception?.let{ renderException(it) } cityError.isVisible = viewState.wrongCity countryError.isVisible = viewState.wrongCountry addressError.isVisible = viewState.wrongAddress renderOrder(order) } } Activity
  36. private fun render(viewState: Address) { if (viewState.isCompleted){ renderCompleted() else {

    progress.isVisible = viewState.isLoading viewState.exception?.let{ renderException(it) } cityError.isVisible = viewState.wrongCity countryError.isVisible = viewState.wrongCountry addressError.isVisible = viewState.wrongAddress renderOrder(order) } } Activity
  37. private fun render(viewState: Address) { if (viewState.isCompleted){ renderCompleted() else {

    progress.isVisible = viewState.isLoading viewState.exception?.let{ renderException(it) } cityError.isVisible = viewState.wrongCity countryError.isVisible = viewState.wrongCountry addressError.isVisible = viewState.wrongAddress renderOrder(order) } }
  38. private fun render(viewState: Address) { if (viewState.isCompleted){ renderCompleted() else {

    progress.isVisible = viewState.isLoading viewState.exception?.let{ renderException(it) } cityError.isVisible = viewState.wrongCity countryError.isVisible = viewState.wrongCountry addressError.isVisible = viewState.wrongAddress renderOrder(order) } }
  39. renderCompleted() else { progress.isVisible = viewState.isLoading viewState.exception?.let{ renderException(it) } cityError.isVisible

    = viewState.wrongCity countryError.isVisible = viewState.wrongCountry addressError.isVisible = viewState.wrongAddress renderOrder(order) } }
  40. progress.isVisible = viewState.isLoading viewState.exception?.let{ renderException(it) } cityError.isVisible = viewState.wrongCity countryError.isVisible

    = viewState.wrongCountry addressError.isVisible = viewState.wrongAddress renderOrder(order) } }
  41. sealed class ChangeAddressViewAction { object TalkToUs : ChangeAddressViewAction() data class

    LoadOrder(val orderId: String): ChangeAddressViewAction() data class ChangeAddress( val orderId: String, val shippingAddress: ShippingAddress): ChangeAddressViewAction() } Action
  42. fun interpret(action: ChangeAddressViewAction) { when (action) { is LoadOrder ->

    loadOrder(action.orderId) is ChangeAddress -> changeAddress(action) is TalkToUs -> navigator.toChat() } } private fun changeAddress(orderId: String, address: ShippingAddress) { store.dispatchState { State { Loading } } store.dispatchState (changeOrderAddress(orderId, shippingAddress)) } ViewModel
  43. when (action) { is LoadOrder -> loadOrder(action.orderId) is ChangeAddress ->

    changeAddress(action) is TalkToUs -> navigator.toChat() } } private fun changeAddress(orderId: String, address: ShippingAddress) { store.dispatchState { State { Loading } } store.dispatchState { changeOrderAddress(orderId, shippingAddress)} }
  44. Moar problems… Navigation Event vs View State ViewModel is calling

    store twice, once per state
  45. sealed class Either<out L, out R> { // Represents the

    left side of Either class // which by convention is a "Failure" data class Left<out L>(val a: L) : Either<L, Nothing>() //Represents the right side of Either class //which by convention is a "Success" data class Right<out R>(val b: R) : Either<Nothing, R>() } https://arrow-kt.io/docs/arrow/core/either/
  46. sealed class Failure { // App wide failures data class

    ServerError(val error: String) : Failure() object NetworkConnection : Failure() data class GenericError(val exception: Exception) : Failure() // Extend this class for feature specific failures abstract class FeatureFailure : Failure() }
  47. sealed class Success { // Extend this classes for feature

    specific success abstract class ViewState : Success() abstract class ViewEvent : Success() // App wide success view states object Idle : ViewState() object Loading : ViewState() }
  48. class Store(initialState: ViewState) : CoroutineScope { val stateLiveData = MutableLiveData<Either<Failure,

    Success>>() .apply { value = initialState } fun dispatchState(f: suspend (Either<Failure, Success>) -> State<Either<Failure, Success>>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) }
  49. class Store(initialState: ViewState) : CoroutineScope { val stateLiveData = MutableLiveData<Either<Failure,

    Success>>() .apply { value = initialState } fun dispatchState(f: suspend (Either<Failure, Success>) -> State<Either<Failure, Success>>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) }
  50. class Store(initialState: ViewState) : CoroutineScope { val stateLiveData = MutableLiveData<Either<Failure,

    Success>>() .apply { value = initialState } fun dispatchState(f: suspend (Either<Failure, Success>) -> State<Either<Failure, Success>>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) }
  51. val stateLiveData = MutableLiveData<Either<Failure, Success>>() .apply { value = initialState

    } fun dispatchState(f: suspend (Either<Failure, Success>) -> State<Either<Failure, Success>>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) }
  52. open class MainViewModel : ViewModel() { private val successLiveData =

    LiveData<Success>() private val failureLiveData = LiveData<Failure>() fun observe(owner: LifecycleOwner, successObserver: (Success) -> Unit, failureObserver: (Failure) -> Unit) { successLiveData.observe(owner, Observer { successObserver(it!!) }) failureLiveData.observe(owner, Observer { failureObserver(it) }) store.observeState(owner) { when (it) {
  53. open class MainViewModel : ViewModel() { private val successLiveData =

    LiveData<Success>() private val failureLiveData = LiveData<Failure>() fun observe(owner: LifecycleOwner, successObserver: (Success) -> Unit, failureObserver: (Failure) -> Unit) { successLiveData.observe(owner, Observer { successObserver(it!!) }) failureLiveData.observe(owner, Observer { failureObserver(it) }) store.observeState(owner) { when (it) {
  54. open class MainViewModel : ViewModel() { private val successLiveData =

    LiveData<Success>() private val failureLiveData = LiveData<Failure>() fun observe(owner: LifecycleOwner, successObserver: (Success) -> Unit, failureObserver: (Failure) -> Unit) { successLiveData.observe(owner, Observer { successObserver(it!!) }) failureLiveData.observe(owner, Observer { failureObserver(it) }) store.observeState(owner) { when (it) { is Either.Left -> failureLiveData.value = it.a is Either.Right -> successLiveData.value = it.b
  55. private val failureLiveData = LiveData<Failure>() fun observe(owner: LifecycleOwner, successObserver: (Success)

    -> Unit, failureObserver: (Failure) -> Unit) { successLiveData.observe(owner, Observer { successObserver(it!!) }) failureLiveData.observe(owner, Observer { failureObserver(it) }) store.observeState(owner) { when (it) { is Either.Left -> failureLiveData.value = it.a is Either.Right -> successLiveData.value = it.b } }
  56. failureObserver: (Failure) -> Unit) { successLiveData.observe(owner, Observer { successObserver(it!!) })

    failureLiveData.observe(owner, Observer { failureObserver(it) }) store.observeState(owner) { when (it) { is Either.Left -> failureLiveData.value = it.a is Either.Right -> successLiveData.value = it.b } }
  57. data class Address(val order: Order = Order.empty(), val wrongCity: Boolean,

    val wrongCountry: Boolean, val wrongAddress: Boolean, val isLoading: Boolean, val isCompleted: Boolean, val exception: Exception) : ViewState() }
  58. data class Address(val order: Order = Order.empty()) : Success.ViewState() object

    TalkToUs : Success.ViewEvent() object AddressChanged : Success.ViewEvent() data class InvalidAddress(val wrongCity: Boolean, val wrongCountry: Boolean, val wrongAddress: Boolean) : FeatureFailure()
  59. viewModel = ViewModelProviders.of(this, viewModelFactory)[MyViewModel::class.java] viewModel.observe(this, ::renderSuccess, ::renderFailure) protected fun renderSuccess(success:

    Success) { when (success) { is Success.ViewEvent -> reactToEvent(success) is Success.ViewState -> renderViewState(success) } } override fun renderViewState(viewState: Success.ViewState) { when (viewState) { Activity
  60. viewModel = ViewModelProviders.of(this, viewModelFactory)[MyViewModel::class.java] viewModel.observe(this, ::renderSuccess, ::renderFailure) protected fun renderSuccess(success:

    Success) { when (success) { is Success.ViewEvent -> reactToEvent(success) is Success.ViewState -> renderViewState(success) } } override fun renderViewState(viewState: Success.ViewState) { when (viewState) { is Orders -> renderOrders(viewState) } }
  61. when (success) { is Success.ViewEvent -> reactToEvent(success) is Success.ViewState ->

    renderViewState(success) } } override fun renderViewState(viewState: Success.ViewState) { when (viewState) { is Orders -> renderOrders(viewState) } } override fun reactToEvent(event: Success.ViewEvent) { when (event) { is Success.TalkToUs -> navigator.toSupportChat() is SelectAddress -> navigator.toChangeAddress(this, event.orderId) is OrderCancelled -> displaySnackbar(event.failureMessage) is CancelOrder -> navigator.toCancelOrder(this, event.orderId)
  62. override fun renderViewState(viewState: Success.ViewState) { when (viewState) { is Orders

    -> renderOrders(viewState) } } override fun reactToEvent(event: Success.ViewEvent) { when (event) { is Success.TalkToUs -> navigator.toSupportChat() is SelectAddress -> navigator.toChangeAddress(this, event.orderId) is OrderCancelled -> displaySnackbar(event.failureMessage) is CancelOrder -> navigator.toCancelOrder(this, event.orderId) is TrackShipment -> navigator.toTrackingShipment(event.url) } } override fun renderFailure(failure: Failure) { ordersList.gone()
  63. is Success.TalkToUs -> navigator.toSupportChat() is SelectAddress -> navigator.toChangeAddress(this, event.orderId) is

    OrderCancelled -> displaySnackbar(event.failureMessage) is CancelOrder -> navigator.toCancelOrder(this, event.orderId) is TrackShipment -> navigator.toTrackingShipment(event.url) } } override fun renderFailure(failure: Failure) { ordersList.gone() loading.gone() empty.show() }
  64. The final step…

  65. None
  66. public interface SendChannel<in E> { public suspend fun send(element: E)

    public fun offer(element: E) public fun close(cause: Throwable? = null): Boolean } public interface ReceiveChannel<out E> { public suspend fun receive(): E public fun close(cause: Throwable? = null): Boolean } Deferred vs Channel
  67. fun main() = runBlocking<Unit> { val channel = Channel<Int>() launch

    { for (i in 1..5) { channel.send(i) } launch { for (value in channel) { println("received: $value") } //console output received: 1 received: 2 received: 3 received: 4 received: 5 Channel
  68. fun main() = runBlocking<Unit> { val channel = Channel<Int>() launch

    { for (i in 1..5) { channel.send(i) } launch { for (value in channel) { println("received: $value") } //console output received: 1 received: 2 received: 3 received: 4 received: 5 Channel
  69. fun main() = runBlocking<Unit> { val channel = Channel<Int>() launch

    { for (i in 1..5) { channel.send(i) } launch { for (value in channel) { println("received: $value") } //console output received: 1 received: 2 received: 3 received: 4 received: 5 Channel
  70. fun main() = runBlocking<Unit> { val channel = Channel<Int>() launch

    { for (i in 1..5) { channel.send(i) } launch { for (value in channel) { println("received: $value") } //console output received: 1 received: 2 received: 3 received: 4 received: 5 Channel
  71. 
 public fun <E> CoroutineScope.produce( context: CoroutineContext = EmptyCoroutineContext, capacity:

    Int = 0, block: suspend ProducerScope<E>.() -> Unit ): ReceiveChannel<E> Producer
  72. val publisher = produce(capacity = 2){ for (i in 1..5){

    send(i) } launch { publisher.consumeEach { println("received: $it”) } } //console output received: 1 received: 2 received: 3 received: 4 received: 5 Producer
  73. val publisher = produce(capacity = 2){ for (i in 1..5){

    send(i) } launch { publisher.consumeEach { println("received: $it”) } } //console output received: 1 received: 2 received: 3 received: 4 received: 5 Producer
  74. val publisher = produce(capacity = 2){ for (i in 1..5){

    send(i) } launch { publisher.consumeEach { println("received: $it”) } } //console output received: 1 received: 2 received: 3 received: 4 received: 5 Producer
  75. val publisher = produce(capacity = 2){ for (i in 1..5){

    send(i) } launch { publisher.consumeEach { println("received: $it”) } } //console output received: 1 received: 2 received: 3 received: 4 received: 5 Producer
  76. open class Reducer { suspend fun <T> ProducerScope<State<T>>.send(f: T.() ->

    T) = send(State(f)) fun <T> produceStates(f: suspend ProducerScope<State<T>>.() -> Unit) : ReceiveChannel<State<T>> = GlobalScope.produce(block = f) }
  77. open class Reducer { suspend fun <T> ProducerScope<State<T>>.send(f: T.() ->

    T) = send(State(f)) fun <T> produceStates(f: suspend ProducerScope<State<T>>.() -> Unit) : ReceiveChannel<State<T>> = GlobalScope.produce(block = f) }
  78. fun dispatchStates(channel: ReceiveChannel<State<Either<Failure, Success>>>) { launch { channel.consumeEach { reducer

    -> withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } } fun loadOrders() { store.dispatchStates(getOrders()) } Store
  79. fun dispatchStates(channel: ReceiveChannel<State<Either<Failure, Success>>>) { launch { channel.consumeEach { reducer

    -> withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } } fun loadOrders() { store.dispatchStates(getOrders()) } Store
  80. launch { channel.consumeEach { action -> withContext(Dispatchers.Main) { dispatchState(action(state())) }

    } } } fun loadOrders() { store.dispatchStates(getOrders()) }
  81. class GetOrders @Inject constructor(private val repository: OrdersRepository) : Reducer() {

    operator fun invoke() = getOrders() private fun getOrders() : ReceiveChannel<State<Either<Failure, Success>>> = produceActions { try { send { Either.Right(Success.Loading) } val orders = repository.allOrders() send { Either.Right(Orders(orders)) } } catch (e: Exception) { GetOrders
  82. @Inject constructor(private val repository: OrdersRepository) : Reducer() { operator fun

    invoke() = getOrders() private fun getOrders() : ReceiveChannel<State<Either<Failure, Success>>> = produceStates { try { send { Either.Right(Success.Loading) } val orders = repository.allOrders() send { Either.Right(Orders(orders)) } } catch (e: Exception) { send { Either.Left(OrdersFailure(e)) } } }
  83. private fun getOrders() : ReceiveChannel<State<Either<Failure, Success>>> = produceStates { try

    { send { Either.Right(Success.Loading) } val orders = repository.allOrders() send { Either.Right(Orders(orders)) } } catch (e: Exception) { send { Either.Left(OrdersFailure(e)) } } }
  84. { try { send { Either.Right(Success.Loading) } val orders =

    repository.allOrders() send { Either.Right(Orders(orders)) } } catch (e: Exception) { send { Either.Left(OrdersFailure(e)) } } }
  85. Testing UI tests on Activity / Fragments 
 Unit +

    Integration tests on ViewModels Unit + instrumentation tests on Reducers
  86. suspend inline fun <T> ReceiveChannel<State<T>>.states(initialState: T): List<T> { return fold(emptyList())

    { states, action -> states + action(states.lastOrNull() ?: initialState) } } ReducerTest
  87. @Test fun `changes state on successful loading`() { val expectedState

    = Orders(listOf(ORDER_1, ORDER_2)) coEvery { ordersRepository.all() } returns listOf(ORDER_1, ORDER_2) val states = runBlocking { useCase.invoke().states(Either.Right(Success.Idle)) } assert(states).hasSize(2) assert(states[0]).isEqualTo(Either.Right(Success.Loading)) assert(states[1]).isEqualTo(Either.Right(expectedState)) } ReducerTest
  88. @Test fun `changes state on error`() { val exception =

    IOException() coEvery { ordersRepository.allOrders() } throws exception val states = runBlocking { useCase.invoke().states(Either.Right(Success.Idle)) } assert(states).hasSize(2) assert(states[0]).isEqualTo(Either.Right(Success.Loading)) assert(states[1]).isEqualTo(Either.Left(OrdersFailure(exception))) } ReducerTest
  89. What’s next? Explore Data Binding - Watch Jose’s talk This

    API will become obsolete in future updates with introduction of lazy asynchronous streams (https://github.com/Kotlin/kotlinx.coroutines/issues/254).
  90. None
  91. Thank you! David González @dggonzalez malmstein@gmail.com

  92. app/build.gradle buildscript { repositories { jcenter() } dependencies { classpath

    'com.android.tools.build:gradle:1.5.0' classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.2.1' } } ../build.gradle dependencies { compile 'com.android.support:herbs-v13:23.1.1' compile 'com.android.support:meat:23.1.1' compile ‘com.android.support:cookware-v7:23.1.1' compile ‘com.google.android.gms:play-services:8.4.0' }
  93. CookingMethod cookingMethod = new CookingMethod.Builder() .withFlavour(Flavours.MediumRare) .withSide(Sides.Fries) .fromButcher(Butchers.GingerPig) .build(); private

    Steak from(CookingMethod cookingMethod){ Steak steak = Steak.from(cookingMethod.getButcher()); steak.addOil(new Oil()); steak.addSalt(new Salt()); steak.addPepper(new Pepper()); return steak; } Steak.java