From c8a115e1819e9865a4c695577725839e751df1c3 Mon Sep 17 00:00:00 2001 From: bullinnyc Date: Sun, 11 Feb 2024 15:25:13 +0700 Subject: [PATCH] Add support tvOS, watchOS, visionOS. --- .github/workflows/build-and-test.yml | 2 +- Examples/ContentView.swift | 6 ++--- Package.swift | 7 ++++-- README.md | 3 +-- .../Resources/ResourcesManager.swift | 24 +++++++++++++++---- .../Services/CrossPlatformImage.swift | 2 +- .../Views/CachedAsyncImage.swift | 18 ++++++++++++-- 7 files changed, 46 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index e24b217..7bbbca0 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -17,7 +17,7 @@ jobs: macos: ['macos-13'] scheme: ['CachedAsyncImage'] command: ['test'] - platform: ['macOS', 'iOS'] + platform: ['iOS', 'macOS', 'tvOS', 'watchOS'] steps: - name: Switch xcode to ${{ matrix.xcode }} uses: maxim-lobanov/setup-xcode@v1.6.0 diff --git a/Examples/ContentView.swift b/Examples/ContentView.swift index 6724e31..be673a8 100644 --- a/Examples/ContentView.swift +++ b/Examples/ContentView.swift @@ -9,7 +9,7 @@ import SwiftUI import CachedAsyncImage -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) struct ContentView: View { // MARK: - Private Properties @@ -97,7 +97,7 @@ struct ContentView: View { // MARK: - Ext. Configure views -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) extension ContentView { func placeholder(_ progress: String) -> some View { ZStack { @@ -149,7 +149,7 @@ extension ContentView { // MARK: - Preview Provider -@available(iOS 15.0, macOS 12.0, *) +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() diff --git a/Package.swift b/Package.swift index 3b8882a..ae3b587 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.8 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -8,7 +8,10 @@ let package = Package( platforms: [ // Add support for all platforms starting from a specific version. .iOS(.v14), - .macOS(.v11) + .macOS(.v11), + .tvOS(.v14), + .watchOS(.v7), + .visionOS(.v1) ], products: [ // Products define the executables and libraries a package produces, making them visible to other packages. diff --git a/README.md b/README.md index d948d2c..44319e8 100644 --- a/README.md +++ b/README.md @@ -135,8 +135,7 @@ struct MyView: View { ``` ## Requirements -- iOS 14.0 + -- macOS 11.0 + +- iOS 14.0 + / macOS 11.0 + / tvOS 14.0 + / watchOS 7.0 + / visionOS 1.0 + - [SwiftUI](https://developer.apple.com/xcode/swiftui/) ## License diff --git a/Sources/CachedAsyncImage/Resources/ResourcesManager.swift b/Sources/CachedAsyncImage/Resources/ResourcesManager.swift index 8723a34..c298269 100644 --- a/Sources/CachedAsyncImage/Resources/ResourcesManager.swift +++ b/Sources/CachedAsyncImage/Resources/ResourcesManager.swift @@ -9,6 +9,10 @@ #if canImport(UIKit) import UIKit +#if os(watchOS) +import SwiftUI +#endif + /// Resources manager typealias. public typealias RM = ResourcesManager @@ -17,11 +21,7 @@ public final class ResourcesManager { // MARK: - Public Properties /// An object that stores color data. - public static let snow = UIColor( - named: "snow", - in: Bundle.module, - compatibleWith: nil - ) ?? UIColor() + public static let snow = getColor(with: "snow") // MARK: - Public Methods @@ -33,6 +33,20 @@ public final class ResourcesManager { public static func image(_ name: String) -> UIImage? { UIImage(named: name, in: Bundle.module, with: nil) } + + // MARK: - Private Methods + + private static func getColor(with name: String) -> UIColor { + #if os(watchOS) + UIColor(Color(name, bundle: Bundle.module)) + #elseif os(iOS) || os(tvOS) || os(visionOS) + UIColor( + named: name, + in: Bundle.module, + compatibleWith: nil + ) ?? UIColor() + #endif + } } #elseif canImport(AppKit) import AppKit diff --git a/Sources/CachedAsyncImage/Services/CrossPlatformImage.swift b/Sources/CachedAsyncImage/Services/CrossPlatformImage.swift index b48bc56..5131307 100644 --- a/Sources/CachedAsyncImage/Services/CrossPlatformImage.swift +++ b/Sources/CachedAsyncImage/Services/CrossPlatformImage.swift @@ -6,7 +6,7 @@ // Copyright © 2023 Dmitry Kononchuk. All rights reserved. // -#if os(iOS) +#if os(iOS) || os(tvOS) || os(watchOS) || os(visionOS) import UIKit import SwiftUI diff --git a/Sources/CachedAsyncImage/Views/CachedAsyncImage.swift b/Sources/CachedAsyncImage/Views/CachedAsyncImage.swift index 291cc31..d5b4c68 100644 --- a/Sources/CachedAsyncImage/Views/CachedAsyncImage.swift +++ b/Sources/CachedAsyncImage/Views/CachedAsyncImage.swift @@ -124,7 +124,14 @@ struct CachedAsyncImage_Previews: PreviewProvider { .font(.footnote) .multilineTextAlignment(.center) .conditional { view in - if #available(iOS 15.0, macOS 12.0, *) { + if #available( + iOS 15.0, + macOS 12.0, + tvOS 15.0, + watchOS 8.0, + visionOS 1.0, + * + ) { view .foregroundStyle(.red) } else { @@ -146,7 +153,14 @@ struct CachedAsyncImage_Previews: PreviewProvider { label: { Text("Retry") .conditional { view in - if #available(iOS 15.0, macOS 12.0, *) { + if #available( + iOS 15.0, + macOS 12.0, + tvOS 15.0, + watchOS 8.0, + visionOS 1.0, + * + ) { view .foregroundStyle(.black) } else {