Created
January 2, 2017 13:03
-
-
Save FireZenk/87a5611ff93f3212c4571e96367fb8ff to your computer and use it in GitHub Desktop.
UseCase executor, that takes handle use cases, their possible errors and return the subscription
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.app.ui.schedulers; | |
import android.support.annotation.NonNull; | |
import rx.Scheduler; | |
/** | |
* Project: app | |
* | |
* Created by Jorge Garrido Oval on 01/12/2016. | |
* Copyright © Jorge Garrido Oval 2016 | |
*/ | |
public interface SchedulerProvider { | |
@NonNull Scheduler computation(); | |
@NonNull Scheduler io(); | |
@NonNull Scheduler ui(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.app.domain; | |
import com.fernandocejas.frodo.annotation.RxLogSubscriber; | |
import rx.Observable; | |
import rx.Scheduler; | |
/** | |
* Project: app | |
* | |
* Created by Jorge Garrido Oval on 05/08/2016. | |
* Copyright © Jorge Garrido Oval 2016 | |
*/ | |
@RxLogSubscriber public abstract class UseCase<T> { | |
public abstract Observable<T> execute(final Scheduler observeOn, final Scheduler subscribeOn); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sample.app.ui; | |
import com.sample.app.domain.usecases.UseCase; | |
import com.sample.app.ui.schedulers.SchedulerProvider; | |
import javax.inject.Inject; | |
import rx.Observable; | |
import rx.Subscription; | |
import rx.functions.Action0; | |
import rx.functions.Action1; | |
/** | |
* Project: app | |
* | |
* Created by Jorge Garrido Oval on 02/01/2017. | |
* Copyright © Jorge Garrido Oval 2016 | |
*/ | |
public class UseCaseExecutor { | |
private final SchedulerProvider schedulerProvider; | |
@Inject public UseCaseExecutor(final SchedulerProvider schedulerProvider) { | |
this.schedulerProvider = schedulerProvider; | |
} | |
@SuppressWarnings("unchecked") public <M> Subscription execute( | |
final UseCase useCase, final Action1<M> onNext, final Action1<Throwable> onError, final Action0 onCompleted) { | |
Observable<M> obs; | |
try { | |
obs = useCase.asObservable().compose(schedulers()); | |
} catch (Exception ex) { | |
obs = Observable.error(ex); | |
} | |
return obs.subscribe(onNext, onError, onCompleted); | |
} | |
public <M> Subscription execute( | |
final UseCase useCase, final Action1<M> onNext, final Action1<Throwable> onError) { | |
return execute(useCase, onNext, onError, () -> {}); | |
} | |
public <M> Subscription execute( | |
final UseCase useCase, final Action1<M> onNext, final Action0 onCompleted) { | |
return execute(useCase, onNext, Throwable::printStackTrace, onCompleted); | |
} | |
public <M> Subscription execute( | |
final UseCase useCase, final Action1<M> onNext) { | |
return execute(useCase, onNext, Throwable::printStackTrace, () -> {}); | |
} | |
private <T> Observable.Transformer<T, T> schedulers() { | |
return observable -> observable.subscribeOn(schedulerProvider.io()) | |
.observeOn(schedulerProvider.ui()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment