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

Commit

Permalink
Merge pull request #62 from elsennov/master
Browse files Browse the repository at this point in the history
Add onPostCreate event for activity only
  • Loading branch information
dlew authored Aug 5, 2016
2 parents ac59bf1 + d592edb commit 5dc0895
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 0 deletions.
13 changes: 13 additions & 0 deletions navi/src/main/java/com/trello/navi/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public final class Event<T> {
*/
public static final Event<Void> START = new Event<>(Type.START, Void.class);

/**
* Emits {@link Activity#onPostCreate(Bundle)}. Emitted after super().
*/
public static final Event<Bundle> POST_CREATE = new Event<>(Type.POST_CREATE, Bundle.class);

/**
* Emits {@link Activity#onCreate(Bundle, PersistableBundle)}. Emitted after super().
*/
public static final Event<BundleBundle> POST_CREATE_PERSISTABLE =
new Event<>(Type.POST_CREATE_PERSISTABLE, BundleBundle.class);

/**
* Emits {@link Activity#onResume()} and {@link Fragment#onResume()}. Emitted after super().
*/
Expand Down Expand Up @@ -237,6 +248,8 @@ public enum Type {

// Activity-only
CREATE_PERSISTABLE,
POST_CREATE,
POST_CREATE_PERSISTABLE,
RESTART,
SAVE_INSTANCE_STATE_PERSISTABLE,
RESTORE_INSTANCE_STATE,
Expand Down
10 changes: 10 additions & 0 deletions navi/src/main/java/com/trello/navi/component/NaviActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public class NaviActivity extends Activity implements NaviComponent {
base.onStart();
}

@Override @CallSuper protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
base.onPostCreate(savedInstanceState);
}

@Override @CallSuper public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
base.onPostCreate(savedInstanceState, persistentState);
}

@Override @CallSuper protected void onResume() {
super.onResume();
base.onResume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.PersistableBundle;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.trello.navi.Event;
import com.trello.navi.Listener;
Expand Down Expand Up @@ -43,6 +44,16 @@ public class NaviAppCompatActivity extends AppCompatActivity implements NaviComp
base.onStart();
}

@Override @CallSuper protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
base.onPostCreate(savedInstanceState);
}

@Override @CallSuper public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
base.onPostCreate(savedInstanceState, persistentState);
}

@Override @CallSuper protected void onResume() {
super.onResume();
base.onResume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final class HandledEvents {
Event.CREATE,
Event.CREATE_PERSISTABLE,
Event.START,
Event.POST_CREATE,
Event.POST_CREATE_PERSISTABLE,
Event.RESUME,
Event.PAUSE,
Event.STOP,
Expand Down
8 changes: 8 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 @@ -205,6 +205,14 @@ public void onPause() {
emitEvent(Event.PAUSE);
}

public void onPostCreate(Bundle savedInstanceState) {
emitEvent(Event.POST_CREATE, savedInstanceState);
}

public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
emitEvent(Event.POST_CREATE_PERSISTABLE, new BundleBundle(savedInstanceState, persistentState));
}

public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
emitEvent(Event.REQUEST_PERMISSIONS_RESULT,
Expand Down
32 changes: 32 additions & 0 deletions navi/src/test/java/com/trello/navi/NaviActivityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ public final class NaviActivityTest {
verifyNoMoreInteractions(listener);
}

@Test public void postCreateListener() {
Listener<Bundle> listener = mock(Listener.class);
emitter.addListener(Event.POST_CREATE, listener);

Bundle bundle = new Bundle();
emitter.onPostCreate(bundle);
verify(listener).call(bundle);

emitter.removeListener(listener);
emitter.onPostCreate(bundle);
verifyNoMoreInteractions(listener);
}

@Test public void postCreatePersistableListener() {
Listener<Bundle> listener = mock(Listener.class);
Listener<BundleBundle> persistableListener = mock(Listener.class);
emitter.addListener(Event.POST_CREATE, listener);
emitter.addListener(Event.POST_CREATE_PERSISTABLE, persistableListener);

Bundle bundle = new Bundle();
PersistableBundle persistableBundle = mock(PersistableBundle.class);
emitter.onPostCreate(bundle, persistableBundle);
verifyZeroInteractions(listener);
verify(persistableListener).call(new BundleBundle(bundle, persistableBundle));

emitter.removeListener(listener);
emitter.removeListener(persistableListener);
emitter.onPostCreate(bundle, persistableBundle);
verifyNoMoreInteractions(listener);
verifyNoMoreInteractions(persistableListener);
}

@Test public void resumeListener() {
Listener<Void> listener = mock(Listener.class);
emitter.addListener(Event.RESUME, listener);
Expand Down
10 changes: 10 additions & 0 deletions navi/src/test/java/com/trello/navi/NaviFragmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ public final class NaviFragmentTest {
emitter.addListener(Event.CREATE_PERSISTABLE, mock(Listener.class));
}

@Test public void postCreateListener() {
exception.expect(IllegalArgumentException.class);
emitter.addListener(Event.POST_CREATE, mock(Listener.class));
}

@Test public void postCreatePersistableListener() {
exception.expect(IllegalArgumentException.class);
emitter.addListener(Event.POST_CREATE_PERSISTABLE, mock(Listener.class));
}

@Test public void restartListener() {
exception.expect(IllegalArgumentException.class);
emitter.addListener(Event.RESTART, mock(Listener.class));
Expand Down
33 changes: 33 additions & 0 deletions navi/src/test/java/com/trello/navi/rx/RxNaviActivityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,39 @@ public final class RxNaviActivityTest {
testSubscriber.assertUnsubscribed();
}

@Test public void observePostCreate() {
TestSubscriber<Bundle> testSubscriber = new TestSubscriber<>();
Subscription subscription =
RxNavi.observe(emitter, Event.POST_CREATE).subscribe(testSubscriber);
testSubscriber.assertNoValues();

Bundle bundle = new Bundle();
emitter.onPostCreate(bundle);
subscription.unsubscribe();
emitter.onPostCreate(bundle);

testSubscriber.assertValue(bundle);
testSubscriber.assertNoTerminalEvent();
testSubscriber.assertUnsubscribed();
}

@Test public void observePostCreatePersistable() {
TestSubscriber<BundleBundle> testSubscriber = new TestSubscriber<>();
Subscription subscription =
RxNavi.observe(emitter, Event.POST_CREATE_PERSISTABLE).subscribe(testSubscriber);
testSubscriber.assertNoValues();

Bundle bundle = new Bundle();
PersistableBundle persistableBundle = mock(PersistableBundle.class);
emitter.onPostCreate(bundle, persistableBundle);
subscription.unsubscribe();
emitter.onPostCreate(bundle, persistableBundle);

testSubscriber.assertValue(new BundleBundle(bundle, persistableBundle));
testSubscriber.assertNoTerminalEvent();
testSubscriber.assertUnsubscribed();
}

@Test public void observeResume() {
TestSubscriber<Void> testSubscriber = new TestSubscriber<>();
Subscription subscription = RxNavi.observe(emitter, Event.RESUME).subscribe(testSubscriber);
Expand Down

0 comments on commit 5dc0895

Please sign in to comment.