mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-19 01:50:42 +02:00
Switches account cache to a flow
This commit is contained in:
@@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.accountsCache
|
|||||||
|
|
||||||
import android.content.ContentResolver
|
import android.content.ContentResolver
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.collection.LruCache
|
|
||||||
import com.vitorpamplona.amethyst.model.Account
|
import com.vitorpamplona.amethyst.model.Account
|
||||||
import com.vitorpamplona.amethyst.model.AccountSettings
|
import com.vitorpamplona.amethyst.model.AccountSettings
|
||||||
import com.vitorpamplona.amethyst.model.LocalCache
|
import com.vitorpamplona.amethyst.model.LocalCache
|
||||||
@@ -40,7 +39,9 @@ import kotlinx.coroutines.CoroutineScope
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.update
|
||||||
|
|
||||||
class AccountCacheState(
|
class AccountCacheState(
|
||||||
val geolocationFlow: StateFlow<LocationState.LocationResult>,
|
val geolocationFlow: StateFlow<LocationState.LocationResult>,
|
||||||
@@ -49,20 +50,15 @@ class AccountCacheState(
|
|||||||
val cache: LocalCache,
|
val cache: LocalCache,
|
||||||
val client: INostrClient,
|
val client: INostrClient,
|
||||||
) {
|
) {
|
||||||
val accounts =
|
val accounts = MutableStateFlow<Map<HexKey, Account>>(emptyMap())
|
||||||
object : LruCache<HexKey, Account>(20) {
|
|
||||||
override fun entryRemoved(
|
|
||||||
evicted: Boolean,
|
|
||||||
key: HexKey,
|
|
||||||
oldValue: Account,
|
|
||||||
newValue: Account?,
|
|
||||||
) {
|
|
||||||
super.entryRemoved(evicted, key, oldValue, newValue)
|
|
||||||
oldValue.scope.cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeAccount(pubkey: HexKey) = accounts.remove(pubkey)
|
fun removeAccount(pubkey: HexKey) {
|
||||||
|
accounts.update { existingAccounts ->
|
||||||
|
val oldValue = existingAccounts[pubkey]
|
||||||
|
oldValue?.scope?.cancel()
|
||||||
|
existingAccounts.minus(pubkey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun loadAccount(accountSettings: AccountSettings): Account =
|
fun loadAccount(accountSettings: AccountSettings): Account =
|
||||||
loadAccount(
|
loadAccount(
|
||||||
@@ -87,7 +83,7 @@ class AccountCacheState(
|
|||||||
signer: NostrSigner,
|
signer: NostrSigner,
|
||||||
accountSettings: AccountSettings,
|
accountSettings: AccountSettings,
|
||||||
): Account {
|
): Account {
|
||||||
val cached = accounts[signer.pubKey]
|
val cached = accounts.value[signer.pubKey]
|
||||||
if (cached != null) return cached
|
if (cached != null) return cached
|
||||||
|
|
||||||
return Account(
|
return Account(
|
||||||
@@ -106,8 +102,19 @@ class AccountCacheState(
|
|||||||
Log.e("AccountCacheState", "Account ${signer.pubKey} caught exception: ${throwable.message}", throwable)
|
Log.e("AccountCacheState", "Account ${signer.pubKey} caught exception: ${throwable.message}", throwable)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
).also {
|
).also { newAccount ->
|
||||||
accounts.put(signer.pubKey, it)
|
accounts.update { existingAccounts ->
|
||||||
|
existingAccounts.plus(Pair(signer.pubKey, newAccount))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun clear() {
|
||||||
|
accounts.update { existingAccounts ->
|
||||||
|
existingAccounts.forEach {
|
||||||
|
it.value.scope.cancel()
|
||||||
|
}
|
||||||
|
emptyMap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user