Cotton code
44 lines · 1.8 KB · KotlinRaw
package com.cottondb.androidimport com.cottondb.android.core.CottonCallingimport com.cottondb.android.generated.SecretKindimport com.cottondb.android.generated.SecretPayloadimport com.cottondb.android.passwords.*import java.time.Clockimport 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<String, PasswordsListModel>()    private val details = mutableMapOf<String, SecretDetailModel>()    private val values = mutableMapOf<String, SecretValueModel>()    private val editors = mutableMapOf<String, SecretEditorModel>()    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()    }}