A sample of Unidirectional Data Flow usage.
Shows a map with markers in cities that have some bike sharing service. When you click in any of those cities, the app will show all the bike sharing stations with current status of bike availability. The app will initially try to position in the current user city obtained from location services.
The abstract class UdfViewModel is a ViewModel subclass that delegates its UdfDispatcher implementation to UdfDispatcherImpl in order to handle all the Unidirectional Data Flow.
Each UdfViewModel should have one instance of UdfDispatcherImpl using specific UdfProcessor and UdfReducer implementations that basically do:
-
Processes each
Action
in UdfProcessor emitting aResult
orViewEffect
.(Action => Result / ViewEffect)
- It receives an
Action
, process some operation (fetching/saving from/to network or database) and returns an LiveData ofResult
that will be used by UdfReducer. - Additionally it may trigger a
ViewEffect
that means that it should be emitted but not kept as state such as: Showing a Snackbar, Navigating to another screen, etc.
- It receives an
-
Reduce the current cached
ViewState
takingResult
in consideration by UdfReducer.(Current ViewState + Result => New ViewState)
- It receives a
Result
from UdfProcessor and takes the currentViewState
to produce anotherViewState
that will be cached.
- It receives a
Bike data are fetched from CityBikes API, a project that provides bike sharing data for apps, research and projects to use.
- Unidirectional User Interface architectures
- Unidirectional data flow on Android using Kotlin
- MVI on Android with LiveData Coroutines
- Easy Coroutines in Android: viewModelScope
- Use Kotlin coroutines with Architecture components
- Async code on Kotlin: coroutines VS RxJava
- Select Current Place and Show Details on a Map
- https://github.com/oldergod/android-architecture/blob/todo-mvi-rxjava-kotlin/app/src/main/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksViewModel.kt
- https://github.com/TheSNAKY/Lives/blob/master/lives/src/main/java/com/snakydesign/livedataextensions/Transforming.kt
Copyright 2019 André Mion
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.