Coder Social home page Coder Social logo

htdangkhoa / kdux Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 1.0 192 KB

:fire::fire::fire: Android + Kotlin + Redux = :heart: https://www.kotlinresources.com/library/kdux/

Home Page: https://www.kotlinresources.com/library/kdux/

License: MIT License

Kotlin 100.00%
android android-library android-architecture kotlin kotlin-android kotlin-library kotlin-language kotlin-coroutines redux redux-thunk

kdux's Introduction

Kdux

Android + Kotlin + Redux = ❤️

platform language downloads

Installation

Add the JitPack repository to your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency:

dependencies {
    implementation 'com.github.htdangkhoa:kdux:<latest_version>'
}

Usage

  • State:

    data class CounterState(val number: Int = 0): State
  • Action:

    sealed class CounterAction: Action {
        object INCREASE: CounterAction()
    
        object DECREASE: CounterAction()
    
        companion object {
            fun increaseAction(dispatch: Dispatch) = dispatch(INCREASE)
    
            fun decreaseAction(dispatch: Dispatch) = dispatch(DECREASE)
        }
    }
  • Reducer:

    class CounterReducer: Reducer<CounterState> {
        override fun reduce(state: CounterState, action: Action): CounterState {
            return when (action) {
                CounterAction.INCREASE -> state.copy(number = state.number + 1)
    
                CounterAction.DECREASE -> state.copy(number = state.number - 1)
    
                else -> state
            }
        }
    }
  • Activity:

    class CounterActivity: AppCompatActivity(), Enhancer<CounterState> {
        private val store: Store<CounterState> by lazy {
            Store(
                CounterReducer(),
                CounterState()
                // applyMiddleware(...)
            )
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_counter)
    
            btnIncrease.setOnClickListener {
                CounterAction.increaseAction { store.dispatch(it) }
            }
    
            btnDecrease.setOnClickListener {
                CounterAction.decreaseAction { store.dispatch(it) }
            }
        }
    
        override fun onStart() {
            super.onStart()
    
            store.subscribe(this)
        }
    
        override fun onStop() {
            super.onStop()
    
            store.unsubscribe(this)
        }
    
        override fun enhance(state: CounterState) {
            txtNumber.text = "${state.number}"
        }
    }
  • Middleware: (List middlewares)

    class DemoMiddleware<S: State>: Middleware<S> {
        override fun onBeforeDispatch(store: Store<S>, action: Action) {
            // Doing somthings...
        }
    
        override fun onDispatch(store: Store<S>, action: Action, next: Dispatcher) {
            // Doing somthings...
    
            next.dispatch(action)
        }
    
        override fun onAfterDispatch(store: Store<S>, action: Action) {
            // Doing somthings...
        }
    }

Examples

Bonus

  • Thunk:

    // Action
    sealed class CounterAction: Action {
        object INCREASE: CounterAction()
    
        object DECREASE: CounterAction()
    
        companion object {
            fun increaseAction(): Action = KduxThunkAction(object: Thunk {
                override fun invoke(dispatcher: Dispatcher, state: State) {
                    dispatcher.dispatch(INCREASE)
                }
            })
    
            fun decreaseAction(): Action = KduxThunkAction(object: Thunk {
                override fun invoke(dispatcher: Dispatcher, state: State) {
                    dispatcher.dispatch(DECREASE)
                }
            })
        }
    }
    
    // Store
    private val store = Store(
        ...,
        applyMiddleware(KduxThunk())
    )
    
    store.dispatch(increaseAction())
    
    store.dispatch(decreaseAction())
  • Logger:

    private val store: Store<CounterState> by lazy {
        Store(
            ...,
            applyMiddleware(KduxLogger())
        )
    }
  • Debounce:

    private val store: Store<CounterState> by lazy {
        Store(
            ...,
            applyMiddleware(KduxDebounce(debounce = 500)) // Range of debounce from 0 to 30000.
        )
    }
  • DevTools:

    private val store: Store<CounterState> by lazy {
        composeWithDevTools(
            Store(
                ...,
                // applyMiddleware(...)
            )
        )
    }
    
    // UNDO
    store.dispatch(KduxDevToolAction.UNDO)
    
    // REDO
    store.dispatch(KduxDevToolAction.REDO)
    
    // RESET
    store.dispatch(KduxDevToolAction.RESET)

License

MIT License

Copyright (c) 2019 Huỳnh Trần Đăng Khoa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

kdux's People

Contributors

htdangkhoa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

teddyscript

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.