Giter VIP home page Giter VIP logo

jedux's Introduction

Jedux - Redux architecture for Android

In the best traditions of Redux, Jedux is a very small and very flexible implementation.

Best used with Anvil, which is React-like library for Android.

Installation

repositories {
	maven { url = "https://jitpack.io" }
}

dependencies {
	compile "com.github.trikita:jedux:+"
}

Apps built with Jedux

Example

// Define state type
class State {
		public final int count;
		public State(int count) {
				this.count = count;
		}
}

// Define action types
enum CounterAction {
		INCREMENT,
		PLUS,
}

// Create store providing reducer and initial state
private Store<Action<CounterAction, ?>, State> store = new Store(this::reduce, new State(0));

// Reducer: transforms old state into a new one depending on the action type and value
public State reduce(Action<CounterAction, ?> action, State old) {
		switch (action.type) {
				case INCREMENT:
						return new State(old.count + 1);
				case PLUS:
						return new State(old.count + (Integer) action.value);
		}
		return old;
}

// Using Anvil you can bind store variables to some view properties
textView(() -> {
	text("Count: " + store.getState().count);
});

// Submit an action (e.g. from your event listeners)
button(() -> {
	onClick(v -> store.dispatch(new Action<>(CounterAction.INCREMENT)));
});
button(() -> {
	onClick(v -> store.dispatch(new Action<>(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 custom middleware (e.g. to handle actions with side effects or talk with your controllers). Here's an example of the builtin Logger middleware, that logs every incoming action and dumps state after the action is dispatched:

public class Logger<A, S> implements Store.Middleware<A, S> {
    private final String tag;
    public Logger(String tag) {
        this.tag = tag;
    }
    public void dispatch(Store<A, S> store, A action, Store.NextDispatcher<A> next) {
        Log.d(tag, "--> " + action.toString());
        next.dispatch(action);
        Log.d(tag, "<-- " + store.getState().toString());
    }
}

To keep the state immutable consider using Immutables or Dexx.

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.