Last active
March 21, 2016 18:05
-
-
Save benoitjadinon/9a2b41e75848a4e9ab4f to your computer and use it in GitHub Desktop.
extends [AbstractRoboController](https://gist.github.com/benoitjadinon/3f1932163c65b4c31be0) and adds RxLifecycle info and helpers to create subscriptions
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 roboutils; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.os.Bundle; | |
| import android.support.annotation.CheckResult; | |
| import android.support.annotation.NonNull; | |
| import com.trello.rxlifecycle.ActivityEvent; | |
| import com.trello.rxlifecycle.ActivityLifecycleProvider; | |
| import com.trello.rxlifecycle.RxLifecycle; | |
| import rx.Observable; | |
| import rx.Subscription; | |
| import rx.subjects.BehaviorSubject; | |
| import rx.subscriptions.CompositeSubscription; | |
| public abstract class AbstractRxRoboController extends AbstractRoboController implements ActivityLifecycleProvider { | |
| CompositeSubscription mCompositeSubscription; | |
| private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create(); | |
| public AbstractRxRoboController(Activity activity) { | |
| super(activity); | |
| mCompositeSubscription = new CompositeSubscription(); | |
| } | |
| protected void addSubscription(Subscription subscription) { | |
| mCompositeSubscription.add(subscription); | |
| } | |
| //region ActivityLifecycleProvider | |
| @Override | |
| @NonNull | |
| @CheckResult | |
| public Observable<ActivityEvent> lifecycle() { | |
| return lifecycleSubject.asObservable(); | |
| } | |
| @Override | |
| @NonNull | |
| @CheckResult | |
| public <T> Observable.Transformer<T, T> bindUntilEvent(@NonNull ActivityEvent event) { | |
| return RxLifecycle.bindUntilEvent(lifecycleSubject, event); | |
| } | |
| @Override | |
| @NonNull | |
| @CheckResult | |
| public <T> Observable.Transformer<T, T> bindToLifecycle() { | |
| return RxLifecycle.bindActivity(lifecycleSubject); | |
| } | |
| //endregion ActivityLifecycleProvider | |
| //region lifecycle registers | |
| //https://github.com/trello/RxLifecycle/blob/c200d680d3dd3da8b17e3e6cec30c9278a7efefd/rxlifecycle-components/src/main/java/com/trello/rxlifecycle/components/RxActivity.java | |
| @Override | |
| protected void onCreateActivity(Context context, Bundle savedInstanceState) { | |
| super.onCreateActivity(context, savedInstanceState); | |
| lifecycleSubject.onNext(ActivityEvent.CREATE); | |
| } | |
| @Override | |
| protected void onStart(Context context) { | |
| super.onStart(context); | |
| lifecycleSubject.onNext(ActivityEvent.START); | |
| } | |
| @Override | |
| protected void onResume(Activity activity) { | |
| super.onResume(activity); | |
| lifecycleSubject.onNext(ActivityEvent.RESUME); | |
| } | |
| @Override | |
| protected void onPause(Activity activity) { | |
| lifecycleSubject.onNext(ActivityEvent.PAUSE); | |
| super.onPause(activity); | |
| } | |
| @Override | |
| protected void onStop(Activity activity) { | |
| lifecycleSubject.onNext(ActivityEvent.STOP); | |
| super.onStop(activity); | |
| } | |
| @Override | |
| protected void onDestroy(Context context) { | |
| lifecycleSubject.onNext(ActivityEvent.DESTROY); | |
| if (mCompositeSubscription != null && !mCompositeSubscription.isUnsubscribed()) | |
| mCompositeSubscription.unsubscribe(); | |
| super.onDestroy(context); | |
| } | |
| //endregion lifecycle | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment