Skip to content

Instantly share code, notes, and snippets.

@gabriel-TheCode
Created October 29, 2021 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabriel-TheCode/4281ec1492e0c10ec5024a3b7be81765 to your computer and use it in GitHub Desktop.
Save gabriel-TheCode/4281ec1492e0c10ec5024a3b7be81765 to your computer and use it in GitHub Desktop.
LiveDataExtensions.kt
import androidx.lifecycle.*
inline fun <T> Resource<T>.mapData(crossinline mapper: (T) -> T?): Resource<T> {
val data = this.toData() ?: return this
val processedData = mapper(data)
return when (this) {
is Resource.Progress -> Resource.loading(progression, processedData)
is Resource.Success -> Resource.success(processedData)
is Resource.Failure -> Resource.failure(this.throwable, processedData)
}
}
inline fun <T, R> Resource<T>.transformData(crossinline mapper: (T?) -> R): Resource<R> {
val data = this.toData()
val processedData = mapper(data)
return when (this) {
is Resource.Progress -> Resource.loading(progression, processedData)
is Resource.Success -> Resource.success(processedData)
is Resource.Failure -> Resource.failure(this.throwable, processedData)
}
}
inline fun <T> Resource<T>?.processNotNull(crossinline process: (T) -> Unit) {
val data = this?.toData() ?: return
process(data)
}
fun <T> Resource<T>?.toProgressWithData(): Resource<T> {
return Resource.loading(null, this?.toData())
}
fun <T> Resource<T>?.toFailureWithData(error: Throwable): Resource<T> {
return Resource.failure(error, this?.toData())
}
fun <T> MutableLiveData<Resource<T>>.toProgress(progression: Int? = null, t: T? = null) {
this.value = Resource.loading(progression, t)
}
fun <T> MutableLiveData<Resource<T>>.toProgressWithData() {
this.value = value.toProgressWithData()
}
fun <T> MutableLiveData<Resource<T>>.toSuccess(t: T?) {
this.value = Resource.success(t)
}
fun <T> MutableLiveData<Resource<T>>.toSuccessWithData() {
this.value = Resource.success(value?.toData())
}
fun <T> MutableLiveData<Resource<T>>.toFailure(throwable: Throwable) {
this.value = Resource.failure(throwable, value?.toData())
}
fun <T> MutableLiveData<Resource<T>>.toFailureWithData(error: Throwable) {
this.value = value.toFailureWithData(error)
}
fun <T> MutableLiveData<Resource<T>>.toResourceWithData(newResource: Resource<T>) {
when (newResource) {
is Resource.Progress -> {
if (newResource.data == null) this.toProgressWithData() else this.value = newResource
}
is Resource.Success -> this.value = newResource
is Resource.Failure -> this.toFailureWithData(newResource.throwable)
}
}
fun <T> LiveData<Resource<T>>.isLoading(): Boolean {
return value is Resource.Progress
}
fun <T> LiveData<Resource<T>>.isLoadingOrSuccess(): Boolean {
return value is Resource.Progress || value is Resource.Success
}
fun <T> LiveData<Resource<T>>.isSuccess(): Boolean {
return value is Resource.Success
}
fun <T> MutableLiveData<Resource<T>>.postProgress(progression: Int? = null, t: T? = null) {
this.postValue(Resource.loading(progression, t))
}
fun <T> MutableLiveData<Resource<T>>.postSuccess(t: T? = null) {
this.postValue(Resource.success(t))
}
fun <T> MutableLiveData<Resource<T>>.postFailure(throwable: Throwable) {
this.postValue(Resource.failure(throwable, value?.toData()))
}
fun <T> LiveData<T>.ensureUniqueObserve(owner: LifecycleOwner, observer: Observer<T>) {
removeObserver(observer)
observe(owner, observer)
}
fun <T, K, R> LiveData<T>.combineWith(
liveData: LiveData<K>,
block: (T?, K?) -> R
): LiveData<R> {
val result = MediatorLiveData<R>()
result.addSource(this) {
result.value = block(this.value, liveData.value)
}
result.addSource(liveData) {
result.value = block(this.value, liveData.value)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment