|
| 1 | +/* |
| 2 | + * Wire |
| 3 | + * Copyright (C) 2024 Wire Swiss GmbH |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.wire.kalium.logic.feature.conversation |
| 20 | + |
| 21 | +import com.wire.kalium.common.error.CoreFailure |
| 22 | +import com.wire.kalium.common.functional.Either |
| 23 | +import com.wire.kalium.common.functional.fold |
| 24 | +import com.wire.kalium.logic.data.conversation.ConversationRepository |
| 25 | +import com.wire.kalium.logic.data.id.ConversationId |
| 26 | +import kotlinx.datetime.Instant |
| 27 | + |
| 28 | +/** |
| 29 | + * Use case to immediately update the conversation read date in the local database |
| 30 | + * |
| 31 | + * This is useful for immediately clearing the unread badge when the user |
| 32 | + * leaves a conversation, while the debounced [UpdateConversationReadDateUseCase] |
| 33 | + * handles sending confirmations and syncing to other devices. |
| 34 | + */ |
| 35 | +public interface MarkConversationAsReadLocallyUseCase { |
| 36 | + /** |
| 37 | + * Updates the conversation read date locally and returns whether |
| 38 | + * the conversation still has unread events. |
| 39 | + * |
| 40 | + * @param conversationId The conversation to mark as read |
| 41 | + * @param time The timestamp to set as the last read date |
| 42 | + * @return [Either.Right] with true if there are still unread events, false otherwise |
| 43 | + * [Either.Left] with [StorageFailure] if the operation failed |
| 44 | + */ |
| 45 | + public suspend operator fun invoke( |
| 46 | + conversationId: ConversationId, |
| 47 | + time: Instant |
| 48 | + ): MarkConversationAsReadResult |
| 49 | +} |
| 50 | + |
| 51 | +public sealed class MarkConversationAsReadResult { |
| 52 | + public data class Success(val hasUnreadEvents: Boolean) : MarkConversationAsReadResult() |
| 53 | + public data class Failure(val failure: CoreFailure) : MarkConversationAsReadResult() |
| 54 | +} |
| 55 | + |
| 56 | +internal class MarkConversationAsReadLocallyUseCaseImpl internal constructor( |
| 57 | + private val conversationRepository: ConversationRepository |
| 58 | +) : MarkConversationAsReadLocallyUseCase { |
| 59 | + |
| 60 | + override suspend fun invoke( |
| 61 | + conversationId: ConversationId, |
| 62 | + time: Instant |
| 63 | + ): MarkConversationAsReadResult = |
| 64 | + conversationRepository.updateReadDateAndGetHasUnreadEvents(conversationId, time).fold( |
| 65 | + { failure -> MarkConversationAsReadResult.Failure(failure) }, |
| 66 | + { hasUnreadEvents -> MarkConversationAsReadResult.Success(hasUnreadEvents) } |
| 67 | + ) |
| 68 | +} |
0 commit comments