ricky9090 / Anduxx

A simple implementation of Redux for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Anduxx

A simple implementation of Redux for Android App

Demo
Screenshot_1 Screenshot_2 Screenshot_3

Store in Activity

  1. Create State, Action
public class CounterState implements StateObject {

    // data variable

    @Override
    public StateObject copy(StateObject old) {
        // copy old state
    }
}

// define actions somewhere
public static final String INCREASE = "increase";
public static final String DECREASE = "decrease";
  1. Implement Reducer
public class CounterReducer implements Reducer<CounterState> {

    @Override
    public CounterState reduce(CounterState currentState, Action action) {
        // handle action
    }
}
  1. Implement Activity, extends ActivityWithStore, implement StateChangeListener, add Listener
public class CounterDemoOneActivity extends ActivityWithStore<CounterState>
        implements StateChangeListener<CounterState> {

    Button increaseButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ...
        increaseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // dispatch action
                getStore().dispatch(Action.create(CounterAction.INCREASE, null));
            }
        });

        // add listener
        getStore().addListener(this);
    }

    @NonNull
    @Override
    public Storeable<CounterState> createStore() {
        // create store in activity
    }

    @Override
    public void onStateChange(CounterState state) {
        // update UI
    }
}

Store in Application

  1. Create State, Action
  2. Create StoreManager in Application
public class MyApplication extends Application {

    private StoreManager storeManager;

    @Override
    public void onCreate() {
        super.onCreate();

        storeManager = new StoreManager();
    }

    public StoreManager getStoreManager() {
        return storeManager;
    }

    public static StoreManager getAppStoreManager(Context context) {
        Application app = (Application) context.getApplicationContext();
        return ((MyApplication) app).getStoreManager();
    }
}
  1. Implement ActivityWithStoreContract, manually call methods, add store to StoreManager
public class MainActivity extends AppCompatActivity implements
        ActivityWithStoreContract<CounterState>,
        StateChangeListener<CounterState> {

    // unique ID for store
    public static final String GLOBAL_COUNTER_STORE_ID = "global_counter";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
        // create store
        createStore();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // destore store when necessary
        destroyStore();
    }

    @NonNull
    @Override
    public Storeable<CounterState> createStore() {
        // create store, add into application's storeManager
        MyApplication.getAppStoreManager(this).addStore(store, GLOBAL_COUNTER_STORE_ID);
        // ...
    }

    @Override
    public void destroyStore() {
        // destroy store, remove from application's storeManager
        if (MyApplication.getAppStoreManager(this).getStoreById(GLOBAL_COUNTER_STORE_ID) != null) {
            MyApplication.getAppStoreManager(this).getStoreById(GLOBAL_COUNTER_STORE_ID).onDestroy();
            MyApplication.getAppStoreManager(this).removeStore(GLOBAL_COUNTER_STORE_ID);
        }
    }

    @NonNull
    @Override
    public Storeable<CounterState> getStore() {
        // return store in application
        return MyApplication.getAppStoreManager(this).getStoreById(MainActivity.GLOBAL_COUNTER_STORE_ID);

    }

    @Override
    public void onStateChange(CounterState state) {
        // update UI
    }
}

About

A simple implementation of Redux for Android

License:Apache License 2.0


Languages

Language:Java 100.0%