Skip to content

Commit

Permalink
Merge pull request #183 from trello/dlew/fix-null-mapping
Browse files Browse the repository at this point in the history
Do not use null filtering for mapping events
  • Loading branch information
dlew authored Nov 21, 2016
2 parents ab4f41d + 99ca30d commit 662207d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;

import static com.trello.rxlifecycle2.navi.NaviLifecycleMaps.ACTIVITY_EVENT_FILTER;
import static com.trello.rxlifecycle2.navi.NaviLifecycleMaps.ACTIVITY_EVENT_MAP;

final class ActivityLifecycleProviderImpl implements LifecycleProvider<ActivityEvent> {
private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();

Expand All @@ -36,8 +39,8 @@ public ActivityLifecycleProviderImpl(final NaviComponent activity) {
}

RxNavi.observe(activity, Event.ALL)
.map(NaviLifecycleMaps.ACTIVITY_EVENT_MAP)
.filter(RxUtils.notNull())
.filter(ACTIVITY_EVENT_FILTER)
.map(ACTIVITY_EVENT_MAP)
.subscribe(lifecycleSubject);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;

import static com.trello.rxlifecycle2.navi.NaviLifecycleMaps.FRAGMENT_EVENT_FILTER;
import static com.trello.rxlifecycle2.navi.NaviLifecycleMaps.FRAGMENT_EVENT_MAP;

final class FragmentLifecycleProviderImpl implements LifecycleProvider<FragmentEvent> {
private final BehaviorSubject<FragmentEvent> lifecycleSubject = BehaviorSubject.create();

Expand All @@ -37,8 +40,8 @@ public FragmentLifecycleProviderImpl(final NaviComponent fragment) {
}

RxNavi.observe(fragment, Event.ALL)
.map(NaviLifecycleMaps.FRAGMENT_EVENT_MAP)
.filter(RxUtils.notNull())
.filter(FRAGMENT_EVENT_FILTER)
.map(FRAGMENT_EVENT_MAP)
.subscribe(lifecycleSubject);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,39 @@

package com.trello.rxlifecycle2.navi;

import android.support.annotation.NonNull;
import com.trello.navi2.Event;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;

/**
* Maps from Navi events to RxLifecycleAndroid events
*/
final class NaviLifecycleMaps {

static final Predicate<Event.Type> ACTIVITY_EVENT_FILTER = new Predicate<Event.Type>() {
@Override
public boolean test(Event.Type type) throws Exception {
switch (type) {
case CREATE:
case START:
case RESUME:
case PAUSE:
case STOP:
case DESTROY:
return true;
default:
return false;
}
}
};

static final Function<Event.Type, ActivityEvent> ACTIVITY_EVENT_MAP =
new Function<Event.Type, ActivityEvent>() {
@Override
@NonNull
public ActivityEvent apply(Event.Type type) throws Exception {
switch (type) {
case CREATE:
Expand All @@ -42,14 +62,36 @@ public ActivityEvent apply(Event.Type type) throws Exception {
case DESTROY:
return ActivityEvent.DESTROY;
default:
return null;
throw new IllegalArgumentException("Cannot map " + type + " to a ActivityEvent.");
}
}
};

static final Predicate<Event.Type> FRAGMENT_EVENT_FILTER = new Predicate<Event.Type>() {
@Override
public boolean test(Event.Type type) throws Exception {
switch (type) {
case ATTACH:
case CREATE:
case CREATE_VIEW:
case START:
case RESUME:
case PAUSE:
case STOP:
case DESTROY_VIEW:
case DESTROY:
case DETACH:
return true;
default:
return false;
}
}
};

static final Function<Event.Type, FragmentEvent> FRAGMENT_EVENT_MAP =
new Function<Event.Type, FragmentEvent>() {
@Override
@NonNull
public FragmentEvent apply(Event.Type type) throws Exception {
switch (type) {
case ATTACH:
Expand All @@ -73,7 +115,7 @@ public FragmentEvent apply(Event.Type type) throws Exception {
case DETACH:
return FragmentEvent.DETACH;
default:
return null;
throw new IllegalArgumentException("Cannot map " + type + " to a FragmentEvent.");
}
}
};
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.trello.rxlifecycle2.components;

import android.os.Bundle;
import com.trello.navi2.Event;
import com.trello.navi2.NaviComponent;
import com.trello.navi2.internal.NaviEmitter;
Expand Down Expand Up @@ -58,6 +59,16 @@ public void testLifecycle() {
);
}

@Test
public void testNonLifecycleEvents() {
NaviEmitter activity = NaviEmitter.createActivityEmitter();
LifecycleProvider<ActivityEvent> provider = NaviLifecycle.createActivityLifecycleProvider(activity);
TestObserver<ActivityEvent> testObserver = provider.lifecycle().test();
activity.onViewStateRestored(new Bundle());
testObserver.assertNoValues();
testObserver.assertNoErrors();
}

@Test
public void testBindUntilEvent() {
NaviEmitter activity = NaviEmitter.createActivityEmitter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.trello.rxlifecycle2.components;

import android.os.Bundle;
import com.trello.navi2.Event;
import com.trello.navi2.NaviComponent;
import com.trello.navi2.internal.NaviEmitter;
Expand Down Expand Up @@ -66,6 +67,16 @@ public void testLifecycle() {
);
}

@Test
public void testNonLifecycleEvents() {
NaviEmitter fragment = NaviEmitter.createFragmentEmitter();
LifecycleProvider<FragmentEvent> provider = NaviLifecycle.createFragmentLifecycleProvider(fragment);
TestObserver<FragmentEvent> testObserver = provider.lifecycle().test();
fragment.onRestoreInstanceState(new Bundle());
testObserver.assertNoValues();
testObserver.assertNoErrors();
}

@Test
public void testBindUntilEvent() {
NaviEmitter fragment = NaviEmitter.createFragmentEmitter();
Expand Down

0 comments on commit 662207d

Please sign in to comment.