Skip to content

Commit

Permalink
Add @UiThread annotation to presenter to make the contract explicit. …
Browse files Browse the repository at this point in the history
…If for

example someone changes
    Retrofit.Builder.callbackExecutor(someBackgroundExecutor)
then the app could crash without pattern due to WeakReference expiration.
  • Loading branch information
Paul Houghton committed Mar 24, 2016
1 parent e31f081 commit 58095a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.hannesdorfmann.mosby.mvp;

import android.support.annotation.Nullable;
import android.support.annotation.UiThread;

import java.lang.ref.WeakReference;

/**
Expand Down Expand Up @@ -71,6 +73,7 @@ public class MvpBasePresenter<V extends MvpView> implements MvpPresenter<V> {

private WeakReference<V> viewRef;

@UiThread
@Override public void attachView(V view) {
viewRef = new WeakReference<V>(view);
}
Expand All @@ -82,6 +85,7 @@ public class MvpBasePresenter<V extends MvpView> implements MvpPresenter<V> {
*
* @return <code>null</code>, if view is not attached, otherwise the concrete view instance
*/
@UiThread
@Nullable public V getView() {
return viewRef == null ? null : viewRef.get();
}
Expand All @@ -90,10 +94,12 @@ public class MvpBasePresenter<V extends MvpView> implements MvpPresenter<V> {
* Checks if a view is attached to this presenter. You should always call this method before
* calling {@link #getView()} to get the view instance.
*/
@UiThread
public boolean isViewAttached() {
return viewRef != null && viewRef.get() != null;
}

@UiThread
@Override public void detachView(boolean retainInstance) {
if (viewRef != null) {
viewRef.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.hannesdorfmann.mosby.mvp;

import android.support.annotation.NonNull;
import android.support.annotation.UiThread;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

Expand All @@ -21,17 +23,20 @@ public class MvpNullObjectBasePresenter<V extends MvpView> implements MvpPresent

private V view;

@UiThread
@Override public void attachView(V view) {
this.view = view;
}

@UiThread
@NonNull public V getView() {
if (view == null) {
throw new NullPointerException("MvpView reference is null. Have you called attachView()?");
}
return view;
}

@UiThread
@Override public void detachView(boolean retainInstance) {
if (view != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.hannesdorfmann.mosby.mvp;

import android.support.annotation.UiThread;

/**
* The base interface for each mvp presenter.
*
Expand All @@ -32,11 +34,13 @@ public interface MvpPresenter<V extends MvpView> {
/**
* Set or attach the view to this presenter
*/
public void attachView(V view);
@UiThread
void attachView(V view);

/**
* Will be called if the view has been destroyed. Typically this method will be invoked from
* <code>Activity.detachView()</code> or <code>Fragment.onDestroyView()</code>
*/
public void detachView(boolean retainInstance);
@UiThread
void detachView(boolean retainInstance);
}

0 comments on commit 58095a3

Please sign in to comment.