diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentState.kt index 4602d5974..e6b9dccc0 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/feeds/FeedContentState.kt @@ -72,6 +72,15 @@ class FeedContentState( viewModelScope.launch(Dispatchers.Default) { refreshSuspended() } } + fun visibleNotes(): List { + val currentState = _feedContent.value + return if (currentState is FeedState.Loaded) { + currentState.feed.value.list + } else { + emptyList() + } + } + fun refreshSuspended() { checkNotInMainThread() 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 1b07a1496..664fd8815 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 @@ -71,7 +71,6 @@ import com.vitorpamplona.amethyst.ui.components.UrlPreviewState import com.vitorpamplona.amethyst.ui.components.toasts.ToastManager import com.vitorpamplona.amethyst.ui.feeds.FeedState import com.vitorpamplona.amethyst.ui.navigation.routes.Route -import com.vitorpamplona.amethyst.ui.navigation.routes.routeFor import com.vitorpamplona.amethyst.ui.note.ZapAmountCommentNotification import com.vitorpamplona.amethyst.ui.note.ZapraiserStatus import com.vitorpamplona.amethyst.ui.note.showAmount @@ -109,6 +108,7 @@ import com.vitorpamplona.quartz.nip19Bech32.entities.NProfile import com.vitorpamplona.quartz.nip19Bech32.entities.NPub import com.vitorpamplona.quartz.nip19Bech32.entities.NRelay import com.vitorpamplona.quartz.nip19Bech32.entities.NSec +import com.vitorpamplona.quartz.nip28PublicChat.base.IsInPublicChatChannel import com.vitorpamplona.quartz.nip37Drafts.DraftEvent import com.vitorpamplona.quartz.nip47WalletConnect.Nip47WalletConnect import com.vitorpamplona.quartz.nip47WalletConnect.Response @@ -1188,26 +1188,16 @@ class AccountViewModel( return onIsNew } - fun markAllAsRead( - notes: ImmutableList, - accountViewModel: AccountViewModel, - onDone: () -> Unit, - ) { + fun markAllChatNotesAsRead(notes: List) { viewModelScope.launch(Dispatchers.IO) { for (note in notes) { - note.event?.createdAt?.let { date -> - val route = routeFor(note, accountViewModel.account) - route?.let { - if (route is Route.Room) { - account.markAsRead("Room/${route.id}", date) - } else if (route is Route.PublicChatChannel) { - account.markAsRead("Channel/${route.id}", date) - } - } + val noteEvent = note.event + if (noteEvent is IsInPublicChatChannel) { + account.markAsRead("Channel/${noteEvent.channelId()}", noteEvent.createdAt) + } else if (noteEvent is ChatroomKeyable) { + account.markAsRead("Room/${noteEvent.chatroomKey(account.signer.pubKey).hashCode()}", noteEvent.createdAt) } } - - onDone() } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/feed/ChatroomListFeedView.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/feed/ChatroomListFeedView.kt index 2901cd505..a43e09e9c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/feed/ChatroomListFeedView.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/feed/ChatroomListFeedView.kt @@ -28,8 +28,6 @@ import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.material3.HorizontalDivider import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.MutableState import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.lifecycle.compose.collectAsStateWithLifecycle @@ -53,11 +51,10 @@ fun ChatroomListFeedView( scrollStateKey: String, accountViewModel: AccountViewModel, nav: INav, - markAsRead: MutableState, ) { RefresheableBox(feedContentState, true) { SaveableFeedContentState(feedContentState, scrollStateKey) { listState -> - CrossFadeState(feedContentState, listState, accountViewModel, nav, markAsRead) + CrossFadeState(feedContentState, listState, accountViewModel, nav) } } } @@ -68,7 +65,6 @@ private fun CrossFadeState( listState: LazyListState, accountViewModel: AccountViewModel, nav: INav, - markAsRead: MutableState, ) { val feedState by feedContentState.feedContent.collectAsStateWithLifecycle() @@ -85,7 +81,7 @@ private fun CrossFadeState( FeedError(state.errorMessage) { feedContentState.invalidateData() } } is FeedState.Loaded -> { - FeedLoaded(state, listState, accountViewModel, nav, markAsRead) + FeedLoaded(state, listState, accountViewModel, nav) } FeedState.Loading -> { LoadingFeed() @@ -100,16 +96,9 @@ private fun FeedLoaded( listState: LazyListState, accountViewModel: AccountViewModel, nav: INav, - markAsRead: MutableState, ) { val items by loaded.feed.collectAsStateWithLifecycle() - LaunchedEffect(key1 = markAsRead.value) { - if (markAsRead.value) { - accountViewModel.markAllAsRead(items.list, accountViewModel) { markAsRead.value = false } - } - } - LazyColumn( contentPadding = FeedPadding, state = listState, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/feed/ChatroomListTabs.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/feed/ChatroomListTabs.kt index 95a77e328..15a8a6371 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/feed/ChatroomListTabs.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/feed/ChatroomListTabs.kt @@ -38,7 +38,6 @@ import androidx.compose.material3.TabRow import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.Immutable -import androidx.compose.runtime.MutableState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -62,7 +61,6 @@ class MessagesTabItem( val resource: Int, val scrollStateKey: String, val feedContentState: FeedContentState, - val markAsRead: MutableState, ) @Composable @@ -132,7 +130,6 @@ fun MessagesPager( scrollStateKey = tabs[page].scrollStateKey, accountViewModel = accountViewModel, nav = nav, - markAsRead = tabs[page].markAsRead, ) } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/singlepane/MessagesSinglePane.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/singlepane/MessagesSinglePane.kt index 28d5be2c3..6503d9f85 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/singlepane/MessagesSinglePane.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/singlepane/MessagesSinglePane.kt @@ -25,7 +25,6 @@ import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.ui.feeds.FeedContentState @@ -53,20 +52,17 @@ fun MessagesSinglePane( ) { val pagerState = rememberPagerState { 2 } - val markKnownAsRead = remember { mutableStateOf(false) } - val markNewAsRead = remember { mutableStateOf(false) } - WatchLifecycleAndUpdateModel(knownFeedContentState) WatchLifecycleAndUpdateModel(newFeedContentState) ChatroomListFilterAssemblerSubscription(accountViewModel) val tabs by - remember(knownFeedContentState, markKnownAsRead) { + remember(knownFeedContentState) { derivedStateOf { listOf( - MessagesTabItem(R.string.known, ScrollStateKeys.MESSAGES_KNOWN, knownFeedContentState, markKnownAsRead), - MessagesTabItem(R.string.new_requests, ScrollStateKeys.MESSAGES_NEW, newFeedContentState, markNewAsRead), + MessagesTabItem(R.string.known, ScrollStateKeys.MESSAGES_KNOWN, knownFeedContentState), + MessagesTabItem(R.string.new_requests, ScrollStateKeys.MESSAGES_NEW, newFeedContentState), ) } } @@ -79,8 +75,8 @@ fun MessagesSinglePane( MessagesTabHeader( pagerState, tabs, - { markKnownAsRead.value = true }, - { markNewAsRead.value = true }, + { accountViewModel.markAllChatNotesAsRead(knownFeedContentState.visibleNotes()) }, + { accountViewModel.markAllChatNotesAsRead(newFeedContentState.visibleNotes()) }, ) } }, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/twopane/ChatroomListPane.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/twopane/ChatroomListPane.kt index 713a6f071..0525c2bf9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/twopane/ChatroomListPane.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/rooms/twopane/ChatroomListPane.kt @@ -26,7 +26,6 @@ import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.unit.dp import com.vitorpamplona.amethyst.R @@ -49,20 +48,17 @@ fun ChatroomList( ) { val pagerState = rememberPagerState { 2 } - val markKnownAsRead = remember { mutableStateOf(false) } - val markNewAsRead = remember { mutableStateOf(false) } - WatchLifecycleAndUpdateModel(knownFeedContentState) WatchLifecycleAndUpdateModel(newFeedContentState) ChatroomListFilterAssemblerSubscription(accountViewModel) val tabs by - remember(knownFeedContentState, markKnownAsRead) { + remember(knownFeedContentState) { derivedStateOf { listOf( - MessagesTabItem(R.string.known, ScrollStateKeys.MESSAGES_KNOWN, knownFeedContentState, markKnownAsRead), - MessagesTabItem(R.string.new_requests, ScrollStateKeys.MESSAGES_NEW, newFeedContentState, markNewAsRead), + MessagesTabItem(R.string.known, ScrollStateKeys.MESSAGES_KNOWN, knownFeedContentState), + MessagesTabItem(R.string.new_requests, ScrollStateKeys.MESSAGES_NEW, newFeedContentState), ) } } @@ -71,8 +67,8 @@ fun ChatroomList( MessagesTabHeader( pagerState, tabs, - { markKnownAsRead.value = true }, - { markNewAsRead.value = true }, + { accountViewModel.markAllChatNotesAsRead(knownFeedContentState.visibleNotes()) }, + { accountViewModel.markAllChatNotesAsRead(newFeedContentState.visibleNotes()) }, ) MessagesPager(