Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Cleanup generic functions & classes #58

Merged
merged 4 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,10 +90,6 @@ typealias MutableStringLiveData = MutableLiveData<String?>

typealias MutableListLiveData<T> = MutableLiveData<List<T>>

typealias ObservableString = ObservableField<String?>

typealias ObservableList<T> = ObservableField<List<T>>

typealias ParamMap = HashMap<String, Any>

// ---------------------Any-------------------------------//
Expand All @@ -107,7 +102,8 @@ inline val currentThreadName: String get() = Thread.currentThread().name

inline val isMainThread: Boolean get() = Looper.getMainLooper() == Looper.myLooper()

fun <T> unsafeLazy(initializer: () -> T): Lazy<T> = lazy(LazyThreadSafetyMode.NONE, initializer)
fun <T : Any> unsafeLazy(initializer: () -> T): Lazy<T> =
lazy(LazyThreadSafetyMode.NONE, initializer)

fun <T> MutableLiveData<T>.asLiveData(): LiveData<T> = this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,19 @@ fun Number.formatMoney(isYuan: Boolean = false, trans2W: Boolean = false, scale:

// ---------------------Rx-------------------------------//

fun <T> Single<T>.subscribeOnIoThread(): Single<T> = subscribeOn(Schedulers.io())
fun <T : Any> Single<T>.subscribeOnIoThread(): Single<T> = subscribeOn(Schedulers.io())

fun <T> Single<T>.observeOnMainThread(): Single<T> = observeOn(AndroidSchedulers.mainThread())
fun <T : Any> Single<T>.observeOnMainThread(): Single<T> = observeOn(AndroidSchedulers.mainThread())

fun <T> Single<T>.subscribeAndObserve(): Single<T> =
fun <T : Any> Single<T>.subscribeAndObserve(): Single<T> =
subscribeOnIoThread().observeOnMainThread()

fun <T> Observable<T>.subscribeOnIoThread(): Observable<T> = subscribeOn(Schedulers.io())
fun <T : Any> Observable<T>.subscribeOnIoThread(): Observable<T> = subscribeOn(Schedulers.io())

fun <T> Observable<T>.observeOnMainThread(): Observable<T> =
fun <T : Any> Observable<T>.observeOnMainThread(): Observable<T> =
observeOn(AndroidSchedulers.mainThread())

fun <T> Observable<T>.subscribeAndObserve(): Observable<T> =
fun <T : Any> Observable<T>.subscribeAndObserve(): Observable<T> =
subscribeOnIoThread().observeOnMainThread()
Comment on lines +186 to 199
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link #32.


// ---------------------Res-------------------------------//
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<T : BaseResponse> :
DisposableSingleObserver<T>(),
sealed interface CustomObserver<T : Any> {

fun onCustomSuccess(t: T)

fun onCustomError(e: ResponseException)
}

abstract class CustomObservableObserver<T : BaseResponse> :
DisposableObserver<T>(),
CustomObserver<T> {

override fun onSuccess(t: T) {
override fun onNext(t: T) {
if (t.code == 200)
onCustomSuccess(t)
else
Expand All @@ -17,11 +25,22 @@ abstract class CustomSingleObserver<T : BaseResponse> :
override fun onError(e: Throwable) {
onCustomError(e.toResponseException())
}

override fun onComplete() {}
}

sealed interface CustomObserver<T> {
abstract class CustomSingleObserver<T : BaseResponse> :
DisposableSingleObserver<T>(),
CustomObserver<T> {

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