From 06d319bb72cf959bf36172cb3246f0d4db36aee1 Mon Sep 17 00:00:00 2001 From: Saeed Masoumi Date: Mon, 29 May 2017 12:56:20 +0430 Subject: [PATCH] Support for different types of UseCases (Fix #18) --- .../java/easymvp/usecase/FlowableUseCase.java | 40 +++++++++++++++++++ .../java/easymvp/usecase/MaybeUseCase.java | 40 +++++++++++++++++++ .../java/easymvp/usecase/SingleUseCase.java | 39 ++++++++++++++++++ .../main/java/easymvp/usecase/UseCase.java | 8 ++++ 4 files changed, 127 insertions(+) create mode 100644 easymvp-rx2-api/src/main/java/easymvp/usecase/FlowableUseCase.java create mode 100644 easymvp-rx2-api/src/main/java/easymvp/usecase/MaybeUseCase.java create mode 100644 easymvp-rx2-api/src/main/java/easymvp/usecase/SingleUseCase.java diff --git a/easymvp-rx2-api/src/main/java/easymvp/usecase/FlowableUseCase.java b/easymvp-rx2-api/src/main/java/easymvp/usecase/FlowableUseCase.java new file mode 100644 index 0000000..c1a05e4 --- /dev/null +++ b/easymvp-rx2-api/src/main/java/easymvp/usecase/FlowableUseCase.java @@ -0,0 +1,40 @@ +package easymvp.usecase; + +import javax.annotation.Nullable; + +import easymvp.executer.PostExecutionThread; +import easymvp.executer.UseCaseExecutor; +import io.reactivex.Flowable; +import io.reactivex.FlowableTransformer; + +/** + * @author Saeed Masoumi (s-masoumi@live.com) + */ + +public abstract class FlowableUseCase extends UseCase { + private final FlowableTransformer schedulersTransformer; + + public FlowableUseCase(final UseCaseExecutor useCaseExecutor, + final PostExecutionThread postExecutionThread) { + super(useCaseExecutor, postExecutionThread); + schedulersTransformer = new FlowableTransformer() { + @Override + public Flowable apply(Flowable rObservable) { + return rObservable.subscribeOn(useCaseExecutor.getScheduler()) + .observeOn(postExecutionThread.getScheduler()); + } + }; + } + + @Override + public Flowable execute(@Nullable Q param) { + return interact(param).compose(getSchedulersTransformer()); + } + + @Override + protected abstract Flowable interact(@Nullable Q param); + + private FlowableTransformer getSchedulersTransformer() { + return schedulersTransformer; + } +} diff --git a/easymvp-rx2-api/src/main/java/easymvp/usecase/MaybeUseCase.java b/easymvp-rx2-api/src/main/java/easymvp/usecase/MaybeUseCase.java new file mode 100644 index 0000000..ff85266 --- /dev/null +++ b/easymvp-rx2-api/src/main/java/easymvp/usecase/MaybeUseCase.java @@ -0,0 +1,40 @@ +package easymvp.usecase; + +import javax.annotation.Nullable; + +import easymvp.executer.PostExecutionThread; +import easymvp.executer.UseCaseExecutor; +import io.reactivex.Maybe; +import io.reactivex.MaybeTransformer; + +/** + * @author Saeed Masoumi (s-masoumi@live.com) + */ + +public abstract class MaybeUseCase extends UseCase { + private final MaybeTransformer schedulersTransformer; + + public MaybeUseCase(final UseCaseExecutor useCaseExecutor, + final PostExecutionThread postExecutionThread) { + super(useCaseExecutor, postExecutionThread); + schedulersTransformer = new MaybeTransformer() { + @Override + public Maybe apply(Maybe single) { + return single.subscribeOn(useCaseExecutor.getScheduler()) + .observeOn(postExecutionThread.getScheduler()); + } + }; + } + + @Override + public Maybe execute(@Nullable Q param) { + return interact(param).compose(getSchedulersTransformer()); + } + + @Override + protected abstract Maybe interact(@Nullable Q param); + + private MaybeTransformer getSchedulersTransformer() { + return schedulersTransformer; + } +} diff --git a/easymvp-rx2-api/src/main/java/easymvp/usecase/SingleUseCase.java b/easymvp-rx2-api/src/main/java/easymvp/usecase/SingleUseCase.java new file mode 100644 index 0000000..96ff7dd --- /dev/null +++ b/easymvp-rx2-api/src/main/java/easymvp/usecase/SingleUseCase.java @@ -0,0 +1,39 @@ +package easymvp.usecase; + +import easymvp.executer.PostExecutionThread; +import easymvp.executer.UseCaseExecutor; +import io.reactivex.Single; +import io.reactivex.SingleTransformer; +import io.reactivex.annotations.Nullable; + +/** + * @author Saeed Masoumi (s-masoumi@live.com) + */ + +public abstract class SingleUseCase extends UseCase { + private final SingleTransformer schedulersTransformer; + + public SingleUseCase(final UseCaseExecutor useCaseExecutor, + final PostExecutionThread postExecutionThread) { + super(useCaseExecutor, postExecutionThread); + schedulersTransformer = new SingleTransformer() { + @Override + public Single apply(Single single) { + return single.subscribeOn(useCaseExecutor.getScheduler()) + .observeOn(postExecutionThread.getScheduler()); + } + }; + } + + @Override + public Single execute(@Nullable Q param) { + return interact(param).compose(getSchedulersTransformer()); + } + + @Override + protected abstract Single interact(@Nullable Q param); + + private SingleTransformer getSchedulersTransformer() { + return schedulersTransformer; + } +} diff --git a/easymvp-rx2-api/src/main/java/easymvp/usecase/UseCase.java b/easymvp-rx2-api/src/main/java/easymvp/usecase/UseCase.java index 1ef7549..6d334d4 100644 --- a/easymvp-rx2-api/src/main/java/easymvp/usecase/UseCase.java +++ b/easymvp-rx2-api/src/main/java/easymvp/usecase/UseCase.java @@ -7,6 +7,14 @@ import javax.annotation.Nullable; /** + * Each {@code UseCase} of the system orchestrate the flow of data to and from the entities. + *

+ * Outer layers of system can execute use cases by calling {@link #execute(Object)}} method. Also + * you can use {@link #useCaseExecutor} to execute the job in a background thread and {@link + * #postExecutionThread} to post the result to another thread(usually UI thread). + * + * @param

The response type of a use case. + * @param The request type of a use case. * Created by megrez on 2017/3/12. */ public abstract class UseCase {