SettingsViewModel.kt

4.2 KB · raw

1package com.peachpasswords.ui.settings23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import com.peachpasswords.license.LicenseRepository6import com.peachpasswords.vault.VaultRepository7import com.peachpasswords.vault.VaultSettings8import dagger.hilt.android.lifecycle.HiltViewModel9import kotlinx.coroutines.flow.MutableStateFlow10import kotlinx.coroutines.flow.StateFlow11import kotlinx.coroutines.flow.asStateFlow12import kotlinx.coroutines.launch13import javax.inject.Inject1415@HiltViewModel16class SettingsViewModel @Inject constructor(17    private val vault: VaultRepository,18    private val license: LicenseRepository,19) : ViewModel() {20    val entries: StateFlow<List<com.peachpasswords.vault.VaultEntry>> = vault.entries21    val settings: StateFlow<VaultSettings> = vault.settings2223    /** Live Pro entitlement, seeded from the stored (locally Ed25519-validated) license. */24    private val _isPro = MutableStateFlow(license.isProActive())25    val isPro: StateFlow<Boolean> = _isPro.asStateFlow()2627    private val _activating = MutableStateFlow(false)28    val activating: StateFlow<Boolean> = _activating.asStateFlow()2930    private val _activationError = MutableStateFlow<String?>(null)31    val activationError: StateFlow<String?> = _activationError.asStateFlow()3233    /** Set true after a fresh activation so the screen can fire ProCelebration once. */34    private val _showCelebration = MutableStateFlow(false)35    val showCelebration: StateFlow<Boolean> = _showCelebration.asStateFlow()3637    /**38     * Validate a license key locally (offline Ed25519 — there is no activation server)39     * and, on success, persist it and flip Pro on. Surfaces only outcomes that can40     * actually occur offline.41     */42    fun activateLicense(rawKey: String) {43        if (_activating.value) return44        _activating.value = true45        _activationError.value = null46        viewModelScope.launch {47            val key = rawKey.trim()48            if (license.validateLicense(key)) {49                license.storeLicense(key)50                _isPro.value = true51                _showCelebration.value = true52            } else {53                _activationError.value = "Invalid license key"54            }55            _activating.value = false56        }57    }5859    fun clearActivationError() { _activationError.value = null }6061    fun dismissCelebration() {62        license.markProCelebrated()63        _showCelebration.value = false64    }6566    fun deactivate() {67        license.clearLicense()68        _isPro.value = false69    }7071    private fun mutate(block: (VaultSettings) -> VaultSettings) {72        viewModelScope.launch { vault.updateSettings(block(vault.settings.value)) }73    }7475    fun setTheme(theme: String) = mutate { it.copy(theme = theme) }76    fun setAccent(name: String) = mutate { it.copy(accentColor = name) }77    fun setTextSize(size: String) = mutate { it.copy(textSize = size) }78    fun setReduceMotion(on: Boolean) = mutate { it.copy(reduceMotion = on) }79    fun setBreachMonitoring(on: Boolean) = mutate { it.copy(breachMonitoring = on) }80    fun setBiometrics(on: Boolean) = mutate { it.copy(biometricsEnabled = on) }81    fun setAutoLockMinutes(min: Int?) = mutate { it.copy(autoLockMinutes = min) }82    fun setTrashRetentionDays(days: Int?) = mutate { it.copy(trashRetentionDays = days) }8384    suspend fun changePassword(current: String, next: String): Boolean =85        vault.changePassword(current, next)8687    /** One-line summary shown under each card title; mirrors the browser ledes. */88    fun lede(id: CardId, entriesCount: Int, s: VaultSettings): String = when (id) {89        CardId.Pro -> if (_isPro.value) "active · paid once, yours forever" else "$39 once · yours forever"90        CardId.Protection ->91            "${if (s.breachMonitoring) "breaches watched" else "breach watch off"} · manual lock"92        CardId.Vault ->93            "$entriesCount ${if (entriesCount == 1) "entry" else "entries"} · backups, history, cleanup"94        CardId.Sync -> if (_isPro.value) "pair by chirp, keep in step" else "Pro · encrypted device pairing"95        CardId.Appearance -> "${s.theme} · ${s.accentColor} · ${s.textSize}"96        CardId.Shortcuts -> "bindings · what your fingers know"97        CardId.Feedback -> "tell us what would make it better"98    }99}