Share
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
Transcript
93 slides
-
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() } -
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() } } -
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() } } -
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() } } -
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() } } -
store.stateLiveData.observe(owner, Observer { observer(it!!) }) fun interpret(action: ViewAction) override fun
onCleared() { store.cancel() } } -
class Store(initialState: ViewState = ViewState.Idle){ val stateLiveData = MutableLiveData<ViewState>() .apply
{ value = initialState } @MainThread fun dispatchState(state: ViewState) { stateLiveData.value = state } } -
class Store(initialState: ViewState = ViewState.Idle){ val stateLiveData = MutableLiveData<ViewState>() .apply
{ value = initialState } @MainThread fun dispatchState(state: ViewState) { stateLiveData.value = state } } -
class Store(initialState: ViewState = ViewState.Idle){ val stateLiveData = MutableLiveData<ViewState>() .apply
{ value = initialState } @MainThread fun dispatchState(state: ViewState) { stateLiveData.value = state } } -
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())) } -
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())) } -
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())) } -
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())) } } -
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())) } } } } -
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())) } } } } -
private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) ->
State<ViewState>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } } -
private fun state() = stateLiveData.value!! fun dispatchState(f: suspend (ViewState) ->
State<ViewState>) { launch { val reducer = f(state()) withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } } -
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) } } } -
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) } } } -
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) } } } -
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) } } } -
private fun initViewModel() { viewModel=ViewModelProviders.of(this,vmf)[…] viewModel.observe(this, ::renderViewState) } override fun
renderViewState(viewState: ViewState) { when (viewState) { is Pitch -> renderPitch(viewState) } } PitchActivity -
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 -
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 -
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 -
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 -
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 -
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? -
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 -
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 -
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 -
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) } } -
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) } } -
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) } } -
progress.isVisible = viewState.isLoading viewState.exception?.let{ renderException(it) } cityError.isVisible = viewState.wrongCity countryError.isVisible
= viewState.wrongCountry addressError.isVisible = viewState.wrongAddress renderOrder(order) } } -
sealed class ChangeAddressViewAction { object TalkToUs : ChangeAddressViewAction() data class
LoadOrder(val orderId: String): ChangeAddressViewAction() data class ChangeAddress( val orderId: String, val shippingAddress: ShippingAddress): ChangeAddressViewAction() } Action -
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 -
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)} } -
Moar problems… Navigation Event vs View State ViewModel is calling
store twice, once per state -
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/ -
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() } -
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() } -
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())) } -
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())) } -
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())) } -
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())) } -
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) { -
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) { -
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 -
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 } } -
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 } } -
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() } -
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() -
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 -
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) } } -
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) -
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() -
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() } -
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 -
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 -
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 -
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 -
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 -
public fun <E> CoroutineScope.produce( context: CoroutineContext = EmptyCoroutineContext, capacity:
Int = 0, block: suspend ProducerScope<E>.() -> Unit ): ReceiveChannel<E> Producer -
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 -
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 -
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 -
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 -
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) } -
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) } -
fun dispatchStates(channel: ReceiveChannel<State<Either<Failure, Success>>>) { launch { channel.consumeEach { reducer
-> withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } } fun loadOrders() { store.dispatchStates(getOrders()) } Store -
fun dispatchStates(channel: ReceiveChannel<State<Either<Failure, Success>>>) { launch { channel.consumeEach { reducer
-> withContext(Dispatchers.Main) { dispatchState(reducer(state())) } } } } fun loadOrders() { store.dispatchStates(getOrders()) } Store -
launch { channel.consumeEach { action -> withContext(Dispatchers.Main) { dispatchState(action(state())) }
} } } fun loadOrders() { store.dispatchStates(getOrders()) } -
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 -
@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)) } } } -
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)) } } } -
{ try { send { Either.Right(Success.Loading) } val orders =
repository.allOrders() send { Either.Right(Orders(orders)) } } catch (e: Exception) { send { Either.Left(OrdersFailure(e)) } } } -
Testing UI tests on Activity / Fragments Unit +
Integration tests on ViewModels Unit + instrumentation tests on Reducers -
suspend inline fun <T> ReceiveChannel<State<T>>.states(initialState: T): List<T> { return fold(emptyList())
{ states, action -> states + action(states.lastOrNull() ?: initialState) } } ReducerTest -
@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 -
@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 -
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). -
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' } -
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