Skip to content

Commit

Permalink
Merge pull request #52 from izumin5210/wakwak3125/rxjava2
Browse files Browse the repository at this point in the history
Update to RxJava2
  • Loading branch information
izumin5210 authored Jan 16, 2018
2 parents 758dba8 + 5b3a91e commit 8510117
Show file tree
Hide file tree
Showing 34 changed files with 379 additions and 205 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Droidux is influenced by [Three principles][three-principles] of Redux.
Features of Droidux are following:

* All mutations can be observed via rx.Observable from [RxJava][rxjava]
* All mutations can be observed via Flowable from [RxJava][rxjava]
* All mutations are automatically notified to views via [Data Binding][databinding]

### Data flow
Expand All @@ -37,8 +37,9 @@ Add to your project build.gradle file:
apply plugin: 'com.android.application'
dependencies {
compile 'io.reactivex:rxjava:1.1.0'
compile 'info.izumin.android:droidux:0.6.0'
compile 'io.reactivex.rxjava2:rxjava:2.1.8'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
annotationProcessor 'info.izumin.android:droidux-processor:0.6.0'
}
```
Expand Down Expand Up @@ -113,7 +114,7 @@ public class CounterReducer {
@Store(CounterReducer.class)
public interface CounterStore extends BaseStore {
Counter getCounter();
Observable<Counter> observeCounter();
Flowable<Counter> observeCounter();
}

/**
Expand All @@ -131,8 +132,8 @@ public class ClearCountAction implements Action {}
// and it should register a reducer instance and an initial state.
//
// Its APIs in this example are following:
// - rx.Observable<Action> dispatch(Action action)
// - rx.Observable<Counter> observeCounter()
// - Flowable<Action> dispatch(Action action)
// - Flowable<Counter> observeCounter()
// - Counter getCounter()
CounterStore store = DroiduxCounterStore.builder()
.setReducer(new CounterReducer(), new Counter(0))
Expand Down Expand Up @@ -195,9 +196,9 @@ Layout file is following:
@Store({CounterReducer.class, TodoListReducer.class})
class RootStore extends BaseStore {
Counter getCounter();
Observable<Counter> observeCounter();
Flowable<Counter> observeCounter();
TodoList getTodoList();
Observable<TodoList> observeTodoList();
Flowable<TodoList> observeTodoList();
}


Expand All @@ -216,16 +217,16 @@ store.dispatch(new AddTodoAction("new task")).subscribe(); // Counter: 1, Todo:
```java
class Logger extends Middleware<CounterStore> {
@Override
public Observable<Action> beforeDispatch(Action action) {
public Flowable<Action> beforeDispatch(Action action) {
Log.d("[prev counter]", String.valueOf(getStore().count()));
Log.d("[action]", action.getClass().getSimpleName());
return Observable.just(action);
return Flowable.just(action);
}

@Override
public Observable<Action> afterDispatch(Action action) {
public Flowable<Action> afterDispatch(Action action) {
Log.d("[next counter]", String.valueOf(getStore().count()));
return Observable.just(action);
return Flowable.just(action);
}
}

Expand Down Expand Up @@ -285,7 +286,7 @@ class TodoListReducer {
@Store(TodoListReducer.class)
public interface TodoListStore {
TodoList todoList();
Observable<TodoList> observeTodoList();
Flowable<TodoList> observeTodoList();
}

class AddTodoAction implements Action {
Expand Down Expand Up @@ -329,7 +330,7 @@ class FetchTodoListAction implements AsyncAction {
this.client = client;
}

public Observable<ReceiveTodoListAction> call(Dispatcher dispatcher) {
public Flowable<ReceiveTodoListAction> call(Dispatcher dispatcher) {
return dispatcher.dispatch(new DoingFetchAction())
.flatMap(_action -> client.fetch())
.map(todoList -> {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ ext {
databindingBaseLibraryVersion = '1.0'
databindingLibraryVersion = '1.0-rc3'
supportLibrariesVersion = '23.1.1'
rxJavaVersion = '1.1.0'
rxAndroidVersion = '1.0.1'
rxJava2Version = '2.1.8'
rxAndroid2Version = '2.0.1'
spockCoreVersion = '1.0-groovy-2.4'
cglibVersion = '2.2'
}
2 changes: 1 addition & 1 deletion droidux-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies {
compile 'com.google.auto:auto-common:0.6'
compile 'com.google.auto.service:auto-service:1.0-rc2'

compile "io.reactivex:rxjava:${project.rxJavaVersion}"
compile "io.reactivex.rxjava2:rxjava:${project.rxJava2Version}"
compile "com.android.databinding:baseLibrary:${project.databindingBaseLibraryVersion}"
compile fileTree(dir: './libs', includes: ['*.jar'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import info.izumin.android.droidux.processor.model.StoreImplModel;
import info.izumin.android.droidux.processor.model.StoreMethodModel;
import info.izumin.android.droidux.processor.model.StoreModel;
import rx.Observable;
import io.reactivex.Flowable;
import io.reactivex.Single;

import static info.izumin.android.droidux.processor.util.PoetUtils.getOverrideAnnotation;
import static info.izumin.android.droidux.processor.util.PoetUtils.getParameterSpec;
Expand Down Expand Up @@ -157,7 +158,7 @@ private MethodSpec createDispatchMethodSpec() {
return MethodSpec.methodBuilder(StoreModel.DISPATCH_METHOD_NAME)
.addAnnotation(getOverrideAnnotation())
.addModifiers(Modifier.PUBLIC)
.returns(ParameterizedTypeName.get(ClassName.get(Observable.class), ClassName.get(Action.class)))
.returns(ParameterizedTypeName.get(ClassName.get(Flowable.class), ClassName.get(Action.class)))
.addParameter(getParameterSpec(Action.class))
.addStatement("return $N.$N($N)",
DispatcherModel.VARIABLE_NAME, DispatcherModel.DISPATCH_METHOD_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;

import rx.Observable;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;

/**
* Created by izumin on 11/28/15.
Expand All @@ -29,16 +30,19 @@ enum Kind {
DISPATCH,
GETTER,
OBSERVE,
OBSERVE_WITH_BPS,
UNKNOWN
}

private static final Class OBSERVE_METHOD_CLASS = Observable.class;
private static final Class OBSERVE_METHOD_CLASS = Flowable.class;
private static final Class BACKPRESSURE_STRATEGY_PARAM_CLASS = BackpressureStrategy.class;

private final ExecutableElement element;
private final StoreModel storeModel;
private final Kind kind;
private final DeclaredType returnType;
private boolean isBindable = false;
private String bpsParamName = "";

public StoreMethodModel(ExecutableElement element, StoreModel storeModel) {
this.element = element;
Expand All @@ -61,7 +65,14 @@ public TypeName apply(StoreImplModel input) {
kind = Kind.DISPATCH;
} else if (MoreTypes.isTypeOf(OBSERVE_METHOD_CLASS, returnType.asElement().asType())
&& states.contains(ClassName.get(returnType.getTypeArguments().get(0)))) {
kind = Kind.OBSERVE;
if (element.getParameters() != null
&& !element.getParameters().isEmpty()
&& MoreTypes.isTypeOf(BACKPRESSURE_STRATEGY_PARAM_CLASS, element.getParameters().get(0).asType())) {
bpsParamName = element.getParameters().get(0).getSimpleName().toString();
kind = Kind.OBSERVE_WITH_BPS;
} else {
kind = Kind.OBSERVE;
}
} else {
kind = Kind.UNKNOWN;
}
Expand Down Expand Up @@ -99,6 +110,17 @@ public boolean apply(StoreImplModel input) {
})
.get(0).getVariableName(),
StoreImplModel.STATE_OBSERVE_METHOD_NAME).build();
case OBSERVE_WITH_BPS:
return CodeBlock.builder().addStatement("return $N.$N(" + bpsParamName + ")",
FluentIterable.from(storeModel.getStoreImplModels())
.filter(new Predicate<StoreImplModel>() {
@Override
public boolean apply(StoreImplModel input) {
return ClassName.get(returnType.getTypeArguments().get(0)).equals(input.getState());
}
})
.get(0).getVariableName(),
StoreImplModel.STATE_OBSERVE_METHOD_NAME).build();
case DISPATCH:
return CodeBlock.builder()
.addStatement("return $N.$N($N)", DispatcherModel.VARIABLE_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.tools.JavaFileObject;

import info.izumin.android.droidux.processor.fixture.Source;
import io.reactivex.BackpressureStrategy;

import static com.google.common.truth.Truth.assert_;
import static com.google.testing.compile.JavaFileObjects.forSourceLines;
Expand Down Expand Up @@ -39,6 +40,15 @@ public void singleReducer() {
);
}

@Test
public void singleReducer_with_BackpressureStrategySpecification() {
assertJavaSource(
forSourceLines("RootStore", Source.CounterWithBackpressureStrategy.TARGET),
forSourceLines("DroiduxRootStore_CounterStoreImpl", Source.StoreImpl.COUNTER),
forSourceLines("DroiduxRootStore", Source.CounterWithBackpressureStrategy.GENERATED_STORE)
);
}

@Test
public void singleReducerBindable() {
assertJavaSource(
Expand Down
Loading

0 comments on commit 8510117

Please sign in to comment.