Skip to content

Commit

Permalink
增加可自定进行订阅注销的RxLifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexiangjys committed Jun 11, 2018
1 parent 05a4cd0 commit 498c4d5
Show file tree
Hide file tree
Showing 13 changed files with 659 additions and 14 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ dependencies {
//butterknife的sdk
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
//rxbinding的sdk
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'

implementation project(':rxutil2')

implementation 'com.github.xuexiangjys.XUtil:xutil-core:1.1.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.widget.Button;
import android.widget.Toast;

import com.xuexiang.rxutil2.lifecycle.RxLifecycle;
import com.xuexiang.rxutil2.logs.RxLog;
import com.xuexiang.rxutil2.rxjava.DisposablePool;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;
Expand All @@ -44,6 +45,7 @@

/**
* RxJavaUtils演示示例
*
* @author xuexiang
* @date 2018/3/8 下午3:37
*/
Expand Down Expand Up @@ -72,7 +74,7 @@ protected void initListener() {

@OnClick({R.id.btn_do_in_io, R.id.btn_do_in_ui, R.id.btn_do_in_io_ui, R.id.btn_loading, R.id.btn_polling, R.id.btn_count_down, R.id.btn_foreach})
void OnClick(View v) {
switch(v.getId()) {
switch (v.getId()) {
case R.id.btn_do_in_io:
RxJavaUtils.doInIOThread(new RxIOTask<String>("我是入参123") {
@Override
Expand Down Expand Up @@ -125,12 +127,14 @@ public void onNext(String s) {
});
break;
case R.id.btn_polling:
DisposablePool.get().add(RxJavaUtils.polling(5, new Consumer<Long>() {
@Override
public void accept(Long o) throws Exception {
Toast.makeText(RxJavaActivity.this, "正在监听", Toast.LENGTH_SHORT).show();
}
}), "polling");
RxJavaUtils.polling(5)
.compose(RxLifecycle.with(this).<Long>bindToLifecycle())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long o) throws Exception {
toast("正在监听:" + o);
}
});
break;
case R.id.btn_count_down:
DisposablePool.get().add(RxJavaUtils.countDown(30, new SimpleSubscriber<Long>() {
Expand All @@ -139,17 +143,19 @@ public void onStart() {
super.onStart();
mBtnCountDown.setEnabled(false);
}

@Override
public void onNext(Long aLong) {
mBtnCountDown.setText(String.format("%s s后重新获取", aLong));
}

@Override
public void onComplete() {
super.onComplete();
mBtnCountDown.setText("重新获取");
mBtnCountDown.setEnabled(true);
}
}),"countDown");
}), "countDown");

break;
case R.id.btn_foreach:
Expand All @@ -172,6 +178,7 @@ public Integer doInIOThread(String s) {
RxLog.e("[doInIOThread]" + getLooperStatus() + ", 入参:" + s);
return Integer.parseInt(s);
}

@Override
public void doInUIThread(Integer integer) {
RxLog.e("[doInUIThread] " + getLooperStatus() + ", 入参:" + integer);
Expand All @@ -186,6 +193,7 @@ public void doInUIThread(Integer integer) {

/**
* 获取当前线程的状态
*
* @return
*/
public String getLooperStatus() {
Expand All @@ -195,7 +203,6 @@ public String getLooperStatus() {

@Override
protected void onDestroy() {
DisposablePool.get().remove("polling");
DisposablePool.get().remove("countDown");
super.onDestroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.widget.Toast;

import com.squareup.leakcanary.RefWatcher;
import com.xuexiang.rxutil2.lifecycle.RxLifecycle;
import com.xuexiang.rxutil2demo.App;

import butterknife.ButterKnife;
Expand All @@ -42,6 +43,7 @@ public abstract class BaseActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
RxLifecycle.injectRxLifecycle(this);
mUnbinder = ButterKnife.bind(this);
initArgs();
initViews();
Expand Down
8 changes: 4 additions & 4 deletions rxutil2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ android {
}

dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
api 'io.reactivex.rxjava2:rxjava:2.1.12'
api 'io.reactivex.rxjava2:rxandroid:2.0.2'
compileOnly 'com.android.support:appcompat-v7:26.1.0'
compileOnly 'io.reactivex.rxjava2:rxjava:2.1.12'
compileOnly 'io.reactivex.rxjava2:rxandroid:2.0.2'

//rxbinding的sdk
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
compileOnly 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
}

apply from: "https://raw.githubusercontent.com/xuexiangjys/XUtil/master/JitPackUpload.gradle"
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,
}
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) {

}
}
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);
}
}
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();

}
Loading

0 comments on commit 498c4d5

Please sign in to comment.