mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Add the initial implementation for managing spaces. (#4963)
This isn't hooked up into the flows yet.
This commit is contained in:
@@ -11,7 +11,8 @@ import Compound
|
||||
import SwiftUI
|
||||
|
||||
struct SpaceRoomCell: View {
|
||||
@Environment(\.dynamicTypeSize) var dynamicTypeSize
|
||||
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
|
||||
@Environment(\.editMode) private var editMode
|
||||
|
||||
let spaceServiceRoom: SpaceServiceRoomProtocol
|
||||
let isSelected: Bool
|
||||
@@ -24,6 +25,9 @@ struct SpaceRoomCell: View {
|
||||
private let verticalInsets = 12.0
|
||||
private let horizontalInsets = 16.0
|
||||
|
||||
private var isEditModeActive: Bool { editMode?.wrappedValue ?? .inactive != .inactive }
|
||||
private var isHighlighted: Bool { isSelected && !isEditModeActive }
|
||||
|
||||
private var subtitle: String {
|
||||
if spaceServiceRoom.isSpace {
|
||||
switch spaceServiceRoom.visibility {
|
||||
@@ -58,22 +62,34 @@ struct SpaceRoomCell: View {
|
||||
Button {
|
||||
action(.select(spaceServiceRoom))
|
||||
} label: {
|
||||
HStack(spacing: 16.0) {
|
||||
avatar
|
||||
|
||||
content
|
||||
.padding(.vertical, verticalInsets)
|
||||
.overlay(alignment: .bottom) {
|
||||
Rectangle()
|
||||
.fill(Color.compound.borderDisabled)
|
||||
.frame(height: 1 / UIScreen.main.scale)
|
||||
.padding(.trailing, -horizontalInsets)
|
||||
HStack(spacing: 0) {
|
||||
if isEditModeActive {
|
||||
ZStack {
|
||||
ListRowAccessory.multiSelection(isSelected)
|
||||
}
|
||||
// Use padding rather than spacing to improve the animation.
|
||||
.padding(.trailing, 16)
|
||||
// Put the transition on a ZStack to prevent it from being applied during selection/deselection.
|
||||
.transition(.move(edge: .leading).combined(with: .opacity))
|
||||
}
|
||||
|
||||
HStack(spacing: 16) {
|
||||
avatar
|
||||
|
||||
content
|
||||
.padding(.vertical, verticalInsets)
|
||||
.overlay(alignment: .bottom) {
|
||||
Rectangle()
|
||||
.fill(Color.compound.borderDisabled)
|
||||
.frame(height: 1 / UIScreen.main.scale)
|
||||
.padding(.trailing, -horizontalInsets)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, horizontalInsets)
|
||||
.accessibilityElement(children: .combine)
|
||||
}
|
||||
.buttonStyle(SpaceRoomCellButtonStyle(isSelected: isSelected))
|
||||
.buttonStyle(SpaceRoomCellButtonStyle(isHighlighted: isHighlighted))
|
||||
.accessibilityIdentifier(A11yIdentifiers.spacesScreen.spaceRoomName(spaceServiceRoom.name))
|
||||
}
|
||||
|
||||
@@ -146,13 +162,13 @@ struct SpaceRoomCell: View {
|
||||
}
|
||||
|
||||
struct SpaceRoomCellButtonStyle: ButtonStyle {
|
||||
let isSelected: Bool
|
||||
let isHighlighted: Bool
|
||||
|
||||
func makeBody(configuration: Configuration) -> some View {
|
||||
configuration.label
|
||||
.background(isSelected || configuration.isPressed ? Color.compound.bgSubtleSecondary : Color.compound.bgCanvasDefault)
|
||||
.background(isHighlighted || configuration.isPressed ? Color.compound.bgSubtleSecondary : Color.compound.bgCanvasDefault)
|
||||
.contentShape(Rectangle())
|
||||
.animation(isSelected ? .none : .easeOut(duration: 0.1).disabledDuringTests(), value: isSelected)
|
||||
.animation(isHighlighted ? .none : .easeOut(duration: 0.1).disabledDuringTests(), value: isHighlighted)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,21 +178,35 @@ struct SpaceRoomCell_Previews: PreviewProvider, TestablePreview {
|
||||
static let spaces = [SpaceServiceRoomProtocol].mockSpaceList
|
||||
|
||||
static var previews: some View {
|
||||
VStack(spacing: 0) {
|
||||
ForEach(spaces, id: \.id) { space in
|
||||
SpaceRoomCell(spaceServiceRoom: space,
|
||||
ScrollView {
|
||||
VStack(spacing: 0) {
|
||||
ForEach(spaces, id: \.id) { space in
|
||||
SpaceRoomCell(spaceServiceRoom: space,
|
||||
isSelected: false,
|
||||
mediaProvider: mediaProvider) { _ in }
|
||||
}
|
||||
|
||||
SpaceRoomCell(spaceServiceRoom: SpaceServiceRoomMock(.init(id: "Space being joined", isSpace: true)),
|
||||
isSelected: false,
|
||||
isJoining: true,
|
||||
mediaProvider: mediaProvider) { _ in }
|
||||
SpaceRoomCell(spaceServiceRoom: SpaceServiceRoomMock(.init(id: "Room being joined", isSpace: false)),
|
||||
isSelected: false,
|
||||
isJoining: true,
|
||||
mediaProvider: mediaProvider) { _ in }
|
||||
|
||||
SpaceRoomCell(spaceServiceRoom: SpaceServiceRoomMock(.init(id: "Selected", isSpace: false, state: .joined)),
|
||||
isSelected: true,
|
||||
isJoining: false,
|
||||
mediaProvider: mediaProvider) { _ in }
|
||||
.environment(\.editMode, .constant(.active))
|
||||
SpaceRoomCell(spaceServiceRoom: SpaceServiceRoomMock(.init(id: "Unselected", isSpace: false, state: .joined)),
|
||||
isSelected: false,
|
||||
isJoining: false,
|
||||
mediaProvider: mediaProvider) { _ in }
|
||||
.environment(\.editMode, .constant(.active))
|
||||
}
|
||||
|
||||
SpaceRoomCell(spaceServiceRoom: SpaceServiceRoomMock(.init(id: "Space being joined", isSpace: true)),
|
||||
isSelected: false,
|
||||
isJoining: true,
|
||||
mediaProvider: mediaProvider) { _ in }
|
||||
SpaceRoomCell(spaceServiceRoom: SpaceServiceRoomMock(.init(id: "Room being joined", isSpace: false)),
|
||||
isSelected: false,
|
||||
isJoining: true,
|
||||
mediaProvider: mediaProvider) { _ in }
|
||||
}
|
||||
.previewLayout(.fixed(width: 390, height: 850))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ struct LeaveSpaceRoomDetailsCell: View {
|
||||
}
|
||||
.padding(.horizontal, 16)
|
||||
}
|
||||
.buttonStyle(SpaceRoomCellButtonStyle(isSelected: false))
|
||||
.buttonStyle(SpaceRoomCellButtonStyle(isHighlighted: false))
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// Please see LICENSE files in the repository root for full details.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
enum SpaceScreenViewModelAction {
|
||||
case selectSpace(SpaceRoomListProxyProtocol)
|
||||
@@ -33,14 +33,30 @@ struct SpaceScreenViewState: BindableState {
|
||||
var canEditRolesAndPermissions = false
|
||||
var canEditSecurityAndPrivacy = false
|
||||
|
||||
var editMode: EditMode = .inactive
|
||||
var editModeSelectedIDs: Set<String> = []
|
||||
|
||||
var bindings = SpaceScreenViewStateBindings()
|
||||
|
||||
var visibleRooms: [SpaceServiceRoomProtocol] {
|
||||
if editMode == .inactive {
|
||||
rooms
|
||||
} else {
|
||||
rooms.filter { !$0.isSpace }
|
||||
}
|
||||
}
|
||||
|
||||
var isSpaceManagementEnabled: Bool {
|
||||
canEditBaseInfo || canEditRolesAndPermissions || canEditSecurityAndPrivacy
|
||||
}
|
||||
|
||||
var bindings = SpaceScreenViewStateBindings()
|
||||
func isSpaceIDSelected(_ spaceID: String) -> Bool {
|
||||
selectedSpaceRoomID == spaceID || editModeSelectedIDs.contains(spaceID)
|
||||
}
|
||||
}
|
||||
|
||||
struct SpaceScreenViewStateBindings {
|
||||
var isPresentingRemoveChildrenConfirmation = false
|
||||
var leaveSpaceViewModel: LeaveSpaceViewModel?
|
||||
}
|
||||
|
||||
@@ -49,4 +65,8 @@ enum SpaceScreenViewAction {
|
||||
case leaveSpace
|
||||
case spaceSettings(roomProxy: JoinedRoomProxyProtocol)
|
||||
case displayMembers(roomProxy: JoinedRoomProxyProtocol)
|
||||
case manageChildren
|
||||
case removeSelectedChildren
|
||||
case confirmRemoveSelectedChildren
|
||||
case finishManagingChildren
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ class SpaceScreenViewModel: SpaceScreenViewModelType, SpaceScreenViewModelProtoc
|
||||
MXLog.info("View model: received view action: \(viewAction)")
|
||||
|
||||
switch viewAction {
|
||||
case .spaceAction(.select(let spaceServiceRoom)):
|
||||
case .spaceAction(.select(let spaceServiceRoom)) where state.editMode == .inactive:
|
||||
if spaceServiceRoom.isSpace {
|
||||
if spaceServiceRoom.state != .joined {
|
||||
actionsSubject.send(.selectUnjoinedSpace(spaceServiceRoom))
|
||||
@@ -118,6 +118,14 @@ class SpaceScreenViewModel: SpaceScreenViewModelType, SpaceScreenViewModelProtoc
|
||||
// No need to check the join state, the room flow will show an appropriately configured join screen if needed.
|
||||
actionsSubject.send(.selectRoom(roomID: spaceServiceRoom.id))
|
||||
}
|
||||
case .spaceAction(.select(let spaceServiceRoom)): // isEditModeActive == true
|
||||
withTransaction(\.disablesAnimations, true) { // The button adds an unwanted animation.
|
||||
if state.editModeSelectedIDs.contains(spaceServiceRoom.id) {
|
||||
state.editModeSelectedIDs.remove(spaceServiceRoom.id)
|
||||
} else {
|
||||
state.editModeSelectedIDs.insert(spaceServiceRoom.id)
|
||||
}
|
||||
}
|
||||
case .spaceAction(.join(let spaceServiceRoom)):
|
||||
Task { await join(spaceServiceRoom) }
|
||||
case .leaveSpace:
|
||||
@@ -126,6 +134,20 @@ class SpaceScreenViewModel: SpaceScreenViewModelType, SpaceScreenViewModelProtoc
|
||||
actionsSubject.send(.displayMembers(roomProxy: roomProxy))
|
||||
case .spaceSettings(let roomProxy):
|
||||
actionsSubject.send(.displaySpaceSettings(roomProxy: roomProxy))
|
||||
case .manageChildren:
|
||||
withAnimation(.easeOut(duration: 0.25).disabledDuringTests()) {
|
||||
state.editMode = .transient
|
||||
}
|
||||
case .removeSelectedChildren:
|
||||
state.bindings.isPresentingRemoveChildrenConfirmation = true
|
||||
case .confirmRemoveSelectedChildren:
|
||||
Task { await removeSelectedChildren() }
|
||||
case .finishManagingChildren:
|
||||
withAnimation(.easeOut(duration: 0.25).disabledDuringTests()) {
|
||||
state.editMode = .inactive
|
||||
} completion: {
|
||||
self.state.editModeSelectedIDs.removeAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,6 +180,25 @@ class SpaceScreenViewModel: SpaceScreenViewModelType, SpaceScreenViewModelProtoc
|
||||
}
|
||||
}
|
||||
|
||||
private func removeSelectedChildren() async {
|
||||
showRemovingIndicator()
|
||||
defer { hideRemovingIndicator() }
|
||||
|
||||
state.bindings.isPresentingRemoveChildrenConfirmation = false
|
||||
|
||||
for childID in state.editModeSelectedIDs {
|
||||
switch await spaceServiceProxy.removeChild(childID, from: spaceRoomListProxy.id) {
|
||||
case .success:
|
||||
MXLog.info("Successfully removed \(childID) from \(spaceRoomListProxy.id)")
|
||||
case .failure:
|
||||
showFailureIndicator()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
process(viewAction: .finishManagingChildren)
|
||||
}
|
||||
|
||||
private func showLeaveSpaceConfirmation() async {
|
||||
guard case let .success(leaveHandle) = await spaceServiceProxy.leaveSpace(spaceID: spaceRoomListProxy.id) else {
|
||||
showFailureIndicator()
|
||||
@@ -192,9 +233,20 @@ class SpaceScreenViewModel: SpaceScreenViewModelType, SpaceScreenViewModelProtoc
|
||||
|
||||
// MARK: - Indicators
|
||||
|
||||
private static var leavingIndicatorID: String { "\(Self.self)-Leaving" }
|
||||
private static var removingIndicatorID: String { "\(Self.self)-Removing" }
|
||||
private static var failureIndicatorID: String { "\(Self.self)-Failure" }
|
||||
|
||||
private func showRemovingIndicator() {
|
||||
userIndicatorController.submitIndicator(UserIndicator(id: Self.removingIndicatorID,
|
||||
type: .modal(progress: .indeterminate, interactiveDismissDisabled: true, allowsInteraction: false),
|
||||
title: L10n.commonRemoving,
|
||||
persistent: true))
|
||||
}
|
||||
|
||||
private func hideRemovingIndicator() {
|
||||
userIndicatorController.retractIndicatorWithId(Self.removingIndicatorID)
|
||||
}
|
||||
|
||||
private func showFailureIndicator() {
|
||||
userIndicatorController.submitIndicator(UserIndicator(id: Self.failureIndicatorID,
|
||||
type: .toast,
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// Copyright 2025 Element Creations Ltd.
|
||||
// Copyright 2025 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
// Please see LICENSE files in the repository root for full details.
|
||||
//
|
||||
|
||||
import Compound
|
||||
import SwiftUI
|
||||
|
||||
struct SpaceRemoveChildrenConfirmationView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
let spaceName: String
|
||||
let action: () -> Void
|
||||
|
||||
@State private var scrollViewHeight: CGFloat = .zero
|
||||
@State private var buttonsHeight: CGFloat = .zero
|
||||
private let topPadding = 19.0
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
TitleAndIcon(title: L10n.screenSpaceRemoveRoomsConfirmationTitleIos(spaceName),
|
||||
subtitle: L10n.screenSpaceRemoveRoomsConfirmationContent,
|
||||
icon: \.errorSolid,
|
||||
iconStyle: .alertSolid)
|
||||
.padding(24)
|
||||
.readHeight($scrollViewHeight)
|
||||
}
|
||||
.backportSafeAreaBar(edge: .bottom, spacing: 0) {
|
||||
buttons
|
||||
.readHeight($buttonsHeight)
|
||||
}
|
||||
.scrollBounceBehavior(.basedOnSize)
|
||||
.padding(.top, topPadding) // For the drag indicator
|
||||
.presentationDetents([.height(scrollViewHeight + buttonsHeight + topPadding)])
|
||||
.presentationDragIndicator(.visible)
|
||||
.presentationBackground(.compound.bgCanvasDefault)
|
||||
}
|
||||
|
||||
var buttons: some View {
|
||||
VStack(spacing: 16) {
|
||||
Button(L10n.actionRemove, role: .destructive, action: action)
|
||||
.buttonStyle(.compound(.primary))
|
||||
|
||||
Button(L10n.actionCancel, action: dismiss.callAsFunction)
|
||||
.buttonStyle(.compound(.tertiary))
|
||||
}
|
||||
.padding(.horizontal, 16)
|
||||
.padding(.top, 16)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
struct SpaceRemoveChildrenConfirmationView_Previews: PreviewProvider, TestablePreview {
|
||||
static var previews: some View {
|
||||
SpaceRemoveChildrenConfirmationView(spaceName: "Company") { }
|
||||
}
|
||||
}
|
||||
@@ -12,19 +12,37 @@ import SwiftUI
|
||||
struct SpaceScreen: View {
|
||||
@Bindable var context: SpaceScreenViewModel.Context
|
||||
|
||||
private var isEditModeActive: Bool { context.viewState.editMode != .inactive }
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
LazyVStack(spacing: 0) {
|
||||
SpaceHeaderView(spaceServiceRoom: context.viewState.space,
|
||||
mediaProvider: context.mediaProvider)
|
||||
if !isEditModeActive {
|
||||
SpaceHeaderView(spaceServiceRoom: context.viewState.space,
|
||||
mediaProvider: context.mediaProvider)
|
||||
}
|
||||
|
||||
rooms
|
||||
}
|
||||
}
|
||||
.environment(\.editMode, .constant(context.viewState.editMode))
|
||||
.background(Color.compound.bgCanvasDefault.ignoresSafeArea())
|
||||
.toolbarRole(RoomHeaderView.toolbarRole)
|
||||
.toolbarRole(isEditModeActive ? .automatic : RoomHeaderView.toolbarRole)
|
||||
.navigationTitle(context.viewState.space.name)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar { toolbar }
|
||||
.navigationBarBackButtonHidden(isEditModeActive)
|
||||
.toolbar {
|
||||
if isEditModeActive {
|
||||
editModeToolbar
|
||||
} else {
|
||||
toolbar
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $context.isPresentingRemoveChildrenConfirmation) {
|
||||
SpaceRemoveChildrenConfirmationView(spaceName: context.viewState.space.name) {
|
||||
context.send(viewAction: .confirmRemoveSelectedChildren)
|
||||
}
|
||||
}
|
||||
.sheet(item: $context.leaveSpaceViewModel) { leaveSpaceViewModel in
|
||||
LeaveSpaceView(context: leaveSpaceViewModel.context)
|
||||
}
|
||||
@@ -32,9 +50,9 @@ struct SpaceScreen: View {
|
||||
|
||||
@ViewBuilder
|
||||
var rooms: some View {
|
||||
ForEach(context.viewState.rooms, id: \.id) { spaceServiceRoom in
|
||||
ForEach(context.viewState.visibleRooms, id: \.id) { spaceServiceRoom in
|
||||
SpaceRoomCell(spaceServiceRoom: spaceServiceRoom,
|
||||
isSelected: spaceServiceRoom.id == context.viewState.selectedSpaceRoomID,
|
||||
isSelected: context.viewState.isSpaceIDSelected(spaceServiceRoom.id),
|
||||
isJoining: context.viewState.joiningRoomIDs.contains(spaceServiceRoom.id),
|
||||
mediaProvider: context.mediaProvider) { action in
|
||||
context.send(viewAction: .spaceAction(action))
|
||||
@@ -100,20 +118,46 @@ struct SpaceScreen: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ToolbarContentBuilder
|
||||
var editModeToolbar: some ToolbarContent {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button(L10n.actionCancel, role: .cancel) {
|
||||
context.send(viewAction: .finishManagingChildren)
|
||||
}
|
||||
}
|
||||
|
||||
ToolbarItem(placement: .principal) {
|
||||
Text(L10n.commonSelectedCount(context.viewState.editModeSelectedIDs.count))
|
||||
}
|
||||
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
ToolbarButton(role: .destructive(title: L10n.actionRemove)) {
|
||||
context.send(viewAction: .removeSelectedChildren)
|
||||
}
|
||||
.disabled(context.viewState.editModeSelectedIDs.isEmpty)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
struct SpaceScreen_Previews: PreviewProvider, TestablePreview {
|
||||
static let viewModel = makeViewModel()
|
||||
static let managingViewModel = makeViewModel(isManagingRooms: true)
|
||||
|
||||
static var previews: some View {
|
||||
NavigationStack {
|
||||
SpaceScreen(context: viewModel.context)
|
||||
}
|
||||
|
||||
NavigationStack {
|
||||
SpaceScreen(context: managingViewModel.context)
|
||||
}
|
||||
.previewDisplayName("Managing")
|
||||
}
|
||||
|
||||
static func makeViewModel() -> SpaceScreenViewModel {
|
||||
static func makeViewModel(isManagingRooms: Bool = false) -> SpaceScreenViewModel {
|
||||
let spaceServiceRoom = SpaceServiceRoomMock(.init(id: "!eng-space:matrix.org",
|
||||
name: "Engineering Team",
|
||||
isSpace: true,
|
||||
@@ -138,6 +182,12 @@ struct SpaceScreen_Previews: PreviewProvider, TestablePreview {
|
||||
userSession: userSession,
|
||||
appSettings: AppSettings(),
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
|
||||
if isManagingRooms {
|
||||
viewModel.state.editMode = .transient
|
||||
viewModel.state.editModeSelectedIDs = [viewModel.state.visibleRooms[0].id]
|
||||
}
|
||||
|
||||
return viewModel
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user