package com.cottondb.android import com.cottondb.android.core.CottonCalling import com.cottondb.android.media.MediaSource import com.cottondb.android.media.detail.DetailBytesModel import com.cottondb.android.navigation.Route import com.cottondb.android.work.meetings.MeetingDetailModel import com.cottondb.android.work.meetings.MeetingsModel import kotlinx.coroutines.CoroutineScope /** * Retained visits for the meetings route family. Import presentation is a list overlay, not a * second list visit: changing `/meetings?importing` must retain the search and read cache. */ class MeetingsModels( private val core: CottonCalling, private val scope: CoroutineScope, ) { private var list: MeetingsModel? = null private val details = mutableMapOf() private val bytes = mutableMapOf() fun meetings(): MeetingsModel = list ?: MeetingsModel(core, scope).also { list = it } fun detail(route: Route.MeetingsMeeting): MeetingDetailModel = details.getOrPut(route) { MeetingDetailModel(core, route.id, scope) } /** Audio is a detail-only visit; no list row is allowed to fetch or open original bytes. */ fun bytes(route: Route.MeetingsMeeting, source: MediaSource): DetailBytesModel = bytes.getOrPut(route) { DetailBytesModel(source, scope) } fun retain(routes: List) { if (routes.none { it is Route.Meetings }) { list?.close() list = null } details.keys.filterNot(routes::contains).forEach { route -> details.remove(route)?.close() } bytes.keys.filterNot(routes::contains).forEach { route -> bytes.remove(route)?.close() } } }