diff --git a/features/createroom/api/src/main/kotlin/io/element/android/features/createroom/api/StartDMAction.kt b/features/createroom/api/src/main/kotlin/io/element/android/features/createroom/api/StartDMAction.kt new file mode 100644 index 0000000000..ef95f15dce --- /dev/null +++ b/features/createroom/api/src/main/kotlin/io/element/android/features/createroom/api/StartDMAction.kt @@ -0,0 +1,31 @@ +/* + * 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. + */ + +package io.element.android.features.createroom.api + +import androidx.compose.runtime.MutableState +import io.element.android.libraries.architecture.Async +import io.element.android.libraries.matrix.api.core.RoomId +import io.element.android.libraries.matrix.api.core.UserId + +interface StartDMAction { + /** + * Try to find an existing DM with the given user, or create one if none exists. + * @param userId The user to start a DM with. + * @param actionState The state to update with the result of the action. + */ + suspend fun execute(userId: UserId, actionState: MutableState>) +} diff --git a/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/DefaultStartDMAction.kt b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/DefaultStartDMAction.kt new file mode 100644 index 0000000000..0aa2e93dcb --- /dev/null +++ b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/DefaultStartDMAction.kt @@ -0,0 +1,54 @@ +/* + * 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. + */ + +package io.element.android.features.createroom.impl + +import androidx.compose.runtime.MutableState +import com.squareup.anvil.annotations.ContributesBinding +import im.vector.app.features.analytics.plan.CreatedRoom +import io.element.android.features.createroom.api.StartDMAction +import io.element.android.libraries.architecture.Async +import io.element.android.libraries.di.AppScope +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.core.UserId +import io.element.android.libraries.matrix.api.room.StartDMResult +import io.element.android.libraries.matrix.api.room.startDM +import io.element.android.services.analytics.api.AnalyticsService +import javax.inject.Inject + +@ContributesBinding(SessionScope::class) +class DefaultStartDMAction @Inject constructor( + private val matrixClient: MatrixClient, + private val analyticsService: AnalyticsService, +) : StartDMAction { + + override suspend fun execute(userId: UserId, actionState: MutableState>) { + actionState.value = Async.Loading() + when (val result = matrixClient.startDM(userId)) { + is StartDMResult.Success -> { + if (result.isNew) { + analyticsService.capture(CreatedRoom(isDM = true)) + } + actionState.value = Async.Success(result.roomId) + } + is StartDMResult.Failure -> { + actionState.value = Async.Failure(result) + } + } + } +} diff --git a/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt index bdccf3d625..9b60924ca4 100644 --- a/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt +++ b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt @@ -21,7 +21,7 @@ import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope -import im.vector.app.features.analytics.plan.CreatedRoom +import io.element.android.features.createroom.api.StartDMAction import io.element.android.features.createroom.impl.userlist.SelectionMode import io.element.android.features.createroom.impl.userlist.UserListDataStore import io.element.android.features.createroom.impl.userlist.UserListPresenter @@ -29,14 +29,8 @@ import io.element.android.features.createroom.impl.userlist.UserListPresenterArg import io.element.android.libraries.architecture.Async import io.element.android.libraries.architecture.Presenter import io.element.android.libraries.core.meta.BuildMeta -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.StartDMResult -import io.element.android.libraries.matrix.api.room.startDM -import io.element.android.libraries.matrix.api.user.MatrixUser import io.element.android.libraries.usersearch.api.UserRepository -import io.element.android.services.analytics.api.AnalyticsService -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import javax.inject.Inject @@ -44,8 +38,7 @@ class CreateRoomRootPresenter @Inject constructor( presenterFactory: UserListPresenter.Factory, userRepository: UserRepository, userListDataStore: UserListDataStore, - private val matrixClient: MatrixClient, - private val analyticsService: AnalyticsService, + private val startDMAction: StartDMAction, private val buildMeta: BuildMeta, ) : Presenter { @@ -62,36 +55,22 @@ class CreateRoomRootPresenter @Inject constructor( val userListState = presenter.present() val localCoroutineScope = rememberCoroutineScope() - val startDmAction: MutableState> = remember { mutableStateOf(Async.Uninitialized) } + val startDmActionState: MutableState> = remember { mutableStateOf(Async.Uninitialized) } fun handleEvents(event: CreateRoomRootEvents) { when (event) { - is CreateRoomRootEvents.StartDM -> localCoroutineScope.startDm(event.matrixUser, startDmAction) - CreateRoomRootEvents.CancelStartDM -> startDmAction.value = Async.Uninitialized + is CreateRoomRootEvents.StartDM -> localCoroutineScope.launch { + startDMAction.execute(event.matrixUser.userId, startDmActionState) + } + CreateRoomRootEvents.CancelStartDM -> startDmActionState.value = Async.Uninitialized } } return CreateRoomRootState( applicationName = buildMeta.applicationName, userListState = userListState, - startDmAction = startDmAction.value, + startDmAction = startDmActionState.value, eventSink = ::handleEvents, ) } - - private fun CoroutineScope.startDm(matrixUser: MatrixUser, startDmAction: MutableState>) = launch { - startDmAction.value = Async.Loading() - when (val result = matrixClient.startDM(matrixUser)) { - is StartDMResult.Success -> { - if (result.isNew) { - analyticsService.capture(CreatedRoom(isDM = true)) - } - startDmAction.value = Async.Success(result.roomId) - } - is StartDMResult.Failure -> { - startDmAction.value = Async.Failure(result) - } - } - - } } diff --git a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/StartDM.kt b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/StartDM.kt index 4aa280513b..722f598d51 100644 --- a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/StartDM.kt +++ b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/StartDM.kt @@ -18,19 +18,19 @@ package io.element.android.libraries.matrix.api.room import io.element.android.libraries.matrix.api.MatrixClient import io.element.android.libraries.matrix.api.core.RoomId -import io.element.android.libraries.matrix.api.user.MatrixUser +import io.element.android.libraries.matrix.api.core.UserId /** * Try to find an existing DM with the given user, or create one if none exists. */ -suspend fun MatrixClient.startDM(matrixUser: MatrixUser): StartDMResult { - val existingRoomId = findDM(matrixUser.userId)?.use { existingDM -> +suspend fun MatrixClient.startDM(userId: UserId): StartDMResult { + val existingRoomId = findDM(userId)?.use { existingDM -> existingDM.roomId } return if (existingRoomId != null) { StartDMResult.Success(existingRoomId, isNew = false) } else { - createDM(matrixUser.userId).fold( + createDM(userId).fold( { StartDMResult.Success(it, isNew = true) }, { StartDMResult.Failure(it.localizedMessage) } )