EntryEditViewModel.kt

5.2 KB · raw

1package com.peachpasswords.ui.vault23import androidx.lifecycle.SavedStateHandle4import androidx.lifecycle.ViewModel5import androidx.lifecycle.viewModelScope6import com.peachpasswords.vault.PasswordGenerator7import com.peachpasswords.vault.VaultEntry8import com.peachpasswords.vault.VaultRepository9import dagger.hilt.android.lifecycle.HiltViewModel10import kotlinx.coroutines.flow.MutableStateFlow11import kotlinx.coroutines.flow.StateFlow12import kotlinx.coroutines.flow.asStateFlow13import kotlinx.coroutines.flow.update14import kotlinx.coroutines.launch15import javax.inject.Inject1617data class EditForm(18    val type: String = "login",19    val title: String = "",20    val username: String = "",21    val email: String = "",22    val password: String = "",23    val url: String = "",24    val totp: String = "",25    val notes: String = "",26    val showPassword: Boolean = false,27    val generatorOpen: Boolean = false,28    val genLength: Int = 20,29    val genUpper: Boolean = true,30    val genLower: Boolean = true,31    val genNumbers: Boolean = true,32    val genSymbols: Boolean = true,33    val saving: Boolean = false,34    val error: String? = null,35)3637@HiltViewModel38class EntryEditViewModel @Inject constructor(39    private val vault: VaultRepository,40    savedStateHandle: SavedStateHandle,41) : ViewModel() {42    private val id: String? = savedStateHandle.get<String>("id")?.takeIf { it.isNotBlank() }43    val isNew: Boolean = id == null44    private val existing: VaultEntry? = id?.let { vault.getEntry(it) }4546    private val _form = MutableStateFlow(47        existing?.let {48            EditForm(49                type = it.type ?: "login",50                title = it.title,51                username = it.username.orEmpty(),52                email = it.email.orEmpty(),53                password = it.password.orEmpty(),54                url = it.url.orEmpty(),55                totp = it.totp.orEmpty(),56                notes = it.notes.orEmpty(),57            )58        } ?: EditForm(),59    )60    val form: StateFlow<EditForm> = _form.asStateFlow()6162    fun setType(t: String) = _form.update { it.copy(type = t) }63    fun setTitle(v: String) = _form.update { it.copy(title = v, error = null) }64    fun setUsername(v: String) = _form.update { it.copy(username = v) }65    fun setEmail(v: String) = _form.update { it.copy(email = v) }66    fun setPassword(v: String) = _form.update { it.copy(password = v) }67    fun setUrl(v: String) = _form.update { it.copy(url = v) }68    fun setTotp(v: String) = _form.update { it.copy(totp = v) }69    fun setNotes(v: String) = _form.update { it.copy(notes = v) }70    fun toggleShowPassword() = _form.update { it.copy(showPassword = !it.showPassword) }71    fun toggleGenerator() = _form.update { it.copy(generatorOpen = !it.generatorOpen) }72    fun setGenLength(n: Int) = _form.update { it.copy(genLength = n) }73    fun setGenUpper(b: Boolean) = _form.update { it.copy(genUpper = b) }74    fun setGenLower(b: Boolean) = _form.update { it.copy(genLower = b) }75    fun setGenNumbers(b: Boolean) = _form.update { it.copy(genNumbers = b) }76    fun setGenSymbols(b: Boolean) = _form.update { it.copy(genSymbols = b) }7778    fun generate() {79        val f = _form.value80        val pw = PasswordGenerator.generate(81            PasswordGenerator.Options(82                length = f.genLength,83                includeUppercase = f.genUpper,84                includeLowercase = f.genLower,85                includeNumbers = f.genNumbers,86                includeSymbols = f.genSymbols,87            ),88        )89        _form.update { it.copy(password = pw, showPassword = true) }90    }9192    fun save(onDone: () -> Unit) {93        val f = _form.value94        if (f.title.isBlank()) {95            _form.update { it.copy(error = "title is required") }96            return97        }98        _form.update { it.copy(saving = true, error = null) }99        viewModelScope.launch {100            runCatching {101                if (isNew) {102                    vault.addEntry(103                        VaultEntry(104                            id = "", type = f.type, title = f.title.trim(),105                            username = f.username.ifBlank { null },106                            email = f.email.ifBlank { null },107                            password = f.password.ifBlank { null },108                            url = f.url.ifBlank { null },109                            totp = f.totp.ifBlank { null },110                            notes = f.notes.ifBlank { null },111                            createdAt = 0, modifiedAt = 0,112                        ),113                    )114                } else {115                    vault.updateEntry(116                        existing!!.copy(117                            type = f.type, title = f.title.trim(),118                            username = f.username.ifBlank { null },119                            email = f.email.ifBlank { null },120                            password = f.password.ifBlank { null },121                            url = f.url.ifBlank { null },122                            totp = f.totp.ifBlank { null },123                            notes = f.notes.ifBlank { null },124                        ),125                    )126                }127            }.onSuccess { onDone() }128                .onFailure { e -> _form.update { it.copy(saving = false, error = e.message ?: "save failed") } }129        }130    }131}