From 13edefc0dd60a15971bc011cfc349863e65bfdeb Mon Sep 17 00:00:00 2001 From: Alexander Solovev Date: Thu, 9 Jun 2022 13:05:09 +0200 Subject: [PATCH] Fix Swift PM compatibility. Move Objectve-C files to a separate folder and target. --- Package.swift | 20 +++++++++++++++++-- SKPhotoBrowser.xcodeproj/project.pbxproj | 16 +++++++++++++-- SKPhotoBrowser/SKButtons.swift | 6 +----- SKPhotoBrowser/SKPaginationView.swift | 8 ++------ SKPhotoBrowser/SKPhoto.swift | 3 +++ SKPhotoBrowser/SKPhotoBrowser.h | 3 +++ .../{ => ObjC}/UIImage+animatedGIF.h | 0 .../{ => ObjC}/UIImage+animatedGIF.m | 0 .../extensions/UIImage+BundledImage.swift | 20 +++++++++++++++++++ 9 files changed, 61 insertions(+), 15 deletions(-) rename SKPhotoBrowser/extensions/{ => ObjC}/UIImage+animatedGIF.h (100%) rename SKPhotoBrowser/extensions/{ => ObjC}/UIImage+animatedGIF.m (100%) create mode 100644 SKPhotoBrowser/extensions/UIImage+BundledImage.swift diff --git a/Package.swift b/Package.swift index 32b83c7d..4f8372e4 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.0 +// swift-tools-version:5.3 // // Package.swift // @@ -18,6 +18,22 @@ let package = Package( targets: [ .target( name: "SKPhotoBrowser", - path: "SKPhotoBrowser") + dependencies: ["SKPhotoBrowserObjC"], + path: "SKPhotoBrowser", + exclude: ["Info.plist", + "extensions/ObjC"], + resources: [ + .copy("SKPhotoBrowser.bundle") + ]), + .target( + name: "SKPhotoBrowserObjC", + path: "SKPhotoBrowser/extensions/ObjC", + publicHeadersPath: "."), + .testTarget( + name: "SKPhotoBrowserTests", + dependencies: ["SKPhotoBrowser"], + path: "SKPhotoBrowserTests", + exclude: ["Info.plist"] + ) ] ) diff --git a/SKPhotoBrowser.xcodeproj/project.pbxproj b/SKPhotoBrowser.xcodeproj/project.pbxproj index 4e725aa5..d53a5635 100644 --- a/SKPhotoBrowser.xcodeproj/project.pbxproj +++ b/SKPhotoBrowser.xcodeproj/project.pbxproj @@ -34,6 +34,7 @@ 9521CC65232F677A00446D11 /* UIView+Radius.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9521CC62232F677900446D11 /* UIView+Radius.swift */; }; 9521CC6B232F693800446D11 /* UIImage+animatedGIF.h in Headers */ = {isa = PBXBuildFile; fileRef = 9521CC69232F693800446D11 /* UIImage+animatedGIF.h */; settings = {ATTRIBUTES = (Public, ); }; }; 9521CC6C232F693800446D11 /* UIImage+animatedGIF.m in Sources */ = {isa = PBXBuildFile; fileRef = 9521CC6A232F693800446D11 /* UIImage+animatedGIF.m */; }; + 9CF727E1285208180043487A /* UIImage+BundledImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CF727E0285208180043487A /* UIImage+BundledImage.swift */; }; A64B89361CB04222000071B9 /* SKPhotoBrowserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A64B89351CB04222000071B9 /* SKPhotoBrowserTests.swift */; }; A64B89381CB04222000071B9 /* SKPhotoBrowser.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8909B5301BC791280060A053 /* SKPhotoBrowser.framework */; }; /* End PBXBuildFile section */ @@ -78,6 +79,7 @@ 9521CC62232F677900446D11 /* UIView+Radius.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Radius.swift"; sourceTree = ""; }; 9521CC69232F693800446D11 /* UIImage+animatedGIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+animatedGIF.h"; sourceTree = ""; }; 9521CC6A232F693800446D11 /* UIImage+animatedGIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+animatedGIF.m"; sourceTree = ""; }; + 9CF727E0285208180043487A /* UIImage+BundledImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+BundledImage.swift"; sourceTree = ""; }; A64B89331CB04222000071B9 /* SKPhotoBrowserTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SKPhotoBrowserTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; A64B89351CB04222000071B9 /* SKPhotoBrowserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SKPhotoBrowserTests.swift; sourceTree = ""; }; A64B89371CB04222000071B9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -153,15 +155,24 @@ 9521CC5F232F677900446D11 /* extensions */ = { isa = PBXGroup; children = ( - 9521CC69232F693800446D11 /* UIImage+animatedGIF.h */, - 9521CC6A232F693800446D11 /* UIImage+animatedGIF.m */, + 9CF727DF285208020043487A /* ObjC */, 9521CC60232F677900446D11 /* UIApplication+UIWindow.swift */, + 9CF727E0285208180043487A /* UIImage+BundledImage.swift */, 9521CC61232F677900446D11 /* UIImage+Rotation.swift */, 9521CC62232F677900446D11 /* UIView+Radius.swift */, ); path = extensions; sourceTree = ""; }; + 9CF727DF285208020043487A /* ObjC */ = { + isa = PBXGroup; + children = ( + 9521CC69232F693800446D11 /* UIImage+animatedGIF.h */, + 9521CC6A232F693800446D11 /* UIImage+animatedGIF.m */, + ); + path = ObjC; + sourceTree = ""; + }; A64B89341CB04222000071B9 /* SKPhotoBrowserTests */ = { isa = PBXGroup; children = ( @@ -321,6 +332,7 @@ 8909B5461BC791510060A053 /* SKIndicatorView.swift in Sources */, 8917B1B41D5A14B0000CE1C4 /* SKButtons.swift in Sources */, 89C24A821D657AD1005F09A9 /* SKPhotoBrowserOptions.swift in Sources */, + 9CF727E1285208180043487A /* UIImage+BundledImage.swift in Sources */, 26C97AD51D0EB6870039F6CB /* SKCache.swift in Sources */, 8909B5431BC791510060A053 /* SKCaptionView.swift in Sources */, 8909B5491BC791510060A053 /* SKPhotoBrowser.swift in Sources */, diff --git a/SKPhotoBrowser/SKButtons.swift b/SKPhotoBrowser/SKButtons.swift index 13ccac0b..e6de5b8e 100644 --- a/SKPhotoBrowser/SKButtons.swift +++ b/SKPhotoBrowser/SKButtons.swift @@ -8,9 +8,6 @@ import UIKit -// helpers which often used -private let bundle = Bundle(for: SKPhotoBrowser.self) - class SKButton: UIButton { internal var showFrame: CGRect! internal var hideFrame: CGRect! @@ -33,8 +30,7 @@ class SKButton: UIButton { translatesAutoresizingMaskIntoConstraints = true autoresizingMask = [.flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin] - let image = UIImage(named: "SKPhotoBrowser.bundle/images/\(imageName)", in: bundle, compatibleWith: nil) ?? UIImage() - setImage(image, for: .normal) + setImage(UIImage.bundledImage(named: imageName), for: .normal) } func setFrameSize(_ size: CGSize? = nil) { diff --git a/SKPhotoBrowser/SKPaginationView.swift b/SKPhotoBrowser/SKPaginationView.swift index 8398ee85..0c50a2d8 100644 --- a/SKPhotoBrowser/SKPaginationView.swift +++ b/SKPhotoBrowser/SKPaginationView.swift @@ -8,8 +8,6 @@ import UIKit -private let bundle = Bundle(for: SKPhotoBrowser.self) - class SKPaginationView: UIView { var counterLabel: UILabel? var prevButton: UIButton? @@ -142,10 +140,8 @@ class SKPaginationButton: UIButton { .flexibleRightMargin, .flexibleTopMargin] contentMode = .center - - let image = UIImage(named: "SKPhotoBrowser.bundle/images/\(imageName)", - in: bundle, compatibleWith: nil) ?? UIImage() - setImage(image, for: .normal) + + setImage(UIImage.bundledImage(named: imageName), for: .normal) } } diff --git a/SKPhotoBrowser/SKPhoto.swift b/SKPhotoBrowser/SKPhoto.swift index f880a09a..874ef9e8 100644 --- a/SKPhotoBrowser/SKPhoto.swift +++ b/SKPhotoBrowser/SKPhoto.swift @@ -7,6 +7,9 @@ // import UIKit +#if canImport(SKPhotoBrowserObjC) +import SKPhotoBrowserObjC +#endif @objc public protocol SKPhotoProtocol: NSObjectProtocol { var index: Int { get set } diff --git a/SKPhotoBrowser/SKPhotoBrowser.h b/SKPhotoBrowser/SKPhotoBrowser.h index 3822bc38..6bdaea5a 100644 --- a/SKPhotoBrowser/SKPhotoBrowser.h +++ b/SKPhotoBrowser/SKPhotoBrowser.h @@ -7,7 +7,10 @@ // #import + +#if __has_include("UIImage+animatedGIF.h") #import "UIImage+animatedGIF.h" +#endif //! Project version number for SKPhotoBrowser. FOUNDATION_EXPORT double SKPhotoBrowserVersionNumber; diff --git a/SKPhotoBrowser/extensions/UIImage+animatedGIF.h b/SKPhotoBrowser/extensions/ObjC/UIImage+animatedGIF.h similarity index 100% rename from SKPhotoBrowser/extensions/UIImage+animatedGIF.h rename to SKPhotoBrowser/extensions/ObjC/UIImage+animatedGIF.h diff --git a/SKPhotoBrowser/extensions/UIImage+animatedGIF.m b/SKPhotoBrowser/extensions/ObjC/UIImage+animatedGIF.m similarity index 100% rename from SKPhotoBrowser/extensions/UIImage+animatedGIF.m rename to SKPhotoBrowser/extensions/ObjC/UIImage+animatedGIF.m diff --git a/SKPhotoBrowser/extensions/UIImage+BundledImage.swift b/SKPhotoBrowser/extensions/UIImage+BundledImage.swift new file mode 100644 index 00000000..1888c7a4 --- /dev/null +++ b/SKPhotoBrowser/extensions/UIImage+BundledImage.swift @@ -0,0 +1,20 @@ +// +// UIImage+BundledImage.swift +// +// +// Created by Aleksandr Solovev on 09.06.2022. +// + +import UIKit + +extension UIImage { + static func bundledImage(named imageName: String) -> UIImage { + let imagePath = "SKPhotoBrowser.bundle/images/\(imageName)" +#if SWIFT_PACKAGE + return UIImage(named: imagePath, in: .module, compatibleWith: nil) ?? UIImage() +#else + let bundle = Bundle(for: SKPhotoBrowser.self) + return UIImage(named: imagePath, in: bundle, compatibleWith: nil) ?? UIImage() +#endif + } +}