diff --git a/vector/src/main/java/im/vector/riotx/features/discovery/DiscoverySettingsFragment.kt b/vector/src/main/java/im/vector/riotx/features/discovery/DiscoverySettingsFragment.kt index 4878be65bc..8c94d2d9ac 100644 --- a/vector/src/main/java/im/vector/riotx/features/discovery/DiscoverySettingsFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/discovery/DiscoverySettingsFragment.kt @@ -20,7 +20,6 @@ import android.content.Intent import android.os.Bundle import android.view.View import androidx.appcompat.app.AlertDialog -import androidx.lifecycle.Observer import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState import im.vector.matrix.android.api.session.identity.ThreePid @@ -29,6 +28,7 @@ import im.vector.riotx.R import im.vector.riotx.core.extensions.cleanup import im.vector.riotx.core.extensions.configureWith import im.vector.riotx.core.extensions.exhaustive +import im.vector.riotx.core.extensions.observeEvent import im.vector.riotx.core.platform.VectorBaseActivity import im.vector.riotx.core.platform.VectorBaseFragment import im.vector.riotx.features.discovery.change.SetIdentityServerFragment @@ -56,11 +56,12 @@ class DiscoverySettingsFragment @Inject constructor( controller.listener = this recyclerView.configureWith(controller) - sharedViewModel.navigateEvent.observe(viewLifecycleOwner, Observer { - if (it.peekContent().first == DiscoverySharedViewModel.NEW_IDENTITY_SERVER_SET_REQUEST) { - viewModel.handle(DiscoverySettingsAction.ChangeIdentityServer(it.peekContent().second)) - } - }) + sharedViewModel.navigateEvent.observeEvent(this) { + when (it) { + is DiscoverySharedViewModelAction.ChangeIdentityServer -> + viewModel.handle(DiscoverySettingsAction.ChangeIdentityServer(it.newUrl)) + }.exhaustive + } viewModel.observeViewEvents { when (it) { diff --git a/vector/src/main/java/im/vector/riotx/features/discovery/DiscoverySharedViewModel.kt b/vector/src/main/java/im/vector/riotx/features/discovery/DiscoverySharedViewModel.kt index 91e43187c2..e6ebed577b 100644 --- a/vector/src/main/java/im/vector/riotx/features/discovery/DiscoverySharedViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/discovery/DiscoverySharedViewModel.kt @@ -17,19 +17,18 @@ package im.vector.riotx.features.discovery import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import im.vector.riotx.core.extensions.postLiveEvent import im.vector.riotx.core.utils.LiveEvent import javax.inject.Inject -// TODO Rework this +sealed class DiscoverySharedViewModelAction { + data class ChangeIdentityServer(val newUrl: String) : DiscoverySharedViewModelAction() +} + class DiscoverySharedViewModel @Inject constructor() : ViewModel() { + var navigateEvent = MutableLiveData>() - var navigateEvent = MutableLiveData>>() - - companion object { - const val NEW_IDENTITY_SERVER_SET_REQUEST = "NEW_IDENTITY_SERVER_SET_REQUEST" - } - - fun requestChangeToIdentityServer(server: String) { - navigateEvent.postValue(LiveEvent(NEW_IDENTITY_SERVER_SET_REQUEST to server)) + fun requestChangeToIdentityServer(serverUrl: String) { + navigateEvent.postLiveEvent(DiscoverySharedViewModelAction.ChangeIdentityServer(serverUrl)) } }