41 lines · 1.7 KB · KotlinRaw
package com.cottondb.androidimport com.cottondb.android.core.CottonCallingimport com.cottondb.android.media.MediaSourceimport com.cottondb.android.media.detail.DetailBytesModelimport com.cottondb.android.navigation.Routeimport com.cottondb.android.work.meetings.MeetingDetailModelimport com.cottondb.android.work.meetings.MeetingsModelimport 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<Route.MeetingsMeeting, MeetingDetailModel>() private val bytes = mutableMapOf<Route.MeetingsMeeting, DetailBytesModel>() 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<Route>) { 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() } }}