46 lines · 1.5 KB · KotlinRaw
package com.cottondb.androidimport com.cottondb.android.core.CottonCallingimport com.cottondb.android.take.TakeModelimport com.cottondb.android.take.TakeRepositoryimport com.cottondb.android.take.TakeVoiceimport kotlinx.coroutines.CoroutineScope/** * Process-owned Take visits. A model survives activity recreation while its * visit remains in navigation, but a removed visit releases its recognizer. */class TakeModels( private val core: CottonCalling, private val parentScope: CoroutineScope, private val writerScope: CoroutineScope, private val monotonicMillis: () -> Long, private val handback: com.cottondb.android.take.TakeHandback? = null,) { private val visits = mutableMapOf<String, TakeModel>() fun take(visit: String, voiceFactory: () -> TakeVoice): TakeModel = visits.getOrPut(visit) { TakeModel( repository = TakeRepository(core), voice = voiceFactory(), parentScope = parentScope, writerScope = writerScope, nowMillis = monotonicMillis, handback = handback, ) } fun retain(activeVisits: Set<String>) { visits.keys.filterNot(activeVisits::contains).forEach { visit -> visits.remove(visit)?.close() } } /** * The core model owns its token and final-result state. It must release a * listening recognizer without cancelling a finishing or admitted write. */ fun onAppBackgrounded() { visits.values.forEach(TakeModel::releaseForBackground) }}