fix: mark chats as read when opening; closes #260 (#264)

This commit is contained in:
Diego Beraldin 2023-12-09 14:27:14 +01:00 committed by GitHub
parent a4615d964f
commit 60992dd406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 7 deletions

View File

@ -12,6 +12,7 @@ interface InboxMessagesMviModel :
sealed interface Intent {
data object Refresh : Intent
data object LoadNextPage : Intent
data class MarkAsRead(val read: Boolean, val id: Int) : Intent
}
data class UiState(

View File

@ -125,14 +125,23 @@ class InboxMessagesScreen : Tab {
lastMessageDate = chat.publishDate,
onOpenUser = rememberCallbackArgs { user ->
navigationCoordinator.pushScreen(
UserDetailScreen(user)
UserDetailScreen(user),
)
},
onOpen = rememberCallback {
if (!chat.read) {
model.reduce(
InboxMessagesMviModel.Intent.MarkAsRead(
read = true,
id = chat.id
),
)
}
val userId = chat.otherUser(uiState.currentUserId)?.id
if (userId != null) {
navigationCoordinator.pushScreen(
InboxChatScreen(userId)
InboxChatScreen(userId),
)
}
},

View File

@ -6,6 +6,7 @@ import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationC
import com.github.diegoberaldin.raccoonforlemmy.core.notifications.NotificationCenterEvent
import com.github.diegoberaldin.raccoonforlemmy.core.persistence.repository.SettingsRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.identity.repository.IdentityRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.PrivateMessageModel
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.data.otherUser
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.PrivateMessageRepository
import com.github.diegoberaldin.raccoonforlemmy.domain.lemmy.repository.SiteRepository
@ -74,6 +75,13 @@ class InboxMessagesViewModel(
InboxMessagesMviModel.Intent.Refresh -> mvi.scope?.launch(Dispatchers.IO) {
refresh()
}
is InboxMessagesMviModel.Intent.MarkAsRead -> {
markAsRead(
read = intent.read,
message = uiState.value.chats.first { it.id == intent.id },
)
}
}
}
@ -154,6 +162,40 @@ class InboxMessagesViewModel(
}
}
private fun markAsRead(read: Boolean, message: PrivateMessageModel) {
val auth = identityRepository.authToken.value
mvi.scope?.launch(Dispatchers.IO) {
messageRepository.markAsRead(
read = read,
messageId = message.id,
auth = auth,
)
val currentState = uiState.value
if (read && currentState.unreadOnly) {
mvi.updateState {
it.copy(
chats = currentState.chats.filter { c ->
c.id != message.id
}
)
}
} else {
mvi.updateState {
it.copy(
chats = currentState.chats.map { c ->
if (c.id == message.id) {
c.copy(read = read)
} else {
c
}
}
)
}
}
updateUnreadItems()
}
}
private fun handleLogout() {
mvi.updateState { it.copy(chats = emptyList()) }
}

View File

@ -88,7 +88,7 @@ class InboxRepliesViewModel(
is InboxRepliesMviModel.Intent.MarkAsRead -> {
markAsRead(
read = intent.read,
mention = uiState.value.replies.first { it.id == intent.id },
reply = uiState.value.replies.first { it.id == intent.id },
)
}
@ -174,12 +174,12 @@ class InboxRepliesViewModel(
}
}
private fun markAsRead(read: Boolean, mention: PersonMentionModel) {
private fun markAsRead(read: Boolean, reply: PersonMentionModel) {
val auth = identityRepository.authToken.value
mvi.scope?.launch(Dispatchers.IO) {
userRepository.setReplyRead(
read = read,
replyId = mention.id,
replyId = reply.id,
auth = auth,
)
val currentState = uiState.value
@ -187,7 +187,7 @@ class InboxRepliesViewModel(
mvi.updateState {
it.copy(
replies = currentState.replies.filter { r ->
r.id != mention.id
r.id != reply.id
}
)
}
@ -195,7 +195,7 @@ class InboxRepliesViewModel(
mvi.updateState {
it.copy(
replies = currentState.replies.map { r ->
if (r.id == mention.id) {
if (r.id == reply.id) {
r.copy(read = read)
} else {
r