package com.peachpasswords.ui.vault import android.content.ClipData import android.content.ClipboardManager import android.content.Context import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.peachpasswords.vault.TOTPGenerator import com.peachpasswords.vault.VaultEntry import com.peachpasswords.vault.VaultRepository import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel class EntryDetailViewModel @Inject constructor( private val vault: VaultRepository, @ApplicationContext private val appContext: Context, savedStateHandle: SavedStateHandle, ) : ViewModel() { private val id: String = savedStateHandle.get("id").orEmpty() val entry: StateFlow = vault.entries.map { list -> list.firstOrNull { it.id == id } } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), vault.getEntry(id)) /** Distinct prior username/email values for the identity-suggest dropdown. */ val identitySuggestions: StateFlow> = vault.entries .map { vault.getDistinctFieldValues("username").take(4) } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()) private val _revealed = MutableStateFlow(false) val revealed: StateFlow = _revealed.asStateFlow() private val _copiedField = MutableStateFlow(null) val copiedField: StateFlow = _copiedField.asStateFlow() /** Reveal the password and auto-mask after 10 seconds (matches the browser). */ fun reveal() { _revealed.value = true viewModelScope.launch { kotlinx.coroutines.delay(10_000) _revealed.value = false } } fun mask() { _revealed.value = false } private val _cvcRevealed = MutableStateFlow(false) val cvcRevealed: StateFlow = _cvcRevealed.asStateFlow() /** Reveal the CVC and auto-mask after 10s — same policy as the password reveal. */ fun revealCvc() { _cvcRevealed.value = true viewModelScope.launch { kotlinx.coroutines.delay(10_000) _cvcRevealed.value = false } } fun maskCvc() { _cvcRevealed.value = false } fun copy(field: String, value: String?) { val v = value ?: return val cm = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager cm.setPrimaryClip(ClipData.newPlainText("peach", v)) viewModelScope.launch { _copiedField.value = field kotlinx.coroutines.delay(1200) if (_copiedField.value == field) _copiedField.value = null } } /** Copy the live TOTP code (never the seed). */ fun copyTotp() { val totp = entry.value?.totp ?: return copy("totp", TOTPGenerator.generateCode(totp)) } /** * Apply a generated password via updateEntry — VaultRepository captures the * prior password into history automatically, so we only pass the new value. */ fun applyGeneratedPassword(newPassword: String, onDone: () -> Unit = {}) { val current = entry.value ?: return if (newPassword.isEmpty() || newPassword == current.password) { onDone(); return } viewModelScope.launch { runCatching { vault.updateEntry(current.copy(password = newPassword)) } onDone() } } /** * Persist a single field's inline edit via a full-entry updateEntry patch * (which handles password-history capture + timestamps). No-op if entry gone. */ fun updateField(field: String, value: String) { val current = entry.value ?: return val patch = when (field) { "username" -> current.copy(username = value.ifBlank { null }) "email" -> current.copy(email = value.ifBlank { null }) "password" -> current.copy(password = value.ifBlank { null }) "notes" -> current.copy(notes = value.ifBlank { null }) "totp" -> current.copy(totp = value.ifBlank { null }) else -> return } viewModelScope.launch { runCatching { vault.updateEntry(patch) } } } /** * Restore a past password as the current one. updateEntry captures the * now-outgoing password into history, so this is a normal password swap. */ fun restorePassword(entryId: String, oldPassword: String) { viewModelScope.launch { val current = vault.getEntry(entryId) ?: return@launch if (current.password == oldPassword) return@launch runCatching { vault.updateEntry(current.copy(password = oldPassword)) } } } fun delete(onDone: () -> Unit) { viewModelScope.launch { runCatching { vault.deleteEntry(id) } onDone() } } }