diff --git a/app/src/main/java/app/revanced/manager/domain/repository/PatchBundleRepository.kt b/app/src/main/java/app/revanced/manager/domain/repository/PatchBundleRepository.kt index ffdf7d13..2f7a8fe3 100644 --- a/app/src/main/java/app/revanced/manager/domain/repository/PatchBundleRepository.kt +++ b/app/src/main/java/app/revanced/manager/domain/repository/PatchBundleRepository.kt @@ -3,6 +3,7 @@ package app.revanced.manager.domain.repository import android.app.Application import android.content.Context import android.util.Log +import app.revanced.manager.R import app.revanced.manager.data.platform.NetworkInfo import app.revanced.manager.data.room.bundles.PatchBundleEntity import app.revanced.manager.domain.bundles.APIPatchBundle @@ -13,18 +14,19 @@ import app.revanced.manager.domain.bundles.RemotePatchBundle import app.revanced.manager.domain.bundles.PatchBundleSource import app.revanced.manager.util.flatMapLatestAndCombine import app.revanced.manager.util.tag +import app.revanced.manager.util.uiSafe import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch -import kotlinx.coroutines.supervisorScope import kotlinx.coroutines.withContext import java.io.InputStream class PatchBundleRepository( - app: Application, + private val app: Application, private val persistenceRepo: PatchBundlePersistenceRepository, private val networkInfo: NetworkInfo, ) { @@ -124,20 +126,24 @@ class PatchBundleRepository( reload() } - suspend fun redownloadRemoteBundles() = getBundlesByType().forEach { it.downloadLatest() } + suspend fun redownloadRemoteBundles() = + getBundlesByType().forEach { it.downloadLatest() } - suspend fun updateCheck() = supervisorScope { - if (!networkInfo.isSafe()) { - Log.d(tag, "Skipping update check because the network is down or metered.") - return@supervisorScope - } + suspend fun updateCheck() = + uiSafe(app, R.string.source_download_fail, "Failed to update bundles") { + coroutineScope { + if (!networkInfo.isSafe()) { + Log.d(tag, "Skipping update check because the network is down or metered.") + return@coroutineScope + } - getBundlesByType().forEach { - launch { - if (!it.propsFlow().first().autoUpdate) return@launch - Log.d(tag, "Updating patch bundle: ${it.name}") - it.update() + getBundlesByType().forEach { + launch { + if (!it.propsFlow().first().autoUpdate) return@launch + Log.d(tag, "Updating patch bundle: ${it.name}") + it.update() + } + } } } - } } \ No newline at end of file diff --git a/app/src/main/java/app/revanced/manager/util/Util.kt b/app/src/main/java/app/revanced/manager/util/Util.kt index ed10ea6d..908aa0f6 100644 --- a/app/src/main/java/app/revanced/manager/util/Util.kt +++ b/app/src/main/java/app/revanced/manager/util/Util.kt @@ -13,7 +13,10 @@ import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.flatMapLatest @@ -44,16 +47,21 @@ fun Context.toast(string: String, duration: Int = Toast.LENGTH_SHORT) { * @param logMsg The log message. * @param block The code to execute. */ +@OptIn(DelicateCoroutinesApi::class) inline fun uiSafe(context: Context, @StringRes toastMsg: Int, logMsg: String, block: () -> Unit) { try { block() } catch (error: Exception) { - context.toast( - context.getString( - toastMsg, - error.simpleMessage() + // You can only toast on the main thread. + GlobalScope.launch(Dispatchers.Main) { + context.toast( + context.getString( + toastMsg, + error.simpleMessage() + ) ) - ) + } + Log.e(tag, logMsg, error) } }