Add StartDMAction to further share code
This commit is contained in:
parent
69d0a5c7b1
commit
118f1c1459
4 changed files with 97 additions and 33 deletions
|
|
@ -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<Async<RoomId>>)
|
||||||
|
}
|
||||||
|
|
@ -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<Async<RoomId>>) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -21,7 +21,7 @@ import androidx.compose.runtime.MutableState
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
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.SelectionMode
|
||||||
import io.element.android.features.createroom.impl.userlist.UserListDataStore
|
import io.element.android.features.createroom.impl.userlist.UserListDataStore
|
||||||
import io.element.android.features.createroom.impl.userlist.UserListPresenter
|
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.Async
|
||||||
import io.element.android.libraries.architecture.Presenter
|
import io.element.android.libraries.architecture.Presenter
|
||||||
import io.element.android.libraries.core.meta.BuildMeta
|
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.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.libraries.usersearch.api.UserRepository
|
||||||
import io.element.android.services.analytics.api.AnalyticsService
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
@ -44,8 +38,7 @@ class CreateRoomRootPresenter @Inject constructor(
|
||||||
presenterFactory: UserListPresenter.Factory,
|
presenterFactory: UserListPresenter.Factory,
|
||||||
userRepository: UserRepository,
|
userRepository: UserRepository,
|
||||||
userListDataStore: UserListDataStore,
|
userListDataStore: UserListDataStore,
|
||||||
private val matrixClient: MatrixClient,
|
private val startDMAction: StartDMAction,
|
||||||
private val analyticsService: AnalyticsService,
|
|
||||||
private val buildMeta: BuildMeta,
|
private val buildMeta: BuildMeta,
|
||||||
) : Presenter<CreateRoomRootState> {
|
) : Presenter<CreateRoomRootState> {
|
||||||
|
|
||||||
|
|
@ -62,36 +55,22 @@ class CreateRoomRootPresenter @Inject constructor(
|
||||||
val userListState = presenter.present()
|
val userListState = presenter.present()
|
||||||
|
|
||||||
val localCoroutineScope = rememberCoroutineScope()
|
val localCoroutineScope = rememberCoroutineScope()
|
||||||
val startDmAction: MutableState<Async<RoomId>> = remember { mutableStateOf(Async.Uninitialized) }
|
val startDmActionState: MutableState<Async<RoomId>> = remember { mutableStateOf(Async.Uninitialized) }
|
||||||
|
|
||||||
fun handleEvents(event: CreateRoomRootEvents) {
|
fun handleEvents(event: CreateRoomRootEvents) {
|
||||||
when (event) {
|
when (event) {
|
||||||
is CreateRoomRootEvents.StartDM -> localCoroutineScope.startDm(event.matrixUser, startDmAction)
|
is CreateRoomRootEvents.StartDM -> localCoroutineScope.launch {
|
||||||
CreateRoomRootEvents.CancelStartDM -> startDmAction.value = Async.Uninitialized
|
startDMAction.execute(event.matrixUser.userId, startDmActionState)
|
||||||
|
}
|
||||||
|
CreateRoomRootEvents.CancelStartDM -> startDmActionState.value = Async.Uninitialized
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return CreateRoomRootState(
|
return CreateRoomRootState(
|
||||||
applicationName = buildMeta.applicationName,
|
applicationName = buildMeta.applicationName,
|
||||||
userListState = userListState,
|
userListState = userListState,
|
||||||
startDmAction = startDmAction.value,
|
startDmAction = startDmActionState.value,
|
||||||
eventSink = ::handleEvents,
|
eventSink = ::handleEvents,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun CoroutineScope.startDm(matrixUser: MatrixUser, startDmAction: MutableState<Async<RoomId>>) = 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.MatrixClient
|
||||||
import io.element.android.libraries.matrix.api.core.RoomId
|
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.
|
* Try to find an existing DM with the given user, or create one if none exists.
|
||||||
*/
|
*/
|
||||||
suspend fun MatrixClient.startDM(matrixUser: MatrixUser): StartDMResult {
|
suspend fun MatrixClient.startDM(userId: UserId): StartDMResult {
|
||||||
val existingRoomId = findDM(matrixUser.userId)?.use { existingDM ->
|
val existingRoomId = findDM(userId)?.use { existingDM ->
|
||||||
existingDM.roomId
|
existingDM.roomId
|
||||||
}
|
}
|
||||||
return if (existingRoomId != null) {
|
return if (existingRoomId != null) {
|
||||||
StartDMResult.Success(existingRoomId, isNew = false)
|
StartDMResult.Success(existingRoomId, isNew = false)
|
||||||
} else {
|
} else {
|
||||||
createDM(matrixUser.userId).fold(
|
createDM(userId).fold(
|
||||||
{ StartDMResult.Success(it, isNew = true) },
|
{ StartDMResult.Success(it, isNew = true) },
|
||||||
{ StartDMResult.Failure(it.localizedMessage) }
|
{ StartDMResult.Failure(it.localizedMessage) }
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue