- Notifications
You must be signed in to change notification settings - Fork35
Unidirectional Data Flow in Kotlin - Port ofhttps://github.com/ReSwift/ReSwift to Kotlin
License
MIT, MIT licenses found
Licenses found
ReKotlin/ReKotlin
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Port ofReSwift to Kotlin, which corresponds toReSwift/4.0.0
ReKotlin is aRedux-like implementation of the unidirectional data flow architecture in Kotlin. ReKotlin helps you to separate three important concerns of your app's components:
- State: in a ReKotlin app the entire app state is explicitly stored in a data structure. This helps avoid complicated state management code, enables better debugging and has many, many more benefits...
- Views: in a ReKotlin app your views update when your state changes. Your views become simple visualizations of the current app state.
- State Changes: in a ReKotlin app you can only perform state changes through actions. Actions are small pieces of data that describe a state change. By drastically limiting the way state can be mutated, your app becomes easier to understand and it gets easier to work with many collaborators.
The ReKotlin library is tiny - allowing users to dive into the code, understand every single line and hopefully contribute.
ReKotlin relies on a few principles:
- The Store stores your entire app state in the form of a single data structure. This state can only be modified by dispatching Actions to the store. Whenever the state in the store changes, the store will notify all observers.
- Actions are a declarative way of describing a state change. Actions don't contain any code, they are consumed by the store and forwarded to reducers. Reducers will handle the actions by implementing a different state change for each action.
- Reducers provide pure functions, that based on the current action and the current app state, create a new app state
For a very simple app, that maintains a counter that can be increased and decreased, you can define the app state as following:
data classAppState (valcounter:Int =0): StateType
You would also define two actions, one for increasing and one for decreasing the counter. For the simple actions in this example we can define empty data classes that conform to action:
data classCounterActionIncrease(valunit:Unit =Unit): Actiondata classCounterActionDecrease(valunit:Unit =Unit): Action
Your reducer needs to respond to these different action types, that can be done by switching over the type of action:
funcounterReducer(action:Action,state:AppState?):AppState {// if no state has been provided, create the default statevar state= state?:AppState()when(action){isCounterActionIncrease-> { state= state.copy(counter= state.counter+1) }isCounterActionDecrease-> { state= state.copy(counter= state.counter-1) } }return state}
In order to have a predictable app state, it is important that the reducer is always free of side effects, it receives the current app state and an action and returns the new app state.
To maintain our state and delegate the actions to the reducers, we need a store. Let's call itmainStore and define it as a global constant, for example in the Main Activity file:
val mainStore=Store( reducer= ::counterReducer, state=null)classMainActivity :AppCompatActivity(){//...}
Lastly, your view layer, in this case an activity,needs to tie into this system by subscribing to store updates andemitting actions whenever the app state needs to be changed(assuming thatsnake_case View properties are coming fromKotlin Android Extensions):
classMainActivity :AppCompatActivity(), StoreSubscriber<AppState> {overridefunonCreate(savedInstanceState:Bundle?) {super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)// when either button is tapped, an action is dispatched to the store// in order to update the application state button_up.setOnClickListener { mainStore.dispatch(CounterActionIncrease()) } button_down.setOnClickListener { mainStore.dispatch(CounterActionDecrease()) }// subscribe to state changes mainStore.subscribe(this) }overridefunnewState(state:AppState) {// when the state changes, the UI is updated to reflect the current state counter_label.text="${state.counter}" }}
ThenewState method will be called by theStore whenever a new app state is available, this is where we need to adjust our view to reflect the latest app state.
When working with multiple states in a single class, BlockSubscriber can be used for listening to states in it's specific closure instead of using StoreSubscriber<>
classMainActivity :AppCompatActivity() {privateval counterLabel:TextView by lazy {this.findViewById(R.id.counter_label)asTextView }privateval buttonUp:Button by lazy {this.findViewById(R.id.button)asButton }privateval buttonDown:Button by lazy {this.findViewById(R.id.button2)asButton }overridefunonCreate(savedInstanceState:Bundle?) {super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)val appStateSubscriber=BlockSubscriber<AppState> { appState->this.counterLabel.text="${appState.counter}" }// when either button is tapped, an action is dispatched to the store// in order to update the application statethis.buttonUp.setOnClickListener { mainStore.dispatch(CounterActionIncrease()) }this.buttonDown.setOnClickListener { mainStore.dispatch(CounterActionDecrease()) }// subscribe to state changes mainStore.subscribe(appStateSubscriber) }}
Button taps result in dispatched actions that will be handled by the store and its reducers, resulting in a new app state.
This is a very basic example that only shows a subset of ReKotlin's features, read the Getting Started Guide(not ported yet) to see how you can build entire apps with this architecture.
You can also watch this talk on the motivation behind the original ReSwift.
- ReduxMovieExample - An application which uses an API to display movies and stores favorites into a local database.
- rekotlin-router-github-example - An application which displays user's github repositories along with authentication and navigation usingrekotlin-router
- ReKotlin-CounterExample - A simple counter application
Model-View-Controller (MVC) is not a holistic application architecture. Typical apps defer a lot of complexity to controllers since MVC doesn't offer other solutions for state management, one of the most complex issues in app development.
Apps built upon MVC often end up with a lot of complexity around state management and propagation. We need to use callbacks, delegations, Key-Value-Observation and notifications to pass information around in our apps and to ensure that all the relevant views have the latest state.
This approach involves a lot of manual steps and is thus error prone and doesn't scale well in complex code bases.
It also leads to code that is difficult to understand at a glance, since dependencies can be hidden deep inside of view controllers. Lastly, you mostly end up with inconsistent code, where each developer uses the state propagation procedure they personally prefer. You can circumvent this issue by style guides and code reviews but you cannot automatically verify the adherence to these guidelines.
ReKotlin attempts to solve these problems by placing strong constraints on the way applications can be written. This reduces the room for programmer error and leads to applications that can be easily understood - by inspecting the application state data structure, the actions and the reducers.
This architecture provides further benefits beyond improving your code base:
- Stores, Reducers, Actions and extensions such asReKotlin Router are entirely platform independent - you can easily use the same business logic and share it between apps for multiple platforms
- Want to collaborate with a co-worker on fixing an app crash? Use(port not yet available)ReSwift Recorder to record the actions that lead up to the crash and send them the JSON file so that they can replay the actions and reproduce the issue right away.
- Maybe recorded actions can be used to build UI and integration tests?
The ReKotlin tooling is still in a very early stage, but aforementioned prospects excite us and hopefully others in the community as well!
The Getting Started Guide has not yet been ported. In the meantime, please refer to original ReSwift's:Getting Started Guide that describes the core components of apps built with ReSwift.
To get an understanding of the core principles we recommend reading the brilliantredux documentation.
dependencies { implementation'org.rekotlin:rekotlin:1.0.4'}
In ReSwift when you dereference the subscriber or it goes out of the scope, you won't receive new state updates.
varsubscriber:TestSubscriber?=TestSubscriber()store.subscribe(subscriber!)subscriber=nil
However in ReKotlin you need make sure you have unsubscribed explicitly.
val subscriber=TestSubscriber()store.subscribe(subscriber)store.unsubscribe(subscriber)
When subscribing without substate selection likestore.subscribe(someSubscriber) in swift you need to have your state implementing Equatable in order to skipRepeats being applied automatically.
publicstructState:StateType{publicletmapState:MapStatepublicletappState:AppState}extensionState:Equatable{publicstaticfunc==(lhs:State, rhs:State)->Bool{ //...}}
However in Kotlin(JVM) every object implementsequals(), so that skipRepeats will be applied automatically when youstore.subscribe(someSubscriber), with KotlinStructural Equality check used.
Please note, if you implement your states/substates withdata classes, Kotlin compiler will automatically derive non-shallowequals() from all properties declared in the primary constructor.
If you want to opt-out of this behaviour please setautomaticallySkipRepeats tofalse in your store declaration:
val store=Store(reducer::handleAction, state, automaticallySkipRepeats=false)
Under the hood ReKotlin uses aCopyOnWriteArrayList to manage subscriptions (seePR 29 for more details). This implementation detail means that the number of concurrent writes to the subscriptions should be less than the number of concurrent reads.
In terms of using the library this means that un/subscribing may incur a performance overhead if done duringnewState in store subscribers. We recommend to restrict this usage (concurrent write while reading subscriptions) as much as possible, i.e. avoidsubscribe orunsubscribe in calls tonewState.
Please format your code usingkotlinFormatter.xml file fromhere and then running./gradlew spotlessApply
Using this code formatter will help us maintain consistency in code style.
- Many thanks toBenjamin Encz and other ReSwift contributors for building originalReSwift that we really enjoyed working with.
- Also huge thanks toDan Abramov for buildingRedux - all ideas in here and many implementation details were provided by his library.
This was an attempt to bring redux architecture parity between iOS and Android
About
Unidirectional Data Flow in Kotlin - Port ofhttps://github.com/ReSwift/ReSwift to Kotlin
Resources
License
MIT, MIT licenses found
Licenses found
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- Kotlin99.8%
- Shell0.2%
