Chain all operations to save settings.

This commit is contained in:
onurays 2020-06-10 15:54:10 +03:00 committed by Benoit Marty
parent bfebaa5c6c
commit a6e4a328b3
7 changed files with 68 additions and 11 deletions
matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx
matrix-sdk-android/src/main/java/im/vector/matrix/android
api/session/room/state
internal/session/room/state
vector/src/main
java/im/vector/riotx/features/roomprofile/settings
res/values

View file

@ -101,6 +101,14 @@ class RxRoom(private val room: Room) {
fun invite(userId: String, reason: String? = null): Completable = completableBuilder<Unit> { fun invite(userId: String, reason: String? = null): Completable = completableBuilder<Unit> {
room.invite(userId, reason, it) room.invite(userId, reason, it)
} }
fun updateTopic(topic: String): Completable = completableBuilder<Unit> {
room.updateTopic(topic, it)
}
fun updateName(name: String): Completable = completableBuilder<Unit> {
room.updateName(name, it)
}
} }
fun Room.rx(): RxRoom { fun Room.rx(): RxRoom {

View file

@ -31,6 +31,11 @@ interface StateService {
*/ */
fun updateTopic(topic: String, callback: MatrixCallback<Unit>): Cancelable fun updateTopic(topic: String, callback: MatrixCallback<Unit>): Cancelable
/**
* Update the name of the room
*/
fun updateName(name: String, callback: MatrixCallback<Unit>): Cancelable
fun sendStateEvent(eventType: String, stateKey: String?, body: JsonDict, callback: MatrixCallback<Unit>): Cancelable fun sendStateEvent(eventType: String, stateKey: String?, body: JsonDict, callback: MatrixCallback<Unit>): Cancelable
fun getStateEvent(eventType: String, stateKey: QueryStringValue = QueryStringValue.NoCondition): Event? fun getStateEvent(eventType: String, stateKey: QueryStringValue = QueryStringValue.NoCondition): Event?

View file

@ -84,4 +84,13 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
stateKey = null stateKey = null
) )
} }
override fun updateName(name: String, callback: MatrixCallback<Unit>): Cancelable {
return sendStateEvent(
eventType = EventType.STATE_ROOM_NAME,
body = mapOf("name" to name),
callback = callback,
stateKey = null
)
}
} }

View file

@ -31,6 +31,7 @@ import im.vector.riotx.core.extensions.cleanup
import im.vector.riotx.core.extensions.configureWith import im.vector.riotx.core.extensions.configureWith
import im.vector.riotx.core.extensions.exhaustive import im.vector.riotx.core.extensions.exhaustive
import im.vector.riotx.core.platform.VectorBaseFragment import im.vector.riotx.core.platform.VectorBaseFragment
import im.vector.riotx.core.utils.toast
import im.vector.riotx.features.home.AvatarRenderer import im.vector.riotx.features.home.AvatarRenderer
import im.vector.riotx.features.roomprofile.RoomProfileArgs import im.vector.riotx.features.roomprofile.RoomProfileArgs
import kotlinx.android.synthetic.main.fragment_room_setting_generic.* import kotlinx.android.synthetic.main.fragment_room_setting_generic.*
@ -61,10 +62,15 @@ class RoomSettingsFragment @Inject constructor(
viewModel.observeViewEvents { viewModel.observeViewEvents {
when (it) { when (it) {
is RoomSettingsViewEvents.Failure -> showFailure(it.throwable) is RoomSettingsViewEvents.Failure -> showFailure(it.throwable)
is RoomSettingsViewEvents.Success -> showSuccess()
}.exhaustive }.exhaustive
} }
} }
private fun showSuccess() {
activity?.toast(R.string.room_settings_save_success)
}
override fun onDestroyView() { override fun onDestroyView() {
recyclerView.cleanup() recyclerView.cleanup()
super.onDestroyView() super.onDestroyView()

View file

@ -24,4 +24,5 @@ import im.vector.riotx.core.platform.VectorViewEvents
*/ */
sealed class RoomSettingsViewEvents : VectorViewEvents { sealed class RoomSettingsViewEvents : VectorViewEvents {
data class Failure(val throwable: Throwable) : RoomSettingsViewEvents() data class Failure(val throwable: Throwable) : RoomSettingsViewEvents()
object Success : RoomSettingsViewEvents()
} }

View file

@ -26,6 +26,8 @@ import im.vector.matrix.android.api.session.Session
import im.vector.matrix.rx.rx import im.vector.matrix.rx.rx
import im.vector.matrix.rx.unwrap import im.vector.matrix.rx.unwrap
import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.platform.VectorViewModel
import io.reactivex.Completable
import io.reactivex.Observable
class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState: RoomSettingsViewState, class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState: RoomSettingsViewState,
private val session: Session) private val session: Session)
@ -89,28 +91,53 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState:
summary?.topic != state.newTopic summary?.topic != state.newTopic
} }
private fun saveSettings() { private fun saveSettings() = withState { state ->
postLoading(true)
val operationList = mutableListOf<Completable>()
val summary = state.roomSummary.invoke()
if (summary?.displayName != state.newName) {
operationList.add(room.rx().updateName(state.newName ?: ""))
}
if (summary?.topic != state.newTopic) {
operationList.add(room.rx().updateTopic(state.newTopic ?: ""))
}
Observable
.fromIterable(operationList)
.flatMapCompletable { it }
.subscribe(
{
postLoading(false)
_viewEvents.post(RoomSettingsViewEvents.Success)
},
{
postLoading(false)
_viewEvents.post(RoomSettingsViewEvents.Failure(it))
}
)
} }
private fun handleEnableEncryption() { private fun handleEnableEncryption() {
setState { postLoading(true)
copy(isLoading = true)
}
room.enableEncryption(callback = object : MatrixCallback<Unit> { room.enableEncryption(callback = object : MatrixCallback<Unit> {
override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
setState { postLoading(false)
copy(isLoading = false)
}
_viewEvents.post(RoomSettingsViewEvents.Failure(failure)) _viewEvents.post(RoomSettingsViewEvents.Failure(failure))
} }
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
setState { postLoading(false)
copy(isLoading = false)
}
} }
}) })
} }
private fun postLoading(isLoading: Boolean) {
setState {
copy(isLoading = isLoading)
}
}
} }

View file

@ -2499,5 +2499,6 @@ Not all features in Riot are implemented in RiotX yet. Main missing (and coming
<!-- Room Settings --> <!-- Room Settings -->
<string name="room_settings_name_hint">Room Name</string> <string name="room_settings_name_hint">Room Name</string>
<string name="room_settings_topic_hint">Topic</string> <string name="room_settings_topic_hint">Topic</string>
<string name="room_settings_save_success">You changed room settings successfully</string>
</resources> </resources>