1package com.peachpasswords.ui.vault23import android.content.Context4import com.peachpasswords.shared.PeachClipboard5import androidx.lifecycle.SavedStateHandle6import androidx.lifecycle.ViewModel7import androidx.lifecycle.viewModelScope8import com.peachpasswords.vault.LastViewedEntryStore9import com.peachpasswords.vault.TOTPGenerator10import com.peachpasswords.vault.VaultEntry11import com.peachpasswords.vault.VaultRepository12import dagger.hilt.android.lifecycle.HiltViewModel13import dagger.hilt.android.qualifiers.ApplicationContext14import kotlinx.coroutines.flow.MutableStateFlow15import kotlinx.coroutines.flow.SharingStarted16import kotlinx.coroutines.flow.StateFlow17import kotlinx.coroutines.flow.asStateFlow18import kotlinx.coroutines.flow.map19import kotlinx.coroutines.flow.stateIn20import kotlinx.coroutines.launch21import javax.inject.Inject2223@HiltViewModel24class EntryDetailViewModel @Inject constructor(25 private val vault: VaultRepository,26 @ApplicationContext private val appContext: Context,27 private val lastViewed: LastViewedEntryStore,28 savedStateHandle: SavedStateHandle,29) : ViewModel() {30 private val id: String = savedStateHandle.get<String>("id").orEmpty()3132 init {33 34 35 if (id.isNotBlank() && vault.getEntry(id) != null) lastViewed.set(id)36 }3738 val entry: StateFlow<VaultEntry?> =39 vault.entries.map { list -> list.firstOrNull { it.id == id } }40 .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), vault.getEntry(id))4142 43 val identitySuggestions: StateFlow<List<String>> =44 vault.entries45 .map { vault.getDistinctFieldValues("username").take(4) }46 .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())4748 private val _revealed = MutableStateFlow(false)49 val revealed: StateFlow<Boolean> = _revealed.asStateFlow()5051 private val _copiedField = MutableStateFlow<String?>(null)52 val copiedField: StateFlow<String?> = _copiedField.asStateFlow()5354 55 fun reveal() {56 _revealed.value = true57 viewModelScope.launch {58 kotlinx.coroutines.delay(10_000)59 _revealed.value = false60 }61 }6263 fun mask() { _revealed.value = false }6465 private val _cvcRevealed = MutableStateFlow(false)66 val cvcRevealed: StateFlow<Boolean> = _cvcRevealed.asStateFlow()6768 69 fun revealCvc() {70 _cvcRevealed.value = true71 viewModelScope.launch {72 kotlinx.coroutines.delay(10_000)73 _cvcRevealed.value = false74 }75 }7677 fun maskCvc() { _cvcRevealed.value = false }7879 fun copy(field: String, value: String?) {80 val v = value ?: return81 PeachClipboard.copy(appContext, "peach", v)82 viewModelScope.launch {83 _copiedField.value = field84 kotlinx.coroutines.delay(1200)85 if (_copiedField.value == field) _copiedField.value = null86 }87 }8889 90 fun copyTotp() {91 val totp = entry.value?.totp ?: return92 copy("totp", TOTPGenerator.generateCode(totp))93 }9495 96979899 fun applyGeneratedPassword(newPassword: String, onDone: () -> Unit = {}) {100 val current = entry.value ?: return101 if (newPassword.isEmpty() || newPassword == current.password) { onDone(); return }102 viewModelScope.launch {103 runCatching { vault.updateEntry(current.copy(password = newPassword)) }104 onDone()105 }106 }107108 109110111112 fun updateField(field: String, value: String) {113 val current = entry.value ?: return114 val patch = when (field) {115 "username" -> current.copy(username = value.ifBlank { null })116 "email" -> current.copy(email = value.ifBlank { null })117 "password" -> current.copy(password = value.ifBlank { null })118 "notes" -> current.copy(notes = value.ifBlank { null })119 "totp" -> current.copy(totp = value.ifBlank { null })120 else -> return121 }122 viewModelScope.launch {123 runCatching { vault.updateEntry(patch) }124 }125 }126127 128129130131 fun restorePassword(entryId: String, oldPassword: String) {132 viewModelScope.launch {133 val current = vault.getEntry(entryId) ?: return@launch134 if (current.password == oldPassword) return@launch135 runCatching { vault.updateEntry(current.copy(password = oldPassword)) }136 }137 }138139 fun delete(onDone: () -> Unit) {140 viewModelScope.launch {141 runCatching { vault.deleteEntry(id) }142 onDone()143 }144 }145}