Files
element-x-ios/ElementX/Sources/Other/SwiftUI/Layout/FullscreenDialog.swift
T
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

238 lines
8.6 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 layout that separates the main content of a screen from the buttons shown at
/// the bottom for a dialogs that fill the entire screen. On larger devices (iPad/Mac),
/// the height is constrained to keep the content relatively close to the buttons. If
/// the content overflows the space available, it will become scrollable.
///
/// The background color behind the buttons is read from the `backgroundStyle`
/// environment value, so make sure to set this to match the screen's background.
struct FullscreenDialog<Content: View, BottomContent: View>: View {
@Environment(\.dynamicTypeSize) private var dynamicTypeSize
@Environment(\.backgroundStyle) private var backgroundStyle
/// Padding applied to the top of the content automatically. Use `UIConstants` for preset values.
var topPadding: CGFloat = UIConstants.titleTopPaddingToNavigationBar
/// Padding applied to the content automatically
var horizontalPadding: CGFloat = 16
/// Padding applied to the buttons automatically
var bottomHorizontalPadding: CGFloat = 16
/// The spacing between the content and the buttons.
var spacing: CGFloat = 16
/// The type of background that should be shown behind the content. This
/// will be hidden if the main content extends behind the bottom content.
var background: FullscreenDialogBackground?
/// The main content shown at the top of the layout.
@ViewBuilder var content: () -> Content
/// The content shown at the bottom of the layout.
@ViewBuilder var bottomContent: () -> BottomContent
/// Whether or not the screen should show its background.
@State private var showsBackground = true
/// The background style given to the bottom content.
private var bottomContentBackgroundStyle: AnyShapeStyle? {
if background != nil, showsBackground {
AnyShapeStyle(Color.clear)
} else {
backgroundStyle
}
}
var body: some View {
ZStack {
if let background, showsBackground {
Color.clear
.background(alignment: .bottom) {
background.image
}
.ignoresSafeArea()
}
if dynamicTypeSize < .accessibility1 {
standardLayout
} else {
accessibilityLayout
}
}
}
/// A layout where the content scrolls with the bottom content overlaid. Used with regular font sizes.
var standardLayout: some View {
GeometryReader { geometry in
ScrollView {
VStack(spacing: 0) {
Spacer()
.frame(height: UIConstants.spacerHeight(in: geometry))
content()
.readableFrame()
.padding(.horizontal, horizontalPadding)
.padding(.top, topPadding)
}
}
.scrollBounceBehavior(.basedOnSize)
.safeAreaInset(edge: .bottom) {
VStack(spacing: 0) {
bottomContent()
.readableFrame()
.padding(.horizontal, bottomHorizontalPadding)
.padding(.top, spacing)
.padding(.bottom, UIConstants.actionButtonBottomPadding)
Spacer()
.frame(height: UIConstants.spacerHeight(in: geometry))
}
.background(bottomContentBackgroundStyle ?? AnyShapeStyle(Color.clear))
}
}
.introspect(.scrollView, on: .supportedVersions, customize: updateBackgroundVisibility)
}
/// A continuously scrolling layout used for accessibility font sizes.
private var accessibilityLayout: some View {
GeometryReader { geometry in
ScrollView {
VStack(spacing: 0) {
Spacer()
.frame(height: UIConstants.spacerHeight(in: geometry))
content()
.readableFrame()
.padding(.horizontal, horizontalPadding)
.padding(.top, topPadding)
Spacer(minLength: spacing)
bottomContent()
.readableFrame()
.padding(.horizontal, bottomHorizontalPadding)
.padding(.bottom, UIConstants.actionButtonBottomPadding)
Spacer()
.frame(height: UIConstants.spacerHeight(in: geometry))
}
.frame(minHeight: geometry.size.height)
}
.scrollBounceBehavior(.basedOnSize)
}
}
func updateBackgroundVisibility(scrollView: UIScrollView) {
guard dynamicTypeSize < .accessibility1 else {
if !showsBackground {
showsBackground = true
}
return
}
DispatchQueue.main.async { // Don't modify the state during a view update.
let insetHeight = scrollView.adjustedContentInset.top + scrollView.adjustedContentInset.bottom
let availableHeight = scrollView.frame.height - insetHeight
let shouldShowBackground = scrollView.contentSize.height < availableHeight
if showsBackground != shouldShowBackground {
showsBackground = shouldShowBackground
}
}
}
}
/// The different types of background supported by the `FullscreenDialog` view.
enum FullscreenDialogBackground {
/// The bottom gradient from the FTUE flow.
case gradient
private var asset: ImageAsset {
switch self {
case .gradient:
Asset.Images.backgroundBottom
}
}
private var capInsets: EdgeInsets {
switch self {
case .gradient:
EdgeInsets(top: 0, leading: 0, bottom: 250, trailing: 0)
}
}
/// The image that represents the background.
var image: Image {
Image(asset: asset)
.resizable(capInsets: capInsets)
}
}
struct FullscreenDialog_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
FullscreenDialog(topPadding: UIConstants.iconTopPaddingToNavigationBar) {
content
} bottomContent: {
buttons
}
.background()
.backgroundStyle(.compound.bgCanvasDefault)
.previewDisplayName("Plain")
FullscreenDialog(topPadding: UIConstants.iconTopPaddingToNavigationBar, background: .gradient) {
content
} bottomContent: {
buttons
}
.background()
.backgroundStyle(.compound.bgCanvasDefault)
.previewDisplayName("Gradient")
}
private static var content: some View {
VStack(spacing: 8) {
Image(systemName: "globe")
.font(.system(size: 50))
.foregroundColor(.compound.textPrimary)
.padding()
.background(Color.compound.bgSubtlePrimary, in: Circle())
.padding(.bottom, 8)
Text("Hello, World")
.font(.compound.headingLG)
.foregroundColor(.compound.textPrimary)
Text("I am a subtitle")
.font(.compound.bodyLG)
.foregroundColor(.compound.textSecondary)
.padding(.bottom)
VStack(alignment: .leading, spacing: 6) {
Label("We care about you", systemImage: "person")
Label("Environmentally focussed", systemImage: "leaf")
Label("All of the options", systemImage: "wrench")
Label("Fun to use", systemImage: "logo.xbox")
}
}
}
private static var buttons: some View {
VStack(spacing: 16) {
Button { } label: {
Text("Continue")
.font(.compound.bodyLGSemibold)
}
.buttonStyle(.compound(.primary))
Button { } label: {
Text("More options")
.font(.compound.bodyLGSemibold)
.padding(14)
}
}
}
}