2026-09-06 12:38:00 +03:00
|
|
|
package agzam.vyatsu.ui.settings
|
|
|
|
|
|
|
|
|
|
import agzam.vyatsu.domain.repository.PreferencesRepository
|
|
|
|
|
import agzam.vyatsu.update.AppUpdater
|
|
|
|
|
import agzam.vyatsu.update.UpdateState
|
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.asStateFlow
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
|
|
class UpdateViewModel(
|
|
|
|
|
private val updater: AppUpdater,
|
|
|
|
|
private val preferencesRepository: PreferencesRepository
|
|
|
|
|
) : ViewModel() {
|
|
|
|
|
|
|
|
|
|
val updateState: StateFlow<UpdateState> = updater.state
|
|
|
|
|
|
2026-09-06 17:23:55 +03:00
|
|
|
private val _autoEnabled = MutableStateFlow(false)
|
2026-09-06 12:38:00 +03:00
|
|
|
val autoEnabled: StateFlow<Boolean> = _autoEnabled.asStateFlow()
|
|
|
|
|
|
2026-09-06 17:23:55 +03:00
|
|
|
private val _showFirstRunDialog = MutableStateFlow(false)
|
|
|
|
|
val showFirstRunDialog: StateFlow<Boolean> = _showFirstRunDialog.asStateFlow()
|
|
|
|
|
|
2026-09-06 12:38:00 +03:00
|
|
|
init {
|
|
|
|
|
viewModelScope.launch {
|
|
|
|
|
_autoEnabled.value = preferencesRepository.autoUpdateEnabled()
|
2026-09-06 17:23:55 +03:00
|
|
|
_showFirstRunDialog.value = !preferencesRepository.autoUpdateAsked()
|
2026-09-06 12:38:00 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun setAutoEnabled(enabled: Boolean) {
|
|
|
|
|
_autoEnabled.value = enabled
|
|
|
|
|
updater.setAutoEnabled(enabled)
|
2026-09-06 17:23:55 +03:00
|
|
|
if (enabled && !updater.canRequestInstalls()) {
|
|
|
|
|
updater.openUnknownSourcesSettings()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun onFirstRunDecision(enable: Boolean) {
|
|
|
|
|
_showFirstRunDialog.value = false
|
|
|
|
|
viewModelScope.launch {
|
|
|
|
|
preferencesRepository.setAutoUpdateAsked(true)
|
|
|
|
|
}
|
|
|
|
|
setAutoEnabled(enable)
|
2026-09-06 12:38:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun checkNow() = updater.checkForUpdate()
|
|
|
|
|
|
|
|
|
|
fun downloadAndInstall() = updater.downloadAndInstall()
|
|
|
|
|
|
|
|
|
|
fun installDownloadedApk() = updater.installDownloadedApk()
|
|
|
|
|
|
|
|
|
|
fun canRequestInstalls() = updater.canRequestInstalls()
|
|
|
|
|
|
|
|
|
|
fun openUnknownSourcesSettings() = updater.openUnknownSourcesSettings()
|
|
|
|
|
}
|