Skip to content

Commit

Permalink
Support Xcode 16.2 onPreferenceChange SDK change (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippzagar authored Dec 12, 2024
1 parent f875144 commit 2997114
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
with:
runsonlabels: '["macOS", "self-hosted"]'
scheme: SpeziViews-Package
destination: 'platform=watchOS Simulator,name=Apple Watch Series 9 (45mm)'
destination: 'platform=watchOS Simulator,name=Apple Watch Series 10 (46mm)'
resultBundle: SpeziViews-watchOS.xcresult
artifactname: SpeziViews-watchOS.xcresult
buildandtest_visionos:
Expand Down
3 changes: 1 addition & 2 deletions Sources/SpeziValidation/ValidationRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ public struct ValidationRule: Identifiable, Sendable, Equatable {
let effect: CascadingValidationEffect


// swiftlint:disable:next function_default_parameter_at_end
init(
id: UUID = UUID(),
id: UUID = UUID(), // swiftlint:disable:this function_default_parameter_at_end
ruleClosure: @escaping @Sendable (String) -> Bool,
message: LocalizedStringResource,
effect: CascadingValidationEffect = .continue
Expand Down
12 changes: 6 additions & 6 deletions Sources/SpeziViews/Views/Button/AsyncButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ public struct AsyncButton<Label: View>: View {
/// It may also be used to externally control or observe the button's processing state.
/// - action: An asynchronous button action.
@_disfavoredOverload
public init<Title: StringProtocol>( // swiftlint:disable:this function_default_parameter_at_end
public init<Title: StringProtocol>(
_ title: Title,
role: ButtonRole? = nil,
role: ButtonRole? = nil, // swiftlint:disable:this function_default_parameter_at_end
state: Binding<ViewState>,
action: @MainActor @escaping () async throws -> Void
) where Label == Text {
Expand All @@ -140,9 +140,9 @@ public struct AsyncButton<Label: View>: View {
/// - state: A ``ViewState`` binding that it used to propagate any error caught in the button action.
/// It may also be used to externally control or observe the button's processing state.
/// - action: An asynchronous button action.
public init( // swiftlint:disable:this function_default_parameter_at_end
public init(
_ title: LocalizedStringResource,
role: ButtonRole? = nil,
role: ButtonRole? = nil, // swiftlint:disable:this function_default_parameter_at_end
state: Binding<ViewState>,
action: @MainActor @escaping () async throws -> Void
) where Label == Text {
Expand All @@ -158,8 +158,8 @@ public struct AsyncButton<Label: View>: View {
/// It may also be used to externally control or observe the button's processing state.
/// - action: An asynchronous button action.
/// - label: The Button label.
public init( // swiftlint:disable:this function_default_parameter_at_end
role: ButtonRole? = nil,
public init(
role: ButtonRole? = nil, // swiftlint:disable:this function_default_parameter_at_end
state: Binding<ViewState>,
action: @MainActor @escaping () async throws -> Void,
@ViewBuilder label: () -> Label
Expand Down
14 changes: 13 additions & 1 deletion Sources/SpeziViews/Views/Layout/HorizontalGeometryReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,19 @@ public struct HorizontalGeometryReader<Content: View>: View {
}
)
.onPreferenceChange(WidthPreferenceKey.self) { width in
self.width = width
// The `onPreferenceChange` view modfier now takes a `@Sendable` closure, therefore we cannot capture `@MainActor` isolated properties
// on the `View` directly anymore: https://developer.apple.com/documentation/swiftui/view/onpreferencechange(_:perform:)?changes=latest_minor
// However, as the `@Sendable` closure is still run on the MainActor (at least in my testing on 18.2 RC SDKs), we can use `MainActor.assumeIsolated`
// to avoid scheduling a `MainActor` `Task`, which could delay execution and cause unexpected UI behavior.
if Thread.isMainThread {
MainActor.assumeIsolated {
self.width = width
}
} else {
Task { @MainActor in
self.width = width
}
}
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion Tests/UITests/TestApp/ViewsTests/CanvasTestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ struct CanvasTestView: View {
}
.navigationBarTitleDisplayMode(.inline)
.onPreferenceChange(CanvasView.CanvasSizePreferenceKey.self) { size in
self.receivedSize = size
// See `HorizontalGeometryReader.swift`
if Thread.isMainThread {
MainActor.assumeIsolated {
self.receivedSize = size
}
} else {
Task { @MainActor in
self.receivedSize = size
}
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/UITests/UITests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2,3,7";
};
name = Debug;
Expand Down Expand Up @@ -606,7 +606,7 @@
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2,3,7";
};
name = Release;
Expand All @@ -629,7 +629,7 @@
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2,7";
TEST_TARGET_NAME = TestApp;
};
Expand All @@ -652,7 +652,7 @@
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2,7";
TEST_TARGET_NAME = TestApp;
};
Expand Down Expand Up @@ -753,7 +753,7 @@
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2,3,7";
};
name = Test;
Expand All @@ -776,7 +776,7 @@
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2,7";
TEST_TARGET_NAME = TestApp;
};
Expand Down

0 comments on commit 2997114

Please sign in to comment.