From 37625987c16ee75ca0bd16acde814f026a788cd6 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 20 Oct 2025 19:41:27 -0400 Subject: [PATCH] Only listens to notification changes when the UI is visible --- .../ui/screen/loggedIn/AccountViewModel.kt | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 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 6040d63ce..ada2d229f 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 @@ -187,12 +187,22 @@ class AccountViewModel( }.map { it?.list?.firstOrNull()?.createdAt() }, ) { lastRead, newestItemCreatedAt -> emit(newestItemCreatedAt != null && newestItemCreatedAt > lastRead) + }.onStart { + val lastRead = account.loadLastReadFlow("Notification").value + val cards = feedStates.notifications.feedContent.value + if (cards is CardFeedState.Loaded) { + val newestItemCreatedAt = + cards.feed.value.list + .firstOrNull() + ?.createdAt() + emit(newestItemCreatedAt != null && newestItemCreatedAt > lastRead) + } } val notificationHasNewItemsFlow = notificationHasNewItems .flowOn(Dispatchers.Default) - .stateIn(viewModelScope, SharingStarted.Eagerly, false) + .stateIn(viewModelScope, SharingStarted.WhileSubscribed(30000), false) @OptIn(ExperimentalCoroutinesApi::class) val messagesHasNewItems = @@ -221,12 +231,27 @@ class AccountViewModel( } else { MutableStateFlow(false) } + }.onStart { + val feed = feedStates.dmKnown.feedContent.value + if (feed is FeedState.Loaded) { + val newItems = + feed.feed.value.list.any { chat -> + (chat.event as? ChatroomKeyable)?.let { event -> + val room = event.chatroomKey(account.signer.pubKey) + val lastReadAt = + account.settings.lastReadPerRoute.value["Room/${room.hashCode()}"] + ?.value ?: 0L + (chat.event?.createdAt ?: 0) > lastReadAt + } == true + } + emit(newItems) + } } val messagesHasNewItemsFlow = messagesHasNewItems .flowOn(Dispatchers.Default) - .stateIn(viewModelScope, SharingStarted.Eagerly, false) + .stateIn(viewModelScope, SharingStarted.WhileSubscribed(30000), false) @OptIn(ExperimentalCoroutinesApi::class) val homeHasNewItems = @@ -244,12 +269,22 @@ class AccountViewModel( }, ) { lastRead, newestItemCreatedAt -> emit(newestItemCreatedAt != null && newestItemCreatedAt > lastRead) + }.onStart { + val lastRead = account.loadLastReadFlow("HomeFollows").value + val feed = feedStates.homeNewThreads.feedContent.value + if (feed is FeedState.Loaded) { + val newestItemCreatedAt = + feed.feed.value.list + .firstOrNull { it.event != null && it.event !is GenericRepostEvent && it.event !is RepostEvent } + ?.createdAt() + emit(newestItemCreatedAt != null && newestItemCreatedAt > lastRead) + } } val homeHasNewItemsFlow = homeHasNewItems .flowOn(Dispatchers.Default) - .stateIn(viewModelScope, SharingStarted.Eagerly, false) + .stateIn(viewModelScope, SharingStarted.WhileSubscribed(30000), false) val hasNewItems = mapOf(