diff --git a/mail-message/data/src/main/kotlin/ch/protonmail/android/mailmessage/data/repository/RustMessageRepositoryImpl.kt b/mail-message/data/src/main/kotlin/ch/protonmail/android/mailmessage/data/repository/RustMessageRepositoryImpl.kt index 077f63e078..91eab24aff 100644 --- a/mail-message/data/src/main/kotlin/ch/protonmail/android/mailmessage/data/repository/RustMessageRepositoryImpl.kt +++ b/mail-message/data/src/main/kotlin/ch/protonmail/android/mailmessage/data/repository/RustMessageRepositoryImpl.kt @@ -43,7 +43,6 @@ import ch.protonmail.android.mailmessage.domain.repository.MessageRepository import ch.protonmail.android.mailpagination.domain.model.PageKey import ch.protonmail.android.mailpagination.domain.model.PaginationError import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map import me.proton.core.domain.entity.UserId @@ -93,17 +92,12 @@ class RustMessageRepositoryImpl @Inject constructor( rustMessageDataSource.observeMessage(userId, messageId.toLocalMessageId()) .map { either -> either.map { it.toMessage() } } - @Deprecated( - message = "Observing is faked! This won't reflect changes to the message after the first emission", - replaceWith = ReplaceWith("getMessage(userId, messageId)") - ) - override fun observeMessage(userId: UserId, remoteMessageId: RemoteMessageId): Flow> = - flow { - val message = rustMessageDataSource.getMessage(userId, remoteMessageId.toRemoteMessageId()) - .map { it.toMessage() } + override suspend fun getMessageByRemoteId( + userId: UserId, + remoteMessageId: RemoteMessageId + ): Either = rustMessageDataSource.getMessage(userId, remoteMessageId.toRemoteMessageId()) + .map { it.toMessage() } - emit(message) - } override suspend fun getConversationCursor( anchorItemId: CursorId, diff --git a/mail-message/data/src/test/kotlin/ch/protonmail/android/mailmessage/data/repository/RustMessageRepositoryImplTest.kt b/mail-message/data/src/test/kotlin/ch/protonmail/android/mailmessage/data/repository/RustMessageRepositoryImplTest.kt index 88af8d1852..b30be4ca47 100644 --- a/mail-message/data/src/test/kotlin/ch/protonmail/android/mailmessage/data/repository/RustMessageRepositoryImplTest.kt +++ b/mail-message/data/src/test/kotlin/ch/protonmail/android/mailmessage/data/repository/RustMessageRepositoryImplTest.kt @@ -126,15 +126,11 @@ internal class RustMessageRepositoryImplTest { } returns LocalMessageTestData.AugWeatherForecast.right() // When - repository.observeMessage(userId, messageId.toRemoteMessageId()).test { - val result = awaitItem().getOrElse { null } + val result = repository.getMessageByRemoteId(userId, messageId.toRemoteMessageId()).getOrElse { null } - // Then - assertEquals(expectedMessage, result) - coVerify { rustMessageDataSource.getMessage(userId, messageId) } - - awaitComplete() - } + // Then + assertEquals(expectedMessage, result) + coVerify { rustMessageDataSource.getMessage(userId, messageId) } } @Test @@ -167,19 +163,16 @@ internal class RustMessageRepositoryImplTest { DataError.Local.NoDataCached.left() // When - repository.observeMessage(userId, messageId).test { - val result = awaitItem() + val result = repository.getMessageByRemoteId(userId, messageId) - // Then - coVerify { rustMessageDataSource.getMessage(userId, messageId.toRemoteMessageId()) } - assert(result.isLeft()) - assertEquals(DataError.Local.NoDataCached, result.swap().getOrElse { null }) - awaitComplete() - } + // Then + coVerify { rustMessageDataSource.getMessage(userId, messageId.toRemoteMessageId()) } + assert(result.isLeft()) + assertEquals(DataError.Local.NoDataCached, result.swap().getOrElse { null }) } @Test - fun `when getConversationCursor returns a cursor with the first messsageId`() = runTest { + fun `when getConversationCursor returns a cursor with the first messageId`() = runTest { // Given val conversationCursor = mockk { every { current } returns CursorResult.Cursor(ConversationId("100")) diff --git a/mail-message/domain/src/main/kotlin/ch/protonmail/android/mailmessage/domain/repository/MessageRepository.kt b/mail-message/domain/src/main/kotlin/ch/protonmail/android/mailmessage/domain/repository/MessageRepository.kt index b2704db4f0..bc20ff8d83 100644 --- a/mail-message/domain/src/main/kotlin/ch/protonmail/android/mailmessage/domain/repository/MessageRepository.kt +++ b/mail-message/domain/src/main/kotlin/ch/protonmail/android/mailmessage/domain/repository/MessageRepository.kt @@ -76,7 +76,7 @@ interface MessageRepository { * Gets a [Message] metadata for [userId] from the local storage from a [RemoteMessageId]. * @return either the [Message] or a [DataError.Local] */ - fun observeMessage(userId: UserId, remoteMessageId: RemoteMessageId): Flow> + suspend fun getMessageByRemoteId(userId: UserId, remoteMessageId: RemoteMessageId): Either /** * Used to JIT swipe through conversations/ pages with methods exposed to get next and get previous and move to next diff --git a/mail-message/domain/src/main/kotlin/ch/protonmail/android/mailmessage/domain/usecase/GetMessageByRemoteId.kt b/mail-message/domain/src/main/kotlin/ch/protonmail/android/mailmessage/domain/usecase/GetMessageByRemoteId.kt index 93fed7839c..e42e405e69 100644 --- a/mail-message/domain/src/main/kotlin/ch/protonmail/android/mailmessage/domain/usecase/GetMessageByRemoteId.kt +++ b/mail-message/domain/src/main/kotlin/ch/protonmail/android/mailmessage/domain/usecase/GetMessageByRemoteId.kt @@ -23,7 +23,6 @@ import ch.protonmail.android.mailcommon.domain.model.DataError import ch.protonmail.android.mailmessage.domain.model.Message import ch.protonmail.android.mailmessage.domain.model.RemoteMessageId import ch.protonmail.android.mailmessage.domain.repository.MessageRepository -import kotlinx.coroutines.flow.first import me.proton.core.domain.entity.UserId import timber.log.Timber import javax.inject.Inject @@ -53,7 +52,7 @@ class GetMessageByRemoteId @Inject constructor( } private suspend fun getByRemoteId(userId: UserId, messageId: RemoteMessageId): Either = - messageRepository.observeMessage(userId, messageId).first() + messageRepository.getMessageByRemoteId(userId, messageId) } private const val MAX_RETRY_ATTEMPTS = 3 diff --git a/mail-message/domain/src/test/kotlin/ch/protonmail/android/mailmessage/domain/usecase/GetMessageByRemoteIdTest.kt b/mail-message/domain/src/test/kotlin/ch/protonmail/android/mailmessage/domain/usecase/GetMessageByRemoteIdTest.kt index 8f89e7dca7..09002917af 100644 --- a/mail-message/domain/src/test/kotlin/ch/protonmail/android/mailmessage/domain/usecase/GetMessageByRemoteIdTest.kt +++ b/mail-message/domain/src/test/kotlin/ch/protonmail/android/mailmessage/domain/usecase/GetMessageByRemoteIdTest.kt @@ -28,7 +28,6 @@ import ch.protonmail.android.testdata.message.rust.RemoteMessageIdSample import io.mockk.coEvery import io.mockk.coVerify import io.mockk.mockk -import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest import org.junit.Test import kotlin.test.assertEquals @@ -45,7 +44,7 @@ class GetMessageByRemoteIdTest { val userId = UserIdSample.Primary val message = MessageSample.SepWeatherForecast val remoteId = RemoteMessageIdSample.SepWeatherForecast - coEvery { repository.observeMessage(userId, remoteId) } returns flowOf(message.right()) + coEvery { repository.getMessageByRemoteId(userId, remoteId) } returns message.right() // When val actual = getMessage(userId, remoteId) @@ -60,14 +59,14 @@ class GetMessageByRemoteIdTest { val userId = UserIdSample.Primary val error = DataError.Local.NoDataCached val remoteId = RemoteMessageIdSample.SepWeatherForecast - coEvery { repository.observeMessage(userId, remoteId) } returns flowOf(error.left()) + coEvery { repository.getMessageByRemoteId(userId, remoteId) } returns error.left() // When val actual = getMessage(userId, remoteId) // Then assertEquals(error.left(), actual) - coVerify(exactly = 3) { repository.observeMessage(userId, remoteId) } + coVerify(exactly = 3) { repository.getMessageByRemoteId(userId, remoteId) } } } diff --git a/mail-message/domain/src/test/kotlin/ch/protonmail/android/mailmessage/domain/usecase/ObserveMessageTest.kt b/mail-message/domain/src/test/kotlin/ch/protonmail/android/mailmessage/domain/usecase/ObserveMessageTest.kt index ef6e746e39..9277468e9b 100644 --- a/mail-message/domain/src/test/kotlin/ch/protonmail/android/mailmessage/domain/usecase/ObserveMessageTest.kt +++ b/mail-message/domain/src/test/kotlin/ch/protonmail/android/mailmessage/domain/usecase/ObserveMessageTest.kt @@ -28,7 +28,6 @@ import ch.protonmail.android.mailmessage.domain.repository.MessageRepository import ch.protonmail.android.testdata.message.MessageTestData import ch.protonmail.android.testdata.user.UserIdTestData.userId import io.mockk.coEvery -import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest @@ -38,9 +37,9 @@ import kotlin.test.assertEquals internal class ObserveMessageTest { private val repository = mockk { - every { - this@mockk.observeMessage(userId, any()) - } returns flowOf(DataError.Local.NoDataCached.left()) + coEvery { + this@mockk.getMessageByRemoteId(userId, any()) + } returns DataError.Local.NoDataCached.left() coEvery { this@mockk.observeMessage(userId, any())