Rename session use case

This commit is contained in:
Maxime NATUREL 2022-09-19 17:51:49 +02:00
parent b134d35dd6
commit d7afea7b3a
5 changed files with 61 additions and 8 deletions

View file

@ -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()
}
}

View file

@ -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<Unit> {
return runCatching {
awaitCallback<Unit> { matrixCallback ->
activeSessionHolder.getActiveSession()
.cryptoService()
.setDeviceName(deviceId, newName, matrixCallback)
}
}
}
}

View file

@ -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()
}

View file

@ -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<RenameSessionViewState, RenameSessionAction, RenameSessionViewEvent>(initialState) {
companion object : MavericksViewModelFactory<RenameSessionViewModel, RenameSessionViewState> 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)
}
}
}

View file

@ -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