From 182b8c951b069c73ab7b64f038d98cad72fdece4 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 15 Feb 2024 11:07:49 +0100 Subject: [PATCH] favorite : simplify and branch live data again --- features/roomactions/api/build.gradle.kts | 27 --------- .../api/SetRoomIsFavoriteAction.kt | 46 --------------- features/roomactions/impl/build.gradle.kts | 50 ---------------- .../impl/DefaultSetRoomIsFavoriteAction.kt | 51 ---------------- .../DefaultSetRoomIsFavoriteActionTests.kt | 59 ------------------- features/roomactions/test/build.gradle.kts | 28 --------- .../test/FakeSetRoomIsFavoriteAction.kt | 39 ------------ features/roomdetails/impl/build.gradle.kts | 2 - .../roomdetails/impl/RoomDetailsPresenter.kt | 10 +--- features/roomlist/impl/build.gradle.kts | 2 - .../roomlist/impl/RoomListPresenter.kt | 14 ++++- .../roomlist/impl/RoomListContextMenuTest.kt | 2 +- .../roomlist/impl/RoomListPresenterTests.kt | 44 +++++++------- 13 files changed, 41 insertions(+), 333 deletions(-) delete mode 100644 features/roomactions/api/build.gradle.kts delete mode 100644 features/roomactions/api/src/main/kotlin/io/element/android/features/roomactions/api/SetRoomIsFavoriteAction.kt delete mode 100644 features/roomactions/impl/build.gradle.kts delete mode 100644 features/roomactions/impl/src/main/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteAction.kt delete mode 100644 features/roomactions/impl/src/test/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteActionTests.kt delete mode 100644 features/roomactions/test/build.gradle.kts delete mode 100644 features/roomactions/test/src/main/kotlin/io/element/android/features/roomactions/test/FakeSetRoomIsFavoriteAction.kt diff --git a/features/roomactions/api/build.gradle.kts b/features/roomactions/api/build.gradle.kts deleted file mode 100644 index 6bfccef3e5..0000000000 --- a/features/roomactions/api/build.gradle.kts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2024 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -plugins { - id("io.element.android-library") -} - -android { - namespace = "io.element.android.features.roomactions.api" -} - -dependencies { - implementation(projects.libraries.matrix.api) -} diff --git a/features/roomactions/api/src/main/kotlin/io/element/android/features/roomactions/api/SetRoomIsFavoriteAction.kt b/features/roomactions/api/src/main/kotlin/io/element/android/features/roomactions/api/SetRoomIsFavoriteAction.kt deleted file mode 100644 index ecf1bcc4c9..0000000000 --- a/features/roomactions/api/src/main/kotlin/io/element/android/features/roomactions/api/SetRoomIsFavoriteAction.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2024 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.element.android.features.roomactions.api - -import io.element.android.libraries.matrix.api.core.RoomId -import io.element.android.libraries.matrix.api.room.MatrixRoom - -/** - * Set the favorite status of a room. - * Use either the room id or the room instance with the desired favorite status. - */ -interface SetRoomIsFavoriteAction { - - /** - * The result of the action? - */ - sealed interface Result { - data object Success : Result - data object RoomNotFound : Result - data class Exception(val inner: java.lang.Exception) : Result - } - - /** - * Set the favorite status of a room by its id, it'll try to load the room from the session. - */ - suspend operator fun invoke(roomId: RoomId, isFavorite: Boolean): Result - - /** - * Set the favorite status of a room using the provided instance. - */ - suspend operator fun invoke(room: MatrixRoom, isFavorite: Boolean): Result -} diff --git a/features/roomactions/impl/build.gradle.kts b/features/roomactions/impl/build.gradle.kts deleted file mode 100644 index 61f666da08..0000000000 --- a/features/roomactions/impl/build.gradle.kts +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2024 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// TODO: Remove once https://youtrack.jetbrains.com/issue/KTIJ-19369 is fixed -@Suppress("DSL_SCOPE_VIOLATION") -plugins { - id("io.element.android-compose-library") - alias(libs.plugins.anvil) - alias(libs.plugins.ksp) - id("kotlin-parcelize") -} - -android { - namespace = "io.element.android.features.roomactions.impl" -} - -anvil { - generateDaggerFactories.set(true) -} - -dependencies { - implementation(projects.anvilannotations) - anvil(projects.anvilcodegen) - api(projects.features.roomactions.api) - implementation(projects.libraries.core) - implementation(projects.libraries.architecture) - implementation(projects.libraries.matrix.api) - - testImplementation(libs.test.junit) - testImplementation(libs.coroutines.test) - testImplementation(libs.molecule.runtime) - testImplementation(libs.test.truth) - testImplementation(libs.test.turbine) - testImplementation(projects.libraries.matrix.test) - - ksp(libs.showkase.processor) -} diff --git a/features/roomactions/impl/src/main/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteAction.kt b/features/roomactions/impl/src/main/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteAction.kt deleted file mode 100644 index 94abd618a1..0000000000 --- a/features/roomactions/impl/src/main/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteAction.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2024 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.element.android.features.roomactions.impl - -import com.squareup.anvil.annotations.ContributesBinding -import io.element.android.features.roomactions.api.SetRoomIsFavoriteAction -import io.element.android.libraries.di.SessionScope -import io.element.android.libraries.matrix.api.MatrixClient -import io.element.android.libraries.matrix.api.core.RoomId -import io.element.android.libraries.matrix.api.room.MatrixRoom -import javax.inject.Inject -import kotlin.coroutines.cancellation.CancellationException - -@ContributesBinding(SessionScope::class) -class DefaultSetRoomIsFavoriteAction @Inject constructor(private val client: MatrixClient) : SetRoomIsFavoriteAction { - - override suspend operator fun invoke(roomId: RoomId, isFavorite: Boolean): SetRoomIsFavoriteAction.Result { - return client.getRoom(roomId)?.use { room -> - invoke(room, isFavorite) - } ?: SetRoomIsFavoriteAction.Result.RoomNotFound - } - - override suspend fun invoke(room: MatrixRoom, isFavorite: Boolean): SetRoomIsFavoriteAction.Result { - return room.setIsFavorite(isFavorite).fold( - onSuccess = { - SetRoomIsFavoriteAction.Result.Success - }, - onFailure = { throwable -> - if (throwable is Exception && throwable !is CancellationException) { - SetRoomIsFavoriteAction.Result.Exception(throwable) - } else { - throw throwable - } - } - ) - } -} diff --git a/features/roomactions/impl/src/test/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteActionTests.kt b/features/roomactions/impl/src/test/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteActionTests.kt deleted file mode 100644 index 0dd25d35ac..0000000000 --- a/features/roomactions/impl/src/test/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteActionTests.kt +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2024 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.element.android.features.roomactions.impl - -import io.element.android.features.roomactions.api.SetRoomIsFavoriteAction -import io.element.android.libraries.matrix.test.FakeMatrixClient -import io.element.android.libraries.matrix.test.room.FakeMatrixRoom -import kotlinx.coroutines.test.runTest -import org.junit.Test - -class DefaultSetRoomIsFavoriteActionTests { - private val room = FakeMatrixRoom() - - @Test - fun `given a room id and a client without rooms, when action is invoked, then it returns Result_RoomNotFound`() = runTest { - val action = DefaultSetRoomIsFavoriteAction(FakeMatrixClient()) - val result = action(room.roomId, true) - assert(result is SetRoomIsFavoriteAction.Result.RoomNotFound) - } - - @Test - fun `given a room, when action is invoked, then it returns Result_Success`() = runTest { - val action = DefaultSetRoomIsFavoriteAction(FakeMatrixClient()) - val result = action(room, true) - assert(result is SetRoomIsFavoriteAction.Result.Success) - } - - @Test - fun `given a room id and a client with a room, when action is invoked, then it returns Result_Success`() = runTest { - val client = FakeMatrixClient().apply { - givenGetRoomResult(room.roomId, room) - } - val action = DefaultSetRoomIsFavoriteAction(client) - val result = action(room.roomId, true) - assert(result is SetRoomIsFavoriteAction.Result.Success) - } - - @Test - fun `given a room, when action is invoked and fail, then it returns Result_Exception`() = runTest { - val action = DefaultSetRoomIsFavoriteAction(FakeMatrixClient()) - room.givenSetIsFavoriteResult(Result.failure(Exception())) - val result = action(room, true) - assert(result is SetRoomIsFavoriteAction.Result.Exception) - } -} diff --git a/features/roomactions/test/build.gradle.kts b/features/roomactions/test/build.gradle.kts deleted file mode 100644 index d43603e54a..0000000000 --- a/features/roomactions/test/build.gradle.kts +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2023 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -plugins { - id("io.element.android-library") -} - -android { - namespace = "io.element.android.features.roomactions.test" -} - -dependencies { - implementation(projects.features.roomactions.api) - implementation(projects.libraries.matrix.api) - implementation(libs.coroutines.core) -} diff --git a/features/roomactions/test/src/main/kotlin/io/element/android/features/roomactions/test/FakeSetRoomIsFavoriteAction.kt b/features/roomactions/test/src/main/kotlin/io/element/android/features/roomactions/test/FakeSetRoomIsFavoriteAction.kt deleted file mode 100644 index c892079f7b..0000000000 --- a/features/roomactions/test/src/main/kotlin/io/element/android/features/roomactions/test/FakeSetRoomIsFavoriteAction.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2024 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.element.android.features.roomactions.test - -import io.element.android.features.roomactions.api.SetRoomIsFavoriteAction -import io.element.android.libraries.matrix.api.core.RoomId -import io.element.android.libraries.matrix.api.room.MatrixRoom - -class FakeSetRoomIsFavoriteAction : SetRoomIsFavoriteAction { - private var counter = 0 - - fun assertCalled(number: Int) { - assert(counter == number) { "Expected $number calls, got $counter" } - } - - override suspend fun invoke(roomId: RoomId, isFavorite: Boolean): SetRoomIsFavoriteAction.Result { - counter++ - return SetRoomIsFavoriteAction.Result.Success - } - - override suspend fun invoke(room: MatrixRoom, isFavorite: Boolean): SetRoomIsFavoriteAction.Result { - counter++ - return SetRoomIsFavoriteAction.Result.Success - } -} diff --git a/features/roomdetails/impl/build.gradle.kts b/features/roomdetails/impl/build.gradle.kts index d76ebbc9d8..0f1a139f40 100644 --- a/features/roomdetails/impl/build.gradle.kts +++ b/features/roomdetails/impl/build.gradle.kts @@ -54,7 +54,6 @@ dependencies { implementation(projects.features.createroom.api) implementation(projects.services.analytics.api) implementation(projects.features.poll.api) - implementation(projects.features.roomactions.api) testImplementation(libs.test.junit) testImplementation(libs.coroutines.test) @@ -71,7 +70,6 @@ dependencies { testImplementation(projects.tests.testutils) testImplementation(projects.features.leaveroom.test) testImplementation(projects.features.createroom.test) - testImplementation(projects.features.roomactions.test) ksp(libs.showkase.processor) } diff --git a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsPresenter.kt b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsPresenter.kt index 52719cf3f1..a6d60edbac 100644 --- a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsPresenter.kt +++ b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsPresenter.kt @@ -29,7 +29,6 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.lifecycle.Lifecycle import io.element.android.features.leaveroom.api.LeaveRoomEvent import io.element.android.features.leaveroom.api.LeaveRoomPresenter -import io.element.android.features.roomactions.api.SetRoomIsFavoriteAction import io.element.android.features.roomdetails.impl.members.details.RoomMemberDetailsPresenter import io.element.android.libraries.architecture.Presenter import io.element.android.libraries.core.bool.orFalse @@ -62,19 +61,19 @@ class RoomDetailsPresenter @Inject constructor( private val roomMembersDetailsPresenterFactory: RoomMemberDetailsPresenter.Factory, private val leaveRoomPresenter: LeaveRoomPresenter, private val dispatchers: CoroutineDispatchers, - private val setRoomIsFavorite: SetRoomIsFavoriteAction, ) : Presenter { @Composable override fun present(): RoomDetailsState { val scope = rememberCoroutineScope() val leaveRoomState = leaveRoomPresenter.present() val canShowNotificationSettings = remember { mutableStateOf(false) } - val roomInfo = room.roomInfoFlow.collectAsState(initial = null).value + val roomInfo by room.roomInfoFlow.collectAsState(initial = null) val roomAvatar by remember { derivedStateOf { roomInfo?.avatarUrl ?: room.avatarUrl } } val roomName by remember { derivedStateOf { (roomInfo?.name ?: room.name ?: room.displayName).trim() } } val roomTopic by remember { derivedStateOf { roomInfo?.topic ?: room.topic } } + val isFavorite by remember { derivedStateOf { roomInfo?.isFavorite.orFalse() } } LaunchedEffect(Unit) { canShowNotificationSettings.value = featureFlagService.isFeatureEnabled(FeatureFlags.NotificationSettings) @@ -99,9 +98,6 @@ class RoomDetailsPresenter @Inject constructor( val dmMember by room.getDirectRoomMember(membersState) val roomMemberDetailsPresenter = roomMemberDetailsPresenter(dmMember) val roomType by getRoomType(dmMember) - val isFavorite by remember { - derivedStateOf { roomInfo?.isFavorite.orFalse() } - } val topicState = remember(canEditTopic, roomTopic, roomType) { val topic = roomTopic @@ -131,7 +127,7 @@ class RoomDetailsPresenter @Inject constructor( } is RoomDetailsEvent.SetIsFavorite -> { scope.launch { - setRoomIsFavorite(room, event.isFavorite) + room.setIsFavorite(event.isFavorite) } } } diff --git a/features/roomlist/impl/build.gradle.kts b/features/roomlist/impl/build.gradle.kts index ff14371ced..61f40b53ec 100644 --- a/features/roomlist/impl/build.gradle.kts +++ b/features/roomlist/impl/build.gradle.kts @@ -57,7 +57,6 @@ dependencies { implementation(projects.features.networkmonitor.api) implementation(projects.features.leaveroom.api) implementation(projects.services.analytics.api) - implementation(projects.features.roomactions.api) api(projects.features.roomlist.api) ksp(libs.showkase.processor) @@ -80,5 +79,4 @@ dependencies { testImplementation(projects.features.networkmonitor.test) testImplementation(projects.tests.testutils) testImplementation(projects.features.leaveroom.test) - testImplementation(projects.features.roomactions.test) } diff --git a/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt b/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt index 45fa71c6d6..59936db7d4 100644 --- a/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt +++ b/features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/RoomListPresenter.kt @@ -33,7 +33,6 @@ import io.element.android.features.leaveroom.api.LeaveRoomPresenter import io.element.android.features.networkmonitor.api.NetworkMonitor import io.element.android.features.networkmonitor.api.NetworkStatus import io.element.android.features.preferences.api.store.SessionPreferencesStore -import io.element.android.features.roomactions.api.SetRoomIsFavoriteAction import io.element.android.features.roomlist.impl.datasource.InviteStateDataSource import io.element.android.features.roomlist.impl.datasource.RoomListDataSource import io.element.android.features.roomlist.impl.migration.MigrationScreenPresenter @@ -54,8 +53,12 @@ import io.element.android.libraries.matrix.api.user.MatrixUser import io.element.android.libraries.matrix.api.user.getCurrentUser import io.element.android.libraries.matrix.api.verification.SessionVerificationService import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.onCompletion +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject private const val EXTENDED_RANGE_SIZE = 40 @@ -201,6 +204,15 @@ class RoomListPresenter @Inject constructor( hasNewContent = event.roomListRoomSummary.hasNewContent ) contextMenuState.value = initialState + client.getRoom(event.roomListRoomSummary.roomId)?.use { room -> + room.roomInfoFlow + .onEach { roomInfo -> + contextMenuState.value = initialState.copy( + isFavorite = roomInfo.isFavorite, + ) + } + .collect() + } } private fun updateVisibleRange(range: IntRange) { diff --git a/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListContextMenuTest.kt b/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListContextMenuTest.kt index 1a17ab1002..28177d6e0e 100644 --- a/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListContextMenuTest.kt +++ b/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListContextMenuTest.kt @@ -134,7 +134,7 @@ class RoomListContextMenuTest { @Test fun `clicking on Favourites generates expected Event`() { val eventsRecorder = EventsRecorder() - val contextMenu = aContextMenuShown(isDm = false, isFavorite = AsyncData.Success(false)) + val contextMenu = aContextMenuShown(isDm = false, isFavorite = false) val callback = EnsureNeverCalledWithParam() rule.setContent { RoomListContextMenu( diff --git a/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTests.kt b/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTests.kt index 8fae23e5d0..25e6d89b78 100644 --- a/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTests.kt +++ b/features/roomlist/impl/src/test/kotlin/io/element/android/features/roomlist/impl/RoomListPresenterTests.kt @@ -26,8 +26,6 @@ import io.element.android.features.leaveroom.fake.FakeLeaveRoomPresenter import io.element.android.features.networkmonitor.api.NetworkMonitor import io.element.android.features.networkmonitor.test.FakeNetworkMonitor import io.element.android.features.preferences.api.store.SessionPreferencesStore -import io.element.android.features.roomactions.api.SetRoomIsFavoriteAction -import io.element.android.features.roomactions.test.FakeSetRoomIsFavoriteAction import io.element.android.features.roomlist.impl.datasource.FakeInviteDataSource import io.element.android.features.roomlist.impl.datasource.InviteStateDataSource import io.element.android.features.roomlist.impl.datasource.RoomListDataSource @@ -35,7 +33,6 @@ import io.element.android.features.roomlist.impl.datasource.RoomListRoomSummaryF import io.element.android.features.roomlist.impl.migration.InMemoryMigrationScreenStore import io.element.android.features.roomlist.impl.migration.MigrationScreenPresenter import io.element.android.features.roomlist.impl.model.createRoomListRoomSummary -import io.element.android.libraries.architecture.AsyncData import io.element.android.libraries.dateformatter.api.LastMessageTimestampFormatter import io.element.android.libraries.dateformatter.test.A_FORMATTED_DATE import io.element.android.libraries.dateformatter.test.FakeLastMessageTimestampFormatter @@ -363,10 +360,11 @@ class RoomListPresenterTests { roomId = summary.roomId, roomName = summary.name, isDm = false, - isFavorite = AsyncData.Success(false), + isFavorite = false, markAsUnreadFeatureFlagEnabled = true, hasNewContent = false, - )) + ) + ) } room.setIsFavorite(isFavorite = true) @@ -377,10 +375,11 @@ class RoomListPresenterTests { roomId = summary.roomId, roomName = summary.name, isDm = false, - isFavorite = AsyncData.Success(true), + isFavorite = true, markAsUnreadFeatureFlagEnabled = true, hasNewContent = false, - )) + ) + ) } scope.cancel() } @@ -405,14 +404,16 @@ class RoomListPresenterTests { val shownState = awaitItem() assertThat(shownState.contextMenu) - .isEqualTo(RoomListState.ContextMenu.Shown( - roomId = summary.roomId, - roomName = summary.name, - isDm = false, - isFavorite = AsyncData.Success(false), - markAsUnreadFeatureFlagEnabled = true, - hasNewContent = false, - )) + .isEqualTo( + RoomListState.ContextMenu.Shown( + roomId = summary.roomId, + roomName = summary.name, + isDm = false, + isFavorite = false, + markAsUnreadFeatureFlagEnabled = true, + hasNewContent = false, + ) + ) shownState.eventSink(RoomListEvents.HideContextMenu) @@ -469,14 +470,19 @@ class RoomListPresenterTests { @Test fun `present - when set is favorite event is emitted, then the action is called`() = runTest { val scope = CoroutineScope(coroutineContext + SupervisorJob()) - val setRoomIsFavoriteAction = FakeSetRoomIsFavoriteAction() - val presenter = createRoomListPresenter(setRoomIsFavoriteAction = setRoomIsFavoriteAction, coroutineScope = scope) + val room = FakeMatrixRoom() + val client = FakeMatrixClient().apply { + givenGetRoomResult(A_ROOM_ID, room) + } + val presenter = createRoomListPresenter(client = client, coroutineScope = scope) moleculeFlow(RecompositionMode.Immediate) { presenter.present() }.test { val initialState = awaitItem() initialState.eventSink(RoomListEvents.SetRoomIsFavorite(A_ROOM_ID, true)) - setRoomIsFavoriteAction.assertCalled(1) + assertThat(room.setIsFavoriteCalls).isEqualTo(listOf(true)) + initialState.eventSink(RoomListEvents.SetRoomIsFavorite(A_ROOM_ID, false)) + assertThat(room.setIsFavoriteCalls).isEqualTo(listOf(true, false)) cancelAndIgnoreRemainingEvents() scope.cancel() } @@ -560,7 +566,6 @@ class RoomListPresenterTests { encryptionService: EncryptionService = FakeEncryptionService(), sessionPreferencesStore: SessionPreferencesStore = InMemorySessionPreferencesStore(), coroutineScope: CoroutineScope, - setRoomIsFavoriteAction: SetRoomIsFavoriteAction = FakeSetRoomIsFavoriteAction(), migrationScreenPresenter: MigrationScreenPresenter = MigrationScreenPresenter( matrixClient = client, migrationScreenStore = InMemoryMigrationScreenStore(), @@ -589,7 +594,6 @@ class RoomListPresenterTests { encryptionService = encryptionService, featureFlagService = FakeFeatureFlagService(mapOf(FeatureFlags.SecureStorage.key to true)), ), - setRoomIsFavorite = setRoomIsFavoriteAction, migrationScreenPresenter = migrationScreenPresenter, sessionPreferencesStore = sessionPreferencesStore, )