mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
a520d94760
* 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>
195 lines
10 KiB
Swift
195 lines
10 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2023-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 Foundation
|
|
import UIKit
|
|
|
|
nonisolated struct MentionBuilder: MentionBuilderProtocol {
|
|
struct AttributesToRestore {
|
|
let font: UIFont
|
|
let blockquote: Bool?
|
|
let foregroundColor: UIColor
|
|
}
|
|
|
|
func handleUserMention(for attributedString: NSMutableAttributedString,
|
|
in range: NSRange,
|
|
url: URL,
|
|
userID: String,
|
|
userDisplayName: String?) {
|
|
let attributesToRestore = getAttributesToRestore(for: attributedString, in: range)
|
|
|
|
let attachmentData = PillTextAttachmentData(type: .user(userID: userID), font: attributesToRestore.font)
|
|
guard let attachment = PillTextAttachment(attachmentData: attachmentData) else {
|
|
attributedString.addAttribute(.MatrixUserID, value: userID, range: range)
|
|
|
|
if let userDisplayName {
|
|
attributedString.addAttribute(.MatrixUserDisplayName, value: userDisplayName, range: range)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
var attachmentAttributes: [NSAttributedString.Key: Any] = [.link: url,
|
|
.MatrixUserID: userID,
|
|
.font: attributesToRestore.font,
|
|
.foregroundColor: attributesToRestore.foregroundColor]
|
|
attachmentAttributes.addBlockquoteIfNeeded(attributesToRestore.blockquote)
|
|
attachmentAttributes.addMatrixUsernameIfNeeded(userDisplayName)
|
|
|
|
setPillAttachment(attachment: attachment,
|
|
attributedString: attributedString,
|
|
in: range,
|
|
with: attachmentAttributes)
|
|
}
|
|
|
|
func handleAllUsersMention(for attributedString: NSMutableAttributedString, in range: NSRange) {
|
|
let attributesToRestore = getAttributesToRestore(for: attributedString, in: range)
|
|
|
|
let attachmentData = PillTextAttachmentData(type: .allUsers, font: attributesToRestore.font)
|
|
guard let attachment = PillTextAttachment(attachmentData: attachmentData) else {
|
|
return
|
|
}
|
|
|
|
var attachmentAttributes: [NSAttributedString.Key: Any] = [.font: attributesToRestore.font,
|
|
.MatrixAllUsersMention: true,
|
|
.foregroundColor: attributesToRestore.foregroundColor]
|
|
attachmentAttributes.addBlockquoteIfNeeded(attributesToRestore.blockquote)
|
|
|
|
setPillAttachment(attachment: attachment,
|
|
attributedString: attributedString,
|
|
in: range,
|
|
with: attachmentAttributes)
|
|
}
|
|
|
|
func handleRoomIDMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, roomID: String) {
|
|
let attributesToRestore = getAttributesToRestore(for: attributedString, in: range)
|
|
|
|
let attachmentData = PillTextAttachmentData(type: .roomID(roomID), font: attributesToRestore.font)
|
|
guard let attachment = PillTextAttachment(attachmentData: attachmentData) else {
|
|
attributedString.addAttribute(.MatrixRoomID, value: roomID, range: range)
|
|
return
|
|
}
|
|
|
|
var attachmentAttributes: [NSAttributedString.Key: Any] = [.link: url,
|
|
.MatrixRoomID: roomID,
|
|
.font: attributesToRestore.font,
|
|
.foregroundColor: attributesToRestore.foregroundColor]
|
|
attachmentAttributes.addBlockquoteIfNeeded(attributesToRestore.blockquote)
|
|
|
|
setPillAttachment(attachment: attachment,
|
|
attributedString: attributedString,
|
|
in: range,
|
|
with: attachmentAttributes)
|
|
}
|
|
|
|
func handleRoomAliasMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, roomAlias: String, roomDisplayName: String?) {
|
|
let attributesToRestore = getAttributesToRestore(for: attributedString, in: range)
|
|
|
|
let attachmentData = PillTextAttachmentData(type: .roomAlias(roomAlias), font: attributesToRestore.font)
|
|
guard let attachment = PillTextAttachment(attachmentData: attachmentData) else {
|
|
attributedString.addAttribute(.MatrixRoomAlias, value: roomAlias, range: range)
|
|
if let roomDisplayName {
|
|
attributedString.addAttribute(.MatrixRoomDisplayName, value: roomDisplayName, range: range)
|
|
}
|
|
return
|
|
}
|
|
|
|
var attachmentAttributes: [NSAttributedString.Key: Any] = [.link: url,
|
|
.MatrixRoomAlias: roomAlias,
|
|
.font: attributesToRestore.font,
|
|
.foregroundColor: attributesToRestore.foregroundColor]
|
|
attachmentAttributes.addBlockquoteIfNeeded(attributesToRestore.blockquote)
|
|
attachmentAttributes.addMatrixRoomNameIfNeeded(roomDisplayName)
|
|
|
|
setPillAttachment(attachment: attachment,
|
|
attributedString: attributedString,
|
|
in: range,
|
|
with: attachmentAttributes)
|
|
}
|
|
|
|
func handleEventOnRoomAliasMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, eventID: String, roomAlias: String) {
|
|
let attributesToRestore = getAttributesToRestore(for: attributedString, in: range)
|
|
|
|
let attachmentData = PillTextAttachmentData(type: .event(room: .roomAlias(roomAlias)), font: attributesToRestore.font)
|
|
guard let attachment = PillTextAttachment(attachmentData: attachmentData) else {
|
|
attributedString.addAttribute(.MatrixEventOnRoomAlias, value: EventOnRoomAliasAttribute.Value(alias: roomAlias, eventID: eventID), range: range)
|
|
return
|
|
}
|
|
|
|
var attachmentAttributes: [NSAttributedString.Key: Any] = [.link: url,
|
|
.MatrixEventOnRoomAlias: EventOnRoomAliasAttribute.Value(alias: roomAlias, eventID: eventID),
|
|
.font: attributesToRestore.font,
|
|
.foregroundColor: attributesToRestore.foregroundColor]
|
|
attachmentAttributes.addBlockquoteIfNeeded(attributesToRestore.blockquote)
|
|
|
|
setPillAttachment(attachment: attachment,
|
|
attributedString: attributedString,
|
|
in: range,
|
|
with: attachmentAttributes)
|
|
}
|
|
|
|
func handleEventOnRoomIDMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, eventID: String, roomID: String) {
|
|
let attributesToRestore = getAttributesToRestore(for: attributedString, in: range)
|
|
|
|
let attachmentData = PillTextAttachmentData(type: .event(room: .roomID(roomID)), font: attributesToRestore.font)
|
|
guard let attachment = PillTextAttachment(attachmentData: attachmentData) else {
|
|
attributedString.addAttribute(.MatrixEventOnRoomID, value: EventOnRoomIDAttribute.Value(roomID: roomID, eventID: eventID), range: range)
|
|
return
|
|
}
|
|
|
|
var attachmentAttributes: [NSAttributedString.Key: Any] = [.link: url,
|
|
.MatrixEventOnRoomID: EventOnRoomIDAttribute.Value(roomID: roomID, eventID: eventID),
|
|
.font: attributesToRestore.font,
|
|
.foregroundColor: attributesToRestore.foregroundColor]
|
|
attachmentAttributes.addBlockquoteIfNeeded(attributesToRestore.blockquote)
|
|
|
|
setPillAttachment(attachment: attachment,
|
|
attributedString: attributedString,
|
|
in: range,
|
|
with: attachmentAttributes)
|
|
}
|
|
|
|
private func getAttributesToRestore(for attributedString: NSMutableAttributedString, in range: NSRange) -> AttributesToRestore {
|
|
let attributes = attributedString.attributes(at: 0, longestEffectiveRange: nil, in: range)
|
|
let font = attributes[.font] as? UIFont ?? .preferredFont(forTextStyle: .body)
|
|
let blockquote = attributes[.MatrixBlockquote] as? Bool
|
|
let foregroundColor = attributes[.foregroundColor] as? UIColor ?? .compound.textPrimary
|
|
|
|
return AttributesToRestore(font: font, blockquote: blockquote, foregroundColor: foregroundColor)
|
|
}
|
|
|
|
private func setPillAttachment(attachment: PillTextAttachment,
|
|
attributedString: NSMutableAttributedString,
|
|
in range: NSRange,
|
|
with attributes: [NSAttributedString.Key: Any]) {
|
|
let attachmentString = NSMutableAttributedString(attachment: attachment)
|
|
attachmentString.addAttributes(attributes, range: NSRange(location: 0, length: attachmentString.length))
|
|
attributedString.replaceCharacters(in: range, with: attachmentString)
|
|
}
|
|
}
|
|
|
|
private nonisolated extension Dictionary where Key == NSAttributedString.Key, Value == Any {
|
|
mutating func addBlockquoteIfNeeded(_ value: Bool?) {
|
|
if let value {
|
|
self[.MatrixBlockquote] = value
|
|
}
|
|
}
|
|
|
|
mutating func addMatrixUsernameIfNeeded(_ value: String?) {
|
|
if let value {
|
|
self[.MatrixUserDisplayName] = value
|
|
}
|
|
}
|
|
|
|
mutating func addMatrixRoomNameIfNeeded(_ value: String?) {
|
|
if let value {
|
|
self[.MatrixRoomDisplayName] = value
|
|
}
|
|
}
|
|
}
|