项目作者: htdangkhoa

项目描述 :
:fire::fire::fire: Android + Kotlin + Redux = :heart: https://www.kotlinresources.com/library/kdux/
高级语言: Kotlin
项目地址: git://github.com/htdangkhoa/kdux.git
创建时间: 2019-11-10T17:36:50Z
项目社区:https://github.com/htdangkhoa/kdux

开源协议:MIT License

下载



Kdux

Android + Kotlin + Redux = :heart:



platform


language


downloads

Installation

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

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Add the dependency:

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

Usage

  • State:

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

    1. sealed class CounterAction: Action {
    2. object INCREASE: CounterAction()
    3. object DECREASE: CounterAction()
    4. companion object {
    5. fun increaseAction(dispatch: Dispatch) = dispatch(INCREASE)
    6. fun decreaseAction(dispatch: Dispatch) = dispatch(DECREASE)
    7. }
    8. }
  • Reducer:

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

    1. class CounterActivity: AppCompatActivity(), Enhancer<CounterState> {
    2. private val store: Store<CounterState> by lazy {
    3. Store(
    4. CounterReducer(),
    5. CounterState()
    6. // applyMiddleware(...)
    7. )
    8. }
    9. override fun onCreate(savedInstanceState: Bundle?) {
    10. super.onCreate(savedInstanceState)
    11. setContentView(R.layout.activity_counter)
    12. btnIncrease.setOnClickListener {
    13. CounterAction.increaseAction { store.dispatch(it) }
    14. }
    15. btnDecrease.setOnClickListener {
    16. CounterAction.decreaseAction { store.dispatch(it) }
    17. }
    18. }
    19. override fun onStart() {
    20. super.onStart()
    21. store.subscribe(this)
    22. }
    23. override fun onStop() {
    24. super.onStop()
    25. store.unsubscribe(this)
    26. }
    27. override fun enhance(state: CounterState) {
    28. txtNumber.text = "${state.number}"
    29. }
    30. }
  • Middleware: (List middlewares)

    1. class DemoMiddleware<S: State>: Middleware<S> {
    2. override fun onBeforeDispatch(store: Store<S>, action: Action) {
    3. // Doing somthings...
    4. }
    5. override fun onDispatch(store: Store<S>, action: Action, next: Dispatcher) {
    6. // Doing somthings...
    7. next.dispatch(action)
    8. }
    9. override fun onAfterDispatch(store: Store<S>, action: Action) {
    10. // Doing somthings...
    11. }
    12. }

Examples

Bonus

  • Thunk:

    1. // Action
    2. sealed class CounterAction: Action {
    3. object INCREASE: CounterAction()
    4. object DECREASE: CounterAction()
    5. companion object {
    6. fun increaseAction(): Action = KduxThunkAction(object: Thunk {
    7. override fun invoke(dispatcher: Dispatcher, state: State) {
    8. dispatcher.dispatch(INCREASE)
    9. }
    10. })
    11. fun decreaseAction(): Action = KduxThunkAction(object: Thunk {
    12. override fun invoke(dispatcher: Dispatcher, state: State) {
    13. dispatcher.dispatch(DECREASE)
    14. }
    15. })
    16. }
    17. }
    18. // Store
    19. private val store = Store(
    20. ...,
    21. applyMiddleware(KduxThunk())
    22. )
    23. store.dispatch(increaseAction())
    24. store.dispatch(decreaseAction())
  • Logger:

    1. private val store: Store<CounterState> by lazy {
    2. Store(
    3. ...,
    4. applyMiddleware(KduxLogger())
    5. )
    6. }
  • Debounce:

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

    1. private val store: Store<CounterState> by lazy {
    2. composeWithDevTools(
    3. Store(
    4. ...,
    5. // applyMiddleware(...)
    6. )
    7. )
    8. }
    9. // UNDO
    10. store.dispatch(KduxDevToolAction.UNDO)
    11. // REDO
    12. store.dispatch(KduxDevToolAction.REDO)
    13. // RESET
    14. store.dispatch(KduxDevToolAction.RESET)

License

  1. MIT License
  2. Copyright (c) 2019 Hunh Trn Đăng Khoa
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.