/*
 * Copyright (c) 2015  Zvooq LTD.
 * Authors: Renat Sarymsakov, Dmitriy Mozgin, Denis Volyntsev.
 *
 * This file is part of Visum.
 *
 * Visum is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Visum is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Visum.  If not, see <http://www.gnu.org/licenses/>.
 */

package io.reist.visum.presenter;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Reist on 10/15/15.
 */
public abstract class VisumPresenter<V> {

    private CompositeSubscription subscriptions;
    private V view;

    public final void setView(V view) {
        if (view == null) {
            if (subscriptions != null) {
                subscriptions.unsubscribe();
            }

            if (this.view != null) {
                onViewDetached();
            }

            this.view = null;
        } else {
            if (this.view != null) {
                setView(null);
            }

            this.view = view;
            subscriptions = new CompositeSubscription();
            onViewAttached();
        }
    }

    protected final <T> void subscribe(Observable<T> observable, Observer<? super T> observer) {
        subscriptions.add(
                observable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())
                        .subscribe(observer)
        );
    }

    protected abstract void onViewAttached();

    protected void onViewDetached() {
    }

    public final V view() {
        return view;
    }

}

@@ -837,16 +837,34 @@
 server;%0A
+import rx.Single;%0A
 import r
@@ -895,32 +895,61 @@
 roidSchedulers;%0A
+import rx.functions.Action1;%0A
 import rx.schedu
@@ -2069,32 +2069,346 @@
       );%0A    %7D%0A%0A
+    protected final %3CT%3E void subscribe(Single%3CT%3E single, Action1%3CT%3E observer) %7B%0A        subscriptions.add(%0A                single%0A                        .subscribeOn(Schedulers.io())%0A                        .observeOn(AndroidSchedulers.mainThread())%0A                        .subscribe(observer)%0A        );%0A    %7D%0A%0A
     protected ab

add subscribe( rx.Single ) to VisumPresenter