diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt index 2a91378cc1..a6ddddd495 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt @@ -19,6 +19,7 @@ package im.vector.app.features.roomdirectory.createroom import android.net.Uri import android.os.Bundle import android.view.View +import androidx.appcompat.app.AlertDialog import com.airbnb.mvrx.Success import com.airbnb.mvrx.activityViewModel import com.airbnb.mvrx.withState @@ -26,18 +27,20 @@ import im.vector.app.R import im.vector.app.core.dialogs.GalleryOrCameraDialogHelper import im.vector.app.core.extensions.cleanup import im.vector.app.core.extensions.configureWith +import im.vector.app.core.extensions.exhaustive import im.vector.app.core.platform.OnBackPressed import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.resources.ColorProvider import im.vector.app.features.roomdirectory.RoomDirectorySharedAction import im.vector.app.features.roomdirectory.RoomDirectorySharedActionViewModel +import im.vector.app.features.roomprofile.settings.RoomSettingsAction import kotlinx.android.synthetic.main.fragment_create_room.* import timber.log.Timber import javax.inject.Inject class CreateRoomFragment @Inject constructor( private val createRoomController: CreateRoomController, - private val colorProvider: ColorProvider + colorProvider: ColorProvider ) : VectorBaseFragment(), CreateRoomController.Listener, GalleryOrCameraDialogHelper.Listener, @@ -58,6 +61,11 @@ class CreateRoomFragment @Inject constructor( createRoomClose.debouncedClicks { sharedActionViewModel.post(RoomDirectorySharedAction.Back) } + viewModel.observeViewEvents { + when (it) { + CreateRoomViewEvents.Quit -> vectorBaseActivity.onBackPressed() + }.exhaustive + } } override fun onDestroyView() { @@ -113,9 +121,21 @@ class CreateRoomFragment @Inject constructor( } override fun onBackPressed(toolbarButton: Boolean): Boolean { - // TODO BMA, as per the other PR, ask the user if he has started to input elements before leaving the screen - viewModel.handle(CreateRoomAction.Reset) - return false + return withState(viewModel) { + return@withState if (!it.isEmpty()) { + AlertDialog.Builder(requireContext()) + .setTitle(R.string.dialog_title_warning) + .setMessage(R.string.warning_room_not_created_yet) + .setPositiveButton(R.string.yes) { _, _ -> + viewModel.handle(CreateRoomAction.Reset) + } + .setNegativeButton(R.string.no, null) + .show() + true + } else { + false + } + } } override fun invalidate() = withState(viewModel) { state -> diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewEvents.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewEvents.kt new file mode 100644 index 0000000000..4ff4ee4bdf --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewEvents.kt @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.roomdirectory.createroom + +import im.vector.app.core.platform.VectorViewEvents + +/** + * Transient events for room creation screen + */ +sealed class CreateRoomViewEvents : VectorViewEvents { + object Quit : CreateRoomViewEvents() +} diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt index 36ea0f402a..8e4650b467 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt @@ -28,7 +28,6 @@ import com.airbnb.mvrx.ViewModelContext import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.AssistedInject import im.vector.app.core.extensions.exhaustive -import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.raw.wellknown.getElementWellknown import im.vector.app.features.raw.wellknown.isE2EByDefault @@ -46,7 +45,7 @@ import org.matrix.android.sdk.api.session.room.model.create.CreateRoomPreset class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: CreateRoomViewState, private val session: Session, private val rawService: RawService -) : VectorViewModel(initialState) { +) : VectorViewModel(initialState) { @AssistedInject.Factory interface Factory { @@ -113,6 +112,8 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr hsAdminHasDisabledE2E = !adminE2EByDefault ) } + + _viewEvents.post(CreateRoomViewEvents.Quit) } private fun setAvatar(action: CreateRoomAction.SetAvatar) = setState { copy(avatarUri = action.imageUri) } diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt index 6fa8a104c3..433cc02cc9 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt @@ -30,4 +30,10 @@ data class CreateRoomViewState( val isEncrypted: Boolean = false, val hsAdminHasDisabledE2E: Boolean = false, val asyncCreateRoomRequest: Async = Uninitialized -) : MvRxState +) : MvRxState { + + /** + * Return true if there is not important input from user + */ + fun isEmpty() = avatarUri == null && roomName.isEmpty() && roomTopic.isEmpty() +} diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index 93220808f1..dd461123cc 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -2632,6 +2632,7 @@ The link was malformed + The room is not yet created. Cancel the room creation? There are unsaved changes. Discard the changes? Discard changes