setup basic lifecycle helper to auto dispose

This commit is contained in:
longerian
2018-04-08 14:57:00 +08:00
parent e4eb5b2dd8
commit ae5f1e0cba
7 changed files with 425 additions and 0 deletions
@@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2018 Alibaba Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.tmall.wireless.tangram;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.annotation.UiThreadTest;
import android.support.test.filters.SmallTest;
import android.support.test.rule.UiThreadTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Created by longerian on 2018/4/8.
*
* @author longerian
* @date 2018/04/08
*/
@RunWith(AndroidJUnit4.class)
public class LifeCycleHelperTest {
@Rule
public final UiThreadTestRule uiThread = new UiThreadTestRule();
private final Context context = InstrumentationRegistry.getContext();
@Before
public void setUp() {
}
@Test
@SmallTest
@UiThreadTest
public void testLifeCycleProviderImpl() {
}
}
@@ -0,0 +1,69 @@
/*
* MIT License
*
* Copyright (c) 2018 Alibaba Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.alibaba.android.rx.lifecycle;
import io.reactivex.functions.Function;
/**
* Created by longerian on 2018/4/8.
*
* @author longerian
* @date 2018/04/08
*/
public class ActivityEvent extends Event {
public static final ActivityEvent CREATE = new ActivityEvent();
public static final ActivityEvent START = new ActivityEvent();
public static final ActivityEvent RESUME = new ActivityEvent();
public static final ActivityEvent PAUSE = new ActivityEvent();
public static final ActivityEvent STOP = new ActivityEvent();
public static final ActivityEvent DESTROY = new ActivityEvent();
public static final Function<ActivityEvent, ActivityEvent> ACTIVITY_LIFECYCLE =
new Function<ActivityEvent, ActivityEvent>() {
@Override
public ActivityEvent apply(ActivityEvent lastEvent) throws Exception {
if (lastEvent.equals(CREATE)) {
return DESTROY;
}
if (lastEvent.equals(START)) {
return STOP;
}
if (lastEvent.equals(RESUME)) {
return PAUSE;
}
if (lastEvent.equals(PAUSE)) {
return STOP;
}
if (lastEvent.equals(STOP)) {
return DESTROY;
}
throw new IllegalAccessException("Cannot bind to Activity lifecycle when outside of it.");
}
};
}
@@ -0,0 +1,35 @@
/*
* MIT License
*
* Copyright (c) 2018 Alibaba Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.alibaba.android.rx.lifecycle;
/**
* Created by longerian on 2018/4/4.
*
* @author longerian
* @date 2018/04/04
*/
abstract public class Event {
}
@@ -0,0 +1,63 @@
/*
* MIT License
*
* Copyright (c) 2018 Alibaba Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.alibaba.android.rx.lifecycle;
import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
/**
* Created by longerian on 2018/4/8.
*
* @author longerian
* @date 2018/04/08
*/
public class LifeCycleHelper {
public static <T, E> LifecycleTransformer<T> bindUntilEvent(Observable<E> lifecycle, final E event) {
return new LifecycleTransformer<>(lifecycle.filter(new Predicate<E>() {
@Override
public boolean test(E e) throws Exception {
return e.equals(event);
}
}));
}
public static <T, E> LifecycleTransformer<T> bindToLifeCycle(Observable<E> lifecycle, final Function<E, E> correspondingEvents) {
Observable<E> lifecycleCopy = lifecycle.share();
return new LifecycleTransformer<>(Observable.combineLatest(lifecycle.take(1).map(correspondingEvents),
lifecycleCopy.skip(1),
new BiFunction<E,E, Boolean>() {
@Override
public Boolean apply(E e, E e2) throws Exception {
return e.equals(e2);
}
}));
}
}
@@ -0,0 +1,42 @@
/*
* MIT License
*
* Copyright (c) 2018 Alibaba Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.alibaba.android.rx.lifecycle;
/**
* Created by longerian on 2018/4/4.
*
* @author longerian
* @date 2018/04/04
*/
public interface LifeCycleProvider<E extends Event> {
void emitNext(E event);
<T> LifecycleTransformer<T> bindUntil(E event);
<T> LifecycleTransformer<T> bindToLifecycle();
}
@@ -0,0 +1,61 @@
/*
* MIT License
*
* Copyright (c) 2018 Alibaba Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.alibaba.android.rx.lifecycle;
import io.reactivex.functions.Function;
import io.reactivex.subjects.BehaviorSubject;
/**
* Created by longerian on 2018/4/8.
*
* @author longerian
* @date 2018/04/08
*/
public class LifeCycleProviderImpl<E extends Event> implements LifeCycleProvider<E> {
private final BehaviorSubject<E> lifecycleSubject = BehaviorSubject.create();
private final Function<E, E> correspondingEvents;
public LifeCycleProviderImpl(Function<E, E> correspondingEvents) {
this.correspondingEvents = correspondingEvents;
}
@Override
public void emitNext(E event) {
lifecycleSubject.onNext(event);
}
@Override
public <T> LifecycleTransformer<T> bindUntil(E event) {
return LifeCycleHelper.bindUntilEvent(lifecycleSubject, event);
}
@Override
public <T> LifecycleTransformer<T> bindToLifecycle() {
return LifeCycleHelper.bindToLifeCycle(lifecycleSubject, correspondingEvents);
}
}
@@ -0,0 +1,91 @@
/*
* MIT License
*
* Copyright (c) 2018 Alibaba Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.alibaba.android.rx.lifecycle;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Completable;
import io.reactivex.CompletableSource;
import io.reactivex.CompletableTransformer;
import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.Maybe;
import io.reactivex.MaybeSource;
import io.reactivex.MaybeTransformer;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.SingleTransformer;
import io.reactivex.functions.Function;
import org.reactivestreams.Publisher;
/**
* Created by longerian on 2018/4/4.
*
* @author longerian
* @date 2018/04/04
*/
public class LifecycleTransformer<T> implements ObservableTransformer<T, T>, FlowableTransformer<T, T>,
SingleTransformer<T, T>, MaybeTransformer<T, T>, CompletableTransformer {
final Observable<?> mObservable;
public LifecycleTransformer(Observable<?> observable) {
mObservable = observable;
}
@Override
public ObservableSource<T> apply(Observable<T> upstream) {
return upstream.takeUntil(mObservable);
}
@Override
public Publisher<T> apply(Flowable<T> upstream) {
return upstream.takeUntil(mObservable.toFlowable(BackpressureStrategy.LATEST));
}
@Override
public MaybeSource<T> apply(Maybe<T> upstream) {
return upstream.takeUntil(mObservable.firstElement());
}
@Override
public SingleSource<T> apply(Single<T> upstream) {
return upstream.takeUntil(mObservable.firstOrError());
}
@Override
public CompletableSource apply(Completable upstream) {
return Completable.ambArray(upstream, mObservable.flatMapCompletable(
new Function<Object, CompletableSource>() {
@Override
public CompletableSource apply(Object t) throws Exception {
return Completable.complete();
}
}));
}
}