Delete temporary file if user cancel room setting change

This commit is contained in:
Benoit Marty 2020-10-28 14:27:09 +01:00 committed by Benoit Marty
parent be2c4597de
commit 4887f678c5
5 changed files with 21 additions and 4 deletions

View File

@ -106,7 +106,6 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr
private fun doReset() {
setState {
// Delete temporary file with the avatar
// TODO BMA Do this also in the other PR
avatarUri?.let { tryOrNull { it.toFile().delete() }}
CreateRoomViewState(

View File

@ -27,4 +27,5 @@ sealed class RoomSettingsAction : VectorViewModelAction {
data class SetRoomCanonicalAlias(val newCanonicalAlias: String) : RoomSettingsAction()
object EnableEncryption : RoomSettingsAction()
object Save : RoomSettingsAction()
object Cancel : RoomSettingsAction()
}

View File

@ -79,7 +79,11 @@ class RoomSettingsFragment @Inject constructor(
viewModel.observeViewEvents {
when (it) {
is RoomSettingsViewEvents.Failure -> showFailure(it.throwable)
is RoomSettingsViewEvents.Success -> showSuccess()
RoomSettingsViewEvents.Success -> showSuccess()
RoomSettingsViewEvents.GoBack -> {
ignoreChanges = true
vectorBaseActivity.onBackPressed()
}
}.exhaustive
}
}
@ -218,8 +222,7 @@ class RoomSettingsFragment @Inject constructor(
.setTitle(R.string.dialog_title_warning)
.setMessage(R.string.warning_unsaved_change)
.setPositiveButton(R.string.warning_unsaved_change_discard) { _, _ ->
ignoreChanges = true
vectorBaseActivity.onBackPressed()
viewModel.handle(RoomSettingsAction.Cancel)
}
.setNegativeButton(R.string.cancel, null)
.show()

View File

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

View File

@ -16,6 +16,7 @@
package im.vector.app.features.roomprofile.settings
import androidx.core.net.toFile
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
@ -27,6 +28,7 @@ import im.vector.app.features.powerlevel.PowerLevelsObservableFactory
import io.reactivex.Completable
import io.reactivex.Observable
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.events.model.EventType
@ -146,9 +148,20 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState:
is RoomSettingsAction.SetRoomHistoryVisibility -> setState { copy(newHistoryVisibility = action.visibility) }
is RoomSettingsAction.SetRoomCanonicalAlias -> setState { copy(newCanonicalAlias = action.newCanonicalAlias) }
is RoomSettingsAction.Save -> saveSettings()
is RoomSettingsAction.Cancel -> cancel()
}.exhaustive
}
private fun cancel() {
// Maybe delete the pending avatar
withState {
(it.avatarAction as? RoomSettingsViewState.AvatarAction.UpdateAvatar)
?.let { tryOrNull { it.newAvatarUri.toFile().delete() } }
}
_viewEvents.post(RoomSettingsViewEvents.GoBack)
}
private fun saveSettings() = withState { state ->
postLoading(true)