package com.lagradost.cloudstream3.mvvm import android.util.Log import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext fun LifecycleOwner.observe(liveData: LiveData, action: (t: T) -> Unit) { liveData.observe(this, Observer { it?.let { t -> action(t) } }) } fun LifecycleOwner.observeDirectly(liveData: LiveData, action: (t: T) -> Unit) { liveData.observe(this, Observer { it?.let { t -> action(t) } }) val currentValue = liveData.value if (currentValue != null) action(currentValue) } sealed class Resource { data class Success(val value: T) : Resource() data class Failure( val isNetworkError: Boolean, val errorCode: Int?, val errorResponse: Any?, //ResponseBody val errorString: String, ) : Resource() data class Loading(val url : String? = null) : Resource() } suspend fun safeApiCall( apiCall: suspend () -> T, ): Resource { return withContext(Dispatchers.IO) { try { Resource.Success(apiCall.invoke()) } catch (throwable: Throwable) { Log.d("ApiError", "-------------------------------------------------------------------") Log.d("ApiError", "safeApiCall: " + throwable.localizedMessage) Log.d("ApiError", "safeApiCall: " + throwable.message) throwable.printStackTrace() Log.d("ApiError", "-------------------------------------------------------------------") when (throwable) { /*is HttpException -> { Resource.Failure(false, throwable.code(), throwable.response()?.errorBody(), throwable.localizedMessage) } is SocketTimeoutException -> { Resource.Failure(true,null,null,"Please try again later.") } is UnknownHostException ->{ Resource.Failure(true,null,null,"Cannot connect to server, try again later.") }*/ else -> { Resource.Failure(true, null, null, throwable.localizedMessage) } } } } }