package com.peachpasswords.ui.vault import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.peachpasswords.vault.PasswordGenerator import com.peachpasswords.vault.VaultEntry import com.peachpasswords.vault.VaultRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import javax.inject.Inject data class EditForm( val type: String = "login", val title: String = "", val username: String = "", val email: String = "", val password: String = "", val url: String = "", val totp: String = "", val notes: String = "", val showPassword: Boolean = false, val generatorOpen: Boolean = false, val genLength: Int = 20, val genUpper: Boolean = true, val genLower: Boolean = true, val genNumbers: Boolean = true, val genSymbols: Boolean = true, val saving: Boolean = false, val error: String? = null, ) @HiltViewModel class EntryEditViewModel @Inject constructor( private val vault: VaultRepository, savedStateHandle: SavedStateHandle, ) : ViewModel() { private val id: String? = savedStateHandle.get("id")?.takeIf { it.isNotBlank() } val isNew: Boolean = id == null private val existing: VaultEntry? = id?.let { vault.getEntry(it) } private val _form = MutableStateFlow( existing?.let { EditForm( type = it.type ?: "login", title = it.title, username = it.username.orEmpty(), email = it.email.orEmpty(), password = it.password.orEmpty(), url = it.url.orEmpty(), totp = it.totp.orEmpty(), notes = it.notes.orEmpty(), ) } ?: EditForm(), ) val form: StateFlow = _form.asStateFlow() fun setType(t: String) = _form.update { it.copy(type = t) } fun setTitle(v: String) = _form.update { it.copy(title = v, error = null) } fun setUsername(v: String) = _form.update { it.copy(username = v) } fun setEmail(v: String) = _form.update { it.copy(email = v) } fun setPassword(v: String) = _form.update { it.copy(password = v) } fun setUrl(v: String) = _form.update { it.copy(url = v) } fun setTotp(v: String) = _form.update { it.copy(totp = v) } fun setNotes(v: String) = _form.update { it.copy(notes = v) } fun toggleShowPassword() = _form.update { it.copy(showPassword = !it.showPassword) } fun toggleGenerator() = _form.update { it.copy(generatorOpen = !it.generatorOpen) } fun setGenLength(n: Int) = _form.update { it.copy(genLength = n) } fun setGenUpper(b: Boolean) = _form.update { it.copy(genUpper = b) } fun setGenLower(b: Boolean) = _form.update { it.copy(genLower = b) } fun setGenNumbers(b: Boolean) = _form.update { it.copy(genNumbers = b) } fun setGenSymbols(b: Boolean) = _form.update { it.copy(genSymbols = b) } fun generate() { val f = _form.value val pw = PasswordGenerator.generate( PasswordGenerator.Options( length = f.genLength, includeUppercase = f.genUpper, includeLowercase = f.genLower, includeNumbers = f.genNumbers, includeSymbols = f.genSymbols, ), ) _form.update { it.copy(password = pw, showPassword = true) } } fun save(onDone: () -> Unit) { val f = _form.value if (f.title.isBlank()) { _form.update { it.copy(error = "title is required") } return } _form.update { it.copy(saving = true, error = null) } viewModelScope.launch { runCatching { if (isNew) { vault.addEntry( VaultEntry( id = "", type = f.type, title = f.title.trim(), username = f.username.ifBlank { null }, email = f.email.ifBlank { null }, password = f.password.ifBlank { null }, url = f.url.ifBlank { null }, totp = f.totp.ifBlank { null }, notes = f.notes.ifBlank { null }, createdAt = 0, modifiedAt = 0, ), ) } else { vault.updateEntry( existing!!.copy( type = f.type, title = f.title.trim(), username = f.username.ifBlank { null }, email = f.email.ifBlank { null }, password = f.password.ifBlank { null }, url = f.url.ifBlank { null }, totp = f.totp.ifBlank { null }, notes = f.notes.ifBlank { null }, ), ) } }.onSuccess { onDone() } .onFailure { e -> _form.update { it.copy(saving = false, error = e.message ?: "save failed") } } } } }