mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +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>
132 lines
5.3 KiB
Swift
132 lines
5.3 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 SwiftUI
|
|
|
|
/// A custom layout used for quotes and content when using the bubbles timeline style.
|
|
///
|
|
/// A custom layout is required as the embedded quote bubbles and code blocks should fill the entire
|
|
/// width of the message bubble, without causing the width of the bubble to fill all of the available space.
|
|
nonisolated struct TimelineBubbleLayout: Layout {
|
|
struct Cache {
|
|
var sizes = [Int: [ProposedViewSize: CGSize]]()
|
|
}
|
|
|
|
/// The spacing between the components in the bubble.
|
|
let spacing: CGFloat
|
|
|
|
/// Layout size for the bubble content. These sizing types are used within
|
|
/// `TimelineBubbleLayout` to create the layout we would like.
|
|
enum Size: LayoutValueKey, Equatable {
|
|
static let defaultValue: Size = .natural
|
|
|
|
/// Full width mode used for greedy components like blockquotes and code blocks.
|
|
enum BubbleWidthMode {
|
|
/// The view has its natural size and will be used for layout calculations only.
|
|
case layout
|
|
/// The view has a greedy width and will fill the available space within the bubble.
|
|
case rendering
|
|
}
|
|
|
|
/// The view will fill the available width, with different behaviour depending on the mode.
|
|
/// Views using the `.layout` mode should be hidden and are used only for width calculations.
|
|
/// Views using the `.rendering` mode should be visible and are placed to fill the bubble's calculated width.
|
|
case bubbleWidth(mode: BubbleWidthMode)
|
|
/// The view uses its natural size for both layout calculations and rendering.
|
|
case natural
|
|
|
|
var shouldLayout: Bool {
|
|
switch self {
|
|
case .natural, .bubbleWidth(mode: .layout): true
|
|
default: false
|
|
}
|
|
}
|
|
|
|
var shouldRender: Bool {
|
|
switch self {
|
|
case .natural, .bubbleWidth(mode: .rendering): true
|
|
default: false
|
|
}
|
|
}
|
|
}
|
|
|
|
func makeCache(subviews: Subviews) -> Cache {
|
|
Cache()
|
|
}
|
|
|
|
func updateCache(_ cache: inout Cache, subviews: Subviews) {
|
|
// A subview changed, reset everything
|
|
cache = Cache()
|
|
}
|
|
|
|
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout Cache) -> CGSize {
|
|
guard !subviews.isEmpty else { return .zero }
|
|
|
|
// Calculate the natural size using the regular text and non-greedy quote bubbles.
|
|
let layoutSubviews = subviews.filter { $0[Size.self].shouldLayout }
|
|
|
|
let subviewSizes = layoutSubviews.map { size(for: $0, subviews: subviews, proposedSize: proposal, cache: &cache) }
|
|
|
|
let maxWidth = subviewSizes.map(\.width).reduce(0, max)
|
|
let totalHeight = subviewSizes.map(\.height).reduce(0, +)
|
|
let totalSpacing = CGFloat(layoutSubviews.count - 1) * spacing
|
|
|
|
return CGSize(width: maxWidth, height: totalHeight + totalSpacing)
|
|
}
|
|
|
|
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout Cache) {
|
|
guard !subviews.isEmpty else { return }
|
|
|
|
// Calculate the width using the regular text along with non-greedy versions of any greedy components.
|
|
let layoutSubviews = subviews.filter { $0[Size.self].shouldLayout }
|
|
let maxWidth = layoutSubviews.map { size(for: $0, subviews: subviews, proposedSize: proposal, cache: &cache).width }.reduce(0, max)
|
|
|
|
// Place the regular text and greedy components using the calculated width.
|
|
let visibleSubviews = subviews.filter { $0[Size.self].shouldRender }
|
|
|
|
let subviewSizes = visibleSubviews.map { size(for: $0, subviews: subviews, proposedSize: ProposedViewSize(width: maxWidth, height: proposal.height), cache: &cache) }
|
|
|
|
var y = bounds.minY
|
|
for index in visibleSubviews.indices {
|
|
let height = subviewSizes[index].height
|
|
visibleSubviews[index].place(at: CGPoint(x: bounds.minX, y: y),
|
|
anchor: .topLeading,
|
|
proposal: ProposedViewSize(width: maxWidth, height: height))
|
|
y += height + spacing
|
|
}
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func size(for subview: LayoutSubview, subviews: LayoutSubviews, proposedSize: ProposedViewSize, cache: inout Cache) -> CGSize {
|
|
guard let index = subviews.firstIndex(of: subview) else {
|
|
fatalError()
|
|
}
|
|
|
|
if cache.sizes[index] == nil {
|
|
cache.sizes[index] = [:]
|
|
}
|
|
|
|
if let cachedSize = cache.sizes[index]?[proposedSize] {
|
|
return cachedSize
|
|
}
|
|
|
|
let size = subview.sizeThatFits(proposedSize)
|
|
|
|
cache.sizes[index]?[proposedSize] = size
|
|
return size
|
|
}
|
|
}
|
|
|
|
extension View {
|
|
/// Sets the layout size for this view when placed within a `TimelineBubbleLayout`.
|
|
func timelineBubbleLayoutSize(_ size: TimelineBubbleLayout.Size) -> some View {
|
|
layoutValue(key: TimelineBubbleLayout.Size.self, value: size)
|
|
}
|
|
}
|