com.arellomobile.mvp.MvpPresenter Maven / Gradle / Ivy
package com.arellomobile.mvp;
import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;
import com.arellomobile.mvp.presenter.PresenterType;
import com.arellomobile.mvp.viewstate.MvpViewState;
/**
* Date: 15.12.2015
* Time: 19:31
*
* @author Yuri Shmakov
* @author Alexander Blinov
* @author Konstantin Tckhovrebov
*/
public abstract class MvpPresenter {
private boolean mFirstLaunch = true;
private String mTag;
private PresenterType mPresenterType;
private Set mViews;
private View mViewStateAsView;
private MvpViewState mViewState;
private Class extends MvpPresenter> mPresenterClass;
public MvpPresenter() {
Binder.bind(this);
mViews = Collections.newSetFromMap(new WeakHashMap());
}
/**
* Attach view to view state or to presenter(if view state not exists).
* If you use {@link MvpDelegate}, you should not call this method directly.
* It will be called on {@link MvpDelegate#onAttach()}, if view does not attached.
*
* @param view to attachment
*/
public void attachView(View view) {
if (mViewState != null) {
mViewState.attachView(view);
} else {
mViews.add(view);
}
if (mFirstLaunch) {
mFirstLaunch = false;
onFirstViewAttach();
}
}
/**
* Callback after first presenter init and view binding. If this
* presenter instance will have to attach some view in future, this method
* will not be called.
* There you can to interact with {@link #mViewState}.
*/
protected void onFirstViewAttach() {
}
/**
* Detach view from view state or from presenter(if view state not exists).
* If you use {@link MvpDelegate}, you should not call this method directly.
* It will be called on {@link MvpDelegate#onDetach()}.
*
* @param view view to detach
*/
@SuppressWarnings("WeakerAccess")
public void detachView(View view) {
if (mViewState != null) {
mViewState.detachView(view);
} else {
mViews.remove(view);
}
}
public void destroyView(View view) {
if (mViewState != null) {
mViewState.destroyView(view);
}
}
/**
* @return views attached to view state, or attached to presenter(if view state not exists)
*/
@SuppressWarnings("WeakerAccess")
public Set getAttachedViews() {
if (mViewState != null) {
return mViewState.getViews();
}
return mViews;
}
/**
* @return view state, casted to view interface for simplify
*/
@SuppressWarnings("WeakerAccess")
public View getViewState() {
return mViewStateAsView;
}
/**
* Check if view is in restore state or not
*
* @param view view for check
* @return true if view state restore state to incoming view. false otherwise.
*/
@SuppressWarnings("unused")
public boolean isInRestoreState(View view) {
//noinspection SimplifiableIfStatement
if (mViewState != null) {
return mViewState.isInRestoreState(view);
}
return false;
}
/**
* Set view state to presenter
*
* @param viewState that implements type, setted as View generic param
*/
@SuppressWarnings({"unchecked", "unused"})
public void setViewState(MvpViewState viewState) {
mViewStateAsView = (View) viewState;
mViewState = (MvpViewState) viewState;
}
PresenterType getPresenterType() {
return mPresenterType;
}
void setPresenterType(PresenterType presenterType) {
mPresenterType = presenterType;
}
String getTag() {
return mTag;
}
void setTag(String tag) {
mTag = tag;
}
void setPresenterClass(Class extends MvpPresenter> presenterClass) {
mPresenterClass = presenterClass;
}
Class extends MvpPresenter> getPresenterClass() {
return mPresenterClass;
}
/**
* Called before reference on this presenter will be cleared and instance of presenter
* will be never used.
*/
public void onDestroy() {
}
private static class Binder {
static void bind(MvpPresenter presenter) {
MvpView viewState = (MvpView) MoxyReflector.getViewState(presenter.getClass());
presenter.mViewStateAsView = viewState;
presenter.mViewState = (MvpViewState) viewState;
}
}
}