forked from agzam/vyatsu-schedule
fix: no auto download if updated disabled
This commit is contained in:
@@ -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}"
|
||||
|
||||
@@ -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 ?: "Неизвестная ошибка")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user