Skip to content

Commit

Permalink
Use Flowable instead of Single on sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
wakwak3125 committed Jan 2, 2018
1 parent fab66df commit c3b9b69
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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 io.reactivex.Flowable;
import io.reactivex.Single;

import static info.izumin.android.droidux.processor.util.PoetUtils.getOverrideAnnotation;
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(Single.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 @@ -5,7 +5,6 @@

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;

/**
Expand All @@ -26,8 +25,12 @@ protected StoreImpl(T state, R reducer) {
listeners = new HashSet<>();
}

public Observable<T> observe() {
return subject;
public Flowable<T> observe() {
return observe(BackpressureStrategy.DROP);
}

public Flowable<T> observe(BackpressureStrategy strategy) {
return subject.toFlowable(strategy);
}

public T getState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import info.izumin.android.droidux.example.todomvc.action.ClearCompletedTodoAction;
import info.izumin.android.droidux.example.todomvc.action.DeleteTodoAction;
import info.izumin.android.droidux.example.todomvc.action.ToggleCompletedTodoAction;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.SingleSubject;

/**
Expand Down Expand Up @@ -40,7 +43,7 @@ public void onCreate() {

observeOnClickBtnAddTodo()
.filter(s -> !s.isEmpty())
.flatMap(s -> store.dispatch(new AddTodoAction(s)).toMaybe())
.flatMap(s -> store.dispatch(new AddTodoAction(s)))
/*TODO: Version up RxAndroid*/
/*.subscribeOn(AndroidSchedulers.mainThread())*/
.subscribe(action -> {
Expand Down Expand Up @@ -73,34 +76,34 @@ public boolean onOptionItemSelected(MenuItem item) {
}
}

private Single<String> observeOnClickBtnAddTodo() {
SingleSubject<String> subject = SingleSubject.create();
btnAddTodo.setOnClickListener(v -> subject.onSuccess(editNewTodo.getText().toString()));
return subject;
private Flowable<String> observeOnClickBtnAddTodo() {
PublishSubject<String> subject = PublishSubject.create();
btnAddTodo.setOnClickListener(v -> subject.onNext(editNewTodo.getText().toString()));
return subject.toFlowable(BackpressureStrategy.DROP);
}

private Single<Long> observeOnClickListItem() {
SingleSubject<Long> subject = SingleSubject.create();
listTodo.setOnItemClickListener((parent, view, position, id) -> subject.onSuccess(id));
return subject;
private Flowable<Long> observeOnClickListItem() {
PublishSubject<Long> subject = PublishSubject.create();
listTodo.setOnItemClickListener((parent, view, position, id) -> subject.onNext(id));
return subject.toFlowable(BackpressureStrategy.DROP);
}

private Single<Long> observeOnLongClickListItem() {
SingleSubject<Long> subject = SingleSubject.create();
private Flowable<Long> observeOnLongClickListItem() {
PublishSubject<Long> subject = PublishSubject.create();
listTodo.setOnItemLongClickListener((parent, view, position, id) -> {
new AlertDialog.Builder(activity)
.setTitle(R.string.dialog_delete_todo_title)
.setMessage(activity.getString(R.string.dialog_delete_todo_message,
store.todoList().getTodoById((int) id).getText()))
.setPositiveButton(R.string.dialog_delete_todo_btn_positive, (dialog, which) -> {
subject.onSuccess(id);
subject.onNext(id);
})
.setNeutralButton(R.string.dialog_delete_todo_btn_neutral, (dialog, which) -> {
dialog.dismiss();
})
.show();
return true;
});
return subject;
return subject.toFlowable(BackpressureStrategy.DROP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import info.izumin.android.droidux.annotation.Store;
import info.izumin.android.droidux.example.todomvc.entity.TodoList;
import info.izumin.android.droidux.example.todomvc.reducer.TodoListReducer;
import io.reactivex.Observable;
import io.reactivex.Flowable;

/**
* Created by izumin on 11/29/15.
*/
@Store(TodoListReducer.class)
public interface RootStore extends BaseStore {
TodoList todoList();
Observable<TodoList> observeTodoList();
Flowable<TodoList> observeTodoList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import info.izumin.android.droidux.Action;
import info.izumin.android.droidux.Middleware;
import info.izumin.android.droidux.example.todomvc.RootStore;
import io.reactivex.Single;
import rx.Observable;
import io.reactivex.Flowable;

/**
* Created by izumin on 11/4/15.
Expand All @@ -15,15 +14,15 @@ public class Logger extends Middleware<RootStore> {
public static final String TAG = Logger.class.getSimpleName();

@Override
public Single<Action> beforeDispatch(Action action) {
public Flowable<Action> beforeDispatch(Action action) {
Log.d("[prev todo]", getStore().todoList().toString());
Log.d("[" + action.getClass().getSimpleName() + "]", action.toString());
return Single.just(action);
return Flowable.just(action);
}

@Override
public Single<Action> afterDispatch(Action action) {
public Flowable<Action> afterDispatch(Action action) {
Log.d("[next todo]", getStore().todoList().toString());
return Single.just(action);
return Flowable.just(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import info.izumin.android.droidux.annotation.Store;
import info.izumin.android.droidux.example.todoswithdagger.entity.TodoList;
import info.izumin.android.droidux.example.todoswithdagger.reducer.TodoListReducer;
import io.reactivex.Observable;
import io.reactivex.Flowable;

/**
* Created by izumin on 11/29/15.
*/
@Store(TodoListReducer.class)
public interface RootStore extends BaseStore {
TodoList todoList();
Observable<TodoList> observeTodoList();
Flowable<TodoList> observeTodoList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import info.izumin.android.droidux.example.todoswithdagger.action.DeleteTodoAction;
import info.izumin.android.droidux.example.todoswithdagger.action.ToggleCompletedTodoAction;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.subjects.SingleSubject;
import io.reactivex.subjects.PublishSubject;

/**
* Created by izumin on 11/5/15.
Expand All @@ -19,9 +19,9 @@ public class MainPresenter {
private final MainView view;
private final RootStore store;

private final SingleSubject<String> clickAddTodoSubject = SingleSubject.create();
private final SingleSubject<Long> clickItemSubject = SingleSubject.create();
private final SingleSubject<Long> longClickItemSubject = SingleSubject.create();
private final PublishSubject<String> clickAddTodoSubject = PublishSubject.create();
private final PublishSubject<Long> clickItemSubject = PublishSubject.create();
private final PublishSubject<Long> longClickItemSubject = PublishSubject.create();

private CompositeDisposable compositeDisposable;

Expand All @@ -35,21 +35,21 @@ void onStart() {

compositeDisposable.add(clickAddTodoSubject
.filter(s -> !s.isEmpty())
.flatMap(s -> store.dispatch(new AddTodoAction(s)).toMaybe())
.flatMap(s -> store.dispatch(new AddTodoAction(s)).toObservable())
/*TODO: Version up RxAndroid*/
/*.subscribeOn(AndroidSchedulers.mainThread())*/
.flatMap(_a -> store.dispatch(new ClearNewTodoTextAction()).toMaybe())
.flatMap(_a -> store.dispatch(new ClearNewTodoTextAction()).toObservable())
.subscribe(_a -> {
view.clearNewTodoText();
view.showToast(R.string.toast_add_todo);
}));

compositeDisposable.add(clickItemSubject
.flatMap(id -> store.dispatch(new ToggleCompletedTodoAction(id.intValue())))
.flatMap(id -> store.dispatch(new ToggleCompletedTodoAction(id.intValue())).toObservable())
.subscribe());

compositeDisposable.add(longClickItemSubject
.flatMap(id -> store.dispatch(new DeleteTodoAction(id)))
.flatMap(id -> store.dispatch(new DeleteTodoAction(id)).toObservable())
/*TODO: Version up RxAndroid*/
/*.subscribeOn(AndroidSchedulers.mainThread())*/
.subscribe(action -> view.showToast(R.string.toast_delete_todo)));
Expand All @@ -60,15 +60,15 @@ void onStop() {
}

void onClickBtnAddTodo(String text) {
clickAddTodoSubject.onSuccess(text);
clickAddTodoSubject.onNext(text);
}

void onClickListItem(long id) {
clickItemSubject.onSuccess(id);
clickItemSubject.onNext(id);
}

void onLongClickListItem(long id) {
longClickItemSubject.onSuccess(id);
longClickItemSubject.onNext(id);
}

void clearCompletedTodo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import info.izumin.android.droidux.example.todoswithundo.action.DeleteTodoAction;
import info.izumin.android.droidux.example.todoswithundo.action.ToggleCompletedTodoAction;
import info.izumin.android.droidux.example.todoswithundo.reducer.TodoListReducer;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.SingleSubject;

/**
Expand Down Expand Up @@ -43,7 +47,7 @@ public void onCreate() {

observeOnClickBtnAddTodo()
.filter(s -> !s.isEmpty())
.flatMap(s -> store.dispatch(new AddTodoAction(s)).toMaybe())
.flatMap(s -> store.dispatch(new AddTodoAction(s)))
/*TODO: Version up RxAndroid*/
/*.subscribeOn(AndroidSchedulers.mainThread())*/
.subscribe(action -> {
Expand Down Expand Up @@ -82,34 +86,34 @@ public boolean onOptionItemSelected(MenuItem item) {
}
}

private io.reactivex.Single<String> observeOnClickBtnAddTodo() {
SingleSubject<String> subject = SingleSubject.create();
btnAddTodo.setOnClickListener(v -> subject.onSuccess(editNewTodo.getText().toString()));
return subject;
private Flowable<String> observeOnClickBtnAddTodo() {
PublishSubject<String> subject = PublishSubject.create();
btnAddTodo.setOnClickListener(v -> subject.onNext(editNewTodo.getText().toString()));
return subject.toFlowable(BackpressureStrategy.DROP);
}

private io.reactivex.Single<Long> observeOnClickListItem() {
SingleSubject<Long> subject = SingleSubject.create();
listTodo.setOnItemClickListener((parent, view, position, id) -> subject.onSuccess(id));
return subject;
private Flowable<Long> observeOnClickListItem() {
PublishSubject<Long> subject = PublishSubject.create();
listTodo.setOnItemClickListener((parent, view, position, id) -> subject.onNext(id));
return subject.toFlowable(BackpressureStrategy.DROP);
}

private Single<Long> observeOnLongClickListItem() {
SingleSubject<Long> subject = SingleSubject.create();
private Flowable<Long> observeOnLongClickListItem() {
PublishSubject<Long> subject = PublishSubject.create();
listTodo.setOnItemLongClickListener((parent, view, position, id) -> {
new AlertDialog.Builder(activity)
.setTitle(R.string.dialog_delete_todo_title)
.setMessage(activity.getString(R.string.dialog_delete_todo_message,
store.todoList().getTodoById((int) id).getText()))
.setPositiveButton(R.string.dialog_delete_todo_btn_positive, (dialog, which) -> {
subject.onSuccess(id);
subject.onNext(id);
})
.setNeutralButton(R.string.dialog_delete_todo_btn_neutral, (dialog, which) -> {
dialog.dismiss();
})
.show();
return true;
});
return subject;
return subject.toFlowable(BackpressureStrategy.DROP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import info.izumin.android.droidux.annotation.Store;
import info.izumin.android.droidux.example.todoswithundo.entity.TodoList;
import info.izumin.android.droidux.example.todoswithundo.reducer.TodoListReducer;
import io.reactivex.Observable;
import io.reactivex.Flowable;

/**
* Created by izumin on 11/29/15.
*/
@Store(TodoListReducer.class)
public interface RootStore extends BaseStore {
TodoList todoList();
Observable<TodoList> observeTodoList();
Flowable<TodoList> observeTodoList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import info.izumin.android.droidux.Action;
import info.izumin.android.droidux.Middleware;
import info.izumin.android.droidux.example.todoswithundo.RootStore;
import io.reactivex.Flowable;
import io.reactivex.Single;

/**
Expand All @@ -15,15 +16,15 @@ public class Logger extends Middleware<RootStore> {


@Override
public Single<Action> beforeDispatch(Action action) {
public Flowable<Action> beforeDispatch(Action action) {
Log.d("[prev todo]", getStore().todoList().toString());
Log.d("[" + action.getClass().getSimpleName() + "]", action.toString());
return Single.just(action);
return Flowable.just(action);
}

@Override
public Single<Action> afterDispatch(Action action) {
public Flowable<Action> afterDispatch(Action action) {
Log.d("[next todo]", getStore().todoList().toString());
return Single.just(action);
return Flowable.just(action);
}
}

0 comments on commit c3b9b69

Please sign in to comment.