1package com.peachpasswords.ui.settings23import android.content.Context4import androidx.biometric.BiometricPrompt5import androidx.fragment.app.FragmentActivity6import androidx.lifecycle.ViewModel7import androidx.lifecycle.viewModelScope8import com.peachpasswords.feedback.FeedbackRepository9import com.peachpasswords.feedback.FeedbackState10import com.peachpasswords.license.LicenseRepository11import com.peachpasswords.ui.lock.BiometricKeyStore12import com.peachpasswords.ui.lock.Biometrics13import com.peachpasswords.vault.VaultCrypto14import com.peachpasswords.vault.VaultRepository15import com.peachpasswords.vault.VaultSettings16import dagger.hilt.android.lifecycle.HiltViewModel17import dagger.hilt.android.qualifiers.ApplicationContext18import kotlinx.coroutines.flow.MutableStateFlow19import kotlinx.coroutines.flow.StateFlow20import kotlinx.coroutines.flow.asStateFlow21import kotlinx.coroutines.launch22import javax.inject.Inject2324@HiltViewModel25class SettingsViewModel @Inject constructor(26 private val vault: VaultRepository,27 private val license: LicenseRepository,28 private val feedback: FeedbackRepository,29 private val duress: com.peachpasswords.vault.DuressConfigStore,30 @ApplicationContext private val appContext: Context,31) : ViewModel() {3233 private val _duressConfigured = MutableStateFlow(duress.isConfigured())34 val duressConfigured: StateFlow<Boolean> = _duressConfigured.asStateFlow()3536 suspend fun saveDuress(password: String, action: com.peachpasswords.vault.DuressAction): Boolean {37 val ok = duress.save(password, action)38 if (ok) _duressConfigured.value = true39 return ok40 }4142 fun disableDuress() {43 duress.clear()44 _duressConfigured.value = false45 }4647 private val _feedbackState = MutableStateFlow<FeedbackState>(FeedbackState.Idle)48 val feedbackState: StateFlow<FeedbackState> = _feedbackState.asStateFlow()4950 fun sendFeedback(type: String, message: String) {51 if (_feedbackState.value is FeedbackState.Sending) return52 _feedbackState.value = FeedbackState.Sending53 viewModelScope.launch {54 _feedbackState.value = try {55 feedback.submit(type, message)56 FeedbackState.Sent57 } catch (e: Exception) {58 FeedbackState.Error("Couldn't send feedback — check your connection")59 }60 }61 }6263 64 fun resetFeedback() {65 _feedbackState.value = FeedbackState.Idle66 }67 val entries: StateFlow<List<com.peachpasswords.vault.VaultEntry>> = vault.entries68 val settings: StateFlow<VaultSettings> = vault.settings6970 71 private val _isPro = MutableStateFlow(license.isProActive())72 val isPro: StateFlow<Boolean> = _isPro.asStateFlow()7374 private val _activating = MutableStateFlow(false)75 val activating: StateFlow<Boolean> = _activating.asStateFlow()7677 private val _activationError = MutableStateFlow<String?>(null)78 val activationError: StateFlow<String?> = _activationError.asStateFlow()7980 81 private val _showCelebration = MutableStateFlow(false)82 val showCelebration: StateFlow<Boolean> = _showCelebration.asStateFlow()8384 8586878889 fun activateLicense(rawKey: String) {90 if (_activating.value) return91 _activating.value = true92 _activationError.value = null93 viewModelScope.launch {94 val key = rawKey.trim()95 if (license.validateLicense(key)) {96 license.storeLicense(key)97 _isPro.value = true98 _showCelebration.value = true99 } else {100 _activationError.value = "Invalid license key"101 }102 _activating.value = false103 }104 }105106 fun clearActivationError() { _activationError.value = null }107108 fun dismissCelebration() {109 license.markProCelebrated()110 _showCelebration.value = false111 }112113 fun deactivate() {114 license.clearLicense()115 _isPro.value = false116 }117118 private fun mutate(block: (VaultSettings) -> VaultSettings) {119 viewModelScope.launch { vault.updateSettings(block(vault.settings.value)) }120 }121122 fun setTheme(theme: String) = mutate { it.copy(theme = theme) }123 fun setAccent(name: String) = mutate { it.copy(accentColor = name) }124 fun setTextSize(size: String) = mutate { it.copy(textSize = size) }125 fun setReduceMotion(on: Boolean) = mutate { it.copy(reduceMotion = on) }126 fun setBreachMonitoring(on: Boolean) = mutate { it.copy(breachMonitoring = on) }127 fun setBiometrics(on: Boolean) = mutate { it.copy(biometricsEnabled = on) }128129 130131132133134135 fun enableBiometrics(activity: FragmentActivity) {136 val key = vault.currentVaultKey() ?: return137 val cipher = try {138 BiometricKeyStore.encryptCipher()139 } catch (_: Exception) {140 VaultCrypto.zero(key); return141 }142 Biometrics.authenticateCrypto(143 activity = activity,144 title = "Enable biometric unlock",145 subtitle = "Confirm to secure your vault key",146 crypto = BiometricPrompt.CryptoObject(cipher),147 onSuccess = { result ->148 val used = result.cryptoObject?.cipher149 if (used != null) {150 runCatching { BiometricKeyStore.wrapAndStore(appContext, used, key) }151 .onSuccess { mutate { it.copy(biometricsEnabled = true) } }152 }153 VaultCrypto.zero(key)154 },155 onFail = { VaultCrypto.zero(key) },156 )157 }158159 160 fun disableBiometrics() {161 BiometricKeyStore.clear(appContext)162 mutate { it.copy(biometricsEnabled = false) }163 }164 fun setAutoLockMinutes(min: Int?) = mutate { it.copy(autoLockMinutes = min) }165 fun setTrashRetentionDays(days: Int?) = mutate { it.copy(trashRetentionDays = days) }166167 suspend fun changePassword(current: String, next: String): Boolean =168 vault.changePassword(current, next)169170 171 fun lede(id: CardId, entriesCount: Int, s: VaultSettings): String = when (id) {172 CardId.Pro -> if (_isPro.value) "active · paid once, yours forever" else "$39 once · yours forever"173 CardId.Protection ->174 "${if (s.breachMonitoring) "breaches watched" else "breach watch off"} · manual lock"175 CardId.Vault ->176 "$entriesCount ${if (entriesCount == 1) "entry" else "entries"} · backups, history, cleanup"177 CardId.Sync -> if (_isPro.value) "pair by chirp, keep in step" else "Pro · encrypted device pairing"178 CardId.Appearance -> "${s.theme} · ${s.accentColor} · ${s.textSize}"179 CardId.Shortcuts -> "bindings · what your fingers know"180 CardId.Feedback -> "tell us what would make it better"181 }182}