Skip to content

zyvpeople/Redux

Repository files navigation

Redux (Guide to Redux for Android in Kotlin)

Adding Redux to your Project

  1. Clone repository
  2. Build 'reduxCore' module
  3. Add @aar file to your project

How to use Redux

  1. Define default state
val defaultState = ""
  1. Define action
class AddSymbolAction : Action
  1. Define reducer
class AddSymbolReducer : Reducer<String> {
    override fun reduce(oldState: String, action: Action): String =
        (action as? AddSymbolReducer)?.let { oldState + "$" } ?: oldState 
}
  1. Create middleware
class LogActionMiddleware : Middleware<String> {
    override fun dispatch(state: Observable<String>, action: Action): Observable<String> =
        state.doOnNext { Log.d("TAG", action.toString()) }
}
  1. Create store
val store = Store(
    defaultState, 
    listOf(Observable.just<Action>(AddSymbolAction())), 
    listOf(AddSymbolReducer()), 
    listOf(LogActionMiddleware()))
  1. Call .bind method with scheduler
store
    .bind(AndroidSchedulers.mainThread())
    .subscribe({ /* render state */ }, { /* handle error */ })

About

Redux for Android in Kotlin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published