-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
05a4cd0
commit 498c4d5
Showing
13 changed files
with
659 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
rxutil2/src/main/java/com/xuexiang/rxutil2/lifecycle/ActivityLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.xuexiang.rxutil2.lifecycle; | ||
|
||
/** | ||
* Activity的生命周期 | ||
* | ||
* @author xuexiang | ||
* @since 2018/6/11 上午12:45 | ||
*/ | ||
public enum ActivityLifecycle { | ||
onCreate, | ||
onStart, | ||
onResume, | ||
onPause, | ||
onStop, | ||
onDestroy, | ||
} |
48 changes: 48 additions & 0 deletions
48
rxutil2/src/main/java/com/xuexiang/rxutil2/lifecycle/ActivityRxLifecycleCallbacks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.xuexiang.rxutil2.lifecycle; | ||
|
||
import android.app.Activity; | ||
import android.app.Application; | ||
import android.os.Bundle; | ||
|
||
/** | ||
* 应用的生命周期 | ||
* | ||
* @author xuexiang | ||
* @since 2018/6/11 上午1:09 | ||
*/ | ||
final class ActivityRxLifecycleCallbacks implements Application.ActivityLifecycleCallbacks { | ||
@Override | ||
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | ||
RxLifecycle.injectRxLifecycle(activity); | ||
} | ||
|
||
@Override | ||
public void onActivityStarted(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityResumed(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityPaused(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityStopped(Activity activity) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { | ||
|
||
} | ||
|
||
@Override | ||
public void onActivityDestroyed(Activity activity) { | ||
|
||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
rxutil2/src/main/java/com/xuexiang/rxutil2/lifecycle/LifecycleFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.xuexiang.rxutil2.lifecycle; | ||
|
||
import android.app.Fragment; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
|
||
import io.reactivex.Observable; | ||
import io.reactivex.subjects.BehaviorSubject; | ||
|
||
|
||
/** | ||
* 用于添加到Activity中的Fragment,使得和activity的生命周期同步,从而间接绑定了activity的生命周期 | ||
* | ||
* @author xuexiang | ||
* @since 2018/6/11 上午12:58 | ||
*/ | ||
public class LifecycleFragment extends Fragment implements LifecycleManager { | ||
private final BehaviorSubject<ActivityLifecycle> mLifecycleSubject; | ||
|
||
public LifecycleFragment() { | ||
mLifecycleSubject = BehaviorSubject.create(); | ||
} | ||
|
||
@Override | ||
public void onCreate(@Nullable Bundle savedInstanceState) { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onCreate); | ||
super.onCreate(savedInstanceState); | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onStart); | ||
super.onStart(); | ||
} | ||
|
||
@Override | ||
public void onResume() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onResume); | ||
super.onResume(); | ||
} | ||
|
||
@Override | ||
public void onPause() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onPause); | ||
super.onPause(); | ||
} | ||
|
||
@Override | ||
public void onStop() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onStop); | ||
super.onStop(); | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onDestroy); | ||
super.onDestroy(); | ||
} | ||
|
||
@Override | ||
public Observable<ActivityLifecycle> getActivityLifecycle() { | ||
return mLifecycleSubject; | ||
} | ||
|
||
@Override | ||
public <T> LifecycleTransformer<T> bindToActivityLifecycle(final ActivityLifecycle activityLifecycle) { | ||
return new LifecycleTransformer<>(mLifecycleSubject, activityLifecycle); | ||
} | ||
|
||
@Override | ||
public <T> LifecycleTransformer<T> bindToLifecycle() { | ||
return new LifecycleTransformer<>(mLifecycleSubject); | ||
} | ||
|
||
@Override | ||
public <T> LifecycleTransformer<T> bindOnDestroy() { | ||
return bindToActivityLifecycle(ActivityLifecycle.onDestroy); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
rxutil2/src/main/java/com/xuexiang/rxutil2/lifecycle/LifecycleManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.xuexiang.rxutil2.lifecycle; | ||
|
||
import io.reactivex.Observable; | ||
|
||
/** | ||
* 生命周期管理者,绑定生命周期 | ||
* | ||
* @author xuexiang | ||
* @since 2018/6/11 上午12:49 | ||
*/ | ||
public interface LifecycleManager { | ||
|
||
/** | ||
* 获取Activity绑定的生命周期 | ||
* @return | ||
*/ | ||
Observable<ActivityLifecycle> getActivityLifecycle(); | ||
|
||
/** | ||
* 绑定到特定的Activity生命周期进行订阅注销 | ||
* @param activityLifecycle | ||
* @param <T> | ||
* @return | ||
*/ | ||
<T> LifecycleTransformer<T> bindToActivityLifecycle(ActivityLifecycle activityLifecycle); | ||
|
||
/** | ||
* 自动绑定Activity生命周期进行订阅注销 | ||
* @param <T> | ||
* @return | ||
*/ | ||
<T> LifecycleTransformer<T> bindToLifecycle(); | ||
|
||
/** | ||
* 绑定到Activity的OnDestroy进行订阅注销 | ||
* @param <T> | ||
* @return | ||
*/ | ||
<T> LifecycleTransformer<T> bindOnDestroy(); | ||
|
||
} |
143 changes: 143 additions & 0 deletions
143
rxutil2/src/main/java/com/xuexiang/rxutil2/lifecycle/LifecycleTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package com.xuexiang.rxutil2.lifecycle; | ||
|
||
import org.reactivestreams.Publisher; | ||
|
||
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.annotations.NonNull; | ||
import io.reactivex.functions.BiFunction; | ||
import io.reactivex.functions.Function; | ||
import io.reactivex.functions.Predicate; | ||
|
||
import static com.xuexiang.rxutil2.lifecycle.ActivityLifecycle.onDestroy; | ||
import static com.xuexiang.rxutil2.lifecycle.ActivityLifecycle.onPause; | ||
import static com.xuexiang.rxutil2.lifecycle.ActivityLifecycle.onStop; | ||
|
||
/** | ||
* 生命周期转化器 | ||
* | ||
* @author xuexiang | ||
* @since 2018/6/11 上午12:50 | ||
*/ | ||
public class LifecycleTransformer<T> implements ObservableTransformer<T, T>, FlowableTransformer<T, T>, SingleTransformer<T, T>, MaybeTransformer<T, T>, CompletableTransformer { | ||
private Observable<?> mObservable; | ||
|
||
|
||
LifecycleTransformer(Observable<ActivityLifecycle> lifecycleObservable) { | ||
Observable<ActivityLifecycle> observable = lifecycleObservable.share(); | ||
mObservable = Observable.combineLatest(observable.take(1).map(ACTIVITY_LIFECYCLE), observable.skip(1), | ||
new BiFunction<ActivityLifecycle, ActivityLifecycle, Boolean>() { | ||
@Override | ||
public Boolean apply(@NonNull ActivityLifecycle ActivityLifecycle, @NonNull ActivityLifecycle ActivityLifecycle2) throws Exception { | ||
return ActivityLifecycle.equals(ActivityLifecycle2); | ||
} | ||
}) | ||
.filter(new Predicate<Boolean>() { | ||
@Override | ||
public boolean test(@NonNull Boolean aBoolean) throws Exception { | ||
return aBoolean; | ||
} | ||
}); | ||
|
||
} | ||
|
||
LifecycleTransformer(Observable<ActivityLifecycle> lifecycleObservable, final ActivityLifecycle ActivityLifecycle) { | ||
mObservable = lifecycleObservable | ||
.filter(new Predicate<ActivityLifecycle>() { | ||
@Override | ||
public boolean test(@NonNull ActivityLifecycle event) throws Exception { | ||
return event.equals(ActivityLifecycle); | ||
} | ||
}) | ||
.take(1); | ||
} | ||
|
||
@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 SingleSource<T> apply(Single<T> upstream) { | ||
return upstream.takeUntil(mObservable.firstOrError()); | ||
} | ||
|
||
@Override | ||
public MaybeSource<T> apply(Maybe<T> upstream) { | ||
return upstream.takeUntil(mObservable.firstElement()); | ||
} | ||
|
||
@Override | ||
public CompletableSource apply(Completable upstream) { | ||
return Completable.ambArray(upstream); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
LifecycleTransformer<?> that = (LifecycleTransformer<?>) o; | ||
|
||
return mObservable.equals(that.mObservable); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return mObservable.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LifecycleTransformer{" + | ||
"mObservable=" + mObservable + | ||
'}'; | ||
} | ||
|
||
|
||
// Figures out which corresponding next lifecycle event in which to unsubscribe, for Activities | ||
private static final Function<ActivityLifecycle, ActivityLifecycle> ACTIVITY_LIFECYCLE = | ||
new Function<ActivityLifecycle, ActivityLifecycle>() { | ||
@Override | ||
public ActivityLifecycle apply(@NonNull ActivityLifecycle lastEvent) throws Exception { | ||
switch (lastEvent) { | ||
case onCreate: | ||
return onDestroy; | ||
case onStart: | ||
return onStop; | ||
case onResume: | ||
return onPause; | ||
case onPause: | ||
return onStop; | ||
case onStop: | ||
return onDestroy; | ||
case onDestroy: | ||
throw new IllegalStateException("Cannot injectRxLifecycle to Activity lifecycle when outside of it."); | ||
default: | ||
throw new UnsupportedOperationException("Binding to " + lastEvent + " not yet implemented"); | ||
} | ||
} | ||
}; | ||
} |
79 changes: 79 additions & 0 deletions
79
rxutil2/src/main/java/com/xuexiang/rxutil2/lifecycle/LifecycleV4Fragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.xuexiang.rxutil2.lifecycle; | ||
|
||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.Fragment; | ||
|
||
import io.reactivex.Observable; | ||
import io.reactivex.subjects.BehaviorSubject; | ||
|
||
|
||
/** | ||
* 用于添加到Activity中的Fragment,使得和activity的生命周期同步,从而间接绑定了activity的生命周期 | ||
* | ||
* @author xuexiang | ||
* @since 2018/6/11 上午1:01 | ||
*/ | ||
public class LifecycleV4Fragment extends Fragment implements LifecycleManager { | ||
private final BehaviorSubject<ActivityLifecycle> mLifecycleSubject; | ||
|
||
public LifecycleV4Fragment() { | ||
mLifecycleSubject = BehaviorSubject.create(); | ||
} | ||
|
||
@Override | ||
public void onCreate(@Nullable Bundle savedInstanceState) { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onCreate); | ||
super.onCreate(savedInstanceState); | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onStart); | ||
super.onStart(); | ||
} | ||
|
||
@Override | ||
public void onResume() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onResume); | ||
super.onResume(); | ||
} | ||
|
||
@Override | ||
public void onPause() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onPause); | ||
super.onPause(); | ||
} | ||
|
||
@Override | ||
public void onStop() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onStop); | ||
super.onStop(); | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
mLifecycleSubject.onNext(ActivityLifecycle.onDestroy); | ||
super.onDestroy(); | ||
} | ||
|
||
@Override | ||
public Observable<ActivityLifecycle> getActivityLifecycle() { | ||
return mLifecycleSubject; | ||
} | ||
|
||
@Override | ||
public <T> LifecycleTransformer<T> bindToActivityLifecycle(final ActivityLifecycle activityLifecycle) { | ||
return new LifecycleTransformer<>(mLifecycleSubject, activityLifecycle); | ||
} | ||
|
||
@Override | ||
public <T> LifecycleTransformer<T> bindToLifecycle() { | ||
return new LifecycleTransformer<>(mLifecycleSubject); | ||
} | ||
|
||
@Override | ||
public <T> LifecycleTransformer<T> bindOnDestroy() { | ||
return bindToActivityLifecycle(ActivityLifecycle.onDestroy); | ||
} | ||
} |
180 changes: 180 additions & 0 deletions
180
rxutil2/src/main/java/com/xuexiang/rxutil2/lifecycle/RxLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package com.xuexiang.rxutil2.lifecycle; | ||
|
||
import android.app.Activity; | ||
import android.app.Application; | ||
import android.app.Fragment; | ||
import android.app.FragmentManager; | ||
import android.content.Context; | ||
import android.content.ContextWrapper; | ||
import android.support.v4.app.FragmentActivity; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* RxLifecycle,自动绑定到Activity的生命周期中,自动进行订阅的注销 | ||
* | ||
* @author xuexiang | ||
* @since 2018/6/11 上午1:07 | ||
*/ | ||
public final class RxLifecycle { | ||
|
||
private static final String FRAGMENT_TAG = "rx_lifecycle_tag"; | ||
|
||
private RxLifecycle() { | ||
throw new UnsupportedOperationException("u can't instantiate me..."); | ||
} | ||
|
||
//=========================注入并绑定Activity的生命周期================================// | ||
|
||
/** | ||
* 注入并绑定Activity的生命周期<br> | ||
* use in {@link Activity} onCreate | ||
* <pre> {@code | ||
* public class BaseActivity extends AppCompatActivity { | ||
* protected void onCreate(Bundle savedInstanceState) { | ||
* super.onCreate(savedInstanceState); | ||
* RxLifecycle.injectRxLifecycle(this); | ||
* } | ||
* } | ||
* }</pre> | ||
* | ||
* @param activity | ||
*/ | ||
public static void injectRxLifecycle(Activity activity) { | ||
with(activity); | ||
} | ||
|
||
/** | ||
* 注入并绑定Activity的生命周期<br> | ||
* | ||
* @param object | ||
*/ | ||
private static void injectRxLifecycle(Object object) { | ||
if (object instanceof View) { | ||
with((View) object); | ||
} else { | ||
with(object); | ||
} | ||
} | ||
|
||
/** | ||
* 注入并绑定Activity的生命周期<br> | ||
* <p> | ||
* use in {@link Application} oncreate | ||
* <pre> {@code | ||
* public class RxLifecycleAPP extends Application { | ||
* public void onCreate() { | ||
* super.onCreate(); | ||
* RxLifecycle.injectRxLifecycle(this); | ||
* } | ||
* } | ||
* }</pre> | ||
* | ||
* @param application | ||
*/ | ||
public static void injectRxLifecycle(Application application) { | ||
application.registerActivityLifecycleCallbacks(new ActivityRxLifecycleCallbacks()); | ||
} | ||
|
||
//===========================获得生命周期绑定管理者,进行生命周期的绑定==============================// | ||
|
||
/** | ||
* 获得生命周期绑定管理者,进行生命周期的绑定 | ||
* | ||
* @param activity | ||
* @return | ||
*/ | ||
public static LifecycleManager with(Activity activity) { | ||
if (activity instanceof FragmentActivity) { | ||
return with((FragmentActivity) activity); | ||
} | ||
FragmentManager fm = activity.getFragmentManager(); | ||
Fragment fragment = fm.findFragmentByTag(FRAGMENT_TAG); | ||
if (fragment == null) { | ||
fragment = new LifecycleFragment(); | ||
fm.beginTransaction().add(fragment, FRAGMENT_TAG).commitAllowingStateLoss(); | ||
fm.executePendingTransactions(); | ||
} | ||
return (LifecycleManager) fragment; | ||
} | ||
|
||
/** | ||
* 获得生命周期绑定管理者,进行生命周期的绑定 | ||
* | ||
* @param activity | ||
* @return | ||
*/ | ||
private static LifecycleManager with(FragmentActivity activity) { | ||
android.support.v4.app.FragmentManager fm = activity.getSupportFragmentManager(); | ||
android.support.v4.app.Fragment fragment = fm.findFragmentByTag(FRAGMENT_TAG); | ||
if (fragment == null) { | ||
fragment = new LifecycleV4Fragment(); | ||
fm.beginTransaction().add(fragment, FRAGMENT_TAG).commitNowAllowingStateLoss(); | ||
} | ||
|
||
return (LifecycleManager) fragment; | ||
} | ||
|
||
/** | ||
* 获得生命周期绑定管理者,进行生命周期的绑定 | ||
* | ||
* @param fragment | ||
* @return | ||
*/ | ||
public static LifecycleManager with(Fragment fragment) { | ||
return with(fragment.getActivity()); | ||
} | ||
|
||
/** | ||
* 获得生命周期绑定管理者,进行生命周期的绑定 | ||
* | ||
* @param fragment | ||
* @return | ||
*/ | ||
public static LifecycleManager with(android.support.v4.app.Fragment fragment) { | ||
return with(fragment.getActivity()); | ||
} | ||
|
||
/** | ||
* 获得生命周期绑定管理者,进行生命周期的绑定 | ||
* | ||
* @param context ensure context can be cast {@link Activity} | ||
*/ | ||
public static LifecycleManager with(Context context) { | ||
if (context instanceof AppCompatActivity) { | ||
return with((FragmentActivity) context); | ||
} | ||
if (context instanceof Activity) { | ||
return with((Activity) context); | ||
} | ||
if (context instanceof ContextWrapper) { | ||
return with(((ContextWrapper) context).getBaseContext()); | ||
} | ||
throw new ClassCastException(context.getClass().getSimpleName() + " can\'t cast Activity !"); | ||
} | ||
|
||
public static LifecycleManager with(View view) { | ||
return with(view.getContext()); | ||
} | ||
|
||
private static LifecycleManager with(Object object) { | ||
if (object instanceof Context) { | ||
return with((Context) object); | ||
} | ||
for (Field field : object.getClass().getDeclaredFields()) { | ||
try { | ||
field.setAccessible(true); | ||
Object value = field.get(object); | ||
if (value instanceof Context) { | ||
return with((Context) value); | ||
} | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
throw new ClassCastException(object.getClass().getSimpleName() + " can\'t convert Context !"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters