package vn.tale.architecture.common.base;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import java.lang.ref.WeakReference;

/**
 * Created by Giang Nguyen on 2/21/17.
 */

public abstract class MvpPresenter<MvpView> {

  private WeakReference<MvpView> viewReference;
  private CompositeDisposable compositeDisposable;

  public final void attachView(@NonNull MvpView view) {
    viewReference = new WeakReference<>(view);
    onViewAttached();
  }

  public final void detachView() {
    viewReference.clear();
    if (compositeDisposable != null) {
      compositeDisposable.clear();
      compositeDisposable = null;
    }
    onViewDetached();
  }

  @Nullable protected MvpView getView() {
    if (viewReference == null) {
      return null;
    }
    return viewReference.get();
  }

  protected void disposeOnDetach(Disposable disposable) {
    if (compositeDisposable == null) {
      compositeDisposable = new CompositeDisposable();
    }
    compositeDisposable.add(disposable);
  }

  protected void onViewAttached() {
    // No-op
  }

  protected void onViewDetached() {
    // No-op
  }
}

@@ -625,30 +625,99 @@
     
-viewReference.clear();
+if (viewReference != null) %7B%0A      viewReference.clear();%0A      viewReference = null;%0A    %7D
 %0A   

check null before call