Skip to content

Commit

Permalink
Cleanup (#52)
Browse files Browse the repository at this point in the history
- readingGeometry uses onGeometryChanged internally
  • Loading branch information
muukii authored Dec 14, 2024
1 parent ccfe585 commit 10b463f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 130 deletions.
7 changes: 4 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// 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

let package = Package(
name: "SwiftUISupport",
platforms: [
.iOS(.v15),
.iOS(.v16),
.macOS(.v10_15),
.watchOS(.v6),
.tvOS(.v13),
Expand Down Expand Up @@ -41,5 +41,6 @@ let package = Package(
name: "SwiftUISupportTests",
dependencies: ["SwiftUISupport"]
),
]
],
swiftLanguageModes: [.v6]
)
65 changes: 0 additions & 65 deletions Sources/SwiftUISupport/Components/HostingEdge.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/SwiftUISupport/Components/ObservedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct AsyncObservedView<Object: ObservableObject, Content: View>: 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,
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftUISupportGeometryEffect/TranslationEffect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
62 changes: 5 additions & 57 deletions Sources/SwiftUISupportSizing/View+MeasureSize.swift
Original file line number Diff line number Diff line change
@@ -1,71 +1,19 @@
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 {

/**
Measures the receiver view size using GeometryReader.

!! Should be deprecated in favor of using directly ``readingGeometry(transform:target:)``
*/
@available(*, deprecated, message: "Use directly onGeometryChange")
public func measureSize(_ size: Binding<CGSize>) -> some View {
readingGeometry(transform: \.size, target: size)
}

}

private enum GeometryReaderPreferenceKey<Projected: Equatable>: 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<Projected: Equatable>(
transform: @escaping (GeometryProxy) -> Projected,
target: Binding<Projected>
) -> 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<Projected: Equatable>(
transform: @escaping (GeometryProxy) -> Projected,
onChange: @escaping (Projected) -> Void
) -> some View {
background(
Color.clear.background(
GeometryReader(content: { proxy in
Color.clear
.preference(key: GeometryReaderPreferenceKey<Projected>.self, value: transform(proxy))
})
)
.onPreferenceChange(GeometryReaderPreferenceKey<Projected>.self) { projected in
guard let projected else { return }
onChange(projected)
}
)
}

}

0 comments on commit 10b463f

Please sign in to comment.