EntryDetailViewModel.kt

5.0 KB · raw

1package com.peachpasswords.ui.vault23import android.content.Context4import com.peachpasswords.shared.PeachClipboard5import androidx.lifecycle.SavedStateHandle6import androidx.lifecycle.ViewModel7import androidx.lifecycle.viewModelScope8import com.peachpasswords.vault.TOTPGenerator9import com.peachpasswords.vault.VaultEntry10import com.peachpasswords.vault.VaultRepository11import dagger.hilt.android.lifecycle.HiltViewModel12import dagger.hilt.android.qualifiers.ApplicationContext13import kotlinx.coroutines.flow.MutableStateFlow14import kotlinx.coroutines.flow.SharingStarted15import kotlinx.coroutines.flow.StateFlow16import kotlinx.coroutines.flow.asStateFlow17import kotlinx.coroutines.flow.map18import kotlinx.coroutines.flow.stateIn19import kotlinx.coroutines.launch20import javax.inject.Inject2122@HiltViewModel23class EntryDetailViewModel @Inject constructor(24    private val vault: VaultRepository,25    @ApplicationContext private val appContext: Context,26    savedStateHandle: SavedStateHandle,27) : ViewModel() {28    private val id: String = savedStateHandle.get<String>("id").orEmpty()2930    val entry: StateFlow<VaultEntry?> =31        vault.entries.map { list -> list.firstOrNull { it.id == id } }32            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), vault.getEntry(id))3334    /** Distinct prior username/email values for the identity-suggest dropdown. */35    val identitySuggestions: StateFlow<List<String>> =36        vault.entries37            .map { vault.getDistinctFieldValues("username").take(4) }38            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())3940    private val _revealed = MutableStateFlow(false)41    val revealed: StateFlow<Boolean> = _revealed.asStateFlow()4243    private val _copiedField = MutableStateFlow<String?>(null)44    val copiedField: StateFlow<String?> = _copiedField.asStateFlow()4546    /** Reveal the password and auto-mask after 10 seconds (matches the browser). */47    fun reveal() {48        _revealed.value = true49        viewModelScope.launch {50            kotlinx.coroutines.delay(10_000)51            _revealed.value = false52        }53    }5455    fun mask() { _revealed.value = false }5657    private val _cvcRevealed = MutableStateFlow(false)58    val cvcRevealed: StateFlow<Boolean> = _cvcRevealed.asStateFlow()5960    /** Reveal the CVC and auto-mask after 10s — same policy as the password reveal. */61    fun revealCvc() {62        _cvcRevealed.value = true63        viewModelScope.launch {64            kotlinx.coroutines.delay(10_000)65            _cvcRevealed.value = false66        }67    }6869    fun maskCvc() { _cvcRevealed.value = false }7071    fun copy(field: String, value: String?) {72        val v = value ?: return73        PeachClipboard.copy(appContext, "peach", v)74        viewModelScope.launch {75            _copiedField.value = field76            kotlinx.coroutines.delay(1200)77            if (_copiedField.value == field) _copiedField.value = null78        }79    }8081    /** Copy the live TOTP code (never the seed). */82    fun copyTotp() {83        val totp = entry.value?.totp ?: return84        copy("totp", TOTPGenerator.generateCode(totp))85    }8687    /**88     * Apply a generated password via updateEntry — VaultRepository captures the89     * prior password into history automatically, so we only pass the new value.90     */91    fun applyGeneratedPassword(newPassword: String, onDone: () -> Unit = {}) {92        val current = entry.value ?: return93        if (newPassword.isEmpty() || newPassword == current.password) { onDone(); return }94        viewModelScope.launch {95            runCatching { vault.updateEntry(current.copy(password = newPassword)) }96            onDone()97        }98    }99100    /**101     * Persist a single field's inline edit via a full-entry updateEntry patch102     * (which handles password-history capture + timestamps). No-op if entry gone.103     */104    fun updateField(field: String, value: String) {105        val current = entry.value ?: return106        val patch = when (field) {107            "username" -> current.copy(username = value.ifBlank { null })108            "email" -> current.copy(email = value.ifBlank { null })109            "password" -> current.copy(password = value.ifBlank { null })110            "notes" -> current.copy(notes = value.ifBlank { null })111            "totp" -> current.copy(totp = value.ifBlank { null })112            else -> return113        }114        viewModelScope.launch {115            runCatching { vault.updateEntry(patch) }116        }117    }118119    /**120     * Restore a past password as the current one. updateEntry captures the121     * now-outgoing password into history, so this is a normal password swap.122     */123    fun restorePassword(entryId: String, oldPassword: String) {124        viewModelScope.launch {125            val current = vault.getEntry(entryId) ?: return@launch126            if (current.password == oldPassword) return@launch127            runCatching { vault.updateEntry(current.copy(password = oldPassword)) }128        }129    }130131    fun delete(onDone: () -> Unit) {132        viewModelScope.launch {133            runCatching { vault.deleteEntry(id) }134            onDone()135        }136    }137}