package com.cottondb.android import com.cottondb.android.core.CottonCalling import com.cottondb.android.take.TakeModel import com.cottondb.android.take.TakeRepository import com.cottondb.android.take.TakeVoice import 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() 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) { 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) } }