diff --git a/build.gradle b/build.gradle index df38058..0dece1f 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.1.2' } } diff --git a/navi/src/main/java/com/trello/navi/Event.java b/navi/src/main/java/com/trello/navi/Event.java index 21c2c86..6a681c9 100644 --- a/navi/src/main/java/com/trello/navi/Event.java +++ b/navi/src/main/java/com/trello/navi/Event.java @@ -8,10 +8,12 @@ import android.os.Bundle; import android.os.PersistableBundle; import android.view.LayoutInflater; +import android.view.View; import android.view.ViewGroup; import com.trello.navi.model.ActivityResult; import com.trello.navi.model.BundleBundle; import com.trello.navi.model.RequestPermissionsResult; +import com.trello.navi.model.ViewCreated; /** * Represents an event that can be listened to in an Activity or Fragment. @@ -154,6 +156,11 @@ public final class Event { */ public static final Event CREATE_VIEW = new Event<>(Type.CREATE_VIEW, Bundle.class); + /** + * Emits {@link Fragment#onViewCreated(View, Bundle)} ()}. Emitted before super(). + */ + public static final Event VIEW_CREATED = new Event<>(Type.VIEW_CREATED, ViewCreated.class); + /** * Emits {@link Fragment#onActivityCreated(Bundle)}. Emitted after super(). */ @@ -242,6 +249,7 @@ public enum Type { // Fragment-only ATTACH, CREATE_VIEW, + VIEW_CREATED, ACTIVITY_CREATED, VIEW_STATE_RESTORED, DESTROY_VIEW, diff --git a/navi/src/main/java/com/trello/navi/component/NaviDialogFragment.java b/navi/src/main/java/com/trello/navi/component/NaviDialogFragment.java index cc4458b..c41042d 100644 --- a/navi/src/main/java/com/trello/navi/component/NaviDialogFragment.java +++ b/navi/src/main/java/com/trello/navi/component/NaviDialogFragment.java @@ -16,6 +16,7 @@ import com.trello.navi.Listener; import com.trello.navi.NaviComponent; import com.trello.navi.internal.NaviEmitter; +import com.trello.navi.model.ViewCreated; public class NaviDialogFragment extends DialogFragment implements NaviComponent { @@ -54,6 +55,11 @@ public class NaviDialogFragment extends DialogFragment implements NaviComponent return super.onCreateView(inflater, container, savedInstanceState); } + @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + base.onViewCreated(new ViewCreated(view, savedInstanceState)); + super.onViewCreated(view, savedInstanceState); + } + @Override @CallSuper public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); base.onActivityCreated(savedInstanceState); diff --git a/navi/src/main/java/com/trello/navi/component/NaviFragment.java b/navi/src/main/java/com/trello/navi/component/NaviFragment.java index 37bf0c4..20a3cf2 100644 --- a/navi/src/main/java/com/trello/navi/component/NaviFragment.java +++ b/navi/src/main/java/com/trello/navi/component/NaviFragment.java @@ -16,6 +16,7 @@ import com.trello.navi.Listener; import com.trello.navi.NaviComponent; import com.trello.navi.internal.NaviEmitter; +import com.trello.navi.model.ViewCreated; public class NaviFragment extends Fragment implements NaviComponent { @@ -54,6 +55,11 @@ public class NaviFragment extends Fragment implements NaviComponent { return super.onCreateView(inflater, container, savedInstanceState); } + @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + base.onViewCreated(new ViewCreated(view, savedInstanceState)); + super.onViewCreated(view, savedInstanceState); + } + @Override @CallSuper public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); base.onActivityCreated(savedInstanceState); diff --git a/navi/src/main/java/com/trello/navi/component/support/NaviAppCompatDialogFragment.java b/navi/src/main/java/com/trello/navi/component/support/NaviAppCompatDialogFragment.java index fd3535b..3c9eb57 100644 --- a/navi/src/main/java/com/trello/navi/component/support/NaviAppCompatDialogFragment.java +++ b/navi/src/main/java/com/trello/navi/component/support/NaviAppCompatDialogFragment.java @@ -16,6 +16,7 @@ import com.trello.navi.Listener; import com.trello.navi.NaviComponent; import com.trello.navi.internal.NaviEmitter; +import com.trello.navi.model.ViewCreated; public class NaviAppCompatDialogFragment extends DialogFragment implements NaviComponent { @@ -54,6 +55,11 @@ public class NaviAppCompatDialogFragment extends DialogFragment implements NaviC return super.onCreateView(inflater, container, savedInstanceState); } + @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + base.onViewCreated(new ViewCreated(view, savedInstanceState)); + super.onViewCreated(view, savedInstanceState); + } + @Override @CallSuper public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); base.onActivityCreated(savedInstanceState); diff --git a/navi/src/main/java/com/trello/navi/component/support/NaviDialogFragment.java b/navi/src/main/java/com/trello/navi/component/support/NaviDialogFragment.java index 7c6b6bc..3cf3245 100644 --- a/navi/src/main/java/com/trello/navi/component/support/NaviDialogFragment.java +++ b/navi/src/main/java/com/trello/navi/component/support/NaviDialogFragment.java @@ -16,6 +16,7 @@ import com.trello.navi.Listener; import com.trello.navi.NaviComponent; import com.trello.navi.internal.NaviEmitter; +import com.trello.navi.model.ViewCreated; public class NaviDialogFragment extends DialogFragment implements NaviComponent { @@ -54,6 +55,11 @@ public class NaviDialogFragment extends DialogFragment implements NaviComponent return super.onCreateView(inflater, container, savedInstanceState); } + @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + base.onViewCreated(new ViewCreated(view, savedInstanceState)); + super.onViewCreated(view, savedInstanceState); + } + @Override @CallSuper public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); base.onActivityCreated(savedInstanceState); diff --git a/navi/src/main/java/com/trello/navi/component/support/NaviFragment.java b/navi/src/main/java/com/trello/navi/component/support/NaviFragment.java index fda45b7..458d309 100644 --- a/navi/src/main/java/com/trello/navi/component/support/NaviFragment.java +++ b/navi/src/main/java/com/trello/navi/component/support/NaviFragment.java @@ -16,6 +16,7 @@ import com.trello.navi.Listener; import com.trello.navi.NaviComponent; import com.trello.navi.internal.NaviEmitter; +import com.trello.navi.model.ViewCreated; public class NaviFragment extends Fragment implements NaviComponent { @@ -54,6 +55,11 @@ public class NaviFragment extends Fragment implements NaviComponent { return super.onCreateView(inflater, container, savedInstanceState); } + @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + base.onViewCreated(new ViewCreated(view, savedInstanceState)); + super.onViewCreated(view, savedInstanceState); + } + @Override @CallSuper public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); base.onActivityCreated(savedInstanceState); @@ -115,7 +121,7 @@ public class NaviFragment extends Fragment implements NaviComponent { } @Override @CallSuper public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, - @NonNull int[] grantResults) { + @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); base.onRequestPermissionsResult(requestCode, permissions, grantResults); } diff --git a/navi/src/main/java/com/trello/navi/internal/HandledEvents.java b/navi/src/main/java/com/trello/navi/internal/HandledEvents.java index 6bf470e..aca0d25 100644 --- a/navi/src/main/java/com/trello/navi/internal/HandledEvents.java +++ b/navi/src/main/java/com/trello/navi/internal/HandledEvents.java @@ -37,6 +37,7 @@ final class HandledEvents { Event.ATTACH, Event.CREATE, Event.CREATE_VIEW, + Event.VIEW_CREATED, Event.ACTIVITY_CREATED, Event.VIEW_STATE_RESTORED, Event.START, diff --git a/navi/src/main/java/com/trello/navi/internal/NaviEmitter.java b/navi/src/main/java/com/trello/navi/internal/NaviEmitter.java index 0f2315b..10e4e03 100644 --- a/navi/src/main/java/com/trello/navi/internal/NaviEmitter.java +++ b/navi/src/main/java/com/trello/navi/internal/NaviEmitter.java @@ -8,6 +8,9 @@ import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.View; + import com.trello.navi.Event; import com.trello.navi.Event.Type; import com.trello.navi.Listener; @@ -15,6 +18,8 @@ import com.trello.navi.model.ActivityResult; import com.trello.navi.model.BundleBundle; import com.trello.navi.model.RequestPermissionsResult; +import com.trello.navi.model.ViewCreated; + import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -174,6 +179,10 @@ public void onCreateView(Bundle savedInstanceState) { emitEvent(Event.CREATE_VIEW, savedInstanceState); } + public void onViewCreated(ViewCreated viewCreated) { + emitEvent(Event.VIEW_CREATED, viewCreated); + } + public void onDestroy() { emitEvent(Event.DESTROY); } diff --git a/navi/src/main/java/com/trello/navi/model/ViewCreated.java b/navi/src/main/java/com/trello/navi/model/ViewCreated.java new file mode 100644 index 0000000..8f1d472 --- /dev/null +++ b/navi/src/main/java/com/trello/navi/model/ViewCreated.java @@ -0,0 +1,48 @@ +package com.trello.navi.model; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.view.View; + +public class ViewCreated { + private final Bundle bundle; + private final View view; + + public ViewCreated(View view, @Nullable Bundle bundle) { + this.view = view; + this.bundle = bundle; + } + + public View view() { + return view; + } + + @Nullable + public Bundle bundle() { + return bundle; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ViewCreated that = (ViewCreated) o; + if (bundle != null ? !bundle.equals(that.bundle) : that.bundle != null) return false; + return view.equals(that.view); + } + + @Override + public int hashCode() { + int result = bundle != null ? bundle.hashCode() : 0; + result = 31 * result + view.hashCode(); + return result; + } + + @Override + public String toString() { + return "ViewCreated{" + + "bundle=" + bundle + + ", view=" + view + + '}'; + } +} diff --git a/navi/src/test/java/com/trello/navi/NaviActivityTest.java b/navi/src/test/java/com/trello/navi/NaviActivityTest.java index c55d72a..5ca1909 100644 --- a/navi/src/test/java/com/trello/navi/NaviActivityTest.java +++ b/navi/src/test/java/com/trello/navi/NaviActivityTest.java @@ -296,6 +296,11 @@ public final class NaviActivityTest { emitter.addListener(Event.CREATE_VIEW, mock(Listener.class)); } + @Test public void viewCreatedListener() { + exception.expect(IllegalArgumentException.class); + emitter.addListener(Event.VIEW_CREATED, mock(Listener.class)); + } + @Test public void activityCreatedListener() { exception.expect(IllegalArgumentException.class); emitter.addListener(Event.ACTIVITY_CREATED, mock(Listener.class)); diff --git a/navi/src/test/java/com/trello/navi/NaviFragmentTest.java b/navi/src/test/java/com/trello/navi/NaviFragmentTest.java index 56a2f99..ff2ba49 100644 --- a/navi/src/test/java/com/trello/navi/NaviFragmentTest.java +++ b/navi/src/test/java/com/trello/navi/NaviFragmentTest.java @@ -5,9 +5,13 @@ import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; +import android.view.View; + import com.trello.navi.internal.NaviEmitter; import com.trello.navi.model.ActivityResult; import com.trello.navi.model.RequestPermissionsResult; +import com.trello.navi.model.ViewCreated; + import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -79,6 +83,19 @@ public final class NaviFragmentTest { verifyNoMoreInteractions(listener); } + @Test public void onViewCreatedListener() { + Listener listener = mock(Listener.class); + emitter.addListener(Event.VIEW_CREATED, listener); + + ViewCreated result = mock(ViewCreated.class); + emitter.onViewCreated(result); + verify(listener).call(result); + + emitter.removeListener(listener); + emitter.onCreate(new Bundle()); + verifyNoMoreInteractions(listener); + } + @Test public void activityCreatedListener() { Listener listener = mock(Listener.class); emitter.addListener(Event.ACTIVITY_CREATED, listener);