Show/hide save action button dynamically.

This commit is contained in:
onurays 2020-06-10 10:41:24 +03:00 committed by Benoit Marty
parent 05e848244e
commit bfebaa5c6c
6 changed files with 82 additions and 26 deletions

View file

@ -23,4 +23,5 @@ sealed class RoomSettingsAction : VectorViewModelAction {
data class SetRoomTopic(val newTopic: String) : RoomSettingsAction()
data class SetRoomAvatar(val newAvatarUrl: String) : RoomSettingsAction()
object EnableEncryption : RoomSettingsAction()
object Save : RoomSettingsAction()
}

View file

@ -52,6 +52,28 @@ class RoomSettingsController @Inject constructor(
stringProvider.getString(R.string.settings)
)
formEditTextItem {
id("name")
/*enabled(enableFormElement)*/
value(data.newName ?: roomSummary.displayName)
hint(stringProvider.getString(R.string.room_settings_name_hint))
onTextChange { text ->
callback?.onNameChanged(text)
}
}
formEditTextItem {
id("topic")
/*enabled(enableFormElement)*/
value(data.newTopic ?: roomSummary.topic)
hint(stringProvider.getString(R.string.room_settings_topic_hint))
onTextChange { text ->
callback?.onTopicChanged(text)
}
}
if (roomSummary.isEncrypted) {
buildProfileAction(
id = "encryption",
@ -71,27 +93,5 @@ class RoomSettingsController @Inject constructor(
action = { callback?.onEnableEncryptionClicked() }
)
}
formEditTextItem {
id("name")
/*enabled(enableFormElement)*/
value(roomSummary.displayName)
hint(stringProvider.getString(R.string.room_settings_name_hint))
onTextChange { text ->
callback?.onNameChanged(text)
}
}
formEditTextItem {
id("topic")
/*enabled(enableFormElement)*/
value(roomSummary.topic)
hint(stringProvider.getString(R.string.room_settings_topic_hint))
onTextChange { text ->
callback?.onTopicChanged(text)
}
}
}
}

View file

@ -17,6 +17,8 @@
package im.vector.riotx.features.roomprofile.settings
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
@ -46,6 +48,8 @@ class RoomSettingsFragment @Inject constructor(
override fun getLayoutResId() = R.layout.fragment_room_setting_generic
override fun getMenuRes() = R.menu.vector_room_settings
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
controller.callback = this
@ -66,6 +70,20 @@ class RoomSettingsFragment @Inject constructor(
super.onDestroyView()
}
override fun onPrepareOptionsMenu(menu: Menu) {
withState(viewModel) { state ->
menu.findItem(R.id.roomSettingsSaveAction).isVisible = state.showSaveAction
}
super.onPrepareOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.roomSettingsSaveAction) {
viewModel.handle(RoomSettingsAction.Save)
}
return super.onOptionsItemSelected(item)
}
override fun invalidate() = withState(viewModel) { viewState ->
controller.setData(viewState)
renderRoomSummary(viewState)
@ -97,5 +115,7 @@ class RoomSettingsFragment @Inject constructor(
roomSettingsToolbarTitleView.text = it.displayName
avatarRenderer.render(it.toMatrixItem(), roomSettingsToolbarAvatarImageView)
}
invalidateOptionsMenu()
}
}

View file

@ -55,16 +55,41 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState:
room.rx().liveRoomSummary()
.unwrap()
.execute { async ->
copy(roomSummary = async)
val roomSummary = async.invoke()
copy(
roomSummary = async,
newName = roomSummary?.displayName,
newTopic = roomSummary?.topic
)
}
}
override fun handle(action: RoomSettingsAction) {
when (action) {
is RoomSettingsAction.EnableEncryption -> handleEnableEncryption()
is RoomSettingsAction.SetRoomName -> setState { copy(newName = action.newName) }
is RoomSettingsAction.SetRoomTopic -> setState { copy(newTopic = action.newTopic) }
is RoomSettingsAction.SetRoomName -> setState {
copy(
newName = action.newName,
showSaveAction = shouldShowSaveAction(this)
)
}
is RoomSettingsAction.SetRoomTopic -> setState {
copy(
newTopic = action.newTopic,
showSaveAction = shouldShowSaveAction(this)
)
}
is RoomSettingsAction.Save -> saveSettings()
}
}
private fun shouldShowSaveAction(state: RoomSettingsViewState): Boolean {
val summary = state.roomSummary.invoke()
return summary?.displayName != state.newName ||
summary?.topic != state.newTopic
}
private fun saveSettings() {
}
private fun handleEnableEncryption() {

View file

@ -27,7 +27,8 @@ data class RoomSettingsViewState(
val roomSummary: Async<RoomSummary> = Uninitialized,
val isLoading: Boolean = false,
val newName: String? = null,
val newTopic: String? = null
val newTopic: String? = null,
val showSaveAction: Boolean = false
) : MvRxState {
constructor(args: RoomProfileArgs) : this(roomId = args.roomId)

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/roomSettingsSaveAction"
android:title="@string/save"
app:iconTint="?attr/colorAccent"
app:showAsAction="ifRoom" />
</menu>