Skip to content

Commit

Permalink
use AutoValue class as @CombinedState in example project
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarikx committed Oct 11, 2016
1 parent 5bc107a commit 96f7aca
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
10 changes: 8 additions & 2 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ dependencies {
compile project(':lib')
compile project(':rx-store')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'org.pcollections:pcollections:2.1.2'

compile 'com.facebook.stetho:stetho:1.3.1'
compile 'com.facebook.stetho:stetho-js-rhino:1.3.1'
compile 'com.google.code.gson:gson:2.7'


provided 'com.google.auto.value:auto-value:1.3'
apt 'com.google.auto.value:auto-value:1.3'
provided 'com.ryanharter.auto.value:auto-value-gson:0.4.2'
apt 'com.ryanharter.auto.value:auto-value-gson:0.4.2'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yheriatovych.reductor.example;

import com.google.gson.TypeAdapterFactory;
import com.ryanharter.auto.value.gson.GsonTypeAdapterFactory;

@GsonTypeAdapterFactory
public abstract class MyAdapterFactory implements TypeAdapterFactory {
public static TypeAdapterFactory create() {
return new AutoValueGson_MyAdapterFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import android.app.Application;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;
import com.facebook.stetho.Stetho;
import com.facebook.stetho.inspector.console.RuntimeReplFactory;
import com.facebook.stetho.rhino.JsRuntimeReplFactoryBuilder;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yheriatovych.reductor.Store;
import com.yheriatovych.reductor.example.model.AppState;
import com.yheriatovych.reductor.example.model.AppStateImpl;
import com.yheriatovych.reductor.example.model.AppStateReducer;
import com.yheriatovych.reductor.example.reducers.NotesFilterReducerImpl;
import com.yheriatovych.reductor.example.reducers.NotesListReducer;
Expand All @@ -23,7 +24,9 @@
public class ReductorApp extends Application {

public Store<AppState> store;
Gson gson = new Gson();
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(MyAdapterFactory.create())
.create();

@Override
public void onCreate() {
Expand Down Expand Up @@ -64,7 +67,7 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
Function stringifyFunction = (Function) json.get("stringify", scope);
String jsonString = (String) stringifyFunction.call(cx, json, scope, new Object[]{args[0]});

final AppState arg = gson.fromJson(jsonString, AppStateImpl.class);
final AppState arg = gson.fromJson(jsonString, AppState.class);
Log.d("ReductorApp", arg.toString());
handler.post(() -> store.dispatch(SetStateReducer.setStateAction(arg)));
return arg;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.yheriatovych.reductor.example.model;

import com.google.auto.value.AutoValue;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.yheriatovych.reductor.annotations.CombinedState;

import java.util.List;

@CombinedState
public interface AppState {
List<Note> notes();
@AutoValue
public abstract class AppState {
public abstract List<Note> notes();

NotesFilter filter();
public abstract NotesFilter filter();

public static TypeAdapter<AppState> typeAdapter(Gson gson) {
return new AutoValue_AppState.GsonTypeAdapter(gson);
}
}

0 comments on commit 96f7aca

Please sign in to comment.