From 99ca30d9734167bf52f534a340e55006ade4a06b Mon Sep 17 00:00:00 2001 From: Dan Lew Date: Mon, 21 Nov 2016 14:44:48 -0600 Subject: [PATCH] Do not use null filtering for mapping events Since RxJava 2 doesn't like nulls! Now we filter based on the event beforehand, then map it afterwards. --- .../navi/ActivityLifecycleProviderImpl.java | 7 ++- .../navi/FragmentLifecycleProviderImpl.java | 7 ++- .../rxlifecycle2/navi/NaviLifecycleMaps.java | 46 ++++++++++++++++++- .../com/trello/rxlifecycle2/navi/RxUtils.java | 33 ------------- .../components/NaviActivityLifecycleTest.java | 11 +++++ .../components/NaviFragmentLifecycleTest.java | 11 +++++ 6 files changed, 76 insertions(+), 39 deletions(-) delete mode 100644 rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/RxUtils.java diff --git a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/ActivityLifecycleProviderImpl.java b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/ActivityLifecycleProviderImpl.java index 4b6a9b6..41fbbde 100644 --- a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/ActivityLifecycleProviderImpl.java +++ b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/ActivityLifecycleProviderImpl.java @@ -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 { private final BehaviorSubject lifecycleSubject = BehaviorSubject.create(); @@ -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); } diff --git a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/FragmentLifecycleProviderImpl.java b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/FragmentLifecycleProviderImpl.java index 5e1ffbc..3e720f4 100644 --- a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/FragmentLifecycleProviderImpl.java +++ b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/FragmentLifecycleProviderImpl.java @@ -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 { private final BehaviorSubject lifecycleSubject = BehaviorSubject.create(); @@ -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); } diff --git a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/NaviLifecycleMaps.java b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/NaviLifecycleMaps.java index 39bb562..d058aa8 100644 --- a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/NaviLifecycleMaps.java +++ b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/NaviLifecycleMaps.java @@ -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 ACTIVITY_EVENT_FILTER = new Predicate() { + @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 ACTIVITY_EVENT_MAP = new Function() { @Override + @NonNull public ActivityEvent apply(Event.Type type) throws Exception { switch (type) { case CREATE: @@ -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 FRAGMENT_EVENT_FILTER = new Predicate() { + @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 FRAGMENT_EVENT_MAP = new Function() { @Override + @NonNull public FragmentEvent apply(Event.Type type) throws Exception { switch (type) { case ATTACH: @@ -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."); } } }; diff --git a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/RxUtils.java b/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/RxUtils.java deleted file mode 100644 index fa037c4..0000000 --- a/rxlifecycle-navi/src/main/java/com/trello/rxlifecycle2/navi/RxUtils.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.trello.rxlifecycle2.navi; - -import io.reactivex.functions.Predicate; - -final class RxUtils { - - static Predicate notNull() { - return new Predicate() { - @Override - public boolean test(T t) throws Exception { - return t != null; - } - }; - } - - private RxUtils() { - throw new AssertionError("No instances!"); - } -} diff --git a/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle2/components/NaviActivityLifecycleTest.java b/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle2/components/NaviActivityLifecycleTest.java index f633886..851d6bd 100644 --- a/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle2/components/NaviActivityLifecycleTest.java +++ b/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle2/components/NaviActivityLifecycleTest.java @@ -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; @@ -58,6 +59,16 @@ public void testLifecycle() { ); } + @Test + public void testNonLifecycleEvents() { + NaviEmitter activity = NaviEmitter.createActivityEmitter(); + LifecycleProvider provider = NaviLifecycle.createActivityLifecycleProvider(activity); + TestObserver testObserver = provider.lifecycle().test(); + activity.onViewStateRestored(new Bundle()); + testObserver.assertNoValues(); + testObserver.assertNoErrors(); + } + @Test public void testBindUntilEvent() { NaviEmitter activity = NaviEmitter.createActivityEmitter(); diff --git a/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle2/components/NaviFragmentLifecycleTest.java b/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle2/components/NaviFragmentLifecycleTest.java index ad1100e..930ccae 100644 --- a/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle2/components/NaviFragmentLifecycleTest.java +++ b/rxlifecycle-navi/src/test/java/com/trello/rxlifecycle2/components/NaviFragmentLifecycleTest.java @@ -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; @@ -66,6 +67,16 @@ public void testLifecycle() { ); } + @Test + public void testNonLifecycleEvents() { + NaviEmitter fragment = NaviEmitter.createFragmentEmitter(); + LifecycleProvider provider = NaviLifecycle.createFragmentLifecycleProvider(fragment); + TestObserver testObserver = provider.lifecycle().test(); + fragment.onRestoreInstanceState(new Bundle()); + testObserver.assertNoValues(); + testObserver.assertNoErrors(); + } + @Test public void testBindUntilEvent() { NaviEmitter fragment = NaviEmitter.createFragmentEmitter();