Fixing mark as read

This commit is contained in:
Vitor Pamplona
2025-07-22 14:09:47 -04:00
parent 37ff0070ee
commit 6c77bf94a8
6 changed files with 28 additions and 51 deletions

View File

@@ -72,6 +72,15 @@ class FeedContentState(
viewModelScope.launch(Dispatchers.Default) { refreshSuspended() }
}
fun visibleNotes(): List<Note> {
val currentState = _feedContent.value
return if (currentState is FeedState.Loaded) {
currentState.feed.value.list
} else {
emptyList()
}
}
fun refreshSuspended() {
checkNotInMainThread()

View File

@@ -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<Note>,
accountViewModel: AccountViewModel,
onDone: () -> Unit,
) {
fun markAllChatNotesAsRead(notes: List<Note>) {
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()
}
}

View File

@@ -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<Boolean>,
) {
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<Boolean>,
) {
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<Boolean>,
) {
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,

View File

@@ -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<Boolean>,
)
@Composable
@@ -132,7 +130,6 @@ fun MessagesPager(
scrollStateKey = tabs[page].scrollStateKey,
accountViewModel = accountViewModel,
nav = nav,
markAsRead = tabs[page].markAsRead,
)
}
}

View File

@@ -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()) },
)
}
},

View File

@@ -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(