mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Space Settings: Leave Room (#4700)
* Implementation for all navigations inside the space settings aside the left space action # Conflicts: # ElementX/Sources/FlowCoordinators/SpaceSettingsFlowCoordinator.swift * refactored the leave space view to use its own view model # Conflicts: # ElementX.xcodeproj/project.pbxproj * implemented the leave space view model also in the settings screen, and corrected some tests * reusing the details coordinator for the space settings screen * leave space from settings implemented * fix project * minor pr fixes * code improvements
This commit is contained in:
@@ -1,134 +0,0 @@
|
||||
//
|
||||
// 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 LeaveSpaceRoomDetailsCell: View {
|
||||
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
|
||||
|
||||
let room: LeaveSpaceRoomDetails
|
||||
var hideSelection = false
|
||||
let mediaProvider: MediaProviderProtocol?
|
||||
|
||||
let action: () -> Void
|
||||
|
||||
private var subtitle: String? {
|
||||
guard !room.spaceRoomProxy.isSpace else { return nil }
|
||||
let memberCount = L10n.commonMemberCount(room.spaceRoomProxy.joinedMembersCount)
|
||||
return room.isLastAdmin ? L10n.screenLeaveSpaceLastAdminInfo(memberCount) : memberCount
|
||||
}
|
||||
|
||||
var visibilityIcon: KeyPath<CompoundIcons, Image>? {
|
||||
switch room.spaceRoomProxy.visibility {
|
||||
case .public: \.public
|
||||
case .private: \.lockSolid
|
||||
case .restricted: nil
|
||||
case .none: \.lockSolid
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Button(action: action) {
|
||||
HStack(spacing: 16) {
|
||||
if dynamicTypeSize < .accessibility3 {
|
||||
RoomAvatarImage(avatar: room.spaceRoomProxy.avatar,
|
||||
avatarSize: .room(on: .leaveSpace),
|
||||
mediaProvider: mediaProvider)
|
||||
}
|
||||
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Text(room.spaceRoomProxy.name)
|
||||
.font(.compound.bodyLGSemibold)
|
||||
.foregroundStyle(.compound.textPrimary)
|
||||
.lineLimit(1)
|
||||
.padding(.vertical, 1)
|
||||
.padding(.vertical, subtitle == nil ? 10 : 0)
|
||||
|
||||
subtitleLabel
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.padding(.vertical, 8)
|
||||
|
||||
if !hideSelection {
|
||||
ListRowAccessory.multiSelection(room.isSelected)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 16)
|
||||
}
|
||||
.buttonStyle(SpaceRoomCellButtonStyle(isSelected: false))
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var subtitleLabel: some View {
|
||||
if let subtitle {
|
||||
Label {
|
||||
Text(subtitle)
|
||||
.font(.compound.bodyMD)
|
||||
.foregroundStyle(.compound.textSecondary)
|
||||
.lineLimit(1)
|
||||
.padding(.vertical, 1)
|
||||
} icon: {
|
||||
if let visibilityIcon {
|
||||
CompoundIcon(visibilityIcon,
|
||||
size: .xSmall,
|
||||
relativeTo: .compound.bodyMD)
|
||||
.foregroundStyle(.compound.iconTertiary)
|
||||
}
|
||||
}
|
||||
.labelStyle(.custom(spacing: 4))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
struct LeaveSpaceRoomDetailsCell_Previews: PreviewProvider, TestablePreview {
|
||||
static var previews: some View {
|
||||
VStack(spacing: 0) {
|
||||
LeaveSpaceRoomDetailsCell(room: .init(spaceRoomProxy: SpaceRoomProxyMock(.init(id: "1",
|
||||
name: "Space",
|
||||
isSpace: true)),
|
||||
isLastAdmin: false,
|
||||
isSelected: true),
|
||||
mediaProvider: MediaProviderMock(configuration: .init())) { }
|
||||
LeaveSpaceRoomDetailsCell(room: .init(spaceRoomProxy: SpaceRoomProxyMock(.init(id: "2",
|
||||
name: "My Space",
|
||||
isSpace: true)),
|
||||
isLastAdmin: true,
|
||||
isSelected: false),
|
||||
mediaProvider: MediaProviderMock(configuration: .init())) { }
|
||||
LeaveSpaceRoomDetailsCell(room: .init(spaceRoomProxy: SpaceRoomProxyMock(.init(id: "2",
|
||||
name: "My Space",
|
||||
isSpace: true)),
|
||||
isLastAdmin: true,
|
||||
isSelected: false),
|
||||
hideSelection: true,
|
||||
mediaProvider: MediaProviderMock(configuration: .init())) { }
|
||||
LeaveSpaceRoomDetailsCell(room: .init(spaceRoomProxy: SpaceRoomProxyMock(.init(id: "3",
|
||||
name: "Room",
|
||||
isSpace: false)),
|
||||
isLastAdmin: false,
|
||||
isSelected: true),
|
||||
mediaProvider: MediaProviderMock(configuration: .init())) { }
|
||||
LeaveSpaceRoomDetailsCell(room: .init(spaceRoomProxy: SpaceRoomProxyMock(.init(id: "4",
|
||||
name: "My Room",
|
||||
isSpace: false)),
|
||||
isLastAdmin: true,
|
||||
isSelected: false),
|
||||
mediaProvider: MediaProviderMock(configuration: .init())) { }
|
||||
LeaveSpaceRoomDetailsCell(room: .init(spaceRoomProxy: SpaceRoomProxyMock(.init(id: "4",
|
||||
name: "My Room",
|
||||
isSpace: false)),
|
||||
isLastAdmin: true,
|
||||
isSelected: false),
|
||||
hideSelection: true,
|
||||
mediaProvider: MediaProviderMock(configuration: .init())) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
//
|
||||
// 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 LeaveSpaceView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
let context: SpaceScreenViewModel.Context
|
||||
let leaveHandle: LeaveSpaceHandleProxy
|
||||
|
||||
@State private var scrollViewHeight: CGFloat = .zero
|
||||
@State private var buttonsHeight: CGFloat = .zero
|
||||
private let topPadding = 19.0
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(spacing: 0) {
|
||||
header
|
||||
rooms
|
||||
}
|
||||
.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 header: some View {
|
||||
VStack(spacing: 16) {
|
||||
BigIcon(icon: \.errorSolid, style: .alertSolid)
|
||||
|
||||
VStack(spacing: 8) {
|
||||
Text(leaveHandle.title(spaceName: context.viewState.space.name))
|
||||
.font(.compound.headingMDBold)
|
||||
.foregroundStyle(.compound.textPrimary)
|
||||
.multilineTextAlignment(.center)
|
||||
|
||||
if let subtitle = leaveHandle.subtitle {
|
||||
Text(subtitle)
|
||||
.font(.compound.bodyMD)
|
||||
.foregroundStyle(.compound.textSecondary)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(24)
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
var rooms: some View {
|
||||
if !leaveHandle.rooms.isEmpty, leaveHandle.canLeave {
|
||||
LazyVStack(spacing: 0, pinnedViews: .sectionHeaders) {
|
||||
Section {
|
||||
ForEach(leaveHandle.rooms, id: \.spaceRoomProxy.id) { room in
|
||||
LeaveSpaceRoomDetailsCell(room: room,
|
||||
hideSelection: leaveHandle.mode == .onlyAdminRooms,
|
||||
mediaProvider: context.mediaProvider) {
|
||||
context.send(viewAction: .toggleLeaveSpaceRoomDetails(id: room.spaceRoomProxy.id))
|
||||
}
|
||||
.disabled(room.isLastAdmin)
|
||||
}
|
||||
} header: {
|
||||
if leaveHandle.mode == .manyRooms {
|
||||
Button(leaveHandle.selectedCount > 0 ? L10n.actionDeselectAll : L10n.actionSelectAll) {
|
||||
context.send(viewAction: leaveHandle.selectedCount > 0 ? .deselectAllLeaveRoomDetails : .selectAllLeaveRoomDetails)
|
||||
}
|
||||
.buttonStyle(.compound(.textLink, size: .small))
|
||||
.frame(maxWidth: .infinity, alignment: .trailing)
|
||||
.padding(.horizontal, 16)
|
||||
.padding(.bottom, 8)
|
||||
.background(Color.compound.bgCanvasDefault.ignoresSafeArea())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var buttons: some View {
|
||||
VStack(spacing: 16) {
|
||||
if leaveHandle.canLeave {
|
||||
Button(role: .destructive) {
|
||||
context.send(viewAction: .confirmLeaveSpace)
|
||||
} label: {
|
||||
Label(leaveHandle.confirmationTitle, icon: \.leave)
|
||||
}
|
||||
.buttonStyle(.compound(.primary))
|
||||
} else if context.viewState.canEditRolesAndPermissions {
|
||||
Button {
|
||||
context.send(viewAction: .rolesAndPermissions)
|
||||
} label: {
|
||||
Label(L10n.actionGoToRolesAndPermissions, icon: \.settings)
|
||||
}
|
||||
.buttonStyle(.compound(.primary))
|
||||
}
|
||||
|
||||
Button(L10n.actionCancel, action: dismiss.callAsFunction)
|
||||
.buttonStyle(.compound(.tertiary))
|
||||
}
|
||||
.padding(.horizontal, 16)
|
||||
.padding(.top, 16)
|
||||
}
|
||||
}
|
||||
|
||||
private extension LeaveSpaceHandleProxy {
|
||||
func title(spaceName: String) -> String {
|
||||
switch mode {
|
||||
case .lastSpaceAdmin: L10n.screenLeaveSpaceTitleLastAdmin(spaceName)
|
||||
default: L10n.screenLeaveSpaceTitle(spaceName)
|
||||
}
|
||||
}
|
||||
|
||||
var subtitle: String? {
|
||||
switch mode {
|
||||
case .manyRooms: L10n.screenLeaveSpaceSubtitle
|
||||
case .onlyAdminRooms: L10n.screenLeaveSpaceSubtitleOnlyLastAdmin
|
||||
case .noRooms: nil
|
||||
case .lastSpaceAdmin: L10n.screenLeaveSpaceSubtitleLastAdmin
|
||||
}
|
||||
}
|
||||
|
||||
var confirmationTitle: String {
|
||||
let selectedCount = selectedCount
|
||||
return selectedCount > 0 ? L10n.screenLeaveSpaceSubmit(selectedCount) : L10n.actionLeaveSpace
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
import MatrixRustSDK
|
||||
|
||||
struct LeaveSpaceView_Previews: PreviewProvider, TestablePreview {
|
||||
static let viewModel = makeViewModel()
|
||||
|
||||
static var previews: some View {
|
||||
LeaveSpaceView(context: viewModel.context, leaveHandle: makeLeaveHandle(mode: .manyRooms))
|
||||
.previewDisplayName("Many Rooms")
|
||||
LeaveSpaceView(context: viewModel.context, leaveHandle: makeLeaveHandle(mode: .onlyAdminRooms))
|
||||
.previewDisplayName("Only Admin Rooms")
|
||||
LeaveSpaceView(context: viewModel.context, leaveHandle: makeLeaveHandle(mode: .noRooms))
|
||||
.previewDisplayName("No Rooms")
|
||||
LeaveSpaceView(context: viewModel.context, leaveHandle: makeLeaveHandle(mode: .lastSpaceAdmin))
|
||||
.previewDisplayName("Last Space Admin")
|
||||
}
|
||||
|
||||
static let spaceRoomProxy = SpaceRoomProxyMock(.init(id: "!eng-space:matrix.org",
|
||||
name: "Engineering Team",
|
||||
isSpace: true,
|
||||
childrenCount: 30,
|
||||
joinedMembersCount: 76,
|
||||
heroes: [.mockDan, .mockBob, .mockCharlie, .mockVerbose],
|
||||
topic: "Description of the space goes right here. Lorem ipsum dolor sit amet consectetur. Leo viverra morbi habitant in.",
|
||||
joinRule: .knockRestricted(rules: [.roomMembership(roomId: "")])))
|
||||
|
||||
static func makeViewModel() -> SpaceScreenViewModel {
|
||||
let spaceRoomListProxy = SpaceRoomListProxyMock(.init(spaceRoomProxy: spaceRoomProxy,
|
||||
initialSpaceRooms: .mockSpaceList))
|
||||
let spaceServiceProxy = SpaceServiceProxyMock(.init())
|
||||
|
||||
let viewModel = SpaceScreenViewModel(spaceRoomListProxy: spaceRoomListProxy,
|
||||
spaceServiceProxy: spaceServiceProxy,
|
||||
selectedSpaceRoomPublisher: .init(nil),
|
||||
userSession: UserSessionMock(.init()),
|
||||
appSettings: AppSettings(),
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
return viewModel
|
||||
}
|
||||
|
||||
static func makeLeaveHandle(mode: LeaveSpaceHandleProxy.Mode) -> LeaveSpaceHandleProxy {
|
||||
let rooms: [LeaveSpaceRoom] = switch mode {
|
||||
case .manyRooms: .mockRooms
|
||||
case .onlyAdminRooms: .mockAdminRooms
|
||||
case .noRooms: []
|
||||
case .lastSpaceAdmin: .mockLastSpaceAdmin(spaceRoomProxy: spaceRoomProxy)
|
||||
}
|
||||
|
||||
return LeaveSpaceHandleProxy(spaceID: spaceRoomProxy.id,
|
||||
leaveHandle: LeaveSpaceHandleSDKMock(.init(rooms: rooms)))
|
||||
}
|
||||
}
|
||||
@@ -25,8 +25,8 @@ struct SpaceScreen: View {
|
||||
.navigationTitle(context.viewState.space.name)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar { toolbar }
|
||||
.sheet(item: $context.leaveHandle) { leaveHandle in
|
||||
LeaveSpaceView(context: context, leaveHandle: leaveHandle)
|
||||
.sheet(item: $context.leaveSpaceViewModel) { leaveSpaceViewModel in
|
||||
LeaveSpaceView(context: leaveSpaceViewModel.context)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user