From 10b463fc241552c4c6668700c37d4112ae926fe5 Mon Sep 17 00:00:00 2001 From: Hiroshi Kimura Date: Sun, 15 Dec 2024 01:07:56 +0900 Subject: [PATCH] Cleanup (#52) - readingGeometry uses onGeometryChanged internally --- Package.swift | 7 +- .../Components/HostingEdge.swift | 65 ------------------- .../Components/ObservedView.swift | 2 +- .../TranslationEffect.swift | 8 +-- .../View+MeasureSize.swift | 62 ++---------------- 5 files changed, 14 insertions(+), 130 deletions(-) delete mode 100644 Sources/SwiftUISupport/Components/HostingEdge.swift diff --git a/Package.swift b/Package.swift index e5f9f05ce6..7899d2cbc7 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.7 +// swift-tools-version: 6.0 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "SwiftUISupport", platforms: [ - .iOS(.v15), + .iOS(.v16), .macOS(.v10_15), .watchOS(.v6), .tvOS(.v13), @@ -41,5 +41,6 @@ let package = Package( name: "SwiftUISupportTests", dependencies: ["SwiftUISupport"] ), - ] + ], + swiftLanguageModes: [.v6] ) diff --git a/Sources/SwiftUISupport/Components/HostingEdge.swift b/Sources/SwiftUISupport/Components/HostingEdge.swift deleted file mode 100644 index a70584445f..0000000000 --- a/Sources/SwiftUISupport/Components/HostingEdge.swift +++ /dev/null @@ -1,65 +0,0 @@ -import SwiftUI - -public struct HostingEdge: UIViewControllerRepresentable { - - public typealias UIViewControllerType = UIHostingController<_WrapperView> - - @Environment(\.layoutDirection) private var layoutDirection - - private let content: Content - private let reference: @MainActor (UIHostingController<_WrapperView>) -> Void - - public func makeCoordinator() -> Proxy { - .init(content: content) - } - - public init( - @ViewBuilder content: () -> Content, - reference: @escaping (UIHostingController<_WrapperView>) -> Void = { _ in } - ) { - self.content = content() - self.reference = reference - } - - public func makeUIViewController(context: Context) -> UIViewControllerType { - UIHostingController(rootView: .init(proxy: context.coordinator)) - } - - public func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { - // due to avoid the runtime error about updating state during updating view tree. - // maybe related: https://www.donnywals.com/xcode-14-publishing-changes-from-within-view-updates-is-not-allowed-this-will-cause-undefined-behavior/ - DispatchQueue.main.async { - withTransaction(context.transaction) { - context.coordinator.content = content - } - reference(uiViewController) - } - } - - public final class Proxy: ObservableObject { - - @Published var content: Content - - init(content: Content) { - - self._content = .init(initialValue: content) - } - - } - - public struct _WrapperView: View { - - @ObservedObject var proxy: Proxy - - init(proxy: Proxy) { - self.proxy = proxy - } - - public var body: some View { - proxy.content - } - - } - -} - diff --git a/Sources/SwiftUISupport/Components/ObservedView.swift b/Sources/SwiftUISupport/Components/ObservedView.swift index 2e37207734..8e2f278417 100644 --- a/Sources/SwiftUISupport/Components/ObservedView.swift +++ b/Sources/SwiftUISupport/Components/ObservedView.swift @@ -38,7 +38,7 @@ public struct AsyncObservedView: View { @State var loaded: Object? private let content: (Object) -> Content - private let objectLoader: () async -> Object + private let objectLoader: @Sendable () async -> Object public init( objectLoader: @escaping @Sendable () async -> Object, diff --git a/Sources/SwiftUISupportGeometryEffect/TranslationEffect.swift b/Sources/SwiftUISupportGeometryEffect/TranslationEffect.swift index 73805e6db9..6b48875cb5 100644 --- a/Sources/SwiftUISupportGeometryEffect/TranslationEffect.swift +++ b/Sources/SwiftUISupportGeometryEffect/TranslationEffect.swift @@ -54,23 +54,23 @@ public struct YTranslationEffect: GeometryEffect { extension View { /// Applies offset effect that is animatable against ``SwiftUI/View/offset`` - public func animatableOffset(x: CGFloat) -> some View { + public nonisolated func animatableOffset(x: CGFloat) -> some View { self.modifier(XTranslationEffect(offset: x)) } /// Applies offset effect that is animatable against ``SwiftUI/View/offset`` - public func animatableOffset(y: CGFloat) -> some View { + public nonisolated func animatableOffset(y: CGFloat) -> some View { self.modifier(YTranslationEffect(offset: y)) } /// Applies offset effect that is animatable against ``SwiftUI/View/offset`` - public func animatableOffset(x: CGFloat, y: CGFloat) -> some View { + public nonisolated func animatableOffset(x: CGFloat, y: CGFloat) -> some View { // could not this done with CGSize and AnimatablePair. self.animatableOffset(x: x).animatableOffset(y: y) } /// Applies offset effect that is animatable against ``SwiftUI/View/offset`` - public func animatableOffset(_ offset: CGSize) -> some View { + public nonisolated func animatableOffset(_ offset: CGSize) -> some View { self.animatableOffset(x: offset.width).animatableOffset(y: offset.height) } } diff --git a/Sources/SwiftUISupportSizing/View+MeasureSize.swift b/Sources/SwiftUISupportSizing/View+MeasureSize.swift index 4a18289cfe..15a0e6c589 100644 --- a/Sources/SwiftUISupportSizing/View+MeasureSize.swift +++ b/Sources/SwiftUISupportSizing/View+MeasureSize.swift @@ -1,18 +1,5 @@ import SwiftUI -private struct SizingPreferenceKey: PreferenceKey { - - typealias Value = CGSize - - static var defaultValue: Value = .zero - - static func reduce(value: inout Value, nextValue: () -> Value) { - let next = nextValue() - value = next - } - -} - extension View { /** @@ -20,52 +7,13 @@ extension View { !! Should be deprecated in favor of using directly ``readingGeometry(transform:target:)`` */ + @available(*, deprecated, message: "Use directly onGeometryChange") public func measureSize(_ size: Binding) -> some View { - readingGeometry(transform: \.size, target: size) - } - -} - -private enum GeometryReaderPreferenceKey: PreferenceKey { - - static var defaultValue: Projected? { - return nil - } - - static func reduce(value: inout Value, nextValue: () -> Projected?) { - let next = nextValue() - value = next - } - -} - -extension View { - - public consuming func readingGeometry( - transform: @escaping (GeometryProxy) -> Projected, - target: Binding - ) -> some View { - readingGeometry(transform: transform, onChange: { - target.wrappedValue = $0 + onGeometryChange(for: CGSize.self, of: { proxy in + proxy.size + }, action: { newSize in + size.wrappedValue = newSize }) } - public consuming func readingGeometry( - transform: @escaping (GeometryProxy) -> Projected, - onChange: @escaping (Projected) -> Void - ) -> some View { - background( - Color.clear.background( - GeometryReader(content: { proxy in - Color.clear - .preference(key: GeometryReaderPreferenceKey.self, value: transform(proxy)) - }) - ) - .onPreferenceChange(GeometryReaderPreferenceKey.self) { projected in - guard let projected else { return } - onChange(projected) - } - ) - } - }