From 86cfaf70b491da8f17f45de6b5821b3a8a9f27db Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 17 Oct 2024 14:07:13 +0200 Subject: [PATCH] No need for UserProfilePresenterHelper anymore. --- .../impl/root/UserProfilePresenter.kt | 55 ++++++++++++--- .../shared/UserProfilePresenterHelper.kt | 67 ------------------- 2 files changed, 46 insertions(+), 76 deletions(-) delete mode 100644 features/userprofile/shared/src/main/kotlin/io/element/android/features/userprofile/shared/UserProfilePresenterHelper.kt diff --git a/features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt b/features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt index de58bf2682..30209d49e6 100644 --- a/features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt +++ b/features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt @@ -10,8 +10,10 @@ package io.element.android.features.userprofile.impl.root import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.MutableState +import androidx.compose.runtime.State import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.produceState import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue @@ -22,7 +24,6 @@ import io.element.android.features.createroom.api.StartDMAction import io.element.android.features.userprofile.api.UserProfileEvents import io.element.android.features.userprofile.api.UserProfileState import io.element.android.features.userprofile.api.UserProfileState.ConfirmationDialog -import io.element.android.features.userprofile.shared.UserProfilePresenterHelper import io.element.android.libraries.architecture.AsyncAction import io.element.android.libraries.architecture.AsyncData import io.element.android.libraries.architecture.Presenter @@ -31,6 +32,7 @@ import io.element.android.libraries.matrix.api.MatrixClient import io.element.android.libraries.matrix.api.core.RoomId import io.element.android.libraries.matrix.api.core.UserId import io.element.android.libraries.matrix.api.user.MatrixUser +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map @@ -47,10 +49,23 @@ class UserProfilePresenter @AssistedInject constructor( fun create(userId: UserId): UserProfilePresenter } - private val userProfilePresenterHelper = UserProfilePresenterHelper( - userId = userId, - client = client, - ) + @Composable + fun getDmRoomId(): State { + return produceState(initialValue = null) { + value = client.findDM(userId) + } + } + + @Composable + fun getCanCall(roomId: RoomId?): State { + return produceState(initialValue = false, roomId) { + value = if (client.isMe(userId)) { + false + } else { + roomId?.let { client.getRoom(it)?.canUserJoinCall(client.sessionId)?.getOrNull() == true }.orFalse() + } + } + } @Composable override fun present(): UserProfileState { @@ -60,8 +75,8 @@ class UserProfilePresenter @AssistedInject constructor( var userProfile by remember { mutableStateOf(null) } val startDmActionState: MutableState> = remember { mutableStateOf(AsyncAction.Uninitialized) } val isBlocked: MutableState> = remember { mutableStateOf(AsyncData.Uninitialized) } - val dmRoomId by userProfilePresenterHelper.getDmRoomId() - val canCall by userProfilePresenterHelper.getCanCall(dmRoomId) + val dmRoomId by getDmRoomId() + val canCall by getCanCall(dmRoomId) LaunchedEffect(Unit) { client.ignoredUsersFlow .map { ignoredUsers -> userId in ignoredUsers } @@ -80,7 +95,7 @@ class UserProfilePresenter @AssistedInject constructor( confirmationDialog = ConfirmationDialog.Block } else { confirmationDialog = null - userProfilePresenterHelper.blockUser(coroutineScope, isBlocked) + coroutineScope.blockUser(isBlocked) } } is UserProfileEvents.UnblockUser -> { @@ -88,7 +103,7 @@ class UserProfilePresenter @AssistedInject constructor( confirmationDialog = ConfirmationDialog.Unblock } else { confirmationDialog = null - userProfilePresenterHelper.unblockUser(coroutineScope, isBlocked) + coroutineScope.unblockUser(isBlocked) } } UserProfileEvents.ClearConfirmationDialog -> confirmationDialog = null @@ -119,4 +134,26 @@ class UserProfilePresenter @AssistedInject constructor( eventSink = ::handleEvents ) } + + private fun CoroutineScope.blockUser( + isBlockedState: MutableState>, + ) = launch { + isBlockedState.value = AsyncData.Loading(false) + client.ignoreUser(userId) + .onFailure { + isBlockedState.value = AsyncData.Failure(it, false) + } + // Note: on success, ignoredUsersFlow will emit new item. + } + + private fun CoroutineScope.unblockUser( + isBlockedState: MutableState>, + ) = launch { + isBlockedState.value = AsyncData.Loading(true) + client.unignoreUser(userId) + .onFailure { + isBlockedState.value = AsyncData.Failure(it, true) + } + // Note: on success, ignoredUsersFlow will emit new item. + } } diff --git a/features/userprofile/shared/src/main/kotlin/io/element/android/features/userprofile/shared/UserProfilePresenterHelper.kt b/features/userprofile/shared/src/main/kotlin/io/element/android/features/userprofile/shared/UserProfilePresenterHelper.kt deleted file mode 100644 index 3b206900b2..0000000000 --- a/features/userprofile/shared/src/main/kotlin/io/element/android/features/userprofile/shared/UserProfilePresenterHelper.kt +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2024 New Vector Ltd. - * - * SPDX-License-Identifier: AGPL-3.0-only - * Please see LICENSE in the repository root for full details. - */ - -package io.element.android.features.userprofile.shared - -import androidx.compose.runtime.Composable -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.State -import androidx.compose.runtime.produceState -import io.element.android.libraries.architecture.AsyncData -import io.element.android.libraries.core.bool.orFalse -import io.element.android.libraries.matrix.api.MatrixClient -import io.element.android.libraries.matrix.api.core.RoomId -import io.element.android.libraries.matrix.api.core.UserId -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.launch - -class UserProfilePresenterHelper( - private val userId: UserId, - private val client: MatrixClient, -) { - @Composable - fun getDmRoomId(): State { - return produceState(initialValue = null) { - value = client.findDM(userId) - } - } - - @Composable - fun getCanCall(roomId: RoomId?): State { - return produceState(initialValue = false, roomId) { - value = if (client.isMe(userId)) { - false - } else { - roomId?.let { client.getRoom(it)?.canUserJoinCall(client.sessionId)?.getOrNull() == true }.orFalse() - } - } - } - - fun blockUser( - scope: CoroutineScope, - isBlockedState: MutableState>, - ) = scope.launch { - isBlockedState.value = AsyncData.Loading(false) - client.ignoreUser(userId) - .onFailure { - isBlockedState.value = AsyncData.Failure(it, false) - } - // Note: on success, ignoredUserList will be updated. - } - - fun unblockUser( - scope: CoroutineScope, - isBlockedState: MutableState>, - ) = scope.launch { - isBlockedState.value = AsyncData.Loading(true) - client.unignoreUser(userId) - .onFailure { - isBlockedState.value = AsyncData.Failure(it, true) - } - // Note: on success, ignoredUserList will be updated. - } -}