EntryDetailViewModel.kt

3.0 KB · raw

1package com.peachpasswords.ui.vault23import android.content.ClipData4import android.content.ClipboardManager5import android.content.Context6import androidx.lifecycle.SavedStateHandle7import androidx.lifecycle.ViewModel8import androidx.lifecycle.viewModelScope9import 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    savedStateHandle: SavedStateHandle,28) : ViewModel() {29    private val id: String = savedStateHandle.get<String>("id").orEmpty()3031    val entry: StateFlow<VaultEntry?> =32        vault.entries.map { list -> list.firstOrNull { it.id == id } }33            .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), vault.getEntry(id))3435    private val _revealed = MutableStateFlow(false)36    val revealed: StateFlow<Boolean> = _revealed.asStateFlow()3738    private val _copiedField = MutableStateFlow<String?>(null)39    val copiedField: StateFlow<String?> = _copiedField.asStateFlow()4041    /** Reveal the password and auto-mask after 10 seconds (matches the browser). */42    fun reveal() {43        _revealed.value = true44        viewModelScope.launch {45            kotlinx.coroutines.delay(10_000)46            _revealed.value = false47        }48    }4950    fun mask() { _revealed.value = false }5152    private val _cvcRevealed = MutableStateFlow(false)53    val cvcRevealed: StateFlow<Boolean> = _cvcRevealed.asStateFlow()5455    /** Reveal the CVC and auto-mask after 10s — same policy as the password reveal. */56    fun revealCvc() {57        _cvcRevealed.value = true58        viewModelScope.launch {59            kotlinx.coroutines.delay(10_000)60            _cvcRevealed.value = false61        }62    }6364    fun maskCvc() { _cvcRevealed.value = false }6566    fun copy(field: String, value: String?) {67        val v = value ?: return68        val cm = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager69        cm.setPrimaryClip(ClipData.newPlainText("peach", v))70        viewModelScope.launch {71            _copiedField.value = field72            kotlinx.coroutines.delay(1200)73            if (_copiedField.value == field) _copiedField.value = null74        }75    }7677    /** Copy the live TOTP code (never the seed). */78    fun copyTotp() {79        val totp = entry.value?.totp ?: return80        copy("totp", TOTPGenerator.generateCode(totp))81    }8283    fun delete(onDone: () -> Unit) {84        viewModelScope.launch {85            runCatching { vault.deleteEntry(id) }86            onDone()87        }88    }89}