From 7d6bef9576592c24ed4bd7040b734e8905adef3c Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 21 Aug 2024 15:00:13 -0400 Subject: [PATCH] Moves heavy computations away from IO --- .../ui/screen/loggedIn/AccountViewModel.kt | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index bf837c119..6e3938719 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -378,7 +378,7 @@ class AccountViewModel( onZapAmount: (String) -> Unit, ) { if (zappedNote.zapPayments.isNotEmpty()) { - withContext(Dispatchers.IO) { + withContext(Dispatchers.Default) { account.calculateZappedAmount(zappedNote) { onZapAmount(showAmount(it)) } } } else { @@ -392,7 +392,7 @@ class AccountViewModel( ) { val zapraiserAmount = zappedNote.event?.zapraiserAmount() ?: 0 if (zappedNote.zapPayments.isNotEmpty()) { - withContext(Dispatchers.IO) { + withContext(Dispatchers.Default) { account.calculateZappedAmount(zappedNote) { newZapAmount -> var percentage = newZapAmount.div(zapraiserAmount.toBigDecimal()).toFloat() @@ -1049,9 +1049,11 @@ class AccountViewModel( note: Note, onResult: (Long?) -> Unit, ) { - withContext(Dispatchers.IO) { - onResult(LocalCache.findEarliestOtsForNote(note)) - } + onResult( + withContext(Dispatchers.Default) { + LocalCache.findEarliestOtsForNote(note) + }, + ) } fun cachedModificationEventsForNote(note: Note) = LocalCache.cachedModificationEventsForNote(note) @@ -1060,9 +1062,11 @@ class AccountViewModel( note: Note, onResult: (List) -> Unit, ) { - withContext(Dispatchers.IO) { - onResult(LocalCache.findLatestModificationForNote(note)) - } + onResult( + withContext(Dispatchers.Default) { + LocalCache.findLatestModificationForNote(note) + }, + ) } private suspend fun checkGetOrCreateChannel(key: HexKey): Channel? = LocalCache.checkGetOrCreateChannel(key) @@ -1547,7 +1551,7 @@ class AccountViewModel( 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) { + withContext(Dispatchers.Default) { val baseNote = LocalCache.getOrCreateNote(parsed.event) if (baseNote.event == null) { launch(Dispatchers.IO) {