From 5f1f6f2f4d09f4aa4e0250730a1d43fd2f280be0 Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 17 Mar 2022 15:31:04 +0800 Subject: [PATCH 1/4] Make some generic types extend Any instead of Any? --- .../goooler/demoapp/common/util/CommonExtensions.kt | 12 ++++++------ .../network/exception/CustomObservableObserver.kt | 2 +- .../test/network/exception/CustomSingleObserver.kt | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/common/src/main/kotlin/io/goooler/demoapp/common/util/CommonExtensions.kt b/common/src/main/kotlin/io/goooler/demoapp/common/util/CommonExtensions.kt index 2961e6ec9..f99c561bd 100644 --- a/common/src/main/kotlin/io/goooler/demoapp/common/util/CommonExtensions.kt +++ b/common/src/main/kotlin/io/goooler/demoapp/common/util/CommonExtensions.kt @@ -183,19 +183,19 @@ fun Number.formatMoney(isYuan: Boolean = false, trans2W: Boolean = false, scale: // ---------------------Rx-------------------------------// -fun Single.subscribeOnIoThread(): Single = subscribeOn(Schedulers.io()) +fun Single.subscribeOnIoThread(): Single = subscribeOn(Schedulers.io()) -fun Single.observeOnMainThread(): Single = observeOn(AndroidSchedulers.mainThread()) +fun Single.observeOnMainThread(): Single = observeOn(AndroidSchedulers.mainThread()) -fun Single.subscribeAndObserve(): Single = +fun Single.subscribeAndObserve(): Single = subscribeOnIoThread().observeOnMainThread() -fun Observable.subscribeOnIoThread(): Observable = subscribeOn(Schedulers.io()) +fun Observable.subscribeOnIoThread(): Observable = subscribeOn(Schedulers.io()) -fun Observable.observeOnMainThread(): Observable = +fun Observable.observeOnMainThread(): Observable = observeOn(AndroidSchedulers.mainThread()) -fun Observable.subscribeAndObserve(): Observable = +fun Observable.subscribeAndObserve(): Observable = subscribeOnIoThread().observeOnMainThread() // ---------------------Res-------------------------------// diff --git a/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservableObserver.kt b/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservableObserver.kt index 7de05ff24..db15e9b8c 100644 --- a/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservableObserver.kt +++ b/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservableObserver.kt @@ -14,7 +14,7 @@ abstract class CustomObservableObserver : onCustomError((t.message ?: t.code.toString()).toResponseException()) } - override fun onError(e: Throwable?) { + override fun onError(e: Throwable) { onCustomError(e.toResponseException()) } diff --git a/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomSingleObserver.kt b/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomSingleObserver.kt index eaade112c..f5ba9ce85 100644 --- a/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomSingleObserver.kt +++ b/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomSingleObserver.kt @@ -19,7 +19,7 @@ abstract class CustomSingleObserver : } } -sealed interface CustomObserver { +sealed interface CustomObserver { fun onCustomSuccess(t: T) From c03b5540ff06de09ec94cb71e77e1bd0015f9031 Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 17 Mar 2022 15:34:31 +0800 Subject: [PATCH 2/4] Merge custom observers --- .../exception/CustomObservableObserver.kt | 22 ------------- ...omSingleObserver.kt => CustomObservers.kt} | 31 +++++++++++++++---- 2 files changed, 25 insertions(+), 28 deletions(-) delete mode 100644 test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservableObserver.kt rename test/src/main/kotlin/io/goooler/demoapp/test/network/exception/{CustomSingleObserver.kt => CustomObservers.kt} (59%) diff --git a/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservableObserver.kt b/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservableObserver.kt deleted file mode 100644 index db15e9b8c..000000000 --- a/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservableObserver.kt +++ /dev/null @@ -1,22 +0,0 @@ -package io.goooler.demoapp.test.network.exception - -import io.goooler.demoapp.common.network.BaseResponse -import io.reactivex.rxjava3.observers.DisposableObserver - -abstract class CustomObservableObserver : - DisposableObserver(), - CustomObserver { - - override fun onNext(t: T) { - if (t.code == 200) - onCustomSuccess(t) - else - onCustomError((t.message ?: t.code.toString()).toResponseException()) - } - - override fun onError(e: Throwable) { - onCustomError(e.toResponseException()) - } - - override fun onComplete() {} -} diff --git a/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomSingleObserver.kt b/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservers.kt similarity index 59% rename from test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomSingleObserver.kt rename to test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservers.kt index f5ba9ce85..ab14d0d85 100644 --- a/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomSingleObserver.kt +++ b/test/src/main/kotlin/io/goooler/demoapp/test/network/exception/CustomObservers.kt @@ -1,13 +1,21 @@ package io.goooler.demoapp.test.network.exception import io.goooler.demoapp.common.network.BaseResponse +import io.reactivex.rxjava3.observers.DisposableObserver import io.reactivex.rxjava3.observers.DisposableSingleObserver -abstract class CustomSingleObserver : - DisposableSingleObserver(), +sealed interface CustomObserver { + + fun onCustomSuccess(t: T) + + fun onCustomError(e: ResponseException) +} + +abstract class CustomObservableObserver : + DisposableObserver(), CustomObserver { - override fun onSuccess(t: T) { + override fun onNext(t: T) { if (t.code == 200) onCustomSuccess(t) else @@ -17,11 +25,22 @@ abstract class CustomSingleObserver : override fun onError(e: Throwable) { onCustomError(e.toResponseException()) } + + override fun onComplete() {} } -sealed interface CustomObserver { +abstract class CustomSingleObserver : + DisposableSingleObserver(), + CustomObserver { - fun onCustomSuccess(t: T) + override fun onSuccess(t: T) { + if (t.code == 200) + onCustomSuccess(t) + else + onCustomError((t.message ?: t.code.toString()).toResponseException()) + } - fun onCustomError(e: ResponseException) + override fun onError(e: Throwable) { + onCustomError(e.toResponseException()) + } } From 65a457ff5f65886e50ce4ed763cc5a24f17f9d18 Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 17 Mar 2022 15:46:37 +0800 Subject: [PATCH 3/4] Cleanup observableFields --- .../kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/base/src/main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt b/base/src/main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt index 81a34dcdd..83f6ca0ff 100644 --- a/base/src/main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt +++ b/base/src/main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt @@ -32,7 +32,6 @@ import androidx.core.os.bundleOf import androidx.core.text.parseAsHtml import androidx.core.text.toSpannable import androidx.databinding.DataBindingUtil -import androidx.databinding.ObservableField import androidx.databinding.ViewDataBinding import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity @@ -91,10 +90,6 @@ typealias MutableStringLiveData = MutableLiveData typealias MutableListLiveData = MutableLiveData> -typealias ObservableString = ObservableField - -typealias ObservableList = ObservableField> - typealias ParamMap = HashMap // ---------------------Any-------------------------------// From 6d8fa3e99a077bd03285913c956e0d6f077fd4ff Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 17 Mar 2022 15:53:09 +0800 Subject: [PATCH 4/4] Prevent unsafeLazy return nullable value --- .../main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/src/main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt b/base/src/main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt index 83f6ca0ff..473d65eb8 100644 --- a/base/src/main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt +++ b/base/src/main/kotlin/io/goooler/demoapp/base/util/BaseExtensions.kt @@ -102,7 +102,8 @@ inline val currentThreadName: String get() = Thread.currentThread().name inline val isMainThread: Boolean get() = Looper.getMainLooper() == Looper.myLooper() -fun unsafeLazy(initializer: () -> T): Lazy = lazy(LazyThreadSafetyMode.NONE, initializer) +fun unsafeLazy(initializer: () -> T): Lazy = + lazy(LazyThreadSafetyMode.NONE, initializer) fun MutableLiveData.asLiveData(): LiveData = this