From b1de2bf3262e71c18679a81526692efff5c1c7c3 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 10 Mar 2023 09:37:17 -0500 Subject: [PATCH] Moves MarkAsRead write and read to IO context. --- .../amethyst/ui/navigation/AppBottomBar.kt | 10 +++++++-- .../amethyst/ui/note/BadgeCompose.kt | 8 +++++-- .../amethyst/ui/note/BoostSetCompose.kt | 8 +++++-- .../amethyst/ui/note/ChatroomCompose.kt | 22 ++++++++++++------- .../ui/note/ChatroomMessageCompose.kt | 14 +++++++----- .../amethyst/ui/note/LikeSetCompose.kt | 8 +++++-- .../amethyst/ui/note/MessageSetCompose.kt | 9 ++++++-- .../amethyst/ui/note/MultiSetCompose.kt | 8 +++++-- .../amethyst/ui/note/NoteCompose.kt | 16 +++++++++----- .../amethyst/ui/note/ZapSetCompose.kt | 8 +++++-- 10 files changed, 78 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppBottomBar.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppBottomBar.kt index b83f06155..f59437670 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppBottomBar.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AppBottomBar.kt @@ -40,7 +40,9 @@ import androidx.navigation.NavHostController import com.vitorpamplona.amethyst.NotificationCache import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext val bottomNavigationItems = listOf( Route.Home, @@ -157,11 +159,15 @@ private fun NotifiableIcon(item: Route, currentRoute: String?, accountViewModel: val context = LocalContext.current.applicationContext LaunchedEffect(key1 = notif) { - hasNewItems = item.hasNewItems(account, notif.cache, context) + withContext(Dispatchers.IO) { + hasNewItems = item.hasNewItems(account, notif.cache, context) + } } LaunchedEffect(key1 = db) { - hasNewItems = item.hasNewItems(account, notif.cache, context) + withContext(Dispatchers.IO) { + hasNewItems = item.hasNewItems(account, notif.cache, context) + } } if (hasNewItems) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BadgeCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BadgeCompose.kt index 7cbf6a432..7d63a7df6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BadgeCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BadgeCompose.kt @@ -35,6 +35,8 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent import com.vitorpamplona.amethyst.ui.screen.BadgeCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @Composable @@ -56,9 +58,11 @@ fun BadgeCompose(likeSetCard: BadgeCard, modifier: Modifier = Modifier, isInnerN var isNew by remember { mutableStateOf(false) } LaunchedEffect(key1 = likeSetCard) { - isNew = likeSetCard.createdAt() > NotificationCache.load(routeForLastRead, context) + withContext(Dispatchers.IO) { + isNew = likeSetCard.createdAt() > NotificationCache.load(routeForLastRead, context) - NotificationCache.markAsRead(routeForLastRead, likeSetCard.createdAt(), context) + NotificationCache.markAsRead(routeForLastRead, likeSetCard.createdAt(), context) + } } var backgroundColor = if (isNew) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BoostSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BoostSetCompose.kt index 718bb233e..053a2e253 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BoostSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BoostSetCompose.kt @@ -32,6 +32,8 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent import com.vitorpamplona.amethyst.ui.screen.BoostSetCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @Composable @@ -53,9 +55,11 @@ fun BoostSetCompose(boostSetCard: BoostSetCard, isInnerNote: Boolean = false, ro var isNew by remember { mutableStateOf(false) } LaunchedEffect(key1 = boostSetCard) { - isNew = boostSetCard.createdAt > NotificationCache.load(routeForLastRead, context) + withContext(Dispatchers.IO) { + isNew = boostSetCard.createdAt > NotificationCache.load(routeForLastRead, context) - NotificationCache.markAsRead(routeForLastRead, boostSetCard.createdAt, context) + NotificationCache.markAsRead(routeForLastRead, boostSetCard.createdAt, context) + } } var backgroundColor = if (isNew) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomCompose.kt index 785491414..48f2d9b9b 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomCompose.kt @@ -50,6 +50,8 @@ import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent import com.vitorpamplona.amethyst.ui.components.AsyncImageProxy import com.vitorpamplona.amethyst.ui.components.ResizeImage import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @Composable fun ChatroomCompose( @@ -90,9 +92,11 @@ fun ChatroomCompose( var hasNewMessages by remember { mutableStateOf(false) } LaunchedEffect(key1 = notificationCache, key2 = note) { - note.createdAt()?.let { - hasNewMessages = - it > notificationCache.cache.load("Channel/${channel.idHex}", context) + withContext(Dispatchers.IO) { + note.createdAt()?.let { + hasNewMessages = + it > notificationCache.cache.load("Channel/${channel.idHex}", context) + } } } @@ -152,11 +156,13 @@ fun ChatroomCompose( var hasNewMessages by remember { mutableStateOf(false) } LaunchedEffect(key1 = notificationCache, key2 = note) { - noteEvent?.let { - hasNewMessages = it.createdAt() > notificationCache.cache.load( - "Room/${userToComposeOn.pubkeyHex}", - context - ) + withContext(Dispatchers.IO) { + noteEvent?.let { + hasNewMessages = it.createdAt() > notificationCache.cache.load( + "Room/${userToComposeOn.pubkeyHex}", + context + ) + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomMessageCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomMessageCompose.kt index 437755f99..a4a619c1c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomMessageCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomMessageCompose.kt @@ -64,6 +64,8 @@ import com.vitorpamplona.amethyst.ui.components.AsyncImageProxy import com.vitorpamplona.amethyst.ui.components.ResizeImage import com.vitorpamplona.amethyst.ui.components.TranslateableRichTextViewer import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext val ChatBubbleShapeMe = RoundedCornerShape(15.dp, 15.dp, 3.dp, 15.dp) val ChatBubbleShapeThem = RoundedCornerShape(3.dp, 15.dp, 15.dp, 15.dp) @@ -131,12 +133,14 @@ fun ChatroomMessageCompose( LaunchedEffect(key1 = routeForLastRead) { routeForLastRead?.let { - val lastTime = NotificationCache.load(it, context) + withContext(Dispatchers.IO) { + val lastTime = NotificationCache.load(it, context) - val createdAt = note.createdAt() - if (createdAt != null) { - NotificationCache.markAsRead(it, createdAt, context) - isNew = createdAt > lastTime + val createdAt = note.createdAt() + if (createdAt != null) { + NotificationCache.markAsRead(it, createdAt, context) + isNew = createdAt > lastTime + } } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/LikeSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/LikeSetCompose.kt index e25a270cf..722a74220 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/LikeSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/LikeSetCompose.kt @@ -32,6 +32,8 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent import com.vitorpamplona.amethyst.ui.screen.LikeSetCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @Composable @@ -53,9 +55,11 @@ fun LikeSetCompose(likeSetCard: LikeSetCard, modifier: Modifier = Modifier, isIn var isNew by remember { mutableStateOf(false) } LaunchedEffect(key1 = likeSetCard) { - isNew = likeSetCard.createdAt > NotificationCache.load(routeForLastRead, context) + withContext(Dispatchers.IO) { + isNew = likeSetCard.createdAt > NotificationCache.load(routeForLastRead, context) - NotificationCache.markAsRead(routeForLastRead, likeSetCard.createdAt, context) + NotificationCache.markAsRead(routeForLastRead, likeSetCard.createdAt, context) + } } var backgroundColor = if (isNew) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MessageSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MessageSetCompose.kt index 5d7e07ab7..743b06e73 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MessageSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MessageSetCompose.kt @@ -30,6 +30,8 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent import com.vitorpamplona.amethyst.ui.screen.MessageSetCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @Composable @@ -51,9 +53,12 @@ fun MessageSetCompose(messageSetCard: MessageSetCard, isInnerNote: Boolean = fal var isNew by remember { mutableStateOf(false) } LaunchedEffect(key1 = messageSetCard) { - isNew = messageSetCard.createdAt() > NotificationCache.load(routeForLastRead, context) + withContext(Dispatchers.IO) { + isNew = + messageSetCard.createdAt() > NotificationCache.load(routeForLastRead, context) - NotificationCache.markAsRead(routeForLastRead, messageSetCard.createdAt(), context) + NotificationCache.markAsRead(routeForLastRead, messageSetCard.createdAt(), context) + } } var backgroundColor = if (isNew) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt index bc6a7db78..f1fd8e8fa 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt @@ -36,6 +36,8 @@ import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent import com.vitorpamplona.amethyst.ui.screen.MultiSetCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @Composable @@ -57,9 +59,11 @@ fun MultiSetCompose(multiSetCard: MultiSetCard, modifier: Modifier = Modifier, r var isNew by remember { mutableStateOf(false) } LaunchedEffect(key1 = multiSetCard) { - isNew = multiSetCard.createdAt > NotificationCache.load(routeForLastRead, context) + withContext(Dispatchers.IO) { + isNew = multiSetCard.createdAt > NotificationCache.load(routeForLastRead, context) - NotificationCache.markAsRead(routeForLastRead, multiSetCard.createdAt, context) + NotificationCache.markAsRead(routeForLastRead, multiSetCard.createdAt, context) + } } var backgroundColor = if (isNew) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt index 44a958db5..271120cf7 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt @@ -60,6 +60,8 @@ import com.vitorpamplona.amethyst.ui.components.TranslateableRichTextViewer import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.ChannelHeader import com.vitorpamplona.amethyst.ui.theme.Following +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @Composable @@ -120,13 +122,15 @@ fun NoteCompose( var isNew by remember { mutableStateOf(false) } LaunchedEffect(key1 = routeForLastRead) { - routeForLastRead?.let { - val lastTime = NotificationCache.load(it, context) + withContext(Dispatchers.IO) { + routeForLastRead?.let { + val lastTime = NotificationCache.load(it, context) - val createdAt = note.createdAt() - if (createdAt != null) { - NotificationCache.markAsRead(it, createdAt, context) - isNew = createdAt > lastTime + val createdAt = note.createdAt() + if (createdAt != null) { + NotificationCache.markAsRead(it, createdAt, context) + isNew = createdAt > lastTime + } } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapSetCompose.kt index 6785d3599..98b0c1e09 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapSetCompose.kt @@ -34,6 +34,8 @@ import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent import com.vitorpamplona.amethyst.ui.screen.ZapSetCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @Composable @@ -55,9 +57,11 @@ fun ZapSetCompose(zapSetCard: ZapSetCard, modifier: Modifier = Modifier, isInner var isNew by remember { mutableStateOf(false) } LaunchedEffect(key1 = zapSetCard) { - isNew = zapSetCard.createdAt > NotificationCache.load(routeForLastRead, context) + withContext(Dispatchers.IO) { + isNew = zapSetCard.createdAt > NotificationCache.load(routeForLastRead, context) - NotificationCache.markAsRead(routeForLastRead, zapSetCard.createdAt, context) + NotificationCache.markAsRead(routeForLastRead, zapSetCard.createdAt, context) + } } var backgroundColor = if (isNew) {