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.

jedux's People

Contributors

orzechowskikamil avatar zserge avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jedux's Issues

Working with activities

On your blog, you mention that the middleware might be instantiated with the context. This would be the application context then since the store creation takes place in the application class, right? What if I need the current activity's context, e.g. for launching a new activity? In some examples I've seen that this is simply done in the view->onClick for example and not in the middleware. But what if I want to launch an activity based on some other action that was dispatched or I simply want to separate concerns and only have the view dispatch CLICKED and let a middleware determine what happens (redux-saga style)?
I wouldn't want to hand around my activity context to avoid memory leaks and such or do I have to? Or is there a way to dynamically add/remove middlewares onCreate/onDestroy of an activity?

(posted this on your blog, but maybe here is a better place for it)

Jitpack issues

Gradle could not resolve the library from jitpack by using "com.github.trikita:jedux:+" (as is written in the documentation)

So I visited https://jitpack.io/#trikita/jedux and could not use any of the Releases artifacts.
Only once switching to Commits and using 'com.github.trikita:jedux:-SNAPSHOT' was the library successfully resolved by Gradle.

As I do not like pointing to a SNAPSHOT I changed it to point to the last commit 'com.github.trikita:jedux:80389e26dc'

Also consider not using + for the dependency version as it's not a very good practice (and Android Studio will display a warning every time such a dependency is added to the build file)

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.