Avoid adding twice the same server

This commit is contained in:
Benoit Marty 2021-05-27 15:37:26 +02:00
parent 96720af52f
commit e925d29037
4 changed files with 24 additions and 3 deletions

View File

@ -94,6 +94,10 @@ fun <T : Fragment> AppCompatActivity.addFragmentToBackstack(
}
}
fun AppCompatActivity.popBackstack() {
supportFragmentManager.popBackStack()
}
fun AppCompatActivity.resetBackstack() {
repeat(supportFragmentManager.backStackEntryCount) {
supportFragmentManager.popBackStack()

View File

@ -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<ActivitySimpleBinding>() {
.observe()
.subscribe { sharedAction ->
when (sharedAction) {
is RoomDirectorySharedAction.Back -> onBackPressed()
is RoomDirectorySharedAction.Back -> popBackstack()
is RoomDirectorySharedAction.CreateRoom -> {
// Transmit the filter to the CreateRoomFragment
withState(roomDirectoryViewModel) {

View File

@ -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<RoomDirectoryPickerViewState, RoomDirectoryPickerAction, EmptyViewEvents>(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(

View File

@ -1591,6 +1591,7 @@
<string name="directory_add_a_new_server">Add a new server</string>
<string name="directory_add_a_new_server_prompt">Enter the name of a new server you want to explore.</string>
<string name="directory_add_a_new_server_error">"Can't find this server or its room list"</string>
<string name="directory_add_a_new_server_error_already_added">This server is already present in the list</string>
<!-- Lock screen-->
<string name="lock_screen_hint">Type here…</string>