Search in sources :

Example 1 with OutsideLifecycleException

use of com.trello.rxlifecycle.OutsideLifecycleException in project Conductor by bluelinelabs.

the class ControllerLifecycleSubjectHelper method create.

public static BehaviorSubject<ControllerEvent> create(Controller controller) {
    ControllerEvent initialState;
    if (controller.isBeingDestroyed() || controller.isDestroyed()) {
        throw new OutsideLifecycleException("Cannot bind to Controller lifecycle when outside of it.");
    } else if (controller.isAttached()) {
        initialState = ControllerEvent.ATTACH;
    } else if (controller.getView() != null) {
        initialState = ControllerEvent.CREATE_VIEW;
    } else if (controller.getActivity() != null) {
        initialState = ControllerEvent.CONTEXT_AVAILABLE;
    } else {
        initialState = ControllerEvent.CREATE;
    }
    final BehaviorSubject<ControllerEvent> subject = BehaviorSubject.create(initialState);
    controller.addLifecycleListener(new LifecycleListener() {

        @Override
        public void preContextAvailable(@NonNull Controller controller) {
            subject.onNext(ControllerEvent.CONTEXT_AVAILABLE);
        }

        @Override
        public void preCreateView(@NonNull Controller controller) {
            subject.onNext(ControllerEvent.CREATE_VIEW);
        }

        @Override
        public void preAttach(@NonNull Controller controller, @NonNull View view) {
            subject.onNext(ControllerEvent.ATTACH);
        }

        @Override
        public void preDetach(@NonNull Controller controller, @NonNull View view) {
            subject.onNext(ControllerEvent.DETACH);
        }

        @Override
        public void preDestroyView(@NonNull Controller controller, @NonNull View view) {
            subject.onNext(ControllerEvent.DESTROY_VIEW);
        }

        @Override
        public void preContextUnavailable(@NonNull Controller controller, @NonNull Context context) {
            subject.onNext(ControllerEvent.CONTEXT_UNAVAILABLE);
        }

        @Override
        public void preDestroy(@NonNull Controller controller) {
            subject.onNext(ControllerEvent.DESTROY);
        }
    });
    return subject;
}
Also used : Context(android.content.Context) OutsideLifecycleException(com.trello.rxlifecycle.OutsideLifecycleException) LifecycleListener(com.bluelinelabs.conductor.Controller.LifecycleListener) Controller(com.bluelinelabs.conductor.Controller) View(android.view.View)

Aggregations

Context (android.content.Context)1 View (android.view.View)1 Controller (com.bluelinelabs.conductor.Controller)1 LifecycleListener (com.bluelinelabs.conductor.Controller.LifecycleListener)1 OutsideLifecycleException (com.trello.rxlifecycle.OutsideLifecycleException)1