Files
Mauro a520d94760 Swift 6 Migration (#5733)
* Enable Swift 6.2 with default main actor isolation for the app target

Turn on SWIFT_VERSION 6.2, SWIFT_APPROACHABLE_CONCURRENCY and SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor for the ElementX target, matching the NSE and Share Extension (which already use 6.2 without the main actor default).

# Conflicts:
#	ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

* Update the code generation templates for Swift 6

Emit nonisolated/@unchecked Sendable mocks and nonisolated string enums, and mark async mock methods @concurrent so they keep suspending like the real implementations (otherwise intermediate states are unobservable in tests). Includes the regenerated mocks and strings.

# Conflicts:
#	ElementX/Sources/Mocks/Generated/GeneratedMocks.swift

* Make the data layer and services nonisolated and Sendable

Annotate the value-type models, Rust SDK proxy wrappers, timeline item graph, settings, logging and NSE-shared types as nonisolated, and conform the service protocols to Sendable so their existentials can cross isolation boundaries. These types run off the main actor and no longer inherit the target's default main actor isolation.

* Adopt the new isolation across call sites, mocks and signatures

Update call sites, closure signatures and mock configuration helpers to match the nonisolated/Sendable data layer, including the main actor isolated mock inits and the @Sendable filter on recentlyVisitedRooms.

^ Conflicts:
^	ElementX/Sources/Services/SecureBackup/SecureBackupController.swift

* Remove redundant @MainActor annotations

With the app target defaulting to main actor isolation, the explicit @MainActor on view models, coordinators, views and their protocols is no longer required.

* Fix concurrency hotspots surfaced by the migration

Resolve the room summary main-thread deadlock by moving diff processing onto AsyncStreams, add the SDKListener.onMainActor FIFO bridge for SDK callbacks, hop KVO/timer/UIKit callbacks to the main actor via assumeIsolated, add isolated deinits, and apply the few justified nonisolated(unsafe) escapes for queue-confined Apple APIs.

* Fix the unit tests for Swift 6 concurrency

Hop the waitForConfirmation body onto the caller's actor (Swift Testing runs confirmation closures off-actor), and adjust the affected suites and async signatures.

* offload diffs on concurrent threads instead of main actor

* make room info protocols sendable

* use a task group for fetching data so that everything runs concurrently in the TextRoomTimelineView when getting the preview.

* use the generic onMainActor implementation, which already makes a stream and send the continuation on the main actor.

* small pr comments issues being addressed

* update enterprise

* Update StaticCode to 1.1.0 and specify the required Swift isolation.

* fix ElementCallServiceTests

* update enterprise

* re-generate mocks

* update enterprise

* enterprise new code conformed to swift 6

---------

Co-authored-by: Doug <douglase@element.io>
2026-06-18 17:55:31 +02:00

208 lines
9.1 KiB
Swift

//
// Copyright 2025 Element Creations Ltd.
// Copyright 2024-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.
//
//
// Copyright 2025 Element Creations Ltd.
// Copyright 2022-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 KnockRequestCellInfo: Equatable {
let eventID: String
let userID: String
let displayName: String?
let avatarURL: URL?
let timestamp: String?
let reason: String?
}
struct KnockRequestCell: View {
let cellInfo: KnockRequestCellInfo
var mediaProvider: MediaProviderProtocol?
let onAccept: (@MainActor (String) -> Void)?
let onDecline: (@MainActor (String) -> Void)?
let onDeclineAndBan: (@MainActor (String) -> Void)?
var body: some View {
HStack(alignment: .top, spacing: 16) {
LoadableAvatarImage(url: cellInfo.avatarURL,
name: cellInfo.displayName,
contentID: cellInfo.userID,
avatarSize: .user(on: .knockingUserList),
mediaProvider: mediaProvider)
VStack(alignment: .leading, spacing: 16) {
header
if let reason = cellInfo.reason {
DisclosableText(text: reason)
}
actions
}
.padding(.trailing, 16)
.overlay(alignment: .bottom) {
// Custom separator that uses the same color from the compound one
Color.compound._borderInteractiveSecondaryAlpha
.frame(height: 0.5)
}
}
.padding(.top, 16)
.padding(.leading, 16)
.background(.compound.bgCanvasDefault)
}
private var header: some View {
VStack(alignment: .leading, spacing: 0) {
HStack(alignment: .top, spacing: 0) {
Text(cellInfo.displayName ?? cellInfo.userID)
.font(.compound.bodyLGSemibold)
.foregroundStyle(.compound.textPrimary)
.frame(maxWidth: .infinity, alignment: .leading)
if let timestamp = cellInfo.timestamp {
Text(timestamp)
.font(.compound.bodySM)
.foregroundStyle(.compound.textSecondary)
}
}
if cellInfo.displayName != nil {
Text(cellInfo.userID)
.font(.compound.bodyMD)
.foregroundStyle(.compound.textSecondary)
}
}
}
private var actions: some View {
VStack(spacing: 16) {
if onDecline != nil || onAccept != nil {
HStack(spacing: 16) {
if let onDecline {
Button {
onDecline(cellInfo.eventID)
} label: {
Text(L10n.actionDecline)
.frame(maxWidth: .infinity)
}
.buttonStyle(.compound(.secondary, size: .medium))
}
if let onAccept {
Button {
onAccept(cellInfo.eventID)
} label: {
Text(L10n.actionAccept)
.frame(maxWidth: .infinity)
}
.buttonStyle(.compound(.primary, size: .medium))
}
}
.fixedSize(horizontal: false, vertical: true)
}
if let onDeclineAndBan {
Button(role: .destructive) {
onDeclineAndBan(cellInfo.eventID)
} label: {
Text(L10n.screenKnockRequestsListDeclineAndBanActionTitle)
.frame(maxWidth: .infinity)
}
.buttonStyle(.compound(.tertiary, size: .small))
.padding(.top, 4)
}
}
.padding(.bottom, 16)
}
}
private struct DisclosableText: View {
let text: String
@State private var collapsedHeight = CGFloat.zero
@State private var expandedHeight = CGFloat.zero
@State private var isExpanded = false
var body: some View {
HStack(alignment: .top, spacing: 4) {
Text(text)
.multilineTextAlignment(.leading)
.lineLimit(isExpanded ? nil : 3)
.font(.compound.bodyMD)
.foregroundStyle(.compound.textPrimary)
.frame(maxWidth: .infinity, alignment: .leading)
.onGeometryChange(for: CGFloat.self) { geometry in
geometry.size.height
} action: { newValue in
if !isExpanded {
collapsedHeight = newValue
}
}
.background {
Text(text)
.multilineTextAlignment(.leading)
.font(.compound.bodyMD)
.foregroundStyle(.compound.textPrimary)
.fixedSize(horizontal: false, vertical: true)
.onGeometryChange(for: CGFloat.self) { geometry in
geometry.size.height
} action: { newValue in
expandedHeight = newValue
}
.hidden()
}
Button {
withAnimation {
isExpanded.toggle()
}
} label: {
CompoundIcon(\.chevronDown, size: .medium, relativeTo: .compound.bodyMD)
.foregroundStyle(.compound.iconTertiary)
.rotationEffect(.degrees(isExpanded ? 180 : 0))
}
.buttonStyle(.plain)
.opacity(collapsedHeight < expandedHeight ? 1 : 0)
.disabled(collapsedHeight >= expandedHeight)
}
}
}
extension KnockRequestCellInfo: Identifiable {
var id: String {
eventID
}
}
struct KnockRequestCell_Previews: PreviewProvider, TestablePreview {
// swiftlint:disable:next line_length
static let aliceWithLongReason = KnockRequestCellInfo(eventID: "1", userID: "@alice:matrix.org", displayName: "Alice", avatarURL: nil, timestamp: "20 Nov 2024", reason: "Hello would like to join this room, also this is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long reason")
static let aliceWithShortReason = KnockRequestCellInfo(eventID: "1", userID: "@alice:matrix.org", displayName: "Alice", avatarURL: nil, timestamp: "20 Nov 2024", reason: "Hello, I am Alice and would like to join this room, please")
static let aliceWithNoReason = KnockRequestCellInfo(eventID: "1", userID: "@alice:matrix.org", displayName: "Alice", avatarURL: nil, timestamp: "20 Nov 2024", reason: nil)
static let aliceWithNoName = KnockRequestCellInfo(eventID: "1", userID: "@alice:matrix.org", displayName: nil, avatarURL: nil, timestamp: "20 Nov 2024", reason: nil)
static var previews: some View {
KnockRequestCell(cellInfo: aliceWithLongReason) { _ in } onDecline: { _ in } onDeclineAndBan: { _ in }
.previewDisplayName("Long reason")
KnockRequestCell(cellInfo: aliceWithShortReason) { _ in } onDecline: { _ in } onDeclineAndBan: { _ in }
.previewDisplayName("Short reason")
KnockRequestCell(cellInfo: aliceWithNoReason) { _ in } onDecline: { _ in } onDeclineAndBan: { _ in }
.previewDisplayName("No reason")
KnockRequestCell(cellInfo: aliceWithNoName) { _ in } onDecline: { _ in } onDeclineAndBan: { _ in }
.previewDisplayName("No name")
// KnockRequestCell(cellInfo: aliceWithShortReason, onAccept: nil) onDecline: { _ in } onDeclineAndBan: { _ in }
// .previewDisplayName("No Accept")
// KnockRequestCell(cellInfo: aliceWithShortReason) onDeclineAndBan: { _ in }
// .previewDisplayName("No Accept and Decline")
// KnockRequestCell(cellInfo: aliceWithShortReason) { _ in } onDecline: { _ in })
// .previewDisplayName("No Ban")
}
}