From 9d3c3c8c81485120656201a43220b27e1bdcb31a Mon Sep 17 00:00:00 2001 From: Yuri Shmakov Date: Tue, 29 Nov 2016 19:57:48 +0700 Subject: [PATCH 1/3] [fixbug] Destroy view state if Fragment's onDestroyView was being called --- .../com/arellomobile/mvp/MvpFragment.java | 21 ++++++++++--------- .../src/main/java/android/app/Fragment.java | 11 ++++------ .../mvp/MvpAppCompatFragment.java | 20 +++++++++--------- .../java/android/support/v4/app/Fragment.java | 11 ++++------ .../com/arellomobile/mvp/MvpDelegate.java | 13 ++++++++++++ .../com/arellomobile/mvp/MvpPresenter.java | 6 ++++++ .../mvp/viewstate/MvpViewState.java | 4 ++++ 7 files changed, 52 insertions(+), 34 deletions(-) diff --git a/moxy-android/src/main/java/com/arellomobile/mvp/MvpFragment.java b/moxy-android/src/main/java/com/arellomobile/mvp/MvpFragment.java index 6093dde4..6ef71e70 100644 --- a/moxy-android/src/main/java/com/arellomobile/mvp/MvpFragment.java +++ b/moxy-android/src/main/java/com/arellomobile/mvp/MvpFragment.java @@ -3,9 +3,7 @@ import android.app.Fragment; import android.os.Build; import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; +import android.util.Log; /** * Date: 19-Dec-15 @@ -27,16 +25,11 @@ public void onCreate(Bundle savedInstanceState) { getMvpDelegate().onCreate(savedInstanceState); } - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - mIsStateSaved = false; - - return super.onCreateView(inflater, container, savedInstanceState); - } - public void onResume() { super.onResume(); + mIsStateSaved = false; + getMvpDelegate().onAttach(); } @@ -56,6 +49,14 @@ public void onStop() { getMvpDelegate().onDetach(); } + @Override + public void onDestroyView() { + super.onDestroyView(); + + getMvpDelegate().onDetach(); + getMvpDelegate().onDestroyView(); + } + @Override public void onDestroy() { super.onDestroy(); diff --git a/moxy-android/stub-android/src/main/java/android/app/Fragment.java b/moxy-android/stub-android/src/main/java/android/app/Fragment.java index 405a233a..cd4612ba 100644 --- a/moxy-android/stub-android/src/main/java/android/app/Fragment.java +++ b/moxy-android/stub-android/src/main/java/android/app/Fragment.java @@ -1,9 +1,6 @@ package android.app; import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; /** * Created by senneco on 11.10.2016 @@ -11,10 +8,6 @@ public class Fragment { - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - throw new RuntimeException("Stub!"); - } - public void onCreate(Bundle savedInstanceState) { throw new RuntimeException("Stub!"); } @@ -31,6 +24,10 @@ public void onStop() { throw new RuntimeException("Stub!"); } + public void onDestroyView() { + throw new RuntimeException("Stub!"); + } + public void onDestroy() { throw new RuntimeException("Stub!"); } diff --git a/moxy-app-compat/src/main/java/com/arellomobile/mvp/MvpAppCompatFragment.java b/moxy-app-compat/src/main/java/com/arellomobile/mvp/MvpAppCompatFragment.java index 4c2d5bb8..df61bfe1 100644 --- a/moxy-app-compat/src/main/java/com/arellomobile/mvp/MvpAppCompatFragment.java +++ b/moxy-app-compat/src/main/java/com/arellomobile/mvp/MvpAppCompatFragment.java @@ -2,9 +2,6 @@ import android.os.Bundle; import android.support.v4.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; /** * Date: 19-Dec-15 @@ -26,16 +23,11 @@ public void onCreate(Bundle savedInstanceState) { getMvpDelegate().onCreate(savedInstanceState); } - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - mIsStateSaved = false; - - return super.onCreateView(inflater, container, savedInstanceState); - } - public void onResume() { super.onResume(); + mIsStateSaved = false; + getMvpDelegate().onAttach(); } @@ -55,6 +47,14 @@ public void onStop() { getMvpDelegate().onDetach(); } + @Override + public void onDestroyView() { + super.onDestroyView(); + + getMvpDelegate().onDetach(); + getMvpDelegate().onDestroyView(); + } + @Override public void onDestroy() { super.onDestroy(); diff --git a/moxy-app-compat/stub-appcompat/src/main/java/android/support/v4/app/Fragment.java b/moxy-app-compat/stub-appcompat/src/main/java/android/support/v4/app/Fragment.java index cbf32188..55fd5a95 100644 --- a/moxy-app-compat/stub-appcompat/src/main/java/android/support/v4/app/Fragment.java +++ b/moxy-app-compat/stub-appcompat/src/main/java/android/support/v4/app/Fragment.java @@ -1,9 +1,6 @@ package android.support.v4.app; import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; /** * Date: 30-Sen-16 @@ -12,10 +9,6 @@ * @author Yuri Shmakov */ public class Fragment { - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - throw new RuntimeException("Stub!"); - } - public void onCreate(Bundle savedInstanceState) { throw new RuntimeException("Stub!"); } @@ -32,6 +25,10 @@ public void onStop() { throw new RuntimeException("Stub!"); } + public void onDestroyView() { + throw new RuntimeException("Stub!"); + } + public void onDestroy() { throw new RuntimeException("Stub!"); } diff --git a/moxy/src/main/java/com/arellomobile/mvp/MvpDelegate.java b/moxy/src/main/java/com/arellomobile/mvp/MvpDelegate.java index 65bac02d..52bf5c64 100644 --- a/moxy/src/main/java/com/arellomobile/mvp/MvpDelegate.java +++ b/moxy/src/main/java/com/arellomobile/mvp/MvpDelegate.java @@ -143,6 +143,19 @@ public void onDetach() { } } + /** + *

View was being destroyed, but logical unit still alive

+ */ + public void onDestroyView() { + for (MvpPresenter presenter : mPresenters) { + presenter.destroyView(mDelegated); + } + + for (MvpDelegate childDelegate : mChildDelegates) { + childDelegate.onDestroyView(); + } + } + /** *

Destroy presenters.

*/ diff --git a/moxy/src/main/java/com/arellomobile/mvp/MvpPresenter.java b/moxy/src/main/java/com/arellomobile/mvp/MvpPresenter.java index c825f635..05819964 100644 --- a/moxy/src/main/java/com/arellomobile/mvp/MvpPresenter.java +++ b/moxy/src/main/java/com/arellomobile/mvp/MvpPresenter.java @@ -75,6 +75,12 @@ public void detachView(View 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) */ diff --git a/moxy/src/main/java/com/arellomobile/mvp/viewstate/MvpViewState.java b/moxy/src/main/java/com/arellomobile/mvp/viewstate/MvpViewState.java index 24b74a48..33b07d4f 100644 --- a/moxy/src/main/java/com/arellomobile/mvp/viewstate/MvpViewState.java +++ b/moxy/src/main/java/com/arellomobile/mvp/viewstate/MvpViewState.java @@ -76,6 +76,10 @@ public void detachView(View view) { mInRestoreState.remove(view); } + public void destroyView(View view) { + mViewStates.remove(view); + } + /** * @param view target view * @return commands that was applied already From 75ca071c0ded887c0a53ed14484e63ee57660a1e Mon Sep 17 00:00:00 2001 From: Yuri Shmakov Date: Tue, 29 Nov 2016 19:58:35 +0700 Subject: [PATCH 2/3] [artifact] Sample was updated --- .../presenters/RepositoryWidgetPresenter.java | 62 +++++++++++++++++++ .../ui/adapters/RepositoriesAdapter.java | 1 + .../github/ui/views/RepositoryWidget.java | 16 ++--- 3 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 sample-github/src/main/java/com/arellomobile/mvp/sample/github/mvp/presenters/RepositoryWidgetPresenter.java diff --git a/sample-github/src/main/java/com/arellomobile/mvp/sample/github/mvp/presenters/RepositoryWidgetPresenter.java b/sample-github/src/main/java/com/arellomobile/mvp/sample/github/mvp/presenters/RepositoryWidgetPresenter.java new file mode 100644 index 00000000..6e24894a --- /dev/null +++ b/sample-github/src/main/java/com/arellomobile/mvp/sample/github/mvp/presenters/RepositoryWidgetPresenter.java @@ -0,0 +1,62 @@ +package com.arellomobile.mvp.sample.github.mvp.presenters; + +import java.util.List; + +import android.util.Log; + +import com.arellomobile.mvp.InjectViewState; +import com.arellomobile.mvp.MvpPresenter; +import com.arellomobile.mvp.sample.github.mvp.models.Repository; +import com.arellomobile.mvp.sample.github.mvp.views.RepositoryView; + +/** + * Date: 27.01.2016 + * Time: 21:12 + * + * @author Yuri Shmakov + */ +@InjectViewState +public class RepositoryWidgetPresenter extends MvpPresenter { + private boolean mIsInitialized = false; + private Repository mRepository; + private List mInProgress; + private List mLikedIds; + + public RepositoryWidgetPresenter() { + super(); + + Log.i("***-***", "Create new presenter"); + } + + public void setRepository(Repository repository) { + if (mIsInitialized) { + return; + } + mIsInitialized = true; + + mRepository = repository; + + getViewState().showRepository(repository); + + updateLikes(mInProgress, mLikedIds); + } + + public void updateLikes(List inProgress, List likedIds) { + mInProgress = inProgress; + mLikedIds = likedIds; + + if (mRepository == null || mInProgress == null || mLikedIds == null) { + return; + } + + boolean isInProgress = inProgress.contains(mRepository.getId()); + boolean isLiked = likedIds.contains(mRepository.getId()); + + getViewState().updateLike(isInProgress, isLiked); + } + + @Override + public void onDestroy() { + super.onDestroy(); + } +} diff --git a/sample-github/src/main/java/com/arellomobile/mvp/sample/github/ui/adapters/RepositoriesAdapter.java b/sample-github/src/main/java/com/arellomobile/mvp/sample/github/ui/adapters/RepositoriesAdapter.java index f418421a..c354771d 100644 --- a/sample-github/src/main/java/com/arellomobile/mvp/sample/github/ui/adapters/RepositoriesAdapter.java +++ b/sample-github/src/main/java/com/arellomobile/mvp/sample/github/ui/adapters/RepositoriesAdapter.java @@ -175,6 +175,7 @@ void bind(int position, Repository repository) { if (getMvpDelegate() != null) { getMvpDelegate().onSaveInstanceState(getMvpDelegate().getChildrenSaveState()); getMvpDelegate().onDetach(); + getMvpDelegate().onDestroyView(); mMvpDelegate = null; } diff --git a/sample-github/src/main/java/com/arellomobile/mvp/sample/github/ui/views/RepositoryWidget.java b/sample-github/src/main/java/com/arellomobile/mvp/sample/github/ui/views/RepositoryWidget.java index a5de508c..457659f9 100644 --- a/sample-github/src/main/java/com/arellomobile/mvp/sample/github/ui/views/RepositoryWidget.java +++ b/sample-github/src/main/java/com/arellomobile/mvp/sample/github/ui/views/RepositoryWidget.java @@ -10,7 +10,7 @@ import com.arellomobile.mvp.presenter.InjectPresenter; import com.arellomobile.mvp.presenter.ProvidePresenter; import com.arellomobile.mvp.sample.github.mvp.models.Repository; -import com.arellomobile.mvp.sample.github.mvp.presenters.RepositoryPresenter; +import com.arellomobile.mvp.sample.github.mvp.presenters.RepositoryWidgetPresenter; import com.arellomobile.mvp.sample.github.mvp.views.RepositoryView; /** @@ -28,7 +28,7 @@ public class RepositoryWidget extends TextView implements RepositoryView { private Repository mRepository; @InjectPresenter - RepositoryPresenter mRepositoryPresenter; + RepositoryWidgetPresenter mRepositoryPresenter; public RepositoryWidget(Context context) { super(context); @@ -48,8 +48,8 @@ public RepositoryWidget(Context context, AttributeSet attrs, int defStyleAttr, i } @ProvidePresenter - RepositoryPresenter provideRepositoryPresenter() { - return new RepositoryPresenter(); + RepositoryWidgetPresenter provideRepositoryPresenter() { + return new RepositoryWidgetPresenter(); } public void setRepository(MvpDelegate parentDelegate, Repository repository) { @@ -62,14 +62,6 @@ public void setRepository(MvpDelegate parentDelegate, Repository repository) { mRepositoryPresenter.setRepository(mRepository); } - @Override - protected void onDetachedFromWindow() { - super.onDetachedFromWindow(); - - getMvpDelegate().onSaveInstanceState(); - getMvpDelegate().onDetach(); - } - public MvpDelegate getMvpDelegate() { if (mMvpDelegate == null) { mMvpDelegate = new MvpDelegate<>(this); From 9598e733181df84a1c466a79d5e6f2d4f942dc38 Mon Sep 17 00:00:00 2001 From: Yuri Shmakov Date: Tue, 29 Nov 2016 20:34:37 +0700 Subject: [PATCH 3/3] [artifact] Prepare version 1.3.2 --- README.md | 12 ++++++------ build.gradle | 4 ++-- sample-github/build.gradle | 6 +++--- sample-kotlin/build.gradle | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index b5fe63b3..0d486676 100644 --- a/README.md +++ b/README.md @@ -75,29 +75,29 @@ Base modules integration: ```groovy dependencies { ... - compile 'com.arello-mobile:moxy:1.3.1' - provided 'com.arello-mobile:moxy-compiler:1.3.1' + compile 'com.arello-mobile:moxy:1.3.2' + provided 'com.arello-mobile:moxy-compiler:1.3.2' } ``` If you want to see generated code, use `apt` instead of `provided` dependency type: ```groovy dependencies { ... - apt 'com.arello-mobile:moxy-compiler:1.3.1' + apt 'com.arello-mobile:moxy-compiler:1.3.2' } ``` For additional base view classes `MvpActivity` and `MvpFragment` add this: ```groovy dependencies { ... - compile 'com.arello-mobile:moxy-android:1.3.1' + compile 'com.arello-mobile:moxy-android:1.3.2' } ``` If you planing to use AppCompat, then you can use `MvpAppCompatActivity` and `MvpAppCompatFragment`. Then add this: ```groovy dependencies { ... - compile 'com.arello-mobile:moxy-app-compat:1.3.1' + compile 'com.arello-mobile:moxy-app-compat:1.3.2' compile 'com.android.support:appcompat-v7:$support_version' } ``` @@ -106,7 +106,7 @@ If you are using kotlin, use `kapt` instead of `provided`/`apt` dependency type ```groovy dependencies { ... - kapt 'com.arello-mobile:moxy-compiler:1.3.1' + kapt 'com.arello-mobile:moxy-compiler:1.3.2' } kapt { generateStubs = true diff --git a/build.gradle b/build.gradle index da916518..b6011d21 100644 --- a/build.gradle +++ b/build.gradle @@ -20,8 +20,8 @@ allprojects { } ext { - targetVersionCode = 30 - targetVersionName = "1.3.1" + targetVersionCode = 31 + targetVersionName = "1.3.2" } task clean(type: Delete) { diff --git a/sample-github/build.gradle b/sample-github/build.gradle index cfd756c4..3d2aba89 100644 --- a/sample-github/build.gradle +++ b/sample-github/build.gradle @@ -69,7 +69,7 @@ dependencies { testCompile "org.hamcrest:hamcrest-all:1.3" testCompile "org.robolectric:robolectric:3.1-rc1" - compile 'com.arello-mobile:moxy:1.3.1' - compile 'com.arello-mobile:moxy-app-compat:1.3.1' - apt 'com.arello-mobile:moxy-compiler:1.3.1' + compile 'com.arello-mobile:moxy:1.3.2' + compile 'com.arello-mobile:moxy-app-compat:1.3.2' + apt 'com.arello-mobile:moxy-compiler:1.3.2' } \ No newline at end of file diff --git a/sample-kotlin/build.gradle b/sample-kotlin/build.gradle index 2a24d319..d676c4e8 100644 --- a/sample-kotlin/build.gradle +++ b/sample-kotlin/build.gradle @@ -40,9 +40,9 @@ android { dependencies { compile 'com.android.support:appcompat-v7:25.0.0' - compile 'com.arello-mobile:moxy:1.3.1' - compile 'com.arello-mobile:moxy-app-compat:1.3.1' - kapt 'com.arello-mobile:moxy-compiler:1.3.1' + compile 'com.arello-mobile:moxy:1.3.2' + compile 'com.arello-mobile:moxy-app-compat:1.3.2' + kapt 'com.arello-mobile:moxy-compiler:1.3.2' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" }