Optimize AutoDispose

androidx
Zhanty 5 years ago
parent b11d78d8a0
commit 67fccd64ef
  1. 4
      lib_base/src/main/java/com/android/base/app/mvp/RxPresenter.kt
  2. 4
      lib_base/src/main/java/com/android/base/app/mvvm/ArchViewModel.kt
  3. 75
      lib_base/src/main/java/com/android/base/rx/AutoDispose.kt
  4. 80
      lib_base/src/main/java/com/android/base/rx/LifecycleScopeProviderEx.kt
  5. 9
      lib_base/src/test/java/com/android/base/rx/TestApplication.java
  6. 8
      lib_base/src/test/java/com/android/base/rx/TestBaseActivity.java

@ -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<V : IBaseView> : AbstractPresenter<V>(), LifecycleScopeProviderEx<RxPresenter.LifecycleEvent> {
abstract class RxPresenter<V : IBaseView> : AbstractPresenter<V>(), AutoDisposeLifecycleScopeProviderEx<RxPresenter.LifecycleEvent> {
private val lifecycleSubject = BehaviorSubject.create<LifecycleEvent>()

@ -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<ArchViewModel.ViewModelEvent> {
abstract class ArchViewModel : ViewModel(), AutoDisposeLifecycleScopeProviderEx<ArchViewModel.ViewModelEvent> {
private val archLifecycleSubject = BehaviorSubject.createDefault(ViewModelEvent.CREATED)

@ -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<T> : LifecycleScopeProvider<T> {
fun <T> Flowable<T>.autoDispose(): FlowableSubscribeProxy<T> {
return this.`as`(autoDisposable(this@AutoDisposeLifecycleScopeProviderEx))
}
fun <T> Observable<T>.autoDispose(): ObservableSubscribeProxy<T> {
return this.`as`(autoDisposable(this@AutoDisposeLifecycleScopeProviderEx))
}
fun Completable.autoDispose(): CompletableSubscribeProxy {
return this.`as`(autoDisposable<Any>(this@AutoDisposeLifecycleScopeProviderEx))
}
fun <T> Maybe<T>.autoDispose(): MaybeSubscribeProxy<T> {
return this.`as`(autoDisposable(this@AutoDisposeLifecycleScopeProviderEx))
}
fun <T> Single<T>.autoDispose(): SingleSubscribeProxy<T> {
return this.`as`(autoDisposable(this@AutoDisposeLifecycleScopeProviderEx))
}
}
interface AutoDisposeLifecycleOwnerEx : LifecycleOwner {
fun <T> Flowable<T>.autoDispose(): FlowableSubscribeProxy<T> {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx)
}
fun <T> Observable<T>.autoDispose(): ObservableSubscribeProxy<T> {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx)
}
fun Completable.autoDispose(): CompletableSubscribeProxy {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx)
}
fun <T> Maybe<T>.autoDispose(): MaybeSubscribeProxy<T> {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx)
}
fun <T> Single<T>.autoDispose(): SingleSubscribeProxy<T> {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx)
}
fun <T> Flowable<T>.autoDispose(event: Lifecycle.Event): FlowableSubscribeProxy<T> {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event)
}
fun <T> Observable<T>.autoDispose(event: Lifecycle.Event): ObservableSubscribeProxy<T> {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event)
}
fun Completable.autoDispose(event: Lifecycle.Event): CompletableSubscribeProxy {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event)
}
fun <T> Maybe<T>.autoDispose(event: Lifecycle.Event): MaybeSubscribeProxy<T> {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event)
}
fun <T> Single<T>.autoDispose(event: Lifecycle.Event): SingleSubscribeProxy<T> {
return this.bindLifecycle(this@AutoDisposeLifecycleOwnerEx, event)
}
}
fun <T> Flowable<T>.bindLifecycle(lifecycleOwner: LifecycleOwner): FlowableSubscribeProxy<T> {
return this.`as`(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner)))
}

@ -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<T> : LifecycleScopeProvider<T> {
fun <T> Flowable<T>.autoDispose(): FlowableSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(this@LifecycleScopeProviderEx))
}
fun <T> Observable<T>.autoDispose(): ObservableSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(this@LifecycleScopeProviderEx))
}
fun Completable.autoDispose(): CompletableSubscribeProxy {
return this.`as`(AutoDispose.autoDisposable<Any>(this@LifecycleScopeProviderEx))
}
fun <T> Maybe<T>.autoDispose(): MaybeSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(this@LifecycleScopeProviderEx))
}
fun <T> Single<T>.autoDispose(): SingleSubscribeProxy<T> {
return this.`as`(AutoDispose.autoDisposable(this@LifecycleScopeProviderEx))
}
}
interface LifecycleOwnerEx : LifecycleOwner {
fun <T> Flowable<T>.autoDispose(): FlowableSubscribeProxy<T> {
return this.bindLifecycle(this@LifecycleOwnerEx)
}
fun <T> Observable<T>.autoDispose(): ObservableSubscribeProxy<T> {
return this.bindLifecycle(this@LifecycleOwnerEx)
}
fun Completable.autoDispose(): CompletableSubscribeProxy {
return this.bindLifecycle(this@LifecycleOwnerEx)
}
fun <T> Maybe<T>.autoDispose(): MaybeSubscribeProxy<T> {
return this.bindLifecycle(this@LifecycleOwnerEx)
}
fun <T> Single<T>.autoDispose(): SingleSubscribeProxy<T> {
return this.bindLifecycle(this@LifecycleOwnerEx)
}
fun <T> Flowable<T>.autoDispose(event: Lifecycle.Event): FlowableSubscribeProxy<T> {
return this.bindLifecycle(this@LifecycleOwnerEx, event)
}
fun <T> Observable<T>.autoDispose(event: Lifecycle.Event): ObservableSubscribeProxy<T> {
return this.bindLifecycle(this@LifecycleOwnerEx, event)
}
fun Completable.autoDispose(event: Lifecycle.Event): CompletableSubscribeProxy {
return this.bindLifecycle(this@LifecycleOwnerEx, event)
}
fun <T> Maybe<T>.autoDispose(event: Lifecycle.Event): MaybeSubscribeProxy<T> {
return this.bindLifecycle(this@LifecycleOwnerEx, event)
}
fun <T> Single<T>.autoDispose(event: Lifecycle.Event): SingleSubscribeProxy<T> {
return this.bindLifecycle(this@LifecycleOwnerEx, event)
}
}

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

@ -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) {
}
}
}
Loading…
Cancel
Save