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

View File

@ -101,6 +101,14 @@ class RxRoom(private val room: Room) {
fun invite(userId: String, reason: String? = null): Completable = completableBuilder<Unit> {
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 {

View File

@ -31,6 +31,11 @@ interface StateService {
*/
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 getStateEvent(eventType: String, stateKey: QueryStringValue = QueryStringValue.NoCondition): Event?

View File

@ -84,4 +84,13 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
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.exhaustive
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.roomprofile.RoomProfileArgs
import kotlinx.android.synthetic.main.fragment_room_setting_generic.*
@ -61,10 +62,15 @@ class RoomSettingsFragment @Inject constructor(
viewModel.observeViewEvents {
when (it) {
is RoomSettingsViewEvents.Failure -> showFailure(it.throwable)
is RoomSettingsViewEvents.Success -> showSuccess()
}.exhaustive
}
}
private fun showSuccess() {
activity?.toast(R.string.room_settings_save_success)
}
override fun onDestroyView() {
recyclerView.cleanup()
super.onDestroyView()

View File

@ -24,4 +24,5 @@ import im.vector.riotx.core.platform.VectorViewEvents
*/
sealed class RoomSettingsViewEvents : VectorViewEvents {
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.unwrap
import im.vector.riotx.core.platform.VectorViewModel
import io.reactivex.Completable
import io.reactivex.Observable
class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState: RoomSettingsViewState,
private val session: Session)
@ -89,28 +91,53 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState:
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() {
setState {
copy(isLoading = true)
}
postLoading(true)
room.enableEncryption(callback = object : MatrixCallback<Unit> {
override fun onFailure(failure: Throwable) {
setState {
copy(isLoading = false)
}
postLoading(false)
_viewEvents.post(RoomSettingsViewEvents.Failure(failure))
}
override fun onSuccess(data: Unit) {
setState {
copy(isLoading = false)
}
postLoading(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 -->
<string name="room_settings_name_hint">Room Name</string>
<string name="room_settings_topic_hint">Topic</string>
<string name="room_settings_save_success">You changed room settings successfully</string>
</resources>