From f89a7496aa4acdcc08cfc40ce1a9c6fc6b5f1942 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 28 Apr 2021 14:52:15 +0200 Subject: [PATCH] Add action to create new room --- .../features/navigation/DefaultNavigator.kt | 6 +++- .../createroom/CreateRoomFragment.kt | 3 +- .../createroom/CreateRoomViewModel.kt | 22 +++++++++++--- .../createroom/CreateRoomViewState.kt | 6 ++-- .../spaces/SpaceSettingsMenuBottomSheet.kt | 3 +- .../spaces/manage/SpaceAddRoomFragment.kt | 6 ++-- .../spaces/manage/SpaceManageActivity.kt | 26 +++++++++++++++- .../manage/SpaceManageSharedViewModel.kt | 1 + .../spaces/manage/SpaceManagedSharedAction.kt | 1 + .../manage/SpaceManagedSharedViewEvents.kt | 1 + .../res/layout/fragment_space_add_rooms.xml | 30 +++++++++---------- 11 files changed, 77 insertions(+), 28 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt b/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt index 4ada274d77..735a29afa4 100644 --- a/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt +++ b/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt @@ -125,7 +125,11 @@ class DefaultNavigator @Inject constructor( startActivity(context, SpaceManageActivity.newIntent(context, spaceId), false) } is Navigator.PostSwitchSpaceAction.OpenDefaultRoom -> { - val args = RoomDetailArgs(postSwitchSpaceAction.roomId, eventId = null, openShareSpaceForId = spaceId.takeIf { postSwitchSpaceAction.showShareSheet }) + val args = RoomDetailArgs( + postSwitchSpaceAction.roomId, + eventId = null, + openShareSpaceForId = spaceId.takeIf { postSwitchSpaceAction.showShareSheet } + ) val intent = RoomDetailActivity.newIntent(context, args) startActivity(context, intent, false) } 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 5a9e3a13aa..d7b4cb4dad 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 @@ -47,7 +47,8 @@ import javax.inject.Inject @Parcelize data class CreateRoomArgs( - val initialName: String + val initialName: String, + val parentSpaceId: String? = null ) : Parcelable class CreateRoomFragment @Inject constructor( 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 a86f06b142..952f08c380 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 @@ -42,8 +42,9 @@ import org.matrix.android.sdk.api.session.room.failure.CreateRoomFailure import org.matrix.android.sdk.api.session.room.model.RoomDirectoryVisibility import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams import org.matrix.android.sdk.api.session.room.model.create.CreateRoomPreset +import timber.log.Timber -class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: CreateRoomViewState, +class CreateRoomViewModel @AssistedInject constructor(@Assisted val initialState: CreateRoomViewState, private val session: Session, private val rawService: RawService ) : VectorViewModel(initialState) { @@ -133,7 +134,8 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr CreateRoomViewState( isEncrypted = adminE2EByDefault, - hsAdminHasDisabledE2E = !adminE2EByDefault + hsAdminHasDisabledE2E = !adminE2EByDefault, + parentSpaceId = initialState.parentSpaceId ) } @@ -228,9 +230,21 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr // TODO: Should this be non-cancellable? viewModelScope.launch { - val result = runCatching { session.createRoom(createRoomParams) } - result.fold( + runCatching { session.createRoom(createRoomParams) }.fold( { roomId -> + + if (initialState.parentSpaceId != null) { + // add it as a child + try { + val via = session.sessionParams.homeServerHost?.let { listOf(it) } ?: emptyList() + session.spaceService() + .getSpace(initialState.parentSpaceId) + ?.addChildren(roomId, viaServers = via, order = null) + } catch (failure: Throwable) { + Timber.w(failure, "Failed to add as a child") + } + } + setState { copy(asyncCreateRoomRequest = Success(roomId)) } 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 6bc19dfa20..f060e998ad 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 @@ -32,11 +32,13 @@ data class CreateRoomViewState( val disableFederation: Boolean = false, val homeServerName: String = "", val hsAdminHasDisabledE2E: Boolean = false, - val asyncCreateRoomRequest: Async = Uninitialized + val asyncCreateRoomRequest: Async = Uninitialized, + val parentSpaceId: String? ) : MvRxState { constructor(args: CreateRoomArgs) : this( - roomName = args.initialName + roomName = args.initialName, + parentSpaceId = args.parentSpaceId ) /** diff --git a/vector/src/main/java/im/vector/app/features/spaces/SpaceSettingsMenuBottomSheet.kt b/vector/src/main/java/im/vector/app/features/spaces/SpaceSettingsMenuBottomSheet.kt index 82337a3600..5a1d80c240 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/SpaceSettingsMenuBottomSheet.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/SpaceSettingsMenuBottomSheet.kt @@ -117,7 +117,8 @@ class SpaceSettingsMenuBottomSheet : VectorBaseBottomSheetDialogFragment(), SpaceManageSharedViewModel.Factory { @Inject lateinit var sharedViewModelFactory: SpaceManageSharedViewModel.Factory + private lateinit var sharedDirectoryActionViewModel: RoomDirectorySharedActionViewModel override fun injectWith(injector: ScreenComponent) { injector.inject(this) @@ -53,9 +59,20 @@ class SpaceManageActivity : VectorBaseActivity(), SpaceMa override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + sharedDirectoryActionViewModel = viewModelProvider.get(RoomDirectorySharedActionViewModel::class.java) + sharedDirectoryActionViewModel + .observe() + .subscribe { sharedAction -> + when (sharedAction) { + is RoomDirectorySharedAction.Back, + is RoomDirectorySharedAction.Close -> finish() + } + } + .disposeOnDestroy() + + val args = intent?.getParcelableExtra(MvRx.KEY_ARG) if (isFirstCreation()) { val simpleName = SpaceAddRoomFragment::class.java.simpleName - val args = intent?.getParcelableExtra(MvRx.KEY_ARG) if (supportFragmentManager.findFragmentByTag(simpleName) == null) { supportFragmentManager.commitTransaction { replace(R.id.simpleFragmentContainer, @@ -78,6 +95,13 @@ class SpaceManageActivity : VectorBaseActivity(), SpaceMa SpaceManagedSharedViewEvents.ShowLoading -> { views.simpleActivityWaitingView.isVisible = true } + SpaceManagedSharedViewEvents.NavigateToCreateRoom -> { + addFragmentToBackstack( + R.id.simpleFragmentContainer, + CreateRoomFragment::class.java, + CreateRoomArgs("", parentSpaceId = args?.spaceId) + ) + } } } } diff --git a/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManageSharedViewModel.kt b/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManageSharedViewModel.kt index 1d8612f084..24110c081a 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManageSharedViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManageSharedViewModel.kt @@ -54,6 +54,7 @@ class SpaceManageSharedViewModel @AssistedInject constructor( } SpaceManagedSharedAction.HideLoading -> _viewEvents.post(SpaceManagedSharedViewEvents.HideLoading) SpaceManagedSharedAction.ShowLoading -> _viewEvents.post(SpaceManagedSharedViewEvents.ShowLoading) + SpaceManagedSharedAction.CreateRoom -> _viewEvents.post(SpaceManagedSharedViewEvents.NavigateToCreateRoom) } } } diff --git a/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManagedSharedAction.kt b/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManagedSharedAction.kt index 59e2742379..1c0de1041b 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManagedSharedAction.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManagedSharedAction.kt @@ -22,4 +22,5 @@ sealed class SpaceManagedSharedAction : VectorViewModelAction { object HandleBack : SpaceManagedSharedAction() object ShowLoading : SpaceManagedSharedAction() object HideLoading : SpaceManagedSharedAction() + object CreateRoom : SpaceManagedSharedAction() } diff --git a/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManagedSharedViewEvents.kt b/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManagedSharedViewEvents.kt index 067592b6ce..0cf6bcaee1 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManagedSharedViewEvents.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceManagedSharedViewEvents.kt @@ -22,4 +22,5 @@ sealed class SpaceManagedSharedViewEvents : VectorViewEvents { object Finish : SpaceManagedSharedViewEvents() object ShowLoading : SpaceManagedSharedViewEvents() object HideLoading : SpaceManagedSharedViewEvents() + object NavigateToCreateRoom : SpaceManagedSharedViewEvents() } diff --git a/vector/src/main/res/layout/fragment_space_add_rooms.xml b/vector/src/main/res/layout/fragment_space_add_rooms.xml index 23b2807151..6619d06cca 100644 --- a/vector/src/main/res/layout/fragment_space_add_rooms.xml +++ b/vector/src/main/res/layout/fragment_space_add_rooms.xml @@ -82,21 +82,21 @@ app:layout_constraintTop_toBottomOf="@+id/addRoomToSpaceToolbar" app:queryHint="@string/search_hint_room_name" /> - - - - - - - - - - - - - - - +