Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Redux architecture for Android in good old java

License

NotificationsYou must be signed in to change notification settings

trikita/jedux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

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.

Installation

repositories {maven { url="https://jitpack.io" }}dependencies {compile"com.github.trikita:jedux:+"}

Apps built with Jedux

Example

// 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)));});

API

trikita.jedux.Store:

  • new Store(reducer, initialState, middlewares...) - create new store
  • store.dispatch(action) - sends action message to the store, returns new state
  • store.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.

License

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp