Skip to content
This repository has been archived by the owner on May 23, 2020. It is now read-only.

Support new event in fragment #57

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
8 changes: 8 additions & 0 deletions navi/src/main/java/com/trello/navi/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -154,6 +156,11 @@ public final class Event<T> {
*/
public static final Event<Bundle> CREATE_VIEW = new Event<>(Type.CREATE_VIEW, Bundle.class);

/**
* Emits {@link Fragment#onViewCreated(View, Bundle)} ()}. Emitted before super().
*/
public static final Event<ViewCreated> VIEW_CREATED = new Event<>(Type.VIEW_CREATED, ViewCreated.class);

/**
* Emits {@link Fragment#onActivityCreated(Bundle)}. Emitted after super().
*/
Expand Down Expand Up @@ -242,6 +249,7 @@ public enum Type {
// Fragment-only
ATTACH,
CREATE_VIEW,
VIEW_CREATED,
ACTIVITY_CREATED,
VIEW_STATE_RESTORED,
DESTROY_VIEW,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions navi/src/main/java/com/trello/navi/internal/NaviEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
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;
import com.trello.navi.NaviComponent;
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;
Expand Down Expand Up @@ -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);
}
Expand Down
48 changes: 48 additions & 0 deletions navi/src/main/java/com/trello/navi/model/ViewCreated.java
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Kotlin:
data class ViewCreated(val view: View, val bundle: Bundle)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be some newlines between stuff, to match formatting of the other models.

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 +
'}';
}
}
5 changes: 5 additions & 0 deletions navi/src/test/java/com/trello/navi/NaviActivityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
17 changes: 17 additions & 0 deletions navi/src/test/java/com/trello/navi/NaviFragmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,6 +83,19 @@ public final class NaviFragmentTest {
verifyNoMoreInteractions(listener);
}

@Test public void onViewCreatedListener() {
Listener<ViewCreated> 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<Bundle> listener = mock(Listener.class);
emitter.addListener(Event.ACTIVITY_CREATED, listener);
Expand Down