You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Migrate lib to coroutines and remove RX dependencies
This PR aims to update the master branch of the repo with the changes made in the masmovil forked repo.
It contains major changes related to a library migration to Kotlin coroutines instead of RX. Copyright information in all necessary classes has also updated.
Co-authored-by: yamidragut <[email protected]>
Co-authored-by: Adrián García <[email protected]>
Mini is a minimal Flux architecture written in Kotlin that also adds a mix of useful features to build UIs fast.
5
7
6
8
## Purpose
@@ -9,24 +11,24 @@ Feature development using Mini is fast compared to traditional architectures (li
9
11
10
12
## How to Use
11
13
### Dispatcher
12
-
The *Dispatcher* is the hub that manages all data flow in a Flux application. It is basically a holder of store callbacks: each store registers itself and provides a callback for an action.
14
+
The *Dispatcher* is the hub that manages all data flow in a Flux application. It is basically a holder of store callbacks: each store registers itself and provides a callback for an action.
13
15
14
16
One important thing is that the dispatching is always performed in the same thread to avoid possible side-effects.
15
17
16
18
We can dispatch actions in the following ways:
17
19
18
20
```kotlin
19
21
// Dispatch an action on the main thread synchronously
The *Stores* are holders for application state and state mutation logic. In order to do so they expose pure reducer functions that are later invoked by the dispatcher.
29
+
The *Stores* are holders for application state and state mutation logic. In order to do so they expose pure reducer functions that are later invoked by the dispatcher. A *Store* is a type of a *StateContainer*, which is exactly that: a container of states.
28
30
29
-
The state is a plain object (usually a `data class`) that holds all information needed to display the view. States should always be inmutable. State classes should avoid using framework elements (View, Camera, Cursor...) in order to facilitate testing.
31
+
The state is a plain object (usually a `data class`) that holds all information needed to display the view. States should always be immutable. State classes should avoid using framework elements (View, Camera, Cursor...) in order to facilitate testing.
30
32
31
33
Stores subscribe to actions to change the application state after a dispatch. Mini generates the code that links dispatcher actions and stores using the `@Reducer` annotation over a **non-private function that receives an Action as parameter**.
32
34
@@ -52,43 +54,38 @@ An *Action* is a simple class that usually represents a use case. It can also co
52
54
53
55
For example, we may want to log in to a service. We would create an action like this one:
54
56
```kotlin
57
+
@Action
55
58
data classLoginAction(valusername:String, valpassword:String)
56
59
```
57
60
58
61
When we receive the response from the server, we'll dispatch another action with the result:
59
62
```kotlin
63
+
@Action
60
64
data classLoginCompleteAction(valloginTask:Task, valuser:User?)
61
65
```
62
66
63
67
Actions will usually be triggered from Views or Controllers.
64
68
65
69
### View changes
66
-
Each ``Store`` exposes a custom `StoreCallback` though the method `observe` or a `Flowable` if you want to make use of RxJava. Both of them emits changes produced on their states, allowing the view to listen reactive the state changes. Being able to update the UI according to the new `Store` state.
70
+
Each ``StateContainer`` exposes a Kotlin `Flow` that emits changes produced on the state, allowing the view to listen reactive those changes. Being able to update the UI according to the new `StateContainer` state.
67
71
68
-
```kotlin
69
-
//Using RxJava
70
-
userStore
71
-
.flowable()
72
-
.map { it.name }
73
-
.subscribe { updateUserName(it) }
74
-
75
-
// Custom callback
76
-
userStore
77
-
.observe { state -> updateUserName(state.name) }
72
+
```kotlin
73
+
mainStore.flow()
74
+
.onEach { state ->
75
+
// Do whatever you want
76
+
}.launchInLifecycleScope()
78
77
```
79
78
80
-
If you make use of the RxJava methods, you can make use of the `SubscriptionTracker` interface to keep track of the `Disposables` used on your activities and fragments.
81
-
82
79
### Tasks
83
-
A `Task` is a basic object to represent an ongoing process. They should be used in the state of our `Store` to represent ongoing processes that must be represented in the UI.
80
+
A `Task` is a basic object to represent an ongoing process. They should be used in the state of our `StateContainer` (a `Store`, for example) to represent ongoing processes that must be represented in the UI.
84
81
85
82
You can also use `TypedTask` to save metadata related the current task.
86
83
87
84
**IMPORTANT: Do not use TypedTask to hold values that must survive multiple task executions. Save them as a variable in the state instead.**
88
85
89
86
90
87
### Example
91
-
Given the example Stores and Actions explained before, the workflow will be:
88
+
Given the example Stores and Actions explained before, the workflow would be:
92
89
93
90
- View dispatches `LoginAction`.
94
91
- Store changes his `LoginTask` status to running and call though his SessionController which will do all the async work to log in the given user.
@@ -97,12 +94,18 @@ Given the example Stores and Actions explained before, the workflow will be:
97
94
- The Store changes his state to the given values from `LoginCompleteAction`.
98
95
- The View redirect to the HomeActivity if the task was success or shows an error if not.
99
96
100
-
## Rx Utils
101
-
Mini includes some utility extensions over RxJava 2.0 to make easier listen state changes over the `Stores`.
97
+
You can execute another sample in the `app` package. It contains two different samples executing two types of `StateContainer`s:
98
+
-`StoreSampleActivity` class uses a `Store` as a `StateContainer`.
99
+
-`ViewModelSampleActivity` class uses a `ViewModel` as a `StateContainer`.
102
100
103
-
-`mapNotNull`: Will emit only not null values over the given `map` clause.
104
-
-`select`: Like `mapNotNull` but avoiding repeated values.
105
-
-`onNextTerminalState`: Used to map a `Task` inside an state and listen the next terminal state(Success - Error). Executing a different closure depending of the result of the task.
101
+
## Kotlin Flow Utils
102
+
Mini includes some utility extensions over Kotlin `Flow` to make easier listen state changes over the `StateContainer`s.
103
+
104
+
-`select`: Will emit only not null values over the given `map` clause.
105
+
-`selectNotNull`: Like `select` but avoiding null values.
106
+
-`onEachChange`: Emits a value when the values goes from one value to another.
107
+
-`onEachDisable`: Emits when the value goes from true to false.
108
+
-`onEachEnable`: Emits when the value goes from false to true.
106
109
107
110
## Navigation
108
111
To avoid loops over when working with navigation based on a process result. You will need to make use of `onNextTerminalState` after dispatch and `Action` that starts a process which result could navigate to a different screen.
@@ -120,11 +123,11 @@ For example:
120
123
If we continually listen the changes of a `Task` and we navigate to a specific screen when the `Task` becomes successful. The state will stay on SUCCESS and if we navigate back to the last screen we will be redirected again.
121
124
122
125
## Logging
123
-
Mini includes a custom `LoggerInterceptor` to log any change in your `Store` states produced from an `Action`. This will allow you to keep track of your actions, changes and side-effects more easily.
124
-
To add the LoggerInterceptor to your application you just need to add a single instance of it to your `Dispatcher` after initialize it in your `Application` class or dependency injection code.
126
+
Mini includes a custom `LoggerMiddleware` to log any change in your `StateContainer` states produced from an `Action`. This will allow you to keep track of your actions, changes and side-effects more easily.
127
+
To add the LoggerMiddleware to your application you just need to add a single instance of it to your `Dispatcher` after initialize it in your `Application` class or dependency injection code.
0 commit comments