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

166 lines
6.1 KiB
Swift

//
// Copyright 2026 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Foundation
import Observation
struct ClassicAppAccount: Equatable, CustomStringConvertible {
let userID: String
let displayName: String?
let avatarURL: URL?
let serverName: String
let homeserverURL: URL
let cryptoStoreURL: URL
let cryptoStorePassphrase: String
let accessToken: String // For avatar loading and key backup detection.
/// Custom `CustomStringConvertible` without the access token.
var description: String {
"ClassicAppAccount(userID: \(userID), homeserverURL: \(homeserverURL))"
}
enum AvailableSecrets { case complete, requiresBackup, unavailable }
@Observable
class State: Equatable {
static func == (lhs: State, rhs: State) -> Bool {
lhs.isServerSupported == rhs.isServerSupported && lhs.availableSecrets == rhs.availableSecrets
}
/// Whether or not the account's server is supported by Element X (or `nil` whilst determining support).
///
/// The account will be hidden when this value is `false`.
var isServerSupported: Bool?
/// Information about the secrets available from Element X (or `nil` whilst determining availability).
///
/// See ``AuthenticationService.refreshClassicAppAccountState`` for details about how
/// this property's value affects the authentication flow.
var availableSecrets: AvailableSecrets?
}
let state = State()
}
// MARK: NSCoding Types
final class ClassicAppMXAccount: NSObject, NSCoding {
/// The obtained user ID.
let userID: String
/// The access token to create a MXRestClient.
let accessToken: String
/// The homeserver url (ex: "https://matrix.org").
let homeserverURL: URL
/// Disable the account without logging out (NO by default).
///
/// A matrix session is automatically opened for the account when this property is toggled from YES to NO.
/// The session is closed when this property is set to YES.
let isDisabled: Bool
/// Flag to indicate if the account has been logged out by the homeserver admin.
let isSoftLogout: Bool
/// Whether or not the account is considered active.
var isActive: Bool {
!isDisabled && !isSoftLogout
}
/// Override the existing `CustomStringConvertible` conformance.
override nonisolated var description: String {
"ClassicAppMXAccount(userID: \(userID), homeserverURL: \(homeserverURL), isDisabled: \(isDisabled), isSoftLogout: \(isSoftLogout))"
}
// MARK: NSCoding
enum Keys {
static let homeserverURL = "homeserverurl" // String?
static let userID = "userid" // String?
static let accessToken = "accesstoken" // String?
static let accessTokenExpiresAt = "accessTokenExpiresAt" // UInt64
static let refreshToken = "refreshToken" // String?
static let identityServerURL = "identityserverurl" // String?
static let identityServerAccessToken = "identityserveraccesstoken" // String?
static let deviceID = "deviceId" // String?
static let allowedCertificate = "allowedCertificate" // Data?
static let threePIDs = "threePIDs" // [MXThirdPartyIdentifier]?
static let device = "device" // MXDevice?
static let antivirusServerURL = "antivirusserverurl" // String?
static let pushGatewayURL = "pushgatewayurl" // String?
static let hasPusherForPushNotifications = "_enablePushNotifications" // Bool
static let hasPusherForPushKitNotifications = "enablePushKitNotifications" // Bool
static let enableInAppNotifications = "enableInAppNotifications" // Bool
static let isDisabled = "disabled" // Bool
static let isSoftLogout = "isSoftLogout" // Bool
static let isWarnedAboutEncryption = "warnedAboutEncryption" // Bool
static let others = "others" // NSMutableDictionary
}
required init?(coder: NSCoder) {
guard let userID = coder.decodeObject(forKey: Keys.userID) as? String,
let accessToken = coder.decodeObject(forKey: Keys.accessToken) as? String,
let homeserver = coder.decodeObject(forKey: Keys.homeserverURL) as? String,
let homeserverURL = URL(string: homeserver) else {
return nil
}
self.userID = userID
self.accessToken = accessToken
self.homeserverURL = homeserverURL
isDisabled = coder.decodeBool(forKey: Keys.isDisabled)
isSoftLogout = coder.decodeBool(forKey: Keys.isSoftLogout)
super.init()
}
func encode(with coder: NSCoder) {
fatalError("Not available")
}
}
/// `MXUser` represents a user in Matrix.
final class ClassicAppMXUser: NSObject, NSCoding {
/// The user id.
let userID: String
/// The user display name.
let displayName: String?
/// The url of the user of the avatar.
let avatarURL: URL?
// MARK: NSCoding
enum Keys {
static let userID = "userId" // String
static let displayName = "displayname" // String?
static let avatarURL = "avatarUrl" // String?
static let statusMessage = "statusMsg" // String?
static let currentlyActive = "currentlyActive" // Bool
static let lastActiveLocalTimestamp = "lastActiveLocalTS" // UInt64
static let latestUpdateTimestamp = "latestUpdateTS" // UInt64
}
required init?(coder aDecoder: NSCoder) {
guard let userID = aDecoder.decodeObject(forKey: Keys.userID) as? String else {
return nil
}
self.userID = userID
displayName = aDecoder.decodeObject(forKey: Keys.displayName) as? String
let avatarURLString = aDecoder.decodeObject(forKey: Keys.avatarURL) as? String
avatarURL = avatarURLString.flatMap { URL(string: $0) }
super.init()
}
func encode(with coder: NSCoder) {
fatalError("Not available")
}
}