fix: marking channels as unread

Signed-off-by: Infi <infi@infi.sh>
This commit is contained in:
Infi 2025-03-24 13:07:36 +01:00
parent 11439bf987
commit 8f4037830e
1 changed files with 7 additions and 21 deletions

View File

@ -143,7 +143,7 @@ class ChannelScreenViewModel @Inject constructor(
denyMessageFieldIfNeeded() denyMessageFieldIfNeeded()
} }
this.loadMessages(50) this.loadMessages(50, markLastAsRead = true)
} }
suspend fun unlockAgeGate() { suspend fun unlockAgeGate() {
@ -420,7 +420,8 @@ class ChannelScreenViewModel @Inject constructor(
before: String? = null, before: String? = null,
after: String? = null, after: String? = null,
around: String? = null, around: String? = null,
ignoreExisting: Boolean = false ignoreExisting: Boolean = false,
markLastAsRead: Boolean = false
) { ) {
channel?.id?.let { channelId -> channel?.id?.let { channelId ->
loadMessagesJob = viewModelScope.launch { loadMessagesJob = viewModelScope.launch {
@ -451,13 +452,11 @@ class ChannelScreenViewModel @Inject constructor(
RevoltAPI.members.setMember(member.id.server, member) RevoltAPI.members.setMember(member.id.server, member)
} }
} }
}
val ackNewest: Boolean if (markLastAsRead) {
if (!didInitialChannelFetch) { ackMessage(messages.firstOrNull()?.id ?: return@launch)
didInitialChannelFetch = true }
ackNewest = true }
} else ackNewest = false
val newItems = messages.filter { val newItems = messages.filter {
if (ignoreExisting) { if (ignoreExisting) {
@ -490,19 +489,6 @@ class ChannelScreenViewModel @Inject constructor(
} }
updateItems(newItemsWithPosition) updateItems(newItemsWithPosition)
// If ackNewest is true, we ack the newest (first, as initial fetch is newest to oldest) message.
if (ackNewest) {
ackMessage(newItemsWithPosition.first {
it is ChannelScreenItem.RegularMessage || it is ChannelScreenItem.SystemMessage
}.let {
when (it) {
is ChannelScreenItem.RegularMessage -> it.message.id
is ChannelScreenItem.SystemMessage -> it.message.id
else -> null
}
} ?: return@launch)
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e("ChannelScreenViewModel", "Failed to fetch messages", e) Log.e("ChannelScreenViewModel", "Failed to fetch messages", e)
} }