Skip to content

Commit

Permalink
mvp module support retaining activities with retaining presenters
Browse files Browse the repository at this point in the history
  • Loading branch information
sockeqwe committed Sep 1, 2015
1 parent 3a67469 commit 90704a8
Show file tree
Hide file tree
Showing 37 changed files with 435 additions and 74 deletions.
16 changes: 14 additions & 2 deletions mvp/src/main/java/com/hannesdorfmann/mosby/mvp/MvpActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import com.hannesdorfmann.mosby.mvp.delegate.ActivityMvpDelegate;
import com.hannesdorfmann.mosby.mvp.delegate.ActivityMvpDelegateCallback;
import com.hannesdorfmann.mosby.mvp.delegate.ActivityMvpDelegateImpl;
import com.hannesdorfmann.mosby.mvp.delegate.MvpDelegateCallback;

/**
* An Activity that uses an {@link MvpPresenter} to implement a Model-View-Presenter
Expand All @@ -31,7 +31,7 @@
* @since 1.0.0
*/
public abstract class MvpActivity<V extends MvpView, P extends MvpPresenter<V>>
extends AppCompatActivity implements MvpDelegateCallback<V, P>, MvpView {
extends AppCompatActivity implements ActivityMvpDelegateCallback<V, P>, MvpView {

protected ActivityMvpDelegate mvpDelegate;
protected P presenter;
Expand Down Expand Up @@ -130,4 +130,16 @@ public abstract class MvpActivity<V extends MvpView, P extends MvpPresenter<V>>
@Override public boolean isRetainingInstance() {
return false;
}

@Override public Object onRetainNonMosbyCustomNonConfigurationInstance() {
return null;
}

@Override public Object onRetainCustomNonConfigurationInstance() {
return getMvpDelegate().onRetainCustomNonConfigurationInstance();
}

@Override public Object getNonMosbyLastCustomNonConfigurationInstance() {
return getMvpDelegate().getNonMosbyLastCustomNonConfigurationInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import android.view.View;
import com.hannesdorfmann.mosby.mvp.delegate.FragmentMvpDelegateImpl;
import com.hannesdorfmann.mosby.mvp.delegate.FragmentMvpDelegate;
import com.hannesdorfmann.mosby.mvp.delegate.MvpDelegateCallback;
import com.hannesdorfmann.mosby.mvp.delegate.BaseMvpDelegateCallback;

/**
* A Fragment that uses an {@link MvpPresenter} to implement a Model-View-Presenter
Expand All @@ -35,7 +35,7 @@
* @since 1.0.0
*/
public abstract class MvpFragment<V extends MvpView, P extends MvpPresenter<V>>
extends Fragment implements MvpDelegateCallback<V, P>, MvpView {
extends Fragment implements BaseMvpDelegateCallback<V, P>, MvpView {

protected FragmentMvpDelegate<V, P> mvpDelegate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;

Expand Down Expand Up @@ -99,4 +100,16 @@ public interface ActivityMvpDelegate<V extends MvpView, P extends MvpPresenter<V
* This method must be called from {@link Activity#onPostCreate(Bundle)}
*/
void onPostCreate(Bundle savedInstanceState);

/**
* This method must be called from {@link FragmentActivity#onRetainCustomNonConfigurationInstance()}
*
* @return Don't forget to return the value returned by this delegate method
*/
Object onRetainCustomNonConfigurationInstance();

/**
* @return the value returned from {@link ActivityMvpDelegateCallback#onRetainNonMosbyCustomNonConfigurationInstance()}
*/
Object getNonMosbyLastCustomNonConfigurationInstance();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2015 Hannes Dorfmann.
*
* 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 com.hannesdorfmann.mosby.mvp.delegate;

import android.support.v4.app.FragmentActivity;
import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;

/**
* The MvpDelegate callback that will be called from {@link
* ActivityMvpDelegate}. This interface must be implemented by all
* Activities that you want to support mosby's mvp.
*
* @author Hannes Dorfmann
* @see BaseMvpDelegateCallback
* @since 2.0.0
*/
public interface ActivityMvpDelegateCallback<V extends MvpView, P extends MvpPresenter<V>>
extends BaseMvpDelegateCallback<V, P> {

/**
* Return any Object holding the desired state to propagate to the next activity instance. Please
* note that mosby internals like the presenter are already saved internally and you don't have
* to take them into account. You can retrieve this value later with {@link
* ActivityMvpDelegate#getNonMosbyLastCustomNonConfigurationInstance()}.
*
* <p>
* This mechanism works pretty the same way as {@link FragmentActivity#onRetainCustomNonConfigurationInstance()}
* and {@link #getNonMosbyLastCustomNonConfigurationInstance()}
* </p>
*
* @return Object holding state.
*/
Object onRetainNonMosbyCustomNonConfigurationInstance();

/**
* @return Return the value previously returned from {@link FragmentActivity#onRetainCustomNonConfigurationInstance()}.
*/
Object getLastCustomNonConfigurationInstance();

/**
* This method should invoke {@link
* ActivityMvpDelegate#getNonMosbyLastCustomNonConfigurationInstance()}.
*
* <p>
* This method is not really a "callback" method (will not invoked from delegate somehow).
* However, it's part of this interface to ensure that no custom implementation will miss this
* method since this method is the counterpart to {@link #onRetainNonMosbyCustomNonConfigurationInstance()}
* </p>
*/
Object getNonMosbyLastCustomNonConfigurationInstance();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public class ActivityMvpDelegateImpl<V extends MvpView, P extends MvpPresenter<V
implements ActivityMvpDelegate {

protected MvpInternalDelegate<V, P> internalDelegate;
protected MvpDelegateCallback<V, P> delegateCallback;
protected ActivityMvpDelegateCallback<V, P> delegateCallback;

public ActivityMvpDelegateImpl(MvpDelegateCallback<V, P> delegateCallback) {
if (delegateCallback == null){
public ActivityMvpDelegateImpl(ActivityMvpDelegateCallback<V, P> delegateCallback) {
if (delegateCallback == null) {
throw new NullPointerException("MvpDelegateCallback is null!");
}
this.delegateCallback = delegateCallback;
Expand All @@ -54,7 +54,16 @@ protected MvpInternalDelegate<V, P> getInternalDelegate() {
}

@Override public void onCreate(Bundle bundle) {
getInternalDelegate().createPresenter();

ActivityMvpNonConfigurationInstances<V, P> nci =
(ActivityMvpNonConfigurationInstances<V, P>) delegateCallback.getLastCustomNonConfigurationInstance();

if (nci != null && nci.presenter != null) {
delegateCallback.setPresenter(nci.presenter);
} else {
getInternalDelegate().createPresenter();
}

getInternalDelegate().attachView();
}

Expand Down Expand Up @@ -93,4 +102,16 @@ protected MvpInternalDelegate<V, P> getInternalDelegate() {
@Override public void onPostCreate(Bundle savedInstanceState) {

}

@Override public Object onRetainCustomNonConfigurationInstance() {

return new ActivityMvpNonConfigurationInstances<>(delegateCallback.getPresenter(),
delegateCallback.onRetainNonMosbyCustomNonConfigurationInstance());
}

@Override public Object getNonMosbyLastCustomNonConfigurationInstance() {
ActivityMvpNonConfigurationInstances last =
(ActivityMvpNonConfigurationInstances) delegateCallback.getLastCustomNonConfigurationInstance();
return last == null ? null : last.nonMosbyCustomConfigurationInstance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2015 Hannes Dorfmann.
*
* 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 com.hannesdorfmann.mosby.mvp.delegate;

import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;

/**
* This kind of class is used in Activities to save the presenter in retaining activities
*
* @author Hannes Dorfmann
* @since 2.0.0
*/
class ActivityMvpNonConfigurationInstances<V extends MvpView, P extends MvpPresenter<V>> {

/**
* The reference to the presenter
*/
P presenter;

/**
* The reference to the custom non configuration.
*/
Object nonMosbyCustomConfigurationInstance;

/**
* Constructor
*
* @param presenter The retaining presenter
* @param nonMosbyCustomConfigurationInstance the other custom object
*/
ActivityMvpNonConfigurationInstances(P presenter, Object nonMosbyCustomConfigurationInstance) {
this.presenter = presenter;
this.nonMosbyCustomConfigurationInstance = nonMosbyCustomConfigurationInstance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
import com.hannesdorfmann.mosby.mvp.MvpView;

/**
* The MvpDelegate callback that will be called from {@link ActivityMvpDelegate} or {@link
* The MvpDelegate callback that will be called from {@link
* FragmentMvpDelegate} or {@link ViewGroupMvpDelegate}. This interface must be implemented by all
* Activity,
* Fragment or android.view.View that you want to support mosbys mvp.y
* Fragment or android.view.View that you want to support mosbys mvp. Please note that Activties need a special callback {@link ActivityMvpDelegateCallback}
*
* @param <V> The type of {@link MvpView}
* @param <P> The type of {@link MvpPresenter}
* @author Hannes Dorfmann
* @since 1.1.0
* @see ActivityMvpDelegateCallback
*/
public interface MvpDelegateCallback<V extends MvpView, P extends MvpPresenter<V>> {
public interface BaseMvpDelegateCallback<V extends MvpView, P extends MvpPresenter<V>> {

/**
* Creates the presenter instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
public class FragmentMvpDelegateImpl<V extends MvpView, P extends MvpPresenter<V>>
implements FragmentMvpDelegate<V, P> {

protected MvpDelegateCallback<V, P> delegateCallback;
protected BaseMvpDelegateCallback<V, P> delegateCallback;
protected MvpInternalDelegate<V, P> internalDelegate;

public FragmentMvpDelegateImpl(MvpDelegateCallback<V, P> delegateCallback) {
public FragmentMvpDelegateImpl(BaseMvpDelegateCallback<V, P> delegateCallback) {
if (delegateCallback == null) {
throw new NullPointerException("MvpDelegateCallback is null!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
*/
class MvpInternalDelegate<V extends MvpView, P extends MvpPresenter<V>> {

protected MvpDelegateCallback<V, P> delegateCallback;
protected BaseMvpDelegateCallback<V, P> delegateCallback;

MvpInternalDelegate(MvpDelegateCallback<V, P> delegateCallback) {
MvpInternalDelegate(BaseMvpDelegateCallback<V, P> delegateCallback) {

if (delegateCallback == null) {
throw new NullPointerException("MvpDelegateCallback is null!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
public class ViewGroupMvpDelegateImpl<V extends MvpView, P extends MvpPresenter<V>>
implements ViewGroupMvpDelegate<V, P> {

protected MvpDelegateCallback<V, P> delegateCallback;
protected BaseMvpDelegateCallback<V, P> delegateCallback;
protected MvpInternalDelegate<V, P> internalDelegate;

public ViewGroupMvpDelegateImpl(MvpDelegateCallback<V, P> delegateCallback) {
public ViewGroupMvpDelegateImpl(BaseMvpDelegateCallback<V, P> delegateCallback) {
if (delegateCallback == null) {
throw new NullPointerException("MvpDelegateCallback is null!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import android.widget.FrameLayout;
import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;
import com.hannesdorfmann.mosby.mvp.delegate.MvpDelegateCallback;
import com.hannesdorfmann.mosby.mvp.delegate.BaseMvpDelegateCallback;
import com.hannesdorfmann.mosby.mvp.delegate.ViewGroupMvpDelegate;
import com.hannesdorfmann.mosby.mvp.delegate.ViewGroupMvpDelegateImpl;

Expand All @@ -33,7 +33,7 @@
* @since 1.1.0
*/
public abstract class MvpFrameLayout<V extends MvpView, P extends MvpPresenter<V>>
extends FrameLayout implements MvpDelegateCallback<V, P>, MvpView {
extends FrameLayout implements BaseMvpDelegateCallback<V, P>, MvpView {

protected P presenter;
protected ViewGroupMvpDelegate<V, P> mvpDelegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import android.widget.LinearLayout;
import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;
import com.hannesdorfmann.mosby.mvp.delegate.MvpDelegateCallback;
import com.hannesdorfmann.mosby.mvp.delegate.BaseMvpDelegateCallback;
import com.hannesdorfmann.mosby.mvp.delegate.ViewGroupMvpDelegate;
import com.hannesdorfmann.mosby.mvp.delegate.ViewGroupMvpDelegateImpl;

Expand All @@ -33,7 +33,7 @@
* @since 1.1
*/
public abstract class MvpLinearLayout<V extends MvpView, P extends MvpPresenter<V>>
extends LinearLayout implements MvpView, MvpDelegateCallback<V, P> {
extends LinearLayout implements MvpView, BaseMvpDelegateCallback<V, P> {

protected P presenter;
protected ViewGroupMvpDelegate<V, P> mvpDelegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import android.widget.RelativeLayout;
import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;
import com.hannesdorfmann.mosby.mvp.delegate.MvpDelegateCallback;
import com.hannesdorfmann.mosby.mvp.delegate.BaseMvpDelegateCallback;
import com.hannesdorfmann.mosby.mvp.delegate.ViewGroupMvpDelegate;
import com.hannesdorfmann.mosby.mvp.delegate.ViewGroupMvpDelegateImpl;

Expand All @@ -33,7 +33,7 @@
* @since 1.1
*/
public abstract class MvpRelativeLayout<V extends MvpView, P extends MvpPresenter<V>>
extends RelativeLayout implements MvpView, MvpDelegateCallback<V, P> {
extends RelativeLayout implements MvpView, BaseMvpDelegateCallback<V, P> {

protected P presenter;
protected ViewGroupMvpDelegate<V, P> mvpDelegate;
Expand Down
Loading

0 comments on commit 90704a8

Please sign in to comment.