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

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!

David González

April 05, 2019
Tweet

More Decks by David González

Other Decks in Programming

Transcript

  1. 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() }
  2. 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() } }
  3. 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() } }
  4. 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() } }
  5. 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() } }
  6. class Store(initialState: ViewState = ViewState.Idle){ val stateLiveData = MutableLiveData<ViewState>() .apply

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

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

    { value = initialState } @MainThread fun dispatchState(state: ViewState) { stateLiveData.value = state } }
  9. 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())) }
  10. 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())) }
  11. 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())) }
  12. 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())) } }
  13. 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())) } } } }
  14. 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())) } } } }
  15. private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) ->

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

    State<ViewState>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } }
  17. 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) } } }
  18. 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) } } }
  19. 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) } } }
  20. 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) } } }
  21. private fun initViewModel() { viewModel=ViewModelProviders.of(this,vmf)[…] viewModel.observe(this, ::renderViewState) } override fun

    renderViewState(viewState: ViewState) { when (viewState) { is Pitch -> renderPitch(viewState) } } PitchActivity
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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?
  28. 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
  29. 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
  30. 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
  31. 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) } }
  32. 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) } }
  33. 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) } }
  34. sealed class ChangeAddressViewAction { object TalkToUs : ChangeAddressViewAction() data class

    LoadOrder(val orderId: String): ChangeAddressViewAction() data class ChangeAddress( val orderId: String, val shippingAddress: ShippingAddress): ChangeAddressViewAction() } Action
  35. 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
  36. 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)} }
  37. 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/
  38. 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() }
  39. 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() }
  40. 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())) }
  41. 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())) }
  42. 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())) }
  43. 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())) }
  44. 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) {
  45. 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) {
  46. 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
  47. 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 } }
  48. 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 } }
  49. 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() }
  50. 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()
  51. 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
  52. 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) } }
  53. 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)
  54. 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()
  55. 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() }
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. 
 public fun <E> CoroutineScope.produce( context: CoroutineContext = EmptyCoroutineContext, capacity:

    Int = 0, block: suspend ProducerScope<E>.() -> Unit ): ReceiveChannel<E> Producer
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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) }
  67. 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) }
  68. fun dispatchStates(channel: ReceiveChannel<State<Either<Failure, Success>>>) { launch { channel.consumeEach { reducer

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

    -> withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } } fun loadOrders() { store.dispatchStates(getOrders()) } Store
  70. 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
  71. @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)) } } }
  72. 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)) } } }
  73. { try { send { Either.Right(Success.Loading) } val orders =

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

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

    { states, action -> states + action(states.lastOrNull() ?: initialState) } } ReducerTest
  76. @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
  77. @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
  78. 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).
  79. 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' }
  80. 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