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