From 67fccd64efa56d2469c1f442470867b6ca8e2970 Mon Sep 17 00:00:00 2001 From: Zhanty Date: Fri, 16 Aug 2019 10:09:51 +0800 Subject: [PATCH] Optimize AutoDispose --- .../com/android/base/app/mvp/RxPresenter.kt | 4 +- .../android/base/app/mvvm/ArchViewModel.kt | 4 +- .../java/com/android/base/rx/AutoDispose.kt | 75 +++++++++++++++++ .../base/rx/LifecycleScopeProviderEx.kt | 80 ------------------- .../com/android/base/rx/TestApplication.java | 9 +-- .../com/android/base/rx/TestBaseActivity.java | 8 +- 6 files changed, 84 insertions(+), 96 deletions(-) delete mode 100644 lib_base/src/main/java/com/android/base/rx/LifecycleScopeProviderEx.kt diff --git a/lib_base/src/main/java/com/android/base/app/mvp/RxPresenter.kt b/lib_base/src/main/java/com/android/base/app/mvp/RxPresenter.kt index d7b4ac8..65be98b 100644 --- a/lib_base/src/main/java/com/android/base/app/mvp/RxPresenter.kt +++ b/lib_base/src/main/java/com/android/base/app/mvp/RxPresenter.kt @@ -1,7 +1,7 @@ package com.android.base.app.mvp import android.support.annotation.CallSuper -import com.android.base.rx.LifecycleScopeProviderEx +import com.android.base.rx.AutoDisposeLifecycleScopeProviderEx import com.uber.autodispose.lifecycle.CorrespondingEventsFunction import com.uber.autodispose.lifecycle.LifecycleEndedException import com.uber.autodispose.lifecycle.LifecycleScopes @@ -17,7 +17,7 @@ import io.reactivex.subjects.BehaviorSubject * Date : 2016-10-19 12:17 * Email: 1169654504@qq.com */ -abstract class RxPresenter : AbstractPresenter(), LifecycleScopeProviderEx { +abstract class RxPresenter : AbstractPresenter(), AutoDisposeLifecycleScopeProviderEx { private val lifecycleSubject = BehaviorSubject.create() diff --git a/lib_base/src/main/java/com/android/base/app/mvvm/ArchViewModel.kt b/lib_base/src/main/java/com/android/base/app/mvvm/ArchViewModel.kt index d483faa..ae2ca25 100644 --- a/lib_base/src/main/java/com/android/base/app/mvvm/ArchViewModel.kt +++ b/lib_base/src/main/java/com/android/base/app/mvvm/ArchViewModel.kt @@ -2,7 +2,7 @@ package com.android.base.app.mvvm import android.arch.lifecycle.ViewModel import android.support.annotation.CallSuper -import com.android.base.rx.LifecycleScopeProviderEx +import com.android.base.rx.AutoDisposeLifecycleScopeProviderEx import com.uber.autodispose.lifecycle.CorrespondingEventsFunction import com.uber.autodispose.lifecycle.LifecycleEndedException import com.uber.autodispose.lifecycle.LifecycleScopes @@ -17,7 +17,7 @@ import io.reactivex.subjects.BehaviorSubject * Email: 1169654504@qq.com * Date : 2017-04-18 16:25 */ -abstract class ArchViewModel : ViewModel(), LifecycleScopeProviderEx { +abstract class ArchViewModel : ViewModel(), AutoDisposeLifecycleScopeProviderEx { private val archLifecycleSubject = BehaviorSubject.createDefault(ViewModelEvent.CREATED) diff --git a/lib_base/src/main/java/com/android/base/rx/AutoDispose.kt b/lib_base/src/main/java/com/android/base/rx/AutoDispose.kt index eb3e92d..413afdf 100644 --- a/lib_base/src/main/java/com/android/base/rx/AutoDispose.kt +++ b/lib_base/src/main/java/com/android/base/rx/AutoDispose.kt @@ -7,10 +7,85 @@ import android.arch.lifecycle.LifecycleOwner import com.uber.autodispose.* import com.uber.autodispose.AutoDispose.autoDisposable import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider +import com.uber.autodispose.lifecycle.LifecycleScopeProvider import io.reactivex.* import io.reactivex.disposables.Disposable import timber.log.Timber + +/** + *@author Ztiany + * Email: ztiany3@gmail.com + * Date : 2019-05-10 14:41 + */ +interface AutoDisposeLifecycleScopeProviderEx : LifecycleScopeProvider { + + fun Flowable.autoDispose(): FlowableSubscribeProxy { + return this.`as`(autoDisposable(this@AutoDisposeLifecycleScopeProviderEx)) + } + + fun Observable.autoDispose(): ObservableSubscribeProxy { + return this.`as`(autoDisposable(this@AutoDisposeLifecycleScopeProviderEx)) + } + + fun Completable.autoDispose(): CompletableSubscribeProxy { + return this.`as`(autoDisposable(this@AutoDisposeLifecycleScopeProviderEx)) + } + + fun Maybe.autoDispose(): MaybeSubscribeProxy { + return this.`as`(autoDisposable(this@AutoDisposeLifecycleScopeProviderEx)) + } + + fun Single.autoDispose(): SingleSubscribeProxy { + return this.`as`(autoDisposable(this@AutoDisposeLifecycleScopeProviderEx)) + } + +} + +interface AutoDisposeLifecycleOwnerEx : LifecycleOwner { + + fun Flowable.autoDispose(): FlowableSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx) + } + + fun Observable.autoDispose(): ObservableSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx) + } + + fun Completable.autoDispose(): CompletableSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx) + } + + fun Maybe.autoDispose(): MaybeSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx) + } + + fun Single.autoDispose(): SingleSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx) + } + + fun Flowable.autoDispose(event: Lifecycle.Event): FlowableSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event) + } + + fun Observable.autoDispose(event: Lifecycle.Event): ObservableSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event) + } + + fun Completable.autoDispose(event: Lifecycle.Event): CompletableSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event) + } + + fun Maybe.autoDispose(event: Lifecycle.Event): MaybeSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event) + } + + fun Single.autoDispose(event: Lifecycle.Event): SingleSubscribeProxy { + return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event) + } + +} + fun Flowable.bindLifecycle(lifecycleOwner: LifecycleOwner): FlowableSubscribeProxy { return this.`as`(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner))) } diff --git a/lib_base/src/main/java/com/android/base/rx/LifecycleScopeProviderEx.kt b/lib_base/src/main/java/com/android/base/rx/LifecycleScopeProviderEx.kt deleted file mode 100644 index 8a7b65f..0000000 --- a/lib_base/src/main/java/com/android/base/rx/LifecycleScopeProviderEx.kt +++ /dev/null @@ -1,80 +0,0 @@ -package com.android.base.rx - -import android.arch.lifecycle.Lifecycle -import android.arch.lifecycle.LifecycleOwner -import com.uber.autodispose.* -import com.uber.autodispose.lifecycle.LifecycleScopeProvider -import io.reactivex.* - -/** - *@author Ztiany - * Email: ztiany3@gmail.com - * Date : 2019-05-10 14:41 - */ -interface LifecycleScopeProviderEx : LifecycleScopeProvider { - - fun Flowable.autoDispose(): FlowableSubscribeProxy { - return this.`as`(AutoDispose.autoDisposable(this@LifecycleScopeProviderEx)) - } - - fun Observable.autoDispose(): ObservableSubscribeProxy { - return this.`as`(AutoDispose.autoDisposable(this@LifecycleScopeProviderEx)) - } - - fun Completable.autoDispose(): CompletableSubscribeProxy { - return this.`as`(AutoDispose.autoDisposable(this@LifecycleScopeProviderEx)) - } - - fun Maybe.autoDispose(): MaybeSubscribeProxy { - return this.`as`(AutoDispose.autoDisposable(this@LifecycleScopeProviderEx)) - } - - fun Single.autoDispose(): SingleSubscribeProxy { - return this.`as`(AutoDispose.autoDisposable(this@LifecycleScopeProviderEx)) - } - -} - -interface LifecycleOwnerEx : LifecycleOwner { - - fun Flowable.autoDispose(): FlowableSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx) - } - - fun Observable.autoDispose(): ObservableSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx) - } - - fun Completable.autoDispose(): CompletableSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx) - } - - fun Maybe.autoDispose(): MaybeSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx) - } - - fun Single.autoDispose(): SingleSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx) - } - - fun Flowable.autoDispose(event: Lifecycle.Event): FlowableSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx, event) - } - - fun Observable.autoDispose(event: Lifecycle.Event): ObservableSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx, event) - } - - fun Completable.autoDispose(event: Lifecycle.Event): CompletableSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx, event) - } - - fun Maybe.autoDispose(event: Lifecycle.Event): MaybeSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx, event) - } - - fun Single.autoDispose(event: Lifecycle.Event): SingleSubscribeProxy { - return this.bindLifecycle(this@LifecycleOwnerEx, event) - } - -} \ No newline at end of file diff --git a/lib_base/src/test/java/com/android/base/rx/TestApplication.java b/lib_base/src/test/java/com/android/base/rx/TestApplication.java index c8dc357..39a453a 100644 --- a/lib_base/src/test/java/com/android/base/rx/TestApplication.java +++ b/lib_base/src/test/java/com/android/base/rx/TestApplication.java @@ -15,14 +15,7 @@ public class TestApplication extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); - BaseKit.get().onApplicationAttachBaseContext(base); + BaseKit.get().getApplicationDelegate().attachBaseContext(base); } - @Override - public void onCreate() { - super.onCreate(); - BaseKit.get().onApplicationCreate(this); - } - - } diff --git a/lib_base/src/test/java/com/android/base/rx/TestBaseActivity.java b/lib_base/src/test/java/com/android/base/rx/TestBaseActivity.java index 8fe9203..9def28c 100644 --- a/lib_base/src/test/java/com/android/base/rx/TestBaseActivity.java +++ b/lib_base/src/test/java/com/android/base/rx/TestBaseActivity.java @@ -7,8 +7,8 @@ import com.android.base.app.activity.BaseActivity; /** * @author Ztiany - * Email: 1169654504@qq.com - * Date : 2017-06-22 09:37 + * Email: 1169654504@qq.com + * Date : 2017-06-22 09:37 */ public class TestBaseActivity extends BaseActivity { @@ -16,7 +16,7 @@ public class TestBaseActivity extends BaseActivity { protected void initialize(@Nullable Bundle savedInstanceState) { super.initialize(savedInstanceState); } - + @Override protected Object layout() { return 0; @@ -26,4 +26,4 @@ public class TestBaseActivity extends BaseActivity { protected void setupView(@Nullable Bundle savedInstanceState) { } -} +} \ No newline at end of file