fix: handle exceptions when checking for bundle updates

This commit is contained in:
Ax333l 2023-10-20 19:43:26 +02:00
parent 5290713504
commit 32e8a37f33
No known key found for this signature in database
GPG Key ID: D2B4D85271127D23
2 changed files with 33 additions and 19 deletions

View File

@ -3,6 +3,7 @@ package app.revanced.manager.domain.repository
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import app.revanced.manager.R
import app.revanced.manager.data.platform.NetworkInfo import app.revanced.manager.data.platform.NetworkInfo
import app.revanced.manager.data.room.bundles.PatchBundleEntity import app.revanced.manager.data.room.bundles.PatchBundleEntity
import app.revanced.manager.domain.bundles.APIPatchBundle 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.domain.bundles.PatchBundleSource
import app.revanced.manager.util.flatMapLatestAndCombine import app.revanced.manager.util.flatMapLatestAndCombine
import app.revanced.manager.util.tag import app.revanced.manager.util.tag
import app.revanced.manager.util.uiSafe
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.InputStream import java.io.InputStream
class PatchBundleRepository( class PatchBundleRepository(
app: Application, private val app: Application,
private val persistenceRepo: PatchBundlePersistenceRepository, private val persistenceRepo: PatchBundlePersistenceRepository,
private val networkInfo: NetworkInfo, private val networkInfo: NetworkInfo,
) { ) {
@ -124,12 +126,15 @@ class PatchBundleRepository(
reload() reload()
} }
suspend fun redownloadRemoteBundles() = getBundlesByType<RemotePatchBundle>().forEach { it.downloadLatest() } suspend fun redownloadRemoteBundles() =
getBundlesByType<RemotePatchBundle>().forEach { it.downloadLatest() }
suspend fun updateCheck() = supervisorScope { suspend fun updateCheck() =
uiSafe(app, R.string.source_download_fail, "Failed to update bundles") {
coroutineScope {
if (!networkInfo.isSafe()) { if (!networkInfo.isSafe()) {
Log.d(tag, "Skipping update check because the network is down or metered.") Log.d(tag, "Skipping update check because the network is down or metered.")
return@supervisorScope return@coroutineScope
} }
getBundlesByType<RemotePatchBundle>().forEach { getBundlesByType<RemotePatchBundle>().forEach {
@ -141,3 +146,4 @@ class PatchBundleRepository(
} }
} }
} }
}

View File

@ -13,7 +13,10 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest 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 logMsg The log message.
* @param block The code to execute. * @param block The code to execute.
*/ */
@OptIn(DelicateCoroutinesApi::class)
inline fun uiSafe(context: Context, @StringRes toastMsg: Int, logMsg: String, block: () -> Unit) { inline fun uiSafe(context: Context, @StringRes toastMsg: Int, logMsg: String, block: () -> Unit) {
try { try {
block() block()
} catch (error: Exception) { } catch (error: Exception) {
// You can only toast on the main thread.
GlobalScope.launch(Dispatchers.Main) {
context.toast( context.toast(
context.getString( context.getString(
toastMsg, toastMsg,
error.simpleMessage() error.simpleMessage()
) )
) )
}
Log.e(tag, logMsg, error) Log.e(tag, logMsg, error)
} }
} }