parent
260f6a8de5
commit
7095135a4d
@ -1,239 +0,0 @@ |
|||||||
package com.android.base.app.aac |
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData |
|
||||||
import androidx.lifecycle.MutableLiveData |
|
||||||
import com.android.base.data.Resource |
|
||||||
import com.android.base.rx.subscribeIgnoreError |
|
||||||
import com.github.dmstocking.optional.java.util.Optional |
|
||||||
import io.reactivex.* |
|
||||||
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------------- |
|
||||||
|
|
||||||
fun <T> Observable<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.success(it)) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T, R> Observable<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<R>>, map: (T) -> R) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.success(map(it))) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Observable<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<Resource<T>>) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.success(it.orElse(null))) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T, R> Observable<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<Resource<R>>, map: (T?) -> R?) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
val value = map(it.orElse(null)) |
|
||||||
liveData.postValue(Resource.success(value)) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Flowable<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.success(it)) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T, R> Flowable<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<R>>, map: (T) -> R) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.success(map(it))) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Flowable<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<Resource<T>>) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.success(it.orElse(null))) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T, R> Flowable<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<Resource<R>>, map: (T?) -> R?) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
val value = map(it.orElse(null)) |
|
||||||
liveData.postValue(Resource.success(value)) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
fun Completable.subscribeWithLiveData(liveData: MutableLiveData<Resource<Any>>) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.success()) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Completable.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>, provider: () -> T) { |
|
||||||
liveData.postValue(Resource.loading()) |
|
||||||
this.subscribe( |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.success(provider())) |
|
||||||
}, |
|
||||||
{ |
|
||||||
liveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------------- |
|
||||||
|
|
||||||
fun <T> Observable<T>.toResourceLiveData(): LiveData<Resource<T>> { |
|
||||||
val mutableLiveData = MutableLiveData<Resource<T>>() |
|
||||||
mutableLiveData.value = Resource.loading() |
|
||||||
subscribe( |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.success(it)) |
|
||||||
}, |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
return mutableLiveData |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Observable<Optional<T>>.optionalToResourceLiveData(): LiveData<Resource<T>> { |
|
||||||
val mutableLiveData = MutableLiveData<Resource<T>>() |
|
||||||
mutableLiveData.value = Resource.loading() |
|
||||||
subscribe( |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.success(it.orElse(null))) |
|
||||||
}, |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
return mutableLiveData |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Flowable<T>.toResourceLiveData(): LiveData<Resource<T>> { |
|
||||||
val mutableLiveData = MutableLiveData<Resource<T>>() |
|
||||||
mutableLiveData.value = Resource.loading() |
|
||||||
subscribe( |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.success(it)) |
|
||||||
}, |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
return mutableLiveData |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Flowable<Optional<T>>.optionalToResourceLiveData(): LiveData<Resource<T>> { |
|
||||||
val mutableLiveData = MutableLiveData<Resource<T>>() |
|
||||||
mutableLiveData.value = Resource.loading() |
|
||||||
subscribe( |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.success(it.orElse(null))) |
|
||||||
}, |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
return mutableLiveData |
|
||||||
} |
|
||||||
|
|
||||||
fun Completable.toResourceLiveData(): LiveData<Resource<Any>> { |
|
||||||
val mutableLiveData = MutableLiveData<Resource<Any>>() |
|
||||||
mutableLiveData.value = Resource.loading() |
|
||||||
subscribe( |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.success()) |
|
||||||
}, |
|
||||||
{ |
|
||||||
mutableLiveData.postValue(Resource.error(it)) |
|
||||||
} |
|
||||||
) |
|
||||||
return mutableLiveData |
|
||||||
} |
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------------- |
|
||||||
|
|
||||||
fun <T> Observable<T>.toLiveData(): LiveData<T> { |
|
||||||
val liveData = MutableLiveData<T>() |
|
||||||
this.subscribeIgnoreError { |
|
||||||
liveData.postValue(it) |
|
||||||
} |
|
||||||
return liveData |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Flowable<T>.toLiveData(): LiveData<T> { |
|
||||||
val liveData = MutableLiveData<T>() |
|
||||||
this.subscribeIgnoreError { |
|
||||||
liveData.postValue(it) |
|
||||||
} |
|
||||||
return liveData |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Single<T>.toLiveData(): LiveData<T> { |
|
||||||
val liveData = MutableLiveData<T>() |
|
||||||
this.subscribeIgnoreError { |
|
||||||
liveData.postValue(it) |
|
||||||
} |
|
||||||
return liveData |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> Maybe<T>.toLiveData(): LiveData<T> { |
|
||||||
val liveData = MutableLiveData<T>() |
|
||||||
this.subscribeIgnoreError { |
|
||||||
liveData.postValue(it) |
|
||||||
} |
|
||||||
return liveData |
|
||||||
} |
|
@ -1,42 +0,0 @@ |
|||||||
package com.android.base.app.ui |
|
||||||
|
|
||||||
import com.android.base.app.Sword |
|
||||||
import timber.log.Timber |
|
||||||
|
|
||||||
fun <T> RefreshStateLayout.handleResultWithStatus(t: T?, onResult: ((T) -> Unit)) { |
|
||||||
if (isRefreshing) { |
|
||||||
refreshCompleted() |
|
||||||
} |
|
||||||
if (t == null || (t is Collection<*> && t.isEmpty()) || (t is Map<*, *> && t.isEmpty())) { |
|
||||||
showEmptyLayout() |
|
||||||
} else { |
|
||||||
onResult.invoke(t) |
|
||||||
showContentLayout() |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
fun RefreshStateLayout.handleErrorWithStatus(throwable: Throwable?) { |
|
||||||
if (throwable == null) { |
|
||||||
Timber.d("processErrorWithStatus called, but throwable is null") |
|
||||||
return |
|
||||||
} |
|
||||||
if (isRefreshing) { |
|
||||||
refreshCompleted() |
|
||||||
} |
|
||||||
val errorTypeClassifier = Sword.get().errorClassifier() |
|
||||||
if (errorTypeClassifier != null) { |
|
||||||
when { |
|
||||||
errorTypeClassifier.isNetworkError(throwable) -> { |
|
||||||
Timber.d("isNetworkError showNetErrorLayout") |
|
||||||
showNetErrorLayout() |
|
||||||
} |
|
||||||
errorTypeClassifier.isServerError(throwable) -> { |
|
||||||
Timber.d("isServerError showServerErrorLayout") |
|
||||||
showServerErrorLayout() |
|
||||||
} |
|
||||||
else -> showErrorLayout() |
|
||||||
} |
|
||||||
} else { |
|
||||||
showErrorLayout() |
|
||||||
} |
|
||||||
} |
|
@ -1,172 +0,0 @@ |
|||||||
package com.android.base.data |
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* @author Ztiany |
|
||||||
* Email: ztiany3@gmail.com |
|
||||||
* Date : 2018-05-15 16:23 |
|
||||||
*/ |
|
||||||
class Resource<T> private constructor( |
|
||||||
private val error: Throwable?, |
|
||||||
//data or default data |
|
||||||
private val data: T?, |
|
||||||
private val status: Status |
|
||||||
) { |
|
||||||
|
|
||||||
val isSuccess: Boolean |
|
||||||
get() = status == Status.SUCCESS |
|
||||||
|
|
||||||
val isNoChange: Boolean |
|
||||||
get() = status == Status.NOT_CHANGED |
|
||||||
|
|
||||||
val isLoading: Boolean |
|
||||||
get() = status == Status.LOADING |
|
||||||
|
|
||||||
val isError: Boolean |
|
||||||
get() = status == Status.ERROR |
|
||||||
|
|
||||||
fun hasData(): Boolean { |
|
||||||
return data != null |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 获取 Resource 中保存的数据,只有在 success 状态并且存在数据时下才能调用此方法,否则将抛出异常。 |
|
||||||
* |
|
||||||
* @return Resource 中保存的数据。 |
|
||||||
* @throws UnsupportedOperationException 非 success 状态调用此方法。 |
|
||||||
* @throws NullPointerException Resource 中没有保存数据时调用此方法。 |
|
||||||
*/ |
|
||||||
fun data(): T { |
|
||||||
if (isError || isLoading || isNoChange) { |
|
||||||
throw UnsupportedOperationException("This method can only be called when the is state success") |
|
||||||
} |
|
||||||
if (data == null) { |
|
||||||
throw NullPointerException("Data is null") |
|
||||||
} |
|
||||||
return data |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 获取 Resource 中保存的数据,如果不存在数据则返回 defaultData 所设置的默认数据,在不同状态下获取的数据具有不同的意义: |
|
||||||
* |
|
||||||
* * success 状态下,返回一个成功操作所产生的数据 |
|
||||||
* * error 状态下,返回一个默认的数据,如果存在的话 |
|
||||||
* * loading 状态下,返回一个默认的数据,如果存在的话 |
|
||||||
* |
|
||||||
* @param defaultData 如果不存在数据则返回 defaultData 所设置的默认数据。 |
|
||||||
* @return Resource 中保存的数据。 |
|
||||||
*/ |
|
||||||
fun orElse(defaultData: T?): T? { |
|
||||||
return data ?: defaultData |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 获取 Resource 中保存的数据,在不同状态下获取的数据具有不同的意义: |
|
||||||
* |
|
||||||
* * success 状态下,返回一个成功操作所产生的数据 |
|
||||||
* * error 状态下,返回一个默认的数据,如果存在的话 |
|
||||||
* * loading 状态下,返回一个默认的数据,如果存在的话 |
|
||||||
* |
|
||||||
* @return Resource 中保存的数据。 |
|
||||||
*/ |
|
||||||
fun get(): T? { |
|
||||||
return data |
|
||||||
} |
|
||||||
|
|
||||||
fun error(): Throwable { |
|
||||||
return error ?: throw NullPointerException("This method can only be called when the state is error") |
|
||||||
} |
|
||||||
|
|
||||||
override fun toString(): String { |
|
||||||
return "Resource{" + |
|
||||||
"mError=" + error + |
|
||||||
", mStatus=" + status + |
|
||||||
", mData=" + data + |
|
||||||
'}'.toString() |
|
||||||
} |
|
||||||
|
|
||||||
companion object { |
|
||||||
|
|
||||||
fun <T> success(): Resource<T> { |
|
||||||
return Resource(null, null, Status.SUCCESS) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> success(data: T?): Resource<T> { |
|
||||||
return Resource(null, data, Status.SUCCESS) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> error(error: Throwable): Resource<T> { |
|
||||||
return error(error, null) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 创建一个 error 状态,且设置一个默认的数据 |
|
||||||
*/ |
|
||||||
fun <T> error(error: Throwable, defaultValue: T?): Resource<T> { |
|
||||||
return Resource(error, defaultValue, Status.ERROR) |
|
||||||
} |
|
||||||
|
|
||||||
fun <T> loading(): Resource<T> { |
|
||||||
return loading(null) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 创建一个 loading 状态,且设置一个默认的数据 |
|
||||||
*/ |
|
||||||
fun <T> loading(defaultValue: T?): Resource<T> { |
|
||||||
return Resource(null, defaultValue, Status.LOADING) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 如果数据源(比如 Repository)缓存了上一次请求的数据,然后对其当前请求返回的数据,发现数据是一样的,可以使用此状态表示 |
|
||||||
* |
|
||||||
* @return Resource |
|
||||||
*/ |
|
||||||
fun <T> noChange(): Resource<T> { |
|
||||||
return Resource(null, null, Status.NOT_CHANGED) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
/**when in loading*/ |
|
||||||
inline fun <T> Resource<T>.onLoading(onLoading: () -> Unit): Resource<T> { |
|
||||||
if (this.isLoading) { |
|
||||||
onLoading() |
|
||||||
} |
|
||||||
return this |
|
||||||
} |
|
||||||
|
|
||||||
/**when error occurred*/ |
|
||||||
inline fun <T> Resource<T>.onError(onError: (error: Throwable) -> Unit): Resource<T> { |
|
||||||
if (this.isError) { |
|
||||||
onError(error()) |
|
||||||
} |
|
||||||
return this |
|
||||||
} |
|
||||||
|
|
||||||
/**when no change*/ |
|
||||||
inline fun <T> Resource<T>.onNoChange(onNoChange: () -> Unit): Resource<T> { |
|
||||||
if (this.isNoChange) { |
|
||||||
onNoChange() |
|
||||||
} |
|
||||||
return this |
|
||||||
} |
|
||||||
|
|
||||||
/**when succeeded*/ |
|
||||||
inline fun <T> Resource<T>.onSuccess(onSuccess: (data: T?) -> Unit): Resource<T> { |
|
||||||
if (this.isSuccess) { |
|
||||||
onSuccess(this.orElse(null)) |
|
||||||
} |
|
||||||
return this |
|
||||||
} |
|
||||||
|
|
||||||
/**when succeeded and has data*/ |
|
||||||
inline fun <T> Resource<T>.onSuccessWithData(onSuccess: (data: T) -> Unit): Resource<T> { |
|
||||||
val t = this.get() |
|
||||||
if (this.isSuccess && t != null) { |
|
||||||
onSuccess(t) |
|
||||||
} |
|
||||||
return this |
|
||||||
} |
|
@ -0,0 +1,203 @@ |
|||||||
|
package com.android.base.data |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* @author Ztiany |
||||||
|
* Email: ztiany3@gmail.com |
||||||
|
* Date : 2018-05-15 16:23 |
||||||
|
*/ |
||||||
|
class State<T> private constructor( |
||||||
|
private val error: Throwable?, |
||||||
|
//data or default data |
||||||
|
private val data: T?, |
||||||
|
private val status: Int |
||||||
|
) { |
||||||
|
|
||||||
|
val isSuccess: Boolean |
||||||
|
get() = status == SUCCESS |
||||||
|
|
||||||
|
val isNoChange: Boolean |
||||||
|
get() = status == NOT_CHANGED |
||||||
|
|
||||||
|
val isLoading: Boolean |
||||||
|
get() = status == LOADING |
||||||
|
|
||||||
|
val isError: Boolean |
||||||
|
get() = status == ERROR |
||||||
|
|
||||||
|
fun hasData(): Boolean { |
||||||
|
return data != null |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取 State 中保存的数据,只有在 success 状态并且存在数据时下才能调用此方法,否则将抛出异常。 |
||||||
|
* |
||||||
|
* @return State 中保存的数据。 |
||||||
|
* @throws UnsupportedOperationException 非 success 状态调用此方法。 |
||||||
|
* @throws NullPointerException State 中没有保存数据时调用此方法。 |
||||||
|
*/ |
||||||
|
fun data(): T { |
||||||
|
if (isError || isLoading || isNoChange) { |
||||||
|
throw UnsupportedOperationException("This method can only be called when the is state success") |
||||||
|
} |
||||||
|
if (data == null) { |
||||||
|
throw NullPointerException("Data is null") |
||||||
|
} |
||||||
|
return data |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取 State 中保存的数据,如果不存在数据则返回 defaultData 所设置的默认数据,在不同状态下获取的数据具有不同的意义: |
||||||
|
* |
||||||
|
* * success 状态下,返回一个成功操作所产生的数据 |
||||||
|
* * error 状态下,返回一个默认的数据,如果存在的话 |
||||||
|
* * loading 状态下,返回一个默认的数据,如果存在的话 |
||||||
|
* |
||||||
|
* @param defaultData 如果不存在数据则返回 defaultData 所设置的默认数据。 |
||||||
|
* @return State 中保存的数据。 |
||||||
|
*/ |
||||||
|
fun orElse(defaultData: T?): T? { |
||||||
|
return data ?: defaultData |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取 State 中保存的数据,在不同状态下获取的数据具有不同的意义: |
||||||
|
* |
||||||
|
* * success 状态下,返回一个成功操作所产生的数据 |
||||||
|
* * error 状态下,返回一个默认的数据,如果存在的话 |
||||||
|
* * loading 状态下,返回一个默认的数据,如果存在的话 |
||||||
|
* |
||||||
|
* @return State 中保存的数据。 |
||||||
|
*/ |
||||||
|
fun get(): T? { |
||||||
|
return data |
||||||
|
} |
||||||
|
|
||||||
|
fun error(): Throwable { |
||||||
|
return error |
||||||
|
?: throw NullPointerException("This method can only be called when the state is error") |
||||||
|
} |
||||||
|
|
||||||
|
override fun toString(): String { |
||||||
|
return "State{" + |
||||||
|
"mError=" + error + |
||||||
|
", mStatus=" + status + |
||||||
|
", mData=" + data + |
||||||
|
'}'.toString() |
||||||
|
} |
||||||
|
|
||||||
|
companion object { |
||||||
|
|
||||||
|
private const val LOADING = 1 |
||||||
|
private const val ERROR = 2 |
||||||
|
private const val SUCCESS = 3 |
||||||
|
private const val NOT_CHANGED = 4 |
||||||
|
|
||||||
|
fun <T> success(): State<T> { |
||||||
|
return State(null, null, SUCCESS) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> success(data: T?): State<T> { |
||||||
|
return State(null, data, SUCCESS) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> error(error: Throwable): State<T> { |
||||||
|
return error(error, null) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建一个 error 状态,且设置一个默认的数据 |
||||||
|
*/ |
||||||
|
fun <T> error(error: Throwable, defaultValue: T?): State<T> { |
||||||
|
return State(error, defaultValue, ERROR) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> loading(): State<T> { |
||||||
|
return loading(null) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建一个 loading 状态,且设置一个默认的数据 |
||||||
|
*/ |
||||||
|
fun <T> loading(defaultValue: T?): State<T> { |
||||||
|
return State(null, defaultValue, LOADING) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果数据源(比如 Repository)缓存了上一次请求的数据,然后对其当前请求返回的数据,发现数据是一样的,可以使用此状态表示 |
||||||
|
* |
||||||
|
* @return State |
||||||
|
*/ |
||||||
|
fun <T> noChange(): State<T> { |
||||||
|
return State(null, null, NOT_CHANGED) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
class StateHandler<T> { |
||||||
|
var onError: ((Throwable) -> Unit)? = null |
||||||
|
var onLoading: (() -> Unit)? = null |
||||||
|
var onSuccess: ((T?) -> Unit)? = null |
||||||
|
var onSuccessWithData: ((T) -> Unit)? = null |
||||||
|
var onEmpty: (() -> Unit)? = null |
||||||
|
} |
||||||
|
|
||||||
|
/**handle all state*/ |
||||||
|
inline fun <T> State<T>.handleState(handler: StateHandler<T>.() -> Unit) { |
||||||
|
val stateHandler = StateHandler<T>() |
||||||
|
handler(stateHandler) |
||||||
|
when { |
||||||
|
isError -> stateHandler.onError?.invoke(error()) |
||||||
|
isLoading -> stateHandler.onLoading?.invoke() |
||||||
|
isSuccess -> { |
||||||
|
stateHandler.onSuccess?.invoke(get()) |
||||||
|
if (hasData()) { |
||||||
|
stateHandler.onSuccessWithData?.invoke(data()) |
||||||
|
} else { |
||||||
|
stateHandler.onEmpty?.invoke() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**when in loading*/ |
||||||
|
inline fun <T> State<T>.onLoading(onLoading: () -> Unit): State<T> { |
||||||
|
if (this.isLoading) { |
||||||
|
onLoading() |
||||||
|
} |
||||||
|
return this |
||||||
|
} |
||||||
|
|
||||||
|
/**when error occurred*/ |
||||||
|
inline fun <T> State<T>.onError(onError: (error: Throwable) -> Unit): State<T> { |
||||||
|
if (this.isError) { |
||||||
|
onError(error()) |
||||||
|
} |
||||||
|
return this |
||||||
|
} |
||||||
|
|
||||||
|
/**when no change*/ |
||||||
|
inline fun <T> State<T>.onNoChange(onNoChange: () -> Unit): State<T> { |
||||||
|
if (this.isNoChange) { |
||||||
|
onNoChange() |
||||||
|
} |
||||||
|
return this |
||||||
|
} |
||||||
|
|
||||||
|
/**when succeeded*/ |
||||||
|
inline fun <T> State<T>.onSuccess(onSuccess: (data: T?) -> Unit): State<T> { |
||||||
|
if (this.isSuccess) { |
||||||
|
onSuccess(this.orElse(null)) |
||||||
|
} |
||||||
|
return this |
||||||
|
} |
||||||
|
|
||||||
|
/**when succeeded and has data*/ |
||||||
|
inline fun <T> State<T>.onSuccessWithData(onSuccess: (data: T) -> Unit): State<T> { |
||||||
|
val t = this.get() |
||||||
|
if (this.isSuccess && t != null) { |
||||||
|
onSuccess(t) |
||||||
|
} |
||||||
|
return this |
||||||
|
} |
@ -1,15 +0,0 @@ |
|||||||
package com.android.base.data |
|
||||||
|
|
||||||
/** |
|
||||||
* 用于表示各种状态 |
|
||||||
* |
|
||||||
* @author Ztiany |
|
||||||
* Email: ztiany3@gmail.com |
|
||||||
* Date : 2019-02-18 11:49 |
|
||||||
*/ |
|
||||||
enum class Status { |
|
||||||
LOADING, |
|
||||||
ERROR, |
|
||||||
SUCCESS, |
|
||||||
NOT_CHANGED |
|
||||||
} |
|
@ -0,0 +1,207 @@ |
|||||||
|
package com.android.base.rx |
||||||
|
|
||||||
|
import androidx.lifecycle.LiveData |
||||||
|
import androidx.lifecycle.MutableLiveData |
||||||
|
import com.android.base.data.State |
||||||
|
import com.github.dmstocking.optional.java.util.Optional |
||||||
|
import io.reactivex.Completable |
||||||
|
import io.reactivex.Flowable |
||||||
|
import io.reactivex.Observable |
||||||
|
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------------- |
||||||
|
fun <T> Observable<T>.subscribeWithLiveData(liveData: MutableLiveData<State<T>>) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
liveData.postValue(State.success(it)) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T, R> Observable<T>.subscribeWithLiveData(liveData: MutableLiveData<State<R>>, map: (T) -> R) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
liveData.postValue(State.success(map(it))) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Observable<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<State<T>>) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
liveData.postValue(State.success(it.orElse(null))) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T, R> Observable<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<State<R>>, map: (T?) -> R?) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
val value = map(it.orElse(null)) |
||||||
|
liveData.postValue(State.success(value)) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Flowable<T>.subscribeWithLiveData(liveData: MutableLiveData<State<T>>) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
liveData.postValue(State.success(it)) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T, R> Flowable<T>.subscribeWithLiveData(liveData: MutableLiveData<State<R>>, map: (T) -> R) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
liveData.postValue(State.success(map(it))) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Flowable<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<State<T>>) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
liveData.postValue(State.success(it.orElse(null))) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T, R> Flowable<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<State<R>>, map: (T?) -> R?) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
val value = map(it.orElse(null)) |
||||||
|
liveData.postValue(State.success(value)) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
fun Completable.subscribeWithLiveData(liveData: MutableLiveData<State<Any>>) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
liveData.postValue(State.success()) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Completable.subscribeWithLiveData(liveData: MutableLiveData<State<T>>, provider: () -> T) { |
||||||
|
liveData.postValue(State.loading()) |
||||||
|
this.subscribe( |
||||||
|
{ |
||||||
|
liveData.postValue(State.success(provider())) |
||||||
|
}, |
||||||
|
{ |
||||||
|
liveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------------- |
||||||
|
|
||||||
|
fun <T> Observable<T>.toResourceLiveData(): LiveData<State<T>> { |
||||||
|
val mutableLiveData = MutableLiveData<State<T>>() |
||||||
|
mutableLiveData.value = State.loading() |
||||||
|
subscribe( |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.success(it)) |
||||||
|
}, |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
return mutableLiveData |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Observable<Optional<T>>.optionalToResourceLiveData(): LiveData<State<T>> { |
||||||
|
val mutableLiveData = MutableLiveData<State<T>>() |
||||||
|
mutableLiveData.value = State.loading() |
||||||
|
subscribe( |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.success(it.orElse(null))) |
||||||
|
}, |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
return mutableLiveData |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Flowable<T>.toResourceLiveData(): LiveData<State<T>> { |
||||||
|
val mutableLiveData = MutableLiveData<State<T>>() |
||||||
|
mutableLiveData.value = State.loading() |
||||||
|
subscribe( |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.success(it)) |
||||||
|
}, |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
return mutableLiveData |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Flowable<Optional<T>>.optionalToResourceLiveData(): LiveData<State<T>> { |
||||||
|
val mutableLiveData = MutableLiveData<State<T>>() |
||||||
|
mutableLiveData.value = State.loading() |
||||||
|
subscribe( |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.success(it.orElse(null))) |
||||||
|
}, |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
return mutableLiveData |
||||||
|
} |
||||||
|
|
||||||
|
fun Completable.toResourceLiveData(): LiveData<State<Any>> { |
||||||
|
val mutableLiveData = MutableLiveData<State<Any>>() |
||||||
|
mutableLiveData.value = State.loading() |
||||||
|
subscribe( |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.success()) |
||||||
|
}, |
||||||
|
{ |
||||||
|
mutableLiveData.postValue(State.error(it)) |
||||||
|
} |
||||||
|
) |
||||||
|
return mutableLiveData |
||||||
|
} |
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------------- |
@ -0,0 +1,40 @@ |
|||||||
|
package com.android.base.rx |
||||||
|
|
||||||
|
import androidx.lifecycle.LiveData |
||||||
|
import androidx.lifecycle.MutableLiveData |
||||||
|
import io.reactivex.Flowable |
||||||
|
import io.reactivex.Maybe |
||||||
|
import io.reactivex.Observable |
||||||
|
import io.reactivex.Single |
||||||
|
|
||||||
|
fun <T> Observable<T>.toLiveData(): LiveData<T> { |
||||||
|
val liveData = MutableLiveData<T>() |
||||||
|
this.subscribeIgnoreError { |
||||||
|
liveData.postValue(it) |
||||||
|
} |
||||||
|
return liveData |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Flowable<T>.toLiveData(): LiveData<T> { |
||||||
|
val liveData = MutableLiveData<T>() |
||||||
|
this.subscribeIgnoreError { |
||||||
|
liveData.postValue(it) |
||||||
|
} |
||||||
|
return liveData |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Single<T>.toLiveData(): LiveData<T> { |
||||||
|
val liveData = MutableLiveData<T>() |
||||||
|
this.subscribeIgnoreError { |
||||||
|
liveData.postValue(it) |
||||||
|
} |
||||||
|
return liveData |
||||||
|
} |
||||||
|
|
||||||
|
fun <T> Maybe<T>.toLiveData(): LiveData<T> { |
||||||
|
val liveData = MutableLiveData<T>() |
||||||
|
this.subscribeIgnoreError { |
||||||
|
liveData.postValue(it) |
||||||
|
} |
||||||
|
return liveData |
||||||
|
} |
@ -1,204 +1,203 @@ |
|||||||
package com.android.base.app.aac |
package com.android.base.rx.autodispose |
||||||
|
|
||||||
import androidx.lifecycle.LiveData |
import androidx.lifecycle.LiveData |
||||||
import androidx.lifecycle.MutableLiveData |
import androidx.lifecycle.MutableLiveData |
||||||
import com.android.base.data.Resource |
import com.android.base.data.State |
||||||
import com.android.base.rx.subscribeIgnoreError |
|
||||||
import com.github.dmstocking.optional.java.util.Optional |
import com.github.dmstocking.optional.java.util.Optional |
||||||
import com.uber.autodispose.* |
import com.uber.autodispose.* |
||||||
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------------- |
//----------------------------------------------------------------------------------------- |
||||||
|
|
||||||
fun <T> ObservableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>) { |
fun <T> ObservableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<State<T>>) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.success(it)) |
liveData.postValue(State.success(it)) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
fun <T, R> ObservableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<R>>, map: (T) -> R) { |
fun <T, R> ObservableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<State<R>>, map: (T) -> R) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.success(map(it))) |
liveData.postValue(State.success(map(it))) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
fun <T> ObservableSubscribeProxy<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<Resource<T>>) { |
fun <T> ObservableSubscribeProxy<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<State<T>>) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.success(it.orElse(null))) |
liveData.postValue(State.success(it.orElse(null))) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
fun <T, R> ObservableSubscribeProxy<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<Resource<R>>, map: (T?) -> R?) { |
fun <T, R> ObservableSubscribeProxy<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<State<R>>, map: (T?) -> R?) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
val value = map(it.orElse(null)) |
val value = map(it.orElse(null)) |
||||||
liveData.postValue(Resource.success(value)) |
liveData.postValue(State.success(value)) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
fun <T> FlowableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>) { |
fun <T> FlowableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<State<T>>) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.success(it)) |
liveData.postValue(State.success(it)) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
fun <T, R> FlowableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<Resource<R>>, map: (T) -> R) { |
fun <T, R> FlowableSubscribeProxy<T>.subscribeWithLiveData(liveData: MutableLiveData<State<R>>, map: (T) -> R) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.success(map(it))) |
liveData.postValue(State.success(map(it))) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
fun <T> FlowableSubscribeProxy<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<Resource<T>>) { |
fun <T> FlowableSubscribeProxy<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<State<T>>) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.success(it.orElse(null))) |
liveData.postValue(State.success(it.orElse(null))) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
fun <T, R> FlowableSubscribeProxy<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<Resource<R>>, map: (T?) -> R?) { |
fun <T, R> FlowableSubscribeProxy<Optional<T>>.subscribeOptionalWithLiveData(liveData: MutableLiveData<State<R>>, map: (T?) -> R?) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
val value = map(it.orElse(null)) |
val value = map(it.orElse(null)) |
||||||
liveData.postValue(Resource.success(value)) |
liveData.postValue(State.success(value)) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
fun CompletableSubscribeProxy.subscribeWithLiveData(liveData: MutableLiveData<Resource<Any>>) { |
fun CompletableSubscribeProxy.subscribeWithLiveData(liveData: MutableLiveData<State<Any>>) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.success()) |
liveData.postValue(State.success()) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
fun <T> CompletableSubscribeProxy.subscribeWithLiveData(liveData: MutableLiveData<Resource<T>>, provider: () -> T) { |
fun <T> CompletableSubscribeProxy.subscribeWithLiveData(liveData: MutableLiveData<State<T>>, provider: () -> T) { |
||||||
liveData.postValue(Resource.loading()) |
liveData.postValue(State.loading()) |
||||||
this.subscribe( |
this.subscribe( |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.success(provider())) |
liveData.postValue(State.success(provider())) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
liveData.postValue(Resource.error(it)) |
liveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
} |
} |
||||||
|
|
||||||
//----------------------------------------------------------------------------------------- |
//----------------------------------------------------------------------------------------- |
||||||
|
|
||||||
fun <T> ObservableSubscribeProxy<T>.toResourceLiveData(): LiveData<Resource<T>> { |
fun <T> ObservableSubscribeProxy<T>.toResourceLiveData(): LiveData<State<T>> { |
||||||
val mutableLiveData = MutableLiveData<Resource<T>>() |
val mutableLiveData = MutableLiveData<State<T>>() |
||||||
mutableLiveData.value = Resource.loading() |
mutableLiveData.value = State.loading() |
||||||
subscribe( |
subscribe( |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.success(it)) |
mutableLiveData.postValue(State.success(it)) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.error(it)) |
mutableLiveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
return mutableLiveData |
return mutableLiveData |
||||||
} |
} |
||||||
|
|
||||||
fun <T> ObservableSubscribeProxy<Optional<T>>.optionalToResourceLiveData(): LiveData<Resource<T>> { |
fun <T> ObservableSubscribeProxy<Optional<T>>.optionalToResourceLiveData(): LiveData<State<T>> { |
||||||
val mutableLiveData = MutableLiveData<Resource<T>>() |
val mutableLiveData = MutableLiveData<State<T>>() |
||||||
mutableLiveData.value = Resource.loading() |
mutableLiveData.value = State.loading() |
||||||
subscribe( |
subscribe( |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.success(it.orElse(null))) |
mutableLiveData.postValue(State.success(it.orElse(null))) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.error(it)) |
mutableLiveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
return mutableLiveData |
return mutableLiveData |
||||||
} |
} |
||||||
|
|
||||||
fun <T> FlowableSubscribeProxy<T>.toResourceLiveData(): LiveData<Resource<T>> { |
fun <T> FlowableSubscribeProxy<T>.toResourceLiveData(): LiveData<State<T>> { |
||||||
val mutableLiveData = MutableLiveData<Resource<T>>() |
val mutableLiveData = MutableLiveData<State<T>>() |
||||||
mutableLiveData.value = Resource.loading() |
mutableLiveData.value = State.loading() |
||||||
subscribe( |
subscribe( |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.success(it)) |
mutableLiveData.postValue(State.success(it)) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.error(it)) |
mutableLiveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
return mutableLiveData |
return mutableLiveData |
||||||
} |
} |
||||||
|
|
||||||
fun <T> FlowableSubscribeProxy<Optional<T>>.optionalToResourceLiveData(): LiveData<Resource<T>> { |
fun <T> FlowableSubscribeProxy<Optional<T>>.optionalToResourceLiveData(): LiveData<State<T>> { |
||||||
val mutableLiveData = MutableLiveData<Resource<T>>() |
val mutableLiveData = MutableLiveData<State<T>>() |
||||||
mutableLiveData.value = Resource.loading() |
mutableLiveData.value = State.loading() |
||||||
subscribe( |
subscribe( |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.success(it.orElse(null))) |
mutableLiveData.postValue(State.success(it.orElse(null))) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.error(it)) |
mutableLiveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
return mutableLiveData |
return mutableLiveData |
||||||
} |
} |
||||||
|
|
||||||
fun CompletableSubscribeProxy.toResourceLiveData(): LiveData<Resource<Any>> { |
fun CompletableSubscribeProxy.toResourceLiveData(): LiveData<State<Any>> { |
||||||
val mutableLiveData = MutableLiveData<Resource<Any>>() |
val mutableLiveData = MutableLiveData<State<Any>>() |
||||||
mutableLiveData.value = Resource.loading() |
mutableLiveData.value = State.loading() |
||||||
subscribe( |
subscribe( |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.success()) |
mutableLiveData.postValue(State.success()) |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
mutableLiveData.postValue(Resource.error(it)) |
mutableLiveData.postValue(State.error(it)) |
||||||
} |
} |
||||||
) |
) |
||||||
return mutableLiveData |
return mutableLiveData |
@ -0,0 +1,12 @@ |
|||||||
|
package com.android.base.utils.security |
||||||
|
|
||||||
|
import android.annotation.SuppressLint |
||||||
|
|
||||||
|
fun md5(content: String): String { |
||||||
|
return MD5Utils.md5(content) |
||||||
|
} |
||||||
|
|
||||||
|
@SuppressLint("DefaultLocale") |
||||||
|
fun md5UpperCase(content: String): String { |
||||||
|
return MD5Utils.md5(content).toUpperCase() |
||||||
|
} |
Loading…
Reference in new issue