All Projects → sergejsha → knot

sergejsha / knot

Licence: Apache-2.0 license
Unidirectional reactive state container for Android & Kotlin

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to knot

Keemun
No description or website provided.
Stars: ✭ 13 (-94.37%)
Mutual labels:  android-architecture, unidirectional-data-flow, mvi-architecture
WeatherApp MVI sample
🌸[Functional reactive programming (FRP)] 🍁Simple Android weather forecast application written in Kotlin, using RxKotlin, Retrofit2, Mosby, Room Persistence ❄️MVI Pattern with Mosby Library
Stars: ✭ 106 (-54.11%)
Mutual labels:  mvi-architecture, mvi-android
UI-Communication-with-MVI
Effective UI Communication with MVI architecture, a BaseActivity and the Repository Pattern.
Stars: ✭ 17 (-92.64%)
Mutual labels:  mvi-architecture, mvi-android
Pagination-MVI-Flow
Pagination MVI Coroutines Flow. Play MVI with Kotlin Coroutines Flow | MVI pattern on Android using Kotlin Coroutines Flow | Dagger Hilt DI | SharedFlow | StateFlow
Stars: ✭ 45 (-80.52%)
Mutual labels:  mvi-architecture, mvi-android
weather-app-2020-android
Android Weather App 2020
Stars: ✭ 15 (-93.51%)
Mutual labels:  mvi-architecture, mvi-android
fluxy
Fluxy is a Flux architecture implementation written in Kotlin.
Stars: ✭ 25 (-89.18%)
Mutual labels:  android-architecture, mvi-android
Knot
Unidirectional reactive state container for Android & Kotlin
Stars: ✭ 198 (-14.29%)
Mutual labels:  android-architecture, unidirectional-data-flow
MVI-Clean-Architecture
MVI + Clean Architecture + Best Practices | Example of Clean Architecture of Android app using MVI design pattern with Jetpack and popular libraries
Stars: ✭ 50 (-78.35%)
Mutual labels:  mvi-architecture, mvi-android
Roxie
Lightweight Android library for building reactive apps.
Stars: ✭ 441 (+90.91%)
Mutual labels:  android-architecture, unidirectional-data-flow
Unidirectional Architecture On Mobile
Dive into 📱 Unidirectional Architecture!
Stars: ✭ 115 (-50.22%)
Mutual labels:  android-architecture, unidirectional-data-flow
Clean Architecture Android
Sample to practice Clean Architecture in android applications.
Stars: ✭ 207 (-10.39%)
Mutual labels:  android-architecture
Componentcornerstone
🐜🐜 一种全新的android组件化方案,无需下沉公用代码,无需采用路由等硬编码方式分发sdk,独立维护组件暴露的SDK/IMPL,打破官方模块循环且支持组件间互相调用SDK,支持集成组件调试/组件独立运行。A brand new android componentization solution, no need to sink public code, no need to use hard coding to distribute sdk, independent maintenance component exposed SDK / IMPL, break the official module loop and support the call between components The SDK supports integrated component debugging/components to run independently.
Stars: ✭ 207 (-10.39%)
Mutual labels:  android-architecture
Android Mvvm Architecture
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
Stars: ✭ 2,720 (+1077.49%)
Mutual labels:  android-architecture
Androidstandarddevelop
🌟 Best practices in Android develop(final).
Stars: ✭ 2,798 (+1111.26%)
Mutual labels:  android-architecture
Eiffel
Redux-inspired Android architecture library leveraging Architecture Components and Kotlin Coroutines
Stars: ✭ 203 (-12.12%)
Mutual labels:  android-architecture
Moxy
Moxy is MVP library for Android with incremental annotation processor and ktx features
Stars: ✭ 234 (+1.3%)
Mutual labels:  android-architecture
Android Mvp Architecture
🏛 A basic sample android application to understand MVP in a very simple way. Just clone, build, run and understand MVP.
Stars: ✭ 203 (-12.12%)
Mutual labels:  android-architecture
Component
🔥🔥🔥A powerful componentized framework.一个强大、100% 兼容、支持 AndroidX、支持 Kotlin并且灵活的组件化框架
Stars: ✭ 2,434 (+953.68%)
Mutual labels:  android-architecture
Android Cleanarchitecture
This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.
Stars: ✭ 15,062 (+6420.35%)
Mutual labels:  android-architecture
Awesome Android Complete Reference
Awesome Android references for everything like best practices, performance optimization, etc.
Stars: ✭ 2,701 (+1069.26%)
Mutual labels:  android-architecture

Build Status codecov License

🧶 Knot

Concise reactive state container library for Android applications.

Concept

Knot helps managing application state by reacting on events and performing asynchronous actions in a structured way. There are five core concepts Knot defines: State, Change, Action, Reducer and Effect.

State represents an immutable state of an application. It can be a state of a screen or a state of an internal statefull headless component.

Change is an immutable data object with an optional payload intended for changing the State. A Change can be produced from an external source or be a result of execution of an Action.

Action is a synchronous or an asynchronous operation which, when completed, can – but doesn't have to – emit a new Change.

Reducer is a function that takes the previous State and a Change as arguments and returns the new State and an optional Action wrapped by the Effect class. Reducer in Knot is designed to stay side-effects free because each side-effect can be turned into an Action and returned from the reducer function together with a new state in a pure way.

Effect is a convenient wrapper class containing the new State and an optional Action. If Action is present, Knot will perform it and provide resulting Change (if any) back to the Reducer.

In addition to that each Knot can subscribe to Events coming from external sources and turn them into Changes for further processing.

Getting Started

The example below declares a Knot capable of loading data, handling Success and Failure loading results and reloading data automatically when an external "data changed" signal gets received. It also logs all State mutations as well as all processed Changes and Actions in console.

sealed class State {
   object Initial : State()
   object Loading : State()
   data class Content(val data: String) : State()
   data class Failed(val error: Throwable) : State()
}

sealed class Change {
   object Load : Change() {
      data class Success(val data: String) : Change()
      data class Failure(val error: Throwable) : Change()
   }
}

sealed class Action {
   object Load : Action()
}

val knot = knot<State, Change, Action> {
    state { 
        initial = State.Initial 
    }
    changes {
        reduce { change ->
            when (change) {
                is Change.Load -> State.Loading + Action.Load
                is Change.Load.Success -> State.Content(data).only
                is Change.Load.Failure -> State.Failed(error).only
            }
        }
    }
    actions {
        perform<Action.Load> {
            switchMapSingle<String> { 
                loadData()
                    .map<Change> { Change.Load.Success(it) }
                    .onErrorReturn { Change.Load.Failure(it) }
            }
        }
    }
    events {
        source {
            dataChangeObserver.signal.map { Change.Load }
        }
    }
}

val states = knot.state.test()
knot.change.accept(Change.Load)

states.assertValues(
    State.Initial,
    State.Loading,
    State.Content("data")
)

Notice how inside the reduce function a new State can be combined with an Action using + operator. If only the State value should be returned from the reducer, the .only suffix is added to the State.

Composition

If your knot becomes complex and you want to improve its readability and maintainability, you may consider to write a composite knot. You start composition by grouping related functionality into, in a certain sense, indecomposable pieces called Delegates.

Each Delegate is isolated from the other Delegates. It defines its own set of Changes, Actions and Reducers. It's only the State, what is shared between the Delegates. In that respect each Delegate can be seen as a separate Knot working on a shared State. Once all Delegates are defined, they can be composed together and provided to CompositeKnot which implements standard Knot interface. For more information check out Composite ViewModel post.

Documentation

  1. Knot Sample App is the first place to look at.
  2. Async Actions to learn how to perform and cancel asynchronous actions.
  3. External Events to learn how to observe and handle external events.
  4. Terminal events in Actions section
  5. Composite ViewModel to learn more about composition.
  6. Troubleshooting

Other examples

Why Knot?

  • Predictable - state is the single source of truth.
  • Side-effect free reducer - by design.
  • Scalable - single knots can be combined together to build more complex application logic.
  • Composable - complex knots can be composed out of delegates grouped by related functionality.
  • Structured - easy to read and write DSL for writing better structured and less buggy code.
  • Concise - it has minimalistic API and compact implementation.
  • Testable - reducers and transformers are easy to test.
  • Production ready - Knot is used in production.
  • Why not?

RxJava3 Binaries Maven Central

allprojects {
    repositories {
        mavenCentral()
    }
}
dependencies {
    implementation "de.halfbit:knot3:<version>"
    
    // Because Knot is not released for each and every RxJava version, 
    // it is recommended you also explicitly depend on RxJava's latest 
    // version for bug fixes and new features.
    implementation 'io.reactivex.rxjava3:rxjava:3.0.4'    
}

RxJava2 Binaries Maven Central

allprojects {
    repositories {
        mavenCentral()
    }
}
dependencies {
    implementation "de.halfbit:knot:<version>"
    
    // Becase Knot is not released for each and every RxJava version, 
    // it is recommended you also explicitly depend on RxJava's latest 
    // version for bug fixes and new features.
    implementation 'io.reactivex.rxjava2:rxjava:2.2.19'
}

Inspiration

Knot was inspired by two awesome projects

License

Copyright 2019, 2020 Sergej Shafarenka, www.halfbit.de

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].