From d1ffa0db4d8f206bed259ae7310ea6101213dc00 Mon Sep 17 00:00:00 2001 From: Gavriil Sitnikov Date: Thu, 8 Sep 2016 18:08:37 +0300 Subject: [PATCH] appearance added --- .../components/navigation/ViewController.java | 16 ++++++ .../fragments/ViewControllerFragment.java | 22 +++++++-- .../navigation/fragments/ViewFragment.java | 49 ++++++++++++++++++- 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/touchin/roboswag/components/navigation/ViewController.java b/src/main/java/ru/touchin/roboswag/components/navigation/ViewController.java index 89b971c..3f54c83 100644 --- a/src/main/java/ru/touchin/roboswag/components/navigation/ViewController.java +++ b/src/main/java/ru/touchin/roboswag/components/navigation/ViewController.java @@ -158,6 +158,14 @@ public class ViewController, baseLifecycleBindable.onStart(); } + /** + * Called when fragment is moved in started state and it's {@link #getFragment().isMenuVisible()} sets to true. + * Usually it is indicating that user can't see fragment on screen and useful to track analytics events. + */ + public void onAppear() { + //do nothing + } + /** * Calls when {@link ViewController} have resumed. * Happens at {@link ViewControllerFragment#onResume(View, ViewControllerActivity)}. @@ -195,6 +203,14 @@ public class ViewController, // do nothing } + /** + * Called when fragment is moved in stopped state or it's {@link #getFragment().isMenuVisible()} sets to false. + * Usually it is indicating that user can't see fragment on screen and useful to track analytics events. + */ + public void onDisappear() { + //do nothing + } + /** * Calls when {@link ViewController} have stopped. * Happens at {@link ViewControllerFragment#onStop(View, ViewControllerActivity)}. diff --git a/src/main/java/ru/touchin/roboswag/components/navigation/fragments/ViewControllerFragment.java b/src/main/java/ru/touchin/roboswag/components/navigation/fragments/ViewControllerFragment.java index 842b346..daa06f5 100644 --- a/src/main/java/ru/touchin/roboswag/components/navigation/fragments/ViewControllerFragment.java +++ b/src/main/java/ru/touchin/roboswag/components/navigation/fragments/ViewControllerFragment.java @@ -214,6 +214,14 @@ public abstract class ViewControllerFragment extends Fragment implements OnFragmentStartedListener { + private boolean appeared; + private boolean started; + /** * Returns if fragment have parent fragment. * @@ -120,6 +123,7 @@ public abstract class ViewFragment extends @Override public void onStart() { super.onStart(); + started = true; callMethodAfterInstantiation(this::onStart); } @@ -136,6 +140,21 @@ public abstract class ViewFragment extends } else if (activity instanceof OnFragmentStartedListener) { ((OnFragmentStartedListener) activity).onFragmentStarted(this); } + if (!appeared && isMenuVisible()) { + onAppear(view, activity); + appeared = true; + } + } + + /** + * Called when fragment is moved in started state and it's {@link #isMenuVisible()} sets to true. + * Usually it is indicating that user can't see fragment on screen and useful to track analytics events. + * + * @param view Instantiated view. + * @param activity Activity which fragment attached to. + */ + protected void onAppear(@NonNull final View view, @NonNull final TActivity activity) { + //do nothing } @Deprecated @@ -156,6 +175,19 @@ public abstract class ViewFragment extends //do nothing } + @Override + public void setMenuVisibility(final boolean menuVisible) { + super.setMenuVisibility(menuVisible); + if (getBaseActivity() != null && getView() != null) { + if (!appeared && menuVisible && started) { + onAppear(getView(), getBaseActivity()); + } + if (appeared && (!menuVisible || !started)) { + onDisappear(getView(), getBaseActivity()); + } + } + } + @Deprecated @Override public void onPause() { @@ -171,12 +203,24 @@ public abstract class ViewFragment extends */ @CallSuper protected void onPause(@NonNull final View view, @NonNull final TActivity activity) { + // do nothing + } + + /** + * Called when fragment is moved in stopped state or it's {@link #isMenuVisible()} sets to false. + * Usually it is indicating that user can't see fragment on screen and useful to track analytics events. + * + * @param view Instantiated view. + * @param activity Activity which fragment attached to. + */ + protected void onDisappear(@NonNull final View view, @NonNull final TActivity activity) { //do nothing } @Deprecated @Override public void onStop() { + started = false; callMethodAfterInstantiation(this::onStop); super.onStop(); } @@ -189,7 +233,10 @@ public abstract class ViewFragment extends */ @CallSuper protected void onStop(@NonNull final View view, @NonNull final TActivity activity) { - //do nothing + if (appeared) { + onDisappear(view, activity); + appeared = false; + } } @Deprecated