From e925d29037e82cc07977e5b265cbf4dedd39dd93 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 27 May 2021 15:37:26 +0200 Subject: [PATCH] Avoid adding twice the same server --- .../im/vector/app/core/extensions/Activity.kt | 4 ++++ .../roomdirectory/RoomDirectoryActivity.kt | 3 ++- .../picker/RoomDirectoryPickerViewModel.kt | 19 +++++++++++++++++-- vector/src/main/res/values/strings.xml | 1 + 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/extensions/Activity.kt b/vector/src/main/java/im/vector/app/core/extensions/Activity.kt index 55ec8b605e..de469b9e3a 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/Activity.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/Activity.kt @@ -94,6 +94,10 @@ fun AppCompatActivity.addFragmentToBackstack( } } +fun AppCompatActivity.popBackstack() { + supportFragmentManager.popBackStack() +} + fun AppCompatActivity.resetBackstack() { repeat(supportFragmentManager.backStackEntryCount) { supportFragmentManager.popBackStack() diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/RoomDirectoryActivity.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/RoomDirectoryActivity.kt index d8edbcf503..9a63e81a2f 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/RoomDirectoryActivity.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/RoomDirectoryActivity.kt @@ -25,6 +25,7 @@ import im.vector.app.R import im.vector.app.core.di.ScreenComponent import im.vector.app.core.extensions.addFragment import im.vector.app.core.extensions.addFragmentToBackstack +import im.vector.app.core.extensions.popBackstack import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.databinding.ActivitySimpleBinding import im.vector.app.features.roomdirectory.createroom.CreateRoomFragment @@ -58,7 +59,7 @@ class RoomDirectoryActivity : VectorBaseActivity() { .observe() .subscribe { sharedAction -> when (sharedAction) { - is RoomDirectorySharedAction.Back -> onBackPressed() + is RoomDirectorySharedAction.Back -> popBackstack() is RoomDirectorySharedAction.CreateRoom -> { // Transmit the filter to the CreateRoomFragment withState(roomDirectoryViewModel) { diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewModel.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewModel.kt index f790aa7d67..c9c7365ddc 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewModel.kt @@ -27,9 +27,11 @@ import com.airbnb.mvrx.ViewModelContext import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject +import im.vector.app.R import im.vector.app.core.extensions.exhaustive import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.VectorViewModel +import im.vector.app.core.resources.StringProvider import im.vector.app.features.ui.UiStateRepository import kotlinx.coroutines.launch import org.matrix.android.sdk.api.session.Session @@ -39,6 +41,7 @@ class RoomDirectoryPickerViewModel @AssistedInject constructor( @Assisted initialState: RoomDirectoryPickerViewState, private val session: Session, private val uiStateRepository: UiStateRepository, + private val stringProvider: StringProvider, private val roomDirectoryListCreator: RoomDirectoryListCreator ) : VectorViewModel(initialState) { @@ -141,17 +144,29 @@ class RoomDirectoryPickerViewModel @AssistedInject constructor( } private fun handleSubmit() = withState { state -> + // First avoid duplicate + val enteredServer = state.enteredServer + + val existingServerList = state.directories.map { it.serverName } + + if (enteredServer in existingServerList) { + setState { + copy(addServerAsync = Fail(Throwable(stringProvider.getString(R.string.directory_add_a_new_server_error_already_added)))) + } + return@withState + } + viewModelScope.launch { setState { copy(addServerAsync = Loading()) } try { session.getPublicRooms( - server = state.enteredServer, + server = enteredServer, publicRoomsParams = PublicRoomsParams(limit = 1) ) // Success, let add the server to our local repository, and update the state - val newSet = uiStateRepository.getCustomRoomDirectoryHomeservers(session.sessionId) + state.enteredServer + val newSet = uiStateRepository.getCustomRoomDirectoryHomeservers(session.sessionId) + enteredServer uiStateRepository.setCustomRoomDirectoryHomeservers(session.sessionId, newSet) setState { copy( diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index 7b8a933d30..9250301cfd 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -1591,6 +1591,7 @@ Add a new server Enter the name of a new server you want to explore. "Can't find this server or its room list" + This server is already present in the list Type hereā€¦