package com.cottondb.android import com.cottondb.android.core.CottonCalling import com.cottondb.android.generated.SecretKind import com.cottondb.android.generated.SecretPayload import com.cottondb.android.passwords.* import java.time.Clock import kotlinx.coroutines.CoroutineScope /** Session-owned models; neither an Activity nor Android saved state holds a plaintext draft. */ class PasswordModels( core: CottonCalling, private val scope: CoroutineScope, private val clock: Clock, val session: VaultSession, private val copyValue: (String) -> Unit, ) { private val metadata = PasswordsRepository(core) private val payloads = VaultPayloadRepository(core) private val lists = mutableMapOf() private val details = mutableMapOf() private val values = mutableMapOf() private val editors = mutableMapOf() fun list(key: String) = lists.getOrPut(key) { PasswordsListModel(metadata, scope) } fun detail(key: String, id: String) = details.getOrPut(key) { SecretDetailModel(id, metadata, scope) } fun values(key: String, id: String) = values.getOrPut(key) { SecretValueModel(id, payloads, metadata, session, scope, clock, copyValue) } fun editor(key: String, kind: SecretKind, id: String?, initial: PasswordMetadataUpdate, opened: SecretPayload?) = editors.getOrPut(key) { SecretEditorModel(kind, id, initial, opened, payloads, session, scope) } fun releaseEditor(key: String) { editors.remove(key)?.close() } fun release(key: String) { values.remove(key)?.close() releaseEditor(key) details.remove(key) lists.remove(key) } fun clearTransient(key: String) { values[key]?.hide() editors[key]?.forgetSensitiveDraft() } }