From da43446cbb3c49a4c0ce6d87d18f3c7d67be2f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Olejni=CC=81k?= Date: Thu, 28 Nov 2024 14:16:49 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20SwiftUI.View+ReadSize?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 6 ++---- .github/workflows/docbuild.yml | 4 +--- .github/workflows/tests.yml | 8 +++---- ACKategories.xcodeproj/project.pbxproj | 4 ++++ CHANGELOG.md | 2 ++ .../SwiftUIExtensions/View+ReadSize.swift | 21 +++++++++++++++++++ 6 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 Sources/ACKategories/SwiftUIExtensions/View+ReadSize.swift diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 337ef6b..8651187 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,10 +9,9 @@ on: jobs: carthage: name: Carthage - runs-on: macos-latest + runs-on: macos-15 steps: - uses: actions/checkout@v4 - - uses: AckeeCZ/load-xcode-version@v1 - name: Build run: carthage build --no-skip-current --cache-builds --use-xcframeworks - uses: actions/cache@v3 @@ -23,10 +22,9 @@ jobs: ${{ runner.os }}-carthage- spm: name: SPM - runs-on: macos-latest + runs-on: macos-15 steps: - uses: actions/checkout@v4 - - uses: AckeeCZ/load-xcode-version@v1 - name: Build run: swift build -c release - uses: actions/cache@v3 diff --git a/.github/workflows/docbuild.yml b/.github/workflows/docbuild.yml index 786afcc..e0567f9 100644 --- a/.github/workflows/docbuild.yml +++ b/.github/workflows/docbuild.yml @@ -21,12 +21,10 @@ jobs: # Must be set to this for deploying to GitHub Pages name: github-pages url: ${{ steps.deployment.outputs.page_url }} - runs-on: macos-latest + runs-on: macos-15 steps: - name: Checkout 🛎️ uses: actions/checkout@v4 - - name: Check Xcode version ✅ - uses: AckeeCZ/load-xcode-version@v1 - name: Build DocC run: | xcodebuild docbuild -scheme ACKategories \ diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 546a951..6f5f25e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -5,12 +5,11 @@ on: [workflow_call] jobs: xcodebuild: name: Xcodebuild - runs-on: macos-latest + runs-on: macos-15 env: - IOS_DEVICE: iPhone 15 Pro Max + IOS_DEVICE: iPhone 16 Pro steps: - uses: actions/checkout@v4 - - uses: AckeeCZ/load-xcode-version@v1 - name: iOS tests run: set -o pipefail && xcodebuild test -scheme ACKategories -resultBundlePath Tests-iOS.xcresult -sdk iphonesimulator -destination "platform=iOS Simulator,name=$IOS_DEVICE,OS=latest" ONLY_ACTIVE_ARCH=YES | xcpretty - uses: actions/upload-artifact@v4 @@ -48,10 +47,9 @@ jobs: path: Tests-tvOS.xcresult spm: name: SPM - runs-on: macos-latest + runs-on: macos-15 steps: - uses: actions/checkout@v4 - - uses: AckeeCZ/load-xcode-version@v1 - name: SPM build run: swift build - name: SPM tests diff --git a/ACKategories.xcodeproj/project.pbxproj b/ACKategories.xcodeproj/project.pbxproj index 3a51c83..c92b505 100644 --- a/ACKategories.xcodeproj/project.pbxproj +++ b/ACKategories.xcodeproj/project.pbxproj @@ -117,6 +117,7 @@ 69ACD71B2AFD13480021127B /* DateRandomTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69E0A6B12AFD114600C8E8D9 /* DateRandomTests.swift */; }; 69ACD71C2AFD13480021127B /* IntRandomTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69E0A6B22AFD114600C8E8D9 /* IntRandomTests.swift */; }; 69ACD71D2AFD13480021127B /* StringRandomTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69E0A6B32AFD114600C8E8D9 /* StringRandomTests.swift */; }; + 69BC15952CF8A42C00A4B55F /* View+ReadSize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69BC15942CF8A42700A4B55F /* View+ReadSize.swift */; }; 69C6BE902B0BC80E008A4ECF /* UIWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69E0A6A82AFD114600C8E8D9 /* UIWindow.swift */; }; 69DF227B2B459D0D0025C555 /* PushManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69DF22792B459D0D0025C555 /* PushManager.swift */; }; 69DF227C2B459D0D0025C555 /* UNNotificationSettingsExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69DF227A2B459D0D0025C555 /* UNNotificationSettingsExtensions.swift */; }; @@ -266,6 +267,7 @@ 69ACC1812BBDF8DC00A109DB /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = ""; }; 69ACD6C22AFD130C0021127B /* ACKategories.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ACKategories.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 69ACD6C92AFD130D0021127B /* ACKategoriesTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ACKategoriesTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 69BC15942CF8A42700A4B55F /* View+ReadSize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+ReadSize.swift"; sourceTree = ""; }; 69DF225A2B459CC50025C555 /* PushNotifications.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PushNotifications.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 69DF22792B459D0D0025C555 /* PushManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PushManager.swift; sourceTree = ""; }; 69DF227A2B459D0D0025C555 /* UNNotificationSettingsExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UNNotificationSettingsExtensions.swift; sourceTree = ""; }; @@ -651,6 +653,7 @@ 69E0A6032AFD10BE00C8E8D9 /* SwiftUIExtensions */ = { isa = PBXGroup; children = ( + 69BC15942CF8A42700A4B55F /* View+ReadSize.swift */, 69246CFA2BDFE77400AB31A1 /* SwiftUIColorsTheme.swift */, 69E0A6042AFD10BE00C8E8D9 /* FontModifier.swift */, 693B39B62BF2359B00DF7C5E /* ACKHostingController.swift */, @@ -1292,6 +1295,7 @@ 69ACD6F12AFD133A0021127B /* UIControlEvents.swift in Sources */, 69ACD6F22AFD133A0021127B /* UIDeviceExtensions.swift in Sources */, 69ACD6F32AFD133A0021127B /* UIImageExtensions.swift in Sources */, + 69BC15952CF8A42C00A4B55F /* View+ReadSize.swift in Sources */, 69ACD6F42AFD133A0021127B /* UILabelExtensions.swift in Sources */, 69ACD6F52AFD133A0021127B /* UINavigationControllerExtensions.swift in Sources */, 69ACD6F62AFD133A0021127B /* UISearchBarExtensions.swift in Sources */, diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e1fa2a..300ffbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ## Next +- Add `View+ReadSize` for reading a view size in SwiftUI ([#150](https://github.com/AckeeCZ/ACKategories/pull/150), kudos to @olejnjak) + ## 6.15.0 - SwiftUI improvements ([#149](https://github.com/AckeeCZ/ACKategories/pull/149), kudos to @olejnjak) diff --git a/Sources/ACKategories/SwiftUIExtensions/View+ReadSize.swift b/Sources/ACKategories/SwiftUIExtensions/View+ReadSize.swift new file mode 100644 index 0000000..93bfff3 --- /dev/null +++ b/Sources/ACKategories/SwiftUIExtensions/View+ReadSize.swift @@ -0,0 +1,21 @@ +import SwiftUI + +@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *) +public extension View { + func readSize( + onChange: @escaping (CGSize) -> Void + ) -> some View { + background( + GeometryReader { geometryProxy in + Color.clear + .preference(key: SizePreferenceKey.self, value: geometryProxy.size) + } + ) + .onPreferenceChange(SizePreferenceKey.self, perform: onChange) + } +} + +private struct SizePreferenceKey: PreferenceKey { + static var defaultValue: CGSize = .zero + static func reduce(value: inout CGSize, nextValue: () -> CGSize) { } +}