From d7afea7b3a2b61010211ad8ee62aaa981a058bf8 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Mon, 19 Sep 2022 17:51:49 +0200 Subject: [PATCH] Rename session use case --- .../v2/rename/RenameSessionFragment.kt | 7 +++- .../devices/v2/rename/RenameSessionUseCase.kt | 37 +++++++++++++++++++ .../v2/rename/RenameSessionViewEvent.kt | 1 + .../v2/rename/RenameSessionViewModel.kt | 22 ++++++++--- .../v2/rename/RenameSessionViewState.kt | 2 +- 5 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionUseCase.kt diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionFragment.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionFragment.kt index 964fef1a99..96e69e5e2b 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionFragment.kt @@ -76,15 +76,18 @@ class RenameSessionFragment : is RenameSessionViewEvent.SessionRenamed -> { viewNavigator.goBack(requireActivity()) } + is RenameSessionViewEvent.Failure -> { + showFailure(it.throwable) + } } } } override fun invalidate() = withState(viewModel) { state -> if (renameEditTextInitialized.not()) { - views.renameSessionEditText.setText(state.deviceName) + views.renameSessionEditText.setText(state.editedDeviceName) renameEditTextInitialized = true } - views.renameSessionSave.isEnabled = state.deviceName.isNotEmpty() + views.renameSessionSave.isEnabled = state.editedDeviceName.isNotEmpty() } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionUseCase.kt new file mode 100644 index 0000000000..17ae0baa5d --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionUseCase.kt @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 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 im.vector.app.features.settings.devices.v2.rename + +import im.vector.app.core.di.ActiveSessionHolder +import org.matrix.android.sdk.api.util.awaitCallback +import javax.inject.Inject + +// TODO add unit tests +class RenameSessionUseCase @Inject constructor( + private val activeSessionHolder: ActiveSessionHolder, +) { + + suspend fun execute(deviceId: String, newName: String): Result { + return runCatching { + awaitCallback { matrixCallback -> + activeSessionHolder.getActiveSession() + .cryptoService() + .setDeviceName(deviceId, newName, matrixCallback) + } + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewEvent.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewEvent.kt index 656c5bb165..e18ad4361a 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewEvent.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewEvent.kt @@ -20,4 +20,5 @@ import im.vector.app.core.platform.VectorViewEvents sealed class RenameSessionViewEvent : VectorViewEvents { object SessionRenamed : RenameSessionViewEvent() + data class Failure(val throwable: Throwable) : RenameSessionViewEvent() } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewModel.kt index 7e50ecce34..f9e80b5fa5 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewModel.kt @@ -26,11 +26,13 @@ import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.settings.devices.v2.overview.GetDeviceFullInfoUseCase import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch // TODO add unit tests class RenameSessionViewModel @AssistedInject constructor( @Assisted val initialState: RenameSessionViewState, private val getDeviceFullInfoUseCase: GetDeviceFullInfoUseCase, + private val renameSessionUseCase: RenameSessionUseCase, ) : VectorViewModel(initialState) { companion object : MavericksViewModelFactory by hiltMavericksViewModelFactory() @@ -46,7 +48,7 @@ class RenameSessionViewModel @AssistedInject constructor( private fun observeSessionInfo(deviceId: String) { getDeviceFullInfoUseCase.execute(deviceId) - .onEach { setState { copy(deviceName = it.deviceInfo.displayName.orEmpty()) } } + .onEach { setState { copy(editedDeviceName = it.deviceInfo.displayName.orEmpty()) } } .launchIn(viewModelScope) } @@ -58,11 +60,21 @@ class RenameSessionViewModel @AssistedInject constructor( } private fun handleEditLocally(editedName: String) { - setState { copy(deviceName = editedName) } + setState { copy(editedDeviceName = editedName) } } - private fun handleSaveModifications() { - // TODO call use case to save the modifications - _viewEvents.post(RenameSessionViewEvent.SessionRenamed) + private fun handleSaveModifications() = withState { viewState -> + viewModelScope.launch { + val result = renameSessionUseCase.execute( + deviceId = viewState.deviceId, + newName = viewState.editedDeviceName, + ) + val viewEvent = if (result.isSuccess) { + RenameSessionViewEvent.SessionRenamed + } else { + RenameSessionViewEvent.Failure(result.exceptionOrNull() ?: Exception()) + } + _viewEvents.post(viewEvent) + } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewState.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewState.kt index 18abba9c8d..70e11327ca 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewState.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/rename/RenameSessionViewState.kt @@ -20,7 +20,7 @@ import com.airbnb.mvrx.MavericksState data class RenameSessionViewState( val deviceId: String, - val deviceName: String = "", + val editedDeviceName: String = "", ) : MavericksState { constructor(args: RenameSessionArgs) : this( deviceId = args.deviceId