refactor(update): типизированные ошибки обновления, тексты в ресурсах
This commit is contained in:
1 parent
a9489c40d2
commit
66fb675bd2
2 files changed
+25
-10
No files matched your search
@@ -8,6 +8,7 @@ import android.content.pm.Signature
|
||||
import android.os.Build
|
||||
import androidx.core.content.FileProvider
|
||||
import com.freeturn.app.data.config.toHex
|
||||
import com.freeturn.app.domain.UpdateError
|
||||
import com.freeturn.app.domain.UpdateState
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -46,7 +47,7 @@ class AppUpdater(private val context: Context) {
|
||||
val release = withContext(Dispatchers.IO) { fetchLatestRelease() }
|
||||
if (release == null) {
|
||||
_state.value = if (silent) UpdateState.Idle
|
||||
else UpdateState.Error("Не удалось получить информацию о релизе")
|
||||
else UpdateState.Error(UpdateError.RELEASE_UNAVAILABLE)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -58,7 +59,7 @@ class AppUpdater(private val context: Context) {
|
||||
_state.value = UpdateState.Available(remoteVersion)
|
||||
} else {
|
||||
_state.value = if (silent) UpdateState.Idle
|
||||
else UpdateState.Error("APK не найден в релизе")
|
||||
else UpdateState.Error(UpdateError.NO_APK)
|
||||
}
|
||||
} else {
|
||||
_state.value = UpdateState.NoUpdate
|
||||
@@ -68,13 +69,13 @@ class AppUpdater(private val context: Context) {
|
||||
throw e
|
||||
} catch (_: Exception) {
|
||||
_state.value = if (silent) UpdateState.Idle
|
||||
else UpdateState.Error("Нет соединения с сервером")
|
||||
else UpdateState.Error(UpdateError.NETWORK)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun downloadUpdate() {
|
||||
val url = latestApkUrl ?: run {
|
||||
_state.value = UpdateState.Error("URL обновления не найден")
|
||||
_state.value = UpdateState.Error(UpdateError.RELEASE_UNAVAILABLE)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -118,22 +119,22 @@ class AppUpdater(private val context: Context) {
|
||||
val trusted = withContext(Dispatchers.IO) { isSignedBySameCert(apkFile) }
|
||||
if (!trusted) {
|
||||
apkFile.delete()
|
||||
_state.value = UpdateState.Error("Подпись обновления не совпадает - установка отменена")
|
||||
_state.value = UpdateState.Error(UpdateError.SIGNATURE_MISMATCH)
|
||||
return
|
||||
}
|
||||
_state.value = UpdateState.ReadyToInstall
|
||||
} catch (e: CancellationException) {
|
||||
apkFile.delete()
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
} catch (_: Exception) {
|
||||
apkFile.delete()
|
||||
_state.value = UpdateState.Error("Ошибка загрузки: ${e.message}")
|
||||
_state.value = UpdateState.Error(UpdateError.DOWNLOAD_FAILED)
|
||||
}
|
||||
}
|
||||
|
||||
fun installUpdate() {
|
||||
if (!apkFile.exists()) {
|
||||
_state.value = UpdateState.Error("Файл обновления не найден")
|
||||
_state.value = UpdateState.Error(UpdateError.FILE_MISSING)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.content.Intent
|
||||
import android.os.PowerManager
|
||||
import android.provider.Settings
|
||||
import android.net.Uri
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.animation.AnimatedContent
|
||||
@@ -60,6 +61,7 @@ import androidx.lifecycle.LifecycleEventObserver
|
||||
import androidx.lifecycle.compose.LocalLifecycleOwner
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.freeturn.app.R
|
||||
import com.freeturn.app.domain.UpdateError
|
||||
import com.freeturn.app.domain.UpdateState
|
||||
import com.freeturn.app.ui.util.HapticUtil
|
||||
import com.freeturn.app.ui.components.BackupPasswordDialog
|
||||
@@ -264,6 +266,7 @@ fun AppScreen(
|
||||
text = { Text(stringResource(R.string.reset_all_settings_desc)) },
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
shapes = ButtonDefaults.shapes(),
|
||||
onClick = {
|
||||
showResetDialog = false
|
||||
backupViewModel.resetAllSettings()
|
||||
@@ -274,7 +277,7 @@ fun AppScreen(
|
||||
) { Text(stringResource(R.string.reset)) }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showResetDialog = false }) {
|
||||
TextButton(shapes = ButtonDefaults.shapes(), onClick = { showResetDialog = false }) {
|
||||
Text(stringResource(R.string.cancel))
|
||||
}
|
||||
}
|
||||
@@ -361,6 +364,7 @@ private fun UpdateCard(
|
||||
else -> onCheck
|
||||
}
|
||||
FilledTonalButton(
|
||||
shapes = ButtonDefaults.shapes(),
|
||||
onClick = action,
|
||||
enabled = state !is UpdateState.Checking,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
@@ -391,7 +395,17 @@ private fun updateStatusText(state: UpdateState, appVersion: String): String = w
|
||||
is UpdateState.Downloading -> stringResource(R.string.update_downloading, state.progress)
|
||||
is UpdateState.ReadyToInstall -> stringResource(R.string.update_ready_desc_short)
|
||||
is UpdateState.NoUpdate -> stringResource(R.string.update_no_update)
|
||||
is UpdateState.Error -> stringResource(R.string.update_error, state.message)
|
||||
is UpdateState.Error -> stringResource(state.reason.labelRes())
|
||||
}
|
||||
|
||||
@StringRes
|
||||
private fun UpdateError.labelRes(): Int = when (this) {
|
||||
UpdateError.RELEASE_UNAVAILABLE -> R.string.update_error_release
|
||||
UpdateError.NO_APK -> R.string.update_error_no_apk
|
||||
UpdateError.NETWORK -> R.string.update_error_network
|
||||
UpdateError.DOWNLOAD_FAILED -> R.string.update_error_download
|
||||
UpdateError.SIGNATURE_MISMATCH -> R.string.update_error_signature
|
||||
UpdateError.FILE_MISSING -> R.string.update_error_file
|
||||
}
|
||||
|
||||
/** Строка сброса: error-тинт иконки и заголовка, без trailing-шеврона. */
|
||||
|
||||
Reference in new issue
Block a user