Refactoring caching systems for the Compose layer.

This commit is contained in:
Vitor Pamplona
2024-03-19 14:17:15 -04:00
parent e9830c61aa
commit b75c3e3031
3 changed files with 103 additions and 46 deletions
@@ -69,6 +69,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.halilibo.richtext.markdown.Markdown
import com.halilibo.richtext.markdown.MarkdownParseOptions
import com.halilibo.richtext.ui.material3.Material3RichText
import com.vitorpamplona.amethyst.commons.compose.produceCachedState
import com.vitorpamplona.amethyst.commons.richtext.BechSegment
import com.vitorpamplona.amethyst.commons.richtext.CashuSegment
import com.vitorpamplona.amethyst.commons.richtext.EmailSegment
@@ -98,7 +99,6 @@ import com.vitorpamplona.amethyst.ui.note.NoteCompose
import com.vitorpamplona.amethyst.ui.note.toShortenHex
import com.vitorpamplona.amethyst.ui.screen.SharedPreferencesViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.LoadedBechLink
import com.vitorpamplona.amethyst.ui.theme.Font17SP
import com.vitorpamplona.amethyst.ui.theme.HalfVertPadding
import com.vitorpamplona.amethyst.ui.theme.MarkdownTextStyle
@@ -562,15 +562,15 @@ fun ObserveNIP19(
accountViewModel: AccountViewModel,
onRefresh: () -> Unit,
) {
when (val parsed = entity) {
is Nip19Bech32.NPub -> ObserveNIP19User(parsed.hex, accountViewModel, onRefresh)
is Nip19Bech32.NProfile -> ObserveNIP19User(parsed.hex, accountViewModel, onRefresh)
when (entity) {
is Nip19Bech32.NPub -> ObserveNIP19User(entity.hex, accountViewModel, onRefresh)
is Nip19Bech32.NProfile -> ObserveNIP19User(entity.hex, accountViewModel, onRefresh)
is Nip19Bech32.Note -> ObserveNIP19Event(parsed.hex, accountViewModel, onRefresh)
is Nip19Bech32.NEvent -> ObserveNIP19Event(parsed.hex, accountViewModel, onRefresh)
is Nip19Bech32.NEmbed -> ObserveNIP19Event(parsed.event.id, accountViewModel, onRefresh)
is Nip19Bech32.Note -> ObserveNIP19Event(entity.hex, accountViewModel, onRefresh)
is Nip19Bech32.NEvent -> ObserveNIP19Event(entity.hex, accountViewModel, onRefresh)
is Nip19Bech32.NEmbed -> ObserveNIP19Event(entity.event.id, accountViewModel, onRefresh)
is Nip19Bech32.NAddress -> ObserveNIP19Event(parsed.atag, accountViewModel, onRefresh)
is Nip19Bech32.NAddress -> ObserveNIP19Event(entity.atag, accountViewModel, onRefresh)
is Nip19Bech32.NSec -> {}
is Nip19Bech32.NRelay -> {}
@@ -652,13 +652,7 @@ fun BechLink(
accountViewModel: AccountViewModel,
nav: (String) -> Unit,
) {
var loadedLink by remember { mutableStateOf<LoadedBechLink?>(null) }
if (loadedLink == null) {
LaunchedEffect(key1 = word) {
accountViewModel.parseNIP19(word) { loadedLink = it }
}
}
val loadedLink by produceCachedState(cache = accountViewModel.bechLinkCache, key = word)
val baseNote = loadedLink?.baseNote
@@ -37,6 +37,7 @@ import coil.imageLoader
import coil.request.ImageRequest
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.commons.compose.GenericBaseCache
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.AccountState
import com.vitorpamplona.amethyst.model.AddressableNote
@@ -1022,37 +1023,6 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View
}
}
suspend fun parseNIP19(
str: String,
onNote: (LoadedBechLink) -> Unit,
) {
withContext(Dispatchers.IO) {
Nip19Bech32.uriToRoute(str)?.let {
var returningNote: Note? = null
when (val parsed = it.entity) {
is Nip19Bech32.NSec -> {}
is Nip19Bech32.NPub -> {}
is Nip19Bech32.NProfile -> {}
is Nip19Bech32.Note -> LocalCache.checkGetOrCreateNote(parsed.hex)?.let { note -> returningNote = note }
is Nip19Bech32.NEvent -> LocalCache.checkGetOrCreateNote(parsed.hex)?.let { note -> returningNote = note }
is Nip19Bech32.NEmbed -> {
loadNEmbedIfNeeded(parsed.event)
LocalCache.checkGetOrCreateNote(parsed.event.id)?.let { note ->
returningNote = note
}
}
is Nip19Bech32.NRelay -> {}
is Nip19Bech32.NAddress -> LocalCache.checkGetOrCreateNote(parsed.atag)?.let { note -> returningNote = note }
else -> {}
}
onNote(LoadedBechLink(returningNote, it))
}
}
}
fun checkIsOnline(
media: String?,
onDone: (Boolean) -> Unit,
@@ -1321,6 +1291,37 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View
}
}
}
val bechLinkCache = CachedLoadedBechLink(this)
class CachedLoadedBechLink(val accountViewModel: AccountViewModel) : GenericBaseCache<String, LoadedBechLink>(20) {
override suspend fun compute(key: String): LoadedBechLink? {
return Nip19Bech32.uriToRoute(key)?.let {
var returningNote: Note? = null
when (val parsed = it.entity) {
is Nip19Bech32.NSec -> {}
is Nip19Bech32.NPub -> {}
is Nip19Bech32.NProfile -> {}
is Nip19Bech32.Note -> withContext(Dispatchers.IO) { LocalCache.checkGetOrCreateNote(parsed.hex)?.let { note -> returningNote = note } }
is Nip19Bech32.NEvent -> withContext(Dispatchers.IO) { LocalCache.checkGetOrCreateNote(parsed.hex)?.let { note -> returningNote = note } }
is Nip19Bech32.NEmbed ->
withContext(Dispatchers.IO) {
accountViewModel.loadNEmbedIfNeeded(parsed.event)
LocalCache.checkGetOrCreateNote(parsed.event.id)?.let { note ->
returningNote = note
}
}
is Nip19Bech32.NRelay -> {}
is Nip19Bech32.NAddress -> withContext(Dispatchers.IO) { LocalCache.checkGetOrCreateNote(parsed.atag)?.let { note -> returningNote = note } }
else -> {}
}
LoadedBechLink(returningNote, it)
}
}
}
}
class HasNotificationDot(bottomNavigationItems: ImmutableList<Route>) {