From bafebfd2533851d9d8c0ae45dca90282bdf8ad92 Mon Sep 17 00:00:00 2001 From: Agzamus Date: Mon, 7 Sep 2026 00:31:34 +0300 Subject: [PATCH] fix: no auto download if updated disabled --- .../java/agzam/vyatsu/ui/settings/SettingsScreen.kt | 10 ++++++---- app/src/main/java/agzam/vyatsu/update/AppUpdater.kt | 9 +++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/agzam/vyatsu/ui/settings/SettingsScreen.kt b/app/src/main/java/agzam/vyatsu/ui/settings/SettingsScreen.kt index 19da39e..0c47d7f 100644 --- a/app/src/main/java/agzam/vyatsu/ui/settings/SettingsScreen.kt +++ b/app/src/main/java/agzam/vyatsu/ui/settings/SettingsScreen.kt @@ -50,12 +50,12 @@ fun SettingsScreen(viewModel: UpdateViewModel) { item { SettingsItem( title = "Проверить наличие обновлений", - subtitle = updateStatusText(updateState), + subtitle = updateStatusText(updateState, autoEnabled), onClick = { viewModel.checkNow() } ) SettingsDivider() } - if (updateState is UpdateState.Available || updateState is UpdateState.ReadyToInstall) { + if (autoEnabled && (updateState is UpdateState.Available || updateState is UpdateState.ReadyToInstall)) { item { SettingsItem( title = "Установить обновление", @@ -89,11 +89,13 @@ fun SettingsScreen(viewModel: UpdateViewModel) { } } -private fun updateStatusText(state: UpdateState): String = when (state) { +private fun updateStatusText(state: UpdateState, autoEnabled: Boolean): String = when (state) { is UpdateState.Idle -> "Проверить сейчас" is UpdateState.Checking -> "Проверка…" is UpdateState.UpToDate -> "Установлена последняя версия" - is UpdateState.Available -> "Доступна версия ${state.version}" + is UpdateState.Available -> + if (autoEnabled) "Доступна версия ${state.version}" + else "Доступна версия ${state.version} — включите автообновления для установки" is UpdateState.Downloading -> "Скачивание: ${state.progress?.let { "$it%" } ?: "…"}" is UpdateState.ReadyToInstall -> "Обновление скачано, нажмите для установки" is UpdateState.Failed -> "Ошибка: ${state.message}" diff --git a/app/src/main/java/agzam/vyatsu/update/AppUpdater.kt b/app/src/main/java/agzam/vyatsu/update/AppUpdater.kt index bb565f7..3b70b73 100644 --- a/app/src/main/java/agzam/vyatsu/update/AppUpdater.kt +++ b/app/src/main/java/agzam/vyatsu/update/AppUpdater.kt @@ -9,6 +9,7 @@ import android.net.Uri import android.provider.Settings import android.util.Log import androidx.core.content.FileProvider +import kotlin.coroutines.cancellation.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -75,6 +76,7 @@ class AppUpdater( if (job?.isActive == true) return val update = pendingUpdate ?: return job = scope.launch { + if (!preferencesRepository.autoUpdateEnabled()) return@launch runCatching { install(download(update)) }.onFailure(::onUpdateFailed) } } @@ -85,6 +87,12 @@ class AppUpdater( } fun setAutoEnabled(enabled: Boolean) { + if (!enabled) { + job?.cancel() + if (_state.value is UpdateState.Downloading || _state.value is UpdateState.Checking) { + _state.value = UpdateState.Idle + } + } scope.launch { preferencesRepository.setAutoUpdateEnabled(enabled) } @@ -183,6 +191,7 @@ class AppUpdater( } private fun onUpdateFailed(error: Throwable) { + if (error is CancellationException) throw error Log.e(TAG, "Ошибка обновления", error) _state.value = UpdateState.Failed(error.message ?: "Неизвестная ошибка") }