- Notifications
You must be signed in to change notification settings - Fork12
Redux architecture for Android in good old java
License
trikita/jedux
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
In the best traditions ofRedux, Jedux is a very small and very flexible implementation.
Best used withAnvil, which is React-likelibrary for Android.
repositories {maven { url="https://jitpack.io" }}dependencies {compile"com.github.trikita:jedux:+"}
- [Talalarmo] alarm clock -https://github.com/trikita/talalarmo
- [Slide] text presentation maker -https://github.com/trikita/slide
// Define state typeclassState {publicfinalintcount;publicState(intcount) {this.count =count;}}// Define action typesenumCounterAction {INCREMENT,PLUS,}// Create store providing reducer and initial stateprivateStore<Action<CounterAction, ?>,State>store =newStore(this::reduce,newState(0));// Reducer: transforms old state into a new one depending on the action type and valuepublicStatereduce(Action<CounterAction, ?>action,Stateold) {switch (action.type) {caseINCREMENT:returnnewState(old.count +1);casePLUS:returnnewState(old.count + (Integer)action.value);}returnold;}// Using Anvil you can bind store variables to some view propertiestextView(() -> {text("Count: " +store.getState().count);});// Submit an action (e.g. from your event listeners)button(() -> {onClick(v ->store.dispatch(newAction<>(CounterAction.INCREMENT)));});button(() -> {onClick(v ->store.dispatch(newAction<>(CounterAction.PLUS,10)));});
trikita.jedux.Store:
new Store(reducer, initialState, middlewares...)- create new storestore.dispatch(action)- sends action message to the store, returns new statestore.getState()- returns current state
trkita.jedux.Action (you are free to use your own action types!):
new Action<>(type, value)- creates an action with given type (enum) and value (any kind of object).
trikita.jedix.Store.Middleware - implenent this interface to add custommiddleware (e.g. to handle actions with side effects or talk with yourcontrollers). Here's an example of the builtin Logger middleware, that logsevery incoming action and dumps state after the action is dispatched:
publicclassLogger<A,S>implementsStore.Middleware<A,S> {privatefinalStringtag;publicLogger(Stringtag) {this.tag =tag; }publicvoiddispatch(Store<A,S>store,Aaction,Store.NextDispatcher<A>next) {Log.d(tag,"--> " +action.toString());next.dispatch(action);Log.d(tag,"<-- " +store.getState().toString()); }}
To keep the state immutable consider usingImmutables orDexx.
Code is distributed under MIT license, feel free to use it in your proprietaryprojects as well.
About
Redux architecture for Android in good old java
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.