/*
 * Copyright (C) 2016 grandcentrix GmbH
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.grandcentrix.thirtyinch.rx;

import net.grandcentrix.thirtyinch.TiLifecycleObserver;
import net.grandcentrix.thirtyinch.TiPresenter;
import net.grandcentrix.thirtyinch.TiView;

import android.support.annotation.NonNull;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

public class RxTiPresenterSubscriptionHandler {

    private CompositeSubscription mPresenterSubscriptions = new CompositeSubscription();

    private CompositeSubscription mUiSubscriptions;

    public RxTiPresenterSubscriptionHandler(final TiPresenter presenter) {
        presenter.addLifecycleObserver(new TiLifecycleObserver() {
            @Override
            public void onChange(final TiPresenter.State state,
                    final boolean beforeLifecycleEvent) {
                if (state == TiPresenter.State.VIEW_DETACHED && beforeLifecycleEvent) {
                    // unsubscribe all UI subscriptions created in wakeUp() and added
                    // via manageViewSubscription(Subscription)
                    if (mUiSubscriptions != null) {
                        mUiSubscriptions.unsubscribe();
                    }
                    // there is no reuse possible. recreation works fine
                    mUiSubscriptions = new CompositeSubscription();
                }

                if (state == TiPresenter.State.DESTROYED && beforeLifecycleEvent) {
                    mPresenterSubscriptions.unsubscribe();
                    mPresenterSubscriptions = null;
                }
            }
        });
    }

    /**
     * Add your subscriptions here and they will automatically unsubscribed when {@link
     * TiPresenter#destroy()} gets called
     *
     * @throws IllegalStateException when the presenter has reached {@link net.grandcentrix.thirtyinch.TiPresenter.State#DESTROYED}
     */
    public void manageSubscription(@NonNull final Subscription subscription) {
        if (mPresenterSubscriptions == null) {
            throw new IllegalStateException("subscription handling doesn't work"
                    + " when the presenter has reached the DESTROYED state");
        }

        if (subscription.isUnsubscribed()) {
            return;
        }
        mPresenterSubscriptions.add(subscription);
    }

    /**
     * Add your subscriptions for View events to this method to get them automatically cleaned up
     * in {@link TiPresenter#detachView()}. typically call this in {@link
     * TiPresenter#attachView(TiView)} where you subscribe to the UI events.
     *
     * @throws IllegalStateException when no view is attached
     */
    public void manageViewSubscription(@NonNull final Subscription subscription) {
        if (mUiSubscriptions == null) {
            throw new IllegalStateException("view subscriptions can't be handled"
                    + " when there is no view");
        }
        mUiSubscriptions.add(subscription);
    }
}

@@ -2239,30 +2239,30 @@
 bed when
- %7B@link
 %0A     *
+ %7B@link
  TiPrese
@@ -2428,16 +2428,66 @@
 TROYED%7D%0A
+     * @see #manageSubscriptions(Subscription...)%0A
      */%0A
@@ -2907,32 +2907,575 @@
 iption);%0A    %7D%0A%0A
+    /**%0A     * Add your subscriptions here and they will automatically unsubscribed when%0A     * %7B@link TiPresenter#destroy()%7D gets called%0A     *%0A     * @throws IllegalStateException when the presenter has reached %7B@link net.grandcentrix.thirtyinch.TiPresenter.State#DESTROYED%7D%0A     * @see #manageSubscription(Subscription)%0A     **/%0A    public void manageSubscriptions(@NonNull final Subscription... subscriptions) %7B%0A        for (final Subscription subscription : subscriptions) %7B%0A            manageSubscription(subscription);%0A        %7D%0A    %7D%0A%0A
     /**%0A     * A
@@ -3784,16 +3784,70 @@
 ttached%0A
+     * @see #manageViewSubscriptions(Subscription...)%0A
      */%0A
@@ -4154,16 +4154,621 @@
 ption);%0A
-
     %7D
+%0A%0A    /**%0A     * Add your subscriptions for View events to this method to get them automatically cleaned up%0A     * in %7B@link TiPresenter#detachView()%7D. typically call this in %7B@link%0A     * TiPresenter#attachView(TiView)%7D where you subscribe to the UI events.%0A     *%0A     * @throws IllegalStateException when no view is attached%0A     * @see #manageViewSubscription(Subscription)%0A     */%0A    public void manageViewSubscriptions(@NonNull final Subscription... subscriptions) %7B%0A        for (final Subscription subscription : subscriptions) %7B%0A            manageViewSubscription(subscription);%0A        %7D%0A    %7D%0A%0A
 %0A%7D%0A

Add manageViewSubscriptions and manageSubscriptions to handle multiple subscriptions at once