cloudstream/app/src/main/java/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt

85 lines
3 KiB
Kotlin
Raw Normal View History

2021-05-12 21:51:02 +00:00
package com.lagradost.cloudstream3.mvvm
import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
2021-07-08 18:03:17 +00:00
import com.bumptech.glide.load.HttpException
2021-07-29 00:19:42 +00:00
import com.lagradost.cloudstream3.ui.ErrorLoadingException
2021-05-12 21:51:02 +00:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
2021-07-08 18:03:17 +00:00
import java.net.SocketTimeoutException
import java.net.UnknownHostException
2021-05-12 21:51:02 +00:00
fun <T> LifecycleOwner.observe(liveData: LiveData<T>, action: (t: T) -> Unit) {
liveData.observe(this, Observer { it?.let { t -> action(t) } })
}
2021-05-23 17:07:43 +00:00
fun <T> LifecycleOwner.observeDirectly(liveData: LiveData<T>, action: (t: T) -> Unit) {
liveData.observe(this, Observer { it?.let { t -> action(t) } })
val currentValue = liveData.value
if (currentValue != null)
action(currentValue)
}
2021-05-12 21:51:02 +00:00
sealed class Resource<out T> {
data class Success<out T>(val value: T) : Resource<T>()
data class Failure(
val isNetworkError: Boolean,
val errorCode: Int?,
val errorResponse: Any?, //ResponseBody
val errorString: String,
) : Resource<Nothing>()
2021-06-16 16:54:07 +00:00
data class Loading(val url : String? = null) : Resource<Nothing>()
2021-05-12 21:51:02 +00:00
}
2021-06-17 16:20:05 +00:00
fun logError(throwable: Throwable) {
Log.d("ApiError", "-------------------------------------------------------------------")
Log.d("ApiError", "safeApiCall: " + throwable.localizedMessage)
Log.d("ApiError", "safeApiCall: " + throwable.message)
throwable.printStackTrace()
Log.d("ApiError", "-------------------------------------------------------------------")
}
fun<T> normalSafeApiCall(apiCall : () -> T) : T? {
return try {
apiCall.invoke()
} catch (throwable: Throwable) {
logError(throwable)
return null
}
}
2021-05-12 21:51:02 +00:00
suspend fun <T> safeApiCall(
apiCall: suspend () -> T,
): Resource<T> {
return withContext(Dispatchers.IO) {
try {
Resource.Success(apiCall.invoke())
} catch (throwable: Throwable) {
2021-06-17 16:20:05 +00:00
logError(throwable)
2021-05-12 21:51:02 +00:00
when (throwable) {
is SocketTimeoutException -> {
2021-07-08 18:03:17 +00:00
Resource.Failure(true, null, null, "Please try again later.")
}
is HttpException -> {
Resource.Failure(false, throwable.statusCode, null, throwable.localizedMessage)
}
is UnknownHostException -> {
Resource.Failure(true, null, null, "Cannot connect to server, try again later.")
2021-05-12 21:51:02 +00:00
}
2021-07-29 00:19:42 +00:00
is ErrorLoadingException -> {
Resource.Failure(true, null, null, "Error loading, try again later.")
}
2021-05-12 21:51:02 +00:00
else -> {
2021-07-08 18:03:17 +00:00
val stackTraceMsg = throwable.localizedMessage + "\n\n" + throwable.stackTrace.joinToString(
separator = "\n"
) {
"${it.fileName} ${it.lineNumber}"
}
Resource.Failure(false, null, null, stackTraceMsg) //
2021-05-12 21:51:02 +00:00
}
}
}
}
}