This library is heavily inspired by Javascript implementation of Redux: https://github.com/reactjs/redux.
The library is intended to be used in Presentation layer of the application. Presenters shouldn't be talking to the IO devices such as Database or Network directly, but should use abstract interfaces to access data. Any change to the State
will be accessible form any part of the app.
Library is based on unidirectional data flow. All the visual State
of the application is stored in the Store
. State
can be modified only via Action
s. State
is an immutable object, so any time we make change to it we need to create new one by reducing it.
Store
- contains the visualState
of the application represented byViewModel
s. ViaReducer
it reduces the actions and triggers theupdate
event via reactive streams to anyPresenter
binded.Reducer
- responsible for creating new state based on current state and passed action.Action
- simple object which contains type of action and state to be updated.Presenter
- creates the actions and dispatches them in theStore
. It interacts with theView
receiving interactions and updating it based on current applicationState
. This is the place where the layer below should be called to interact with IO devices such as database, network etc.
Sample usage can be found in the app
module. It is an Android app using Dagger as a Dependency Injection framework. https://github.com/ktalanda/ReduxAndroid/tree/master/app/src/main/java/pl/k2net/ktalanda/maroubrascanner.
-
Define
ViewModel
s for your visual state. It should be aData Class
in Kotlin and has to implementViewModel
interface fromredux
library. https://github.com/ktalanda/ReduxAndroid/blob/master/app/src/main/java/pl/k2net/ktalanda/maroubrascanner/main/details/DetailsViewModel.kt -
Define
Action
s which represent every change that could happen in your UI. It has to implementAction
interface fromredux
library and can contain extra information about the the state change. https://github.com/ktalanda/ReduxAndroid/blob/master/app/src/main/java/pl/k2net/ktalanda/maroubrascanner/main/details/UpdateDetailsListAction.kt -
Define
Reducer
which is responsible for changing theState
based on passedAction
. It has to extendReducer
class fromredux
library. https://github.com/ktalanda/ReduxAndroid/blob/master/app/src/main/java/pl/k2net/ktalanda/maroubrascanner/MaroubraReducer.kt -
Define
Logger
which is responsible for logs inside of theredux
library. https://github.com/ktalanda/ReduxAndroid/blob/master/app/src/main/java/pl/k2net/ktalanda/maroubrascanner/AppLogger.kt -
Define
Store
as aSingleton
somewhere in your application. Pass theReducer
,Logger
andState
via constructor.State
is a simpleMap
wherekey
has to beViewModel
class name, and value has to be theViewModel
itself. When creating theState
object you are defining the default state of the application. In the sample app it is provided by dagger. https://github.com/ktalanda/ReduxAndroid/blob/master/app/src/main/java/pl/k2net/ktalanda/maroubrascanner/AppModule.kt#L24 -
For every
View
create aPresenter
which has to extendPresenter
class fromredux
library.Store
singleton instance has to be passed to thePresenter
via constructor. It containsaddToUpdateList
method which allows to invoke list of actions on theupdate
event. Each timeupdate
event arises it iterates over action list and clear invoked actions.
MIT