From f5f605b3cb0863ffbced97bd0255ae6556542ce5 Mon Sep 17 00:00:00 2001 From: "sergei.bakhtiarov" Date: Mon, 2 Feb 2026 17:56:04 +0100 Subject: [PATCH] fix: delay before updating unread messages badge (WPB-23126) Co-Authored-By: Claude Opus 4.5 --- .../feature/conversation/ConversationScope.kt | 3 + .../MarkConversationAsReadLocallyUseCase.kt | 59 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/MarkConversationAsReadLocallyUseCase.kt diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/ConversationScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/ConversationScope.kt index d02d81b2d51..0185802c11d 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/ConversationScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/ConversationScope.kt @@ -273,6 +273,9 @@ public class ConversationScope internal constructor( kaliumLogger ) + public val markConversationAsReadLocally: MarkConversationAsReadLocallyUseCase + get() = MarkConversationAsReadLocallyUseCaseImpl(conversationRepository) + public val updateConversationAccess: UpdateConversationAccessRoleUseCase get() = UpdateConversationAccessRoleUseCaseImpl(conversationRepository, conversationGroupRepository, syncManager) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/MarkConversationAsReadLocallyUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/MarkConversationAsReadLocallyUseCase.kt new file mode 100644 index 00000000000..bc9d72c1531 --- /dev/null +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/MarkConversationAsReadLocallyUseCase.kt @@ -0,0 +1,59 @@ +/* + * Wire + * Copyright (C) 2024 Wire Swiss GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ + +package com.wire.kalium.logic.feature.conversation + +import com.wire.kalium.common.error.StorageFailure +import com.wire.kalium.common.functional.Either +import com.wire.kalium.logic.data.conversation.ConversationRepository +import com.wire.kalium.logic.data.id.ConversationId +import kotlinx.datetime.Instant + +/** + * Use case to immediately update the conversation read date in the local database + * + * This is useful for immediately clearing the unread badge when the user + * leaves a conversation, while the debounced [UpdateConversationReadDateUseCase] + * handles sending confirmations and syncing to other devices. + */ +public interface MarkConversationAsReadLocallyUseCase { + /** + * Updates the conversation read date locally and returns whether + * the conversation still has unread events. + * + * @param conversationId The conversation to mark as read + * @param time The timestamp to set as the last read date + * @return [Either.Right] with true if there are still unread events, false otherwise + * [Either.Left] with [StorageFailure] if the operation failed + */ + public suspend operator fun invoke( + conversationId: ConversationId, + time: Instant + ): Either +} + +internal class MarkConversationAsReadLocallyUseCaseImpl internal constructor( + private val conversationRepository: ConversationRepository +) : MarkConversationAsReadLocallyUseCase { + + override suspend fun invoke( + conversationId: ConversationId, + time: Instant + ): Either = + conversationRepository.updateReadDateAndGetHasUnreadEvents(conversationId, time) +}