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

166 lines
5.5 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
2021-07-08 18:03:17 +00:00
import com.bumptech.glide.load.HttpException
import com.lagradost.cloudstream3.BuildConfig
2021-08-04 01:50:24 +00:00
import com.lagradost.cloudstream3.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-12-21 18:58:07 +00:00
import javax.net.ssl.SSLHandshakeException
2021-05-12 21:51:02 +00:00
const val DEBUG_EXCEPTION = "THIS IS A DEBUG EXCEPTION!"
class DebugException(message: String) : Exception("$DEBUG_EXCEPTION\n$message")
inline fun debugException(message: () -> String) {
if (BuildConfig.DEBUG) {
throw DebugException(message.invoke())
}
}
inline fun debugWarning(message: () -> String) {
if (BuildConfig.DEBUG) {
logError(DebugException(message.invoke()))
}
}
inline fun debugAssert(assert: () -> Boolean, message: () -> String) {
if (BuildConfig.DEBUG && assert.invoke()) {
throw DebugException(message.invoke())
}
}
inline fun debugWarning(assert: () -> Boolean, message: () -> String) {
if (BuildConfig.DEBUG && assert.invoke()) {
logError(DebugException(message.invoke()))
}
}
2021-05-12 21:51:02 +00:00
fun <T> LifecycleOwner.observe(liveData: LiveData<T>, action: (t: T) -> Unit) {
2021-08-29 18:42:44 +00:00
liveData.observe(this) { it?.let { t -> action(t) } }
2021-05-12 21:51:02 +00:00
}
2021-05-23 17:07:43 +00:00
fun <T> LifecycleOwner.observeDirectly(liveData: LiveData<T>, action: (t: T) -> Unit) {
2021-08-29 18:42:44 +00:00
liveData.observe(this) { it?.let { t -> action(t) } }
2021-05-23 17:07:43 +00:00
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-08-04 01:50:24 +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", "-------------------------------------------------------------------")
}
2021-08-04 01:50:24 +00:00
fun <T> normalSafeApiCall(apiCall: () -> T): T? {
2021-06-17 16:20:05 +00:00
return try {
apiCall.invoke()
} catch (throwable: Throwable) {
logError(throwable)
return null
}
}
suspend fun <T> suspendSafeApiCall(apiCall: suspend () -> T): T? {
return try {
apiCall.invoke()
} catch (throwable: Throwable) {
logError(throwable)
return null
}
}
2021-12-21 18:58:07 +00:00
fun <T> safeFail(throwable: Throwable): Resource<T> {
val stackTraceMsg =
(throwable.localizedMessage ?: "") + "\n\n" + throwable.stackTrace.joinToString(
separator = "\n"
) {
"${it.fileName} ${it.lineNumber}"
}
2021-12-21 18:58:07 +00:00
return Resource.Failure(false, null, null, stackTraceMsg)
}
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) {
2021-12-21 18:58:07 +00:00
is NullPointerException -> {
for (line in throwable.stackTrace) {
2021-12-27 17:43:15 +00:00
if (line?.fileName?.endsWith("provider.kt", ignoreCase = true) == true) {
2021-12-21 18:58:07 +00:00
return@withContext Resource.Failure(
false,
null,
null,
"NullPointerException at ${line.fileName} ${line.lineNumber}\nSite might have updated or added Cloudflare/DDOS protection"
)
}
}
safeFail(throwable)
}
2021-05-12 21:51:02 +00:00
is SocketTimeoutException -> {
Resource.Failure(
true,
null,
null,
"Connection Timeout\nPlease try again later."
)
2021-07-08 18:03:17 +00:00
}
is HttpException -> {
Resource.Failure(
false,
throwable.statusCode,
null,
throwable.message ?: "HttpException"
)
2021-07-08 18:03:17 +00:00
}
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,
throwable.message ?: "Error loading, try again later."
)
2021-07-29 00:19:42 +00:00
}
2021-08-21 21:06:24 +00:00
is NotImplementedError -> {
Resource.Failure(false, null, null, "This operation is not implemented.")
}
2021-12-21 18:58:07 +00:00
is SSLHandshakeException -> {
Resource.Failure(
true,
null,
null,
(throwable.message ?: "SSLHandshakeException") + "\nTry again later."
)
2021-05-12 21:51:02 +00:00
}
2021-12-21 18:58:07 +00:00
else -> safeFail(throwable)
2021-05-12 21:51:02 +00:00
}
}
}
}