fix: no auto download if updated disabled

This commit is contained in:
2026-09-07 00:31:34 +03:00
parent f19d65abb5
commit bafebfd253
2 changed files with 15 additions and 4 deletions
@@ -50,12 +50,12 @@ fun SettingsScreen(viewModel: UpdateViewModel) {
item { item {
SettingsItem( SettingsItem(
title = "Проверить наличие обновлений", title = "Проверить наличие обновлений",
subtitle = updateStatusText(updateState), subtitle = updateStatusText(updateState, autoEnabled),
onClick = { viewModel.checkNow() } onClick = { viewModel.checkNow() }
) )
SettingsDivider() SettingsDivider()
} }
if (updateState is UpdateState.Available || updateState is UpdateState.ReadyToInstall) { if (autoEnabled && (updateState is UpdateState.Available || updateState is UpdateState.ReadyToInstall)) {
item { item {
SettingsItem( SettingsItem(
title = "Установить обновление", 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.Idle -> "Проверить сейчас"
is UpdateState.Checking -> "Проверка…" is UpdateState.Checking -> "Проверка…"
is UpdateState.UpToDate -> "Установлена последняя версия" 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.Downloading -> "Скачивание: ${state.progress?.let { "$it%" } ?: "…"}"
is UpdateState.ReadyToInstall -> "Обновление скачано, нажмите для установки" is UpdateState.ReadyToInstall -> "Обновление скачано, нажмите для установки"
is UpdateState.Failed -> "Ошибка: ${state.message}" is UpdateState.Failed -> "Ошибка: ${state.message}"
@@ -9,6 +9,7 @@ import android.net.Uri
import android.provider.Settings import android.provider.Settings
import android.util.Log import android.util.Log
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@@ -75,6 +76,7 @@ class AppUpdater(
if (job?.isActive == true) return if (job?.isActive == true) return
val update = pendingUpdate ?: return val update = pendingUpdate ?: return
job = scope.launch { job = scope.launch {
if (!preferencesRepository.autoUpdateEnabled()) return@launch
runCatching { install(download(update)) }.onFailure(::onUpdateFailed) runCatching { install(download(update)) }.onFailure(::onUpdateFailed)
} }
} }
@@ -85,6 +87,12 @@ class AppUpdater(
} }
fun setAutoEnabled(enabled: Boolean) { 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 { scope.launch {
preferencesRepository.setAutoUpdateEnabled(enabled) preferencesRepository.setAutoUpdateEnabled(enabled)
} }
@@ -183,6 +191,7 @@ class AppUpdater(
} }
private fun onUpdateFailed(error: Throwable) { private fun onUpdateFailed(error: Throwable) {
if (error is CancellationException) throw error
Log.e(TAG, "Ошибка обновления", error) Log.e(TAG, "Ошибка обновления", error)
_state.value = UpdateState.Failed(error.message ?: "Неизвестная ошибка") _state.value = UpdateState.Failed(error.message ?: "Неизвестная ошибка")
} }