175 lines · 6.5 KB · KotlinRaw
package com.cottondb.androidimport com.cottondb.android.core.CottonCallingimport com.cottondb.android.navigation.Routeimport com.cottondb.android.work.money.MoneyAccountReadimport com.cottondb.android.work.money.MoneyReadimport com.cottondb.android.work.money.MoneyReadStateimport com.cottondb.android.work.money.MoneyRepositoryimport com.cottondb.android.work.money.MoneyStatementReadimport com.cottondb.android.work.money.MoneyTransactionReadimport com.cottondb.android.work.money.moneyTodayimport java.time.Clockimport java.time.ZoneIdimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Jobimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.cancelimport kotlinx.coroutines.flow.SharingStartedimport kotlinx.coroutines.flow.StateFlowimport kotlinx.coroutines.flow.Flowimport kotlinx.coroutines.flow.mapimport kotlinx.coroutines.flow.stateInimport kotlinx.coroutines.launch/** * Retains the independent read models represented by the current Accounts navigation stack. * * Every visit has a child scope. Popping its route cancels that scope, including its eager state * collection and any refresh it started, so a late core reply cannot republish into a retired UI. */class MoneyModels( private val core: CottonCalling, private val clock: Clock, private val zone: ZoneId, private val scope: CoroutineScope,) { private val repository = MoneyRepository(core) private var overview: MoneyOverviewVisit? = null private val accounts = mutableMapOf<String, MoneyAccountVisit>() private val statements = mutableMapOf<String, MoneyStatementVisit>() private val transactions = mutableMapOf<String, MoneyTransactionVisit>() private val deletions = mutableMapOf<String, MoneyDeleteVisit>() private val lifetimes = mutableMapOf<MoneyVisit, Job>() fun overview(): MoneyOverviewVisit = overview ?: owned { visitScope -> MoneyOverviewVisit(repository, clock, zone, visitScope) }.also { overview = it } fun account(id: String): MoneyAccountVisit = accounts.getOrPut(id) { owned { visitScope -> MoneyAccountVisit(id, repository, clock, zone, visitScope) } } fun statement(id: String): MoneyStatementVisit = statements.getOrPut(id) { owned { visitScope -> MoneyStatementVisit(id, repository, visitScope) } } fun transaction(id: String): MoneyTransactionVisit = transactions.getOrPut(id) { owned { visitScope -> MoneyTransactionVisit(id, repository, visitScope) } } fun deletion(id: String): MoneyDeleteVisit = deletions.getOrPut(id) { owned { visitScope -> MoneyDeleteVisit(id, core, visitScope) } } private fun <T : MoneyVisit> owned(create: (CoroutineScope) -> T): T { val lifetime = SupervisorJob(scope.coroutineContext[Job]) return create(CoroutineScope(scope.coroutineContext + lifetime)).also { lifetimes[it] = lifetime } } fun retain(routes: List<Route>) { if (Route.Money !in routes) overview?.let(::retire).also { overview = null } val accountIds = routes.filterIsInstance<Route.MoneyAccount>().map { it.id }.toSet() val statementIds = routes.filterIsInstance<Route.MoneyStatement>().map { it.id }.toSet() val transactionIds = routes.filterIsInstance<Route.MoneyTransaction>().map { it.id }.toSet() accounts.keys.filterNot(accountIds::contains).forEach { accounts.remove(it)?.let(::retire) } statements.keys.filterNot(statementIds::contains).forEach { statements.remove(it)?.let(::retire) } transactions.keys.filterNot(transactionIds::contains).forEach { transactions.remove(it)?.let(::retire) } deletions.keys.filterNot(transactionIds::contains).forEach { deletions.remove(it)?.let(::retire) } } private fun retire(visit: MoneyVisit) { visit.close() lifetimes.remove(visit)?.cancel() }}interface MoneyVisit : AutoCloseableabstract class MoneyReadVisit<T>( private val id: String?, protected val scope: CoroutineScope, source: Flow<MoneyReadState<T>>, initial: MoneyReadState<T>, private val unavailable: String,) : MoneyVisit { private var closed = false val state: StateFlow<MoneyReadState<T>> = if (id?.trim()?.isNotEmpty() == false) { kotlinx.coroutines.flow.MutableStateFlow<MoneyReadState<T>>(MoneyReadState.Error(unavailable, null)) } else { source.stateIn(scope, SharingStarted.Eagerly, initial) } /** Starts at most while this route visit remains retained. */ fun refresh(): Job = scope.launch { if (!closed && id?.trim()?.isNotEmpty() != false) refreshRead() } protected abstract suspend fun refreshRead() override fun close() { closed = true scope.cancel() }}class MoneyOverviewVisit internal constructor( private val repository: MoneyRepository, private val clock: Clock, private val zone: ZoneId, scope: CoroutineScope,) : MoneyReadVisit<MoneyRead>(null, scope, repository.money, repository.money.value, "Accounts could not be opened.") { override suspend fun refreshRead() { repository.refreshMoney(moneyToday(clock, zone)) }}class MoneyAccountVisit internal constructor( val id: String, private val repository: MoneyRepository, private val clock: Clock, private val zone: ZoneId, scope: CoroutineScope,) : MoneyReadVisit<MoneyAccountRead>( id, scope, repository.accounts.map { it[id.trim()] ?: MoneyReadState.Loading }, MoneyReadState.Loading, "This account could not be opened.",) { override suspend fun refreshRead() { repository.refreshAccount(id.trim(), moneyToday(clock, zone)) }}class MoneyStatementVisit internal constructor( val id: String, private val repository: MoneyRepository, scope: CoroutineScope,) : MoneyReadVisit<MoneyStatementRead>( id, scope, repository.statements.map { it[id.trim()] ?: MoneyReadState.Loading }, MoneyReadState.Loading, "This statement could not be opened.",) { override suspend fun refreshRead() { repository.refreshStatement(id.trim()) }}class MoneyTransactionVisit internal constructor( val id: String, private val repository: MoneyRepository, scope: CoroutineScope,) : MoneyReadVisit<MoneyTransactionRead>( id, scope, repository.transactions.map { it[id.trim()] ?: MoneyReadState.Loading }, MoneyReadState.Loading, "This transaction could not be opened.",) { override suspend fun refreshRead() { repository.refreshTransaction(id.trim()) }}