From 6a8219f5e33948b325176582d75d28bc95b52b74 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Mon, 1 Apr 2024 18:48:12 +0900 Subject: [PATCH 01/18] remove unused files --- Sources/SDCALayer/Extension/NSUIColor+.swift | 38 -------------------- Sources/SDCALayer/Util/typealias.swift | 20 ----------- 2 files changed, 58 deletions(-) delete mode 100644 Sources/SDCALayer/Extension/NSUIColor+.swift delete mode 100644 Sources/SDCALayer/Util/typealias.swift diff --git a/Sources/SDCALayer/Extension/NSUIColor+.swift b/Sources/SDCALayer/Extension/NSUIColor+.swift deleted file mode 100644 index b519381..0000000 --- a/Sources/SDCALayer/Extension/NSUIColor+.swift +++ /dev/null @@ -1,38 +0,0 @@ -// -// NSUIColor+.swift -// -// -// Created by p-x9 on 2022/11/23. -// -// - -import Foundation - -extension NSUIColor { - /// rgb color code - var rgbString: String { - var red: CGFloat = -1 - var blue: CGFloat = -1 - var green: CGFloat = -1 - getRed(&red, green: &green, blue: &blue, alpha: nil) - let rgb: [CGFloat] = [red, green, blue] - return rgb.reduce(into: "") { res, value in - let intval = Int(round(value * 255)) - res += (NSString(format: "%02X", intval) as String) - } - } - - /// rgba color code - var rgbaString: String { - var red: CGFloat = -1 - var blue: CGFloat = -1 - var green: CGFloat = -1 - var alpha: CGFloat = 1 - getRed(&red, green: &green, blue: &blue, alpha: &alpha) - let rgba: [CGFloat] = [red, green, blue, alpha] - return rgba.reduce(into: "") { res, value in - let intval = Int(round(value * 255)) - res += (NSString(format: "%02X", intval) as String) - } - } -} diff --git a/Sources/SDCALayer/Util/typealias.swift b/Sources/SDCALayer/Util/typealias.swift deleted file mode 100644 index 7047c88..0000000 --- a/Sources/SDCALayer/Util/typealias.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// typealias.swift -// -// -// Created by p-x9 on 2022/11/14. -// -// - -#if canImport(UIKit) -import UIKit -typealias NSUIFont = UIFont -typealias NSUIColor = UIColor -#endif - -#if canImport(Cocoa) -import Cocoa -typealias NSUIFont = NSFont -typealias NSUIColor = NSColor -#endif - From 7ac097b9ad190814280fe733b6cf6c0a6abee5c0 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Mon, 1 Apr 2024 18:53:06 +0900 Subject: [PATCH 02/18] format --- .../calayer-ws/calayer-ws/AppDelegate.swift | 4 -- .../calayer-ws/calayer-ws/SceneDelegate.swift | 3 -- Package.swift | 4 +- .../SDCALayer/Extension/CATransform3D+.swift | 5 ++- Sources/SDCALayer/Extension/Codable+.swift | 2 +- Sources/SDCALayer/Model/JCGPath.swift | 1 - .../Model/Layer/JCAGradientLayer.swift | 4 +- Sources/SDCALayer/Model/Layer/JCALayer.swift | 1 - Sources/SDCALayer/Util/PropertyMap.swift | 37 ++++++++++++++----- 9 files changed, 36 insertions(+), 25 deletions(-) diff --git a/Example/calayer-ws/calayer-ws/AppDelegate.swift b/Example/calayer-ws/calayer-ws/AppDelegate.swift index f012c21..3f7a715 100644 --- a/Example/calayer-ws/calayer-ws/AppDelegate.swift +++ b/Example/calayer-ws/calayer-ws/AppDelegate.swift @@ -11,8 +11,6 @@ import UIKit @main class AppDelegate: UIResponder, UIApplicationDelegate { - - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true @@ -32,6 +30,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // Use this method to release any resources that were specific to the discarded scenes, as they will not return. } - } - diff --git a/Example/calayer-ws/calayer-ws/SceneDelegate.swift b/Example/calayer-ws/calayer-ws/SceneDelegate.swift index ee61af4..040ff91 100644 --- a/Example/calayer-ws/calayer-ws/SceneDelegate.swift +++ b/Example/calayer-ws/calayer-ws/SceneDelegate.swift @@ -12,7 +12,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? - func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. @@ -48,6 +47,4 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { // to restore the scene back to its current state. } - } - diff --git a/Package.swift b/Package.swift index 48b30f1..b5cb044 100644 --- a/Package.swift +++ b/Package.swift @@ -12,7 +12,7 @@ let package = Package( .library( name: "SDCALayer", targets: ["SDCALayer"] - ), + ) ], dependencies: [ .package(url: "https://github.com/p-x9/IndirectlyCodable.git", .upToNextMinor(from: "0.0.1")), @@ -31,6 +31,6 @@ let package = Package( .testTarget( name: "SDCALayerTests", dependencies: ["SDCALayer"] - ), + ) ] ) diff --git a/Sources/SDCALayer/Extension/CATransform3D+.swift b/Sources/SDCALayer/Extension/CATransform3D+.swift index 218e7fc..3420203 100644 --- a/Sources/SDCALayer/Extension/CATransform3D+.swift +++ b/Sources/SDCALayer/Extension/CATransform3D+.swift @@ -10,7 +10,10 @@ import QuartzCore extension CATransform3D: Codable { private enum CodingKeys: String, CodingKey { - case m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44 + case m11, m12, m13, m14 + case m21, m22, m23, m24 + case m31, m32, m33, m34 + case m41, m42, m43, m44 } public func encode(to encoder: Encoder) throws { diff --git a/Sources/SDCALayer/Extension/Codable+.swift b/Sources/SDCALayer/Extension/Codable+.swift index 08a3bbb..2b4db54 100644 --- a/Sources/SDCALayer/Extension/Codable+.swift +++ b/Sources/SDCALayer/Extension/Codable+.swift @@ -22,7 +22,7 @@ extension Encodable { return encoder } - var jsonString: String? { + var jsonString: String? { guard let data = try? Self.jsonEncoder.encode(self) else { return nil } diff --git a/Sources/SDCALayer/Model/JCGPath.swift b/Sources/SDCALayer/Model/JCGPath.swift index 4705c0e..66444a8 100644 --- a/Sources/SDCALayer/Model/JCGPath.swift +++ b/Sources/SDCALayer/Model/JCGPath.swift @@ -57,7 +57,6 @@ public class JCGPath: ObjectConvertiblyCodable { } } - public struct JCGPathElement: Codable { public var type: String public var points: [CGPoint] diff --git a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift index 5769453..9d71113 100644 --- a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift @@ -1,9 +1,9 @@ // // JCAGradientLayer.swift -// +// // // Created by p-x9 on 2022/11/11. -// +// // import Foundation diff --git a/Sources/SDCALayer/Model/Layer/JCALayer.swift b/Sources/SDCALayer/Model/Layer/JCALayer.swift index 4df270b..121eef1 100644 --- a/Sources/SDCALayer/Model/Layer/JCALayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCALayer.swift @@ -195,7 +195,6 @@ public class JCALayerCornerCurve: ObjectConvertiblyCodable { } } - public class JCACornerMask: ObjectConvertiblyCodable { public typealias Target = CACornerMask diff --git a/Sources/SDCALayer/Util/PropertyMap.swift b/Sources/SDCALayer/Util/PropertyMap.swift index 5372b37..e575c33 100644 --- a/Sources/SDCALayer/Util/PropertyMap.swift +++ b/Sources/SDCALayer/Util/PropertyMap.swift @@ -1,23 +1,28 @@ // // PropertyMap.swift -// +// // // Created by p-x9 on 2022/11/21. -// +// // import Foundation import IndirectlyCodable import KeyPathValue -public typealias PropertyMap = Dictionary, ReferenceWritableKeyPathValueApplier> - +public typealias PropertyMap = [PartialKeyPath: ReferenceWritableKeyPathValueApplier] extension PropertyMap { /// ObjectConvertiblyCodable -> IndirectlyCodable /// (Codable -> IndirectlyCodable) - public func apply(to target: Target, from object: Object) where Key: PartialKeyPath, Value == ReferenceWritableKeyPathValueApplier, Target: IndirectlyCodable, Object: ObjectConvertiblyCodable { - + public func apply( + to target: Target, + from object: Object + ) where Key: PartialKeyPath, + Value == ReferenceWritableKeyPathValueApplier, + Target: IndirectlyCodable, + Object: ObjectConvertiblyCodable + { self.forEach { keyPath, applier in var value = object[keyPath: keyPath] switch value { @@ -40,8 +45,14 @@ extension PropertyMap { /// IndirectlyCodable -> ObjectConvertiblyCodable /// (IndirectlyCodable -> Codable) @_disfavoredOverload - public func apply(to target: Target, from object: Object) where Key: PartialKeyPath, Value == ReferenceWritableKeyPathValueApplier, Target: ObjectConvertiblyCodable, Object: IndirectlyCodable { - + public func apply( + to target: Target, + from object: Object + ) where Key: PartialKeyPath, + Value == ReferenceWritableKeyPathValueApplier, + Target: ObjectConvertiblyCodable, + Object: IndirectlyCodable + { self.forEach { keyPath, applier in var value = object[keyPath: keyPath] switch value { @@ -64,8 +75,14 @@ import QuartzCore extension PropertyMap { /// CALayer -> CALayerConvertible /// (CaLayer -> Codable) - public func apply(to target: Target, from object: Object) where Key: PartialKeyPath, Value == ReferenceWritableKeyPathValueApplier, Target: ObjectConvertiblyCodable, Object: CALayer & IndirectlyCodable { - + public func apply( + to target: Target, + from object: Object + ) where Key: PartialKeyPath, + Value == ReferenceWritableKeyPathValueApplier, + Target: ObjectConvertiblyCodable, + Object: CALayer & IndirectlyCodable + { self.forEach { keyPath, applier in var value = object[keyPath: keyPath] From 708b4b0230d2001d67e6003aa32559fecb96dc0f Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Mon, 1 Apr 2024 23:28:18 +0900 Subject: [PATCH 03/18] bump `KeyPathValue` to 0.1.0 --- Package.resolved | 4 ++-- Package.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Package.resolved b/Package.resolved index 0be895a..1ef24ed 100644 --- a/Package.resolved +++ b/Package.resolved @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/p-x9/KeyPathValue.git", "state" : { - "revision" : "6aeb41d6c5564ae1f18e8b7a4bc5733d739558e3", - "version" : "0.0.1" + "revision" : "8938b8f8d74a70c6b2d6a94b02e7d5753eb001db", + "version" : "0.1.0" } }, { diff --git a/Package.swift b/Package.swift index b5cb044..abad7fb 100644 --- a/Package.swift +++ b/Package.swift @@ -16,7 +16,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/p-x9/IndirectlyCodable.git", .upToNextMinor(from: "0.0.1")), - .package(url: "https://github.com/p-x9/KeyPathValue.git", .upToNextMinor(from: "0.0.1")), + .package(url: "https://github.com/p-x9/KeyPathValue.git", from: "0.1.0"), .package(url: "https://github.com/jpsim/Yams.git", from: "5.0.1") ], targets: [ From 192e48d1f44c80a5925fafc733704568969cdccd Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:42:06 +0900 Subject: [PATCH 04/18] Bump `IndirectlyCodable` to 0.1.0 --- Package.resolved | 4 ++-- Package.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Package.resolved b/Package.resolved index 1ef24ed..a8f8424 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/p-x9/IndirectlyCodable.git", "state" : { - "revision" : "81bfc339dde53a3739f82517dbef0e4cac1d9cc3", - "version" : "0.0.1" + "revision" : "548af634dc3277a5448ea9801b206c0037185f1e", + "version" : "0.1.0" } }, { diff --git a/Package.swift b/Package.swift index abad7fb..b20c19c 100644 --- a/Package.swift +++ b/Package.swift @@ -15,7 +15,7 @@ let package = Package( ) ], dependencies: [ - .package(url: "https://github.com/p-x9/IndirectlyCodable.git", .upToNextMinor(from: "0.0.1")), + .package(url: "https://github.com/p-x9/IndirectlyCodable.git", from: "0.1.0"), .package(url: "https://github.com/p-x9/KeyPathValue.git", from: "0.1.0"), .package(url: "https://github.com/jpsim/Yams.git", from: "5.0.1") ], From 4a2032b67df659930a4f34cd736ad7982d568a9b Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 2 Apr 2024 15:05:38 +0900 Subject: [PATCH 05/18] Implemented `PropertyMap` --- Sources/SDCALayer/Util/PropertyMap.swift | 149 ++++++++++++++++------- 1 file changed, 103 insertions(+), 46 deletions(-) diff --git a/Sources/SDCALayer/Util/PropertyMap.swift b/Sources/SDCALayer/Util/PropertyMap.swift index e575c33..af2b97a 100644 --- a/Sources/SDCALayer/Util/PropertyMap.swift +++ b/Sources/SDCALayer/Util/PropertyMap.swift @@ -10,34 +10,73 @@ import Foundation import IndirectlyCodable import KeyPathValue -public typealias PropertyMap = [PartialKeyPath: ReferenceWritableKeyPathValueApplier] +public struct PropertyMap { + public typealias Map = [PartialKeyPath: ReferenceWritableKeyPathValueApplier] + + public typealias ForwardMap = Map + public typealias ReverseMap = Map + + public let forwardMap: ForwardMap + public let reverseMap: ReverseMap +} extension PropertyMap { - /// ObjectConvertiblyCodable -> IndirectlyCodable - /// (Codable -> IndirectlyCodable) - public func apply( - to target: Target, - from object: Object - ) where Key: PartialKeyPath, - Value == ReferenceWritableKeyPathValueApplier, - Target: IndirectlyCodable, - Object: ObjectConvertiblyCodable - { - self.forEach { keyPath, applier in - var value = object[keyPath: keyPath] - switch value { - case let v as (any ObjectConvertiblyCodable): - guard let codable = v.converted() else { return } - value = codable + public struct MappingElement { + public typealias MapElement = (PartialKeyPath, ReferenceWritableKeyPathValueApplier) - case let v as [any ObjectConvertiblyCodable]: - value = v.compactMap { $0.converted() } + public typealias ForwardMapElement = MapElement + public typealias ReverseMapElement = MapElement - default: - break - } - applier.apply(value, target) + public let forwardMapElement: ForwardMapElement + public let reverseMapElement: ReverseMapElement + + public init( + _ sourceKeyPath: ReferenceWritableKeyPath, + _ destinationKeyPath: ReferenceWritableKeyPath + ) { + forwardMapElement = (sourceKeyPath, .init(destinationKeyPath)) + reverseMapElement = (destinationKeyPath, .init(sourceKeyPath)) } + + public init( + _ sourceKeyPath: ReferenceWritableKeyPath, + _ destinationKeyPath: ReferenceWritableKeyPath + ) { + forwardMapElement = (sourceKeyPath, .init(destinationKeyPath)) + reverseMapElement = (destinationKeyPath, .init(sourceKeyPath)) + } + } + + public init(_ elements: [MappingElement]) { + forwardMap = Dictionary(uniqueKeysWithValues: elements.map(\.forwardMapElement)) + reverseMap = Dictionary(uniqueKeysWithValues: elements.map(\.reverseMapElement)) + } +} + +extension PropertyMap.MappingElement { + public init( + _ sourceKeyPath: ReferenceWritableKeyPath, + _ destinationKeyPath: ReferenceWritableKeyPath + ) where SourceValue: IndirectlyCodable, DestinationValue: IndirectlyCodableModel { + forwardMapElement = (sourceKeyPath, .init(destinationKeyPath)) + reverseMapElement = (destinationKeyPath, .init(sourceKeyPath)) + } + + public init( + _ sourceKeyPath: ReferenceWritableKeyPath, + _ destinationKeyPath: ReferenceWritableKeyPath + ) where SourceValue: Sequence, DestinationValue: Sequence, SourceValue.Element: IndirectlyCodable, DestinationValue.Element: IndirectlyCodableModel { + forwardMapElement = (sourceKeyPath, .init(destinationKeyPath)) + reverseMapElement = (destinationKeyPath, .init(sourceKeyPath)) + } + + + public init( + _ sourceKeyPath: ReferenceWritableKeyPath, + _ destinationKeyPath: ReferenceWritableKeyPath + ) { + forwardMapElement = (sourceKeyPath, .init(destinationKeyPath)) + reverseMapElement = (destinationKeyPath, .init(sourceKeyPath)) } } @@ -45,16 +84,12 @@ extension PropertyMap { /// IndirectlyCodable -> ObjectConvertiblyCodable /// (IndirectlyCodable -> Codable) @_disfavoredOverload - public func apply( - to target: Target, - from object: Object - ) where Key: PartialKeyPath, - Value == ReferenceWritableKeyPathValueApplier, - Target: ObjectConvertiblyCodable, - Object: IndirectlyCodable - { - self.forEach { keyPath, applier in - var value = object[keyPath: keyPath] + public func apply( + to target: Destination, + from source: Source + ) { + forwardMap.forEach { keyPath, applier in + var value = source[keyPath: keyPath] switch value { case let v as (any IndirectlyCodable): guard let codable = v.codable() else { return } @@ -66,25 +101,47 @@ extension PropertyMap { default: break } - applier.apply(value, target) + applier.apply(value, to: target) + } + } +} + +extension PropertyMap { + /// ObjectConvertiblyCodable -> IndirectlyCodable + /// (Codable -> IndirectlyCodable) + public func apply( + to source: Source, + from destination: Destination + ) { + reverseMap.forEach { keyPath, applier in + var value = destination[keyPath: keyPath] + switch value { + case let v as (any IndirectlyCodableModel): + guard let codable = v.converted() else { return } + value = codable + + case let v as [any IndirectlyCodableModel]: + value = v.compactMap { $0.converted() } + + default: + break + } + applier.apply(value, to: source) } } } import QuartzCore + extension PropertyMap { /// CALayer -> CALayerConvertible /// (CaLayer -> Codable) - public func apply( - to target: Target, - from object: Object - ) where Key: PartialKeyPath, - Value == ReferenceWritableKeyPathValueApplier, - Target: ObjectConvertiblyCodable, - Object: CALayer & IndirectlyCodable - { - self.forEach { keyPath, applier in - var value = object[keyPath: keyPath] + public func apply( + to target: Destination, + from source: Source + ) where Source: CALayer & IndirectlyCodable { + forwardMap.forEach { keyPath, applier in + var value = source[keyPath: keyPath] switch value { case let v as (any IndirectlyCodable): @@ -99,8 +156,8 @@ extension PropertyMap { } if let keyPath = keyPath._kvcKeyPathString, - object.shouldArchiveValue(forKey: keyPath) { - applier.apply(value, target) + source.shouldArchiveValue(forKey: keyPath) { + applier.apply(value, to: target) } } } From fb1cea12607b5944fb6542b19f83e0b0dd31e2eb Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 2 Apr 2024 15:06:26 +0900 Subject: [PATCH 06/18] Update Package.resolved --- .../xcshareddata/swiftpm/Package.resolved | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Example/calayer-ws/calayer-ws.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/calayer-ws/calayer-ws.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 0be895a..a8f8424 100644 --- a/Example/calayer-ws/calayer-ws.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/calayer-ws/calayer-ws.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/p-x9/IndirectlyCodable.git", "state" : { - "revision" : "81bfc339dde53a3739f82517dbef0e4cac1d9cc3", - "version" : "0.0.1" + "revision" : "548af634dc3277a5448ea9801b206c0037185f1e", + "version" : "0.1.0" } }, { @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/p-x9/KeyPathValue.git", "state" : { - "revision" : "6aeb41d6c5564ae1f18e8b7a4bc5733d739558e3", - "version" : "0.0.1" + "revision" : "8938b8f8d74a70c6b2d6a94b02e7d5753eb001db", + "version" : "0.1.0" } }, { From da58691f07295420b05fe7a85ca41d3a72b7222b Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 2 Apr 2024 15:07:50 +0900 Subject: [PATCH 07/18] Fix to use new `PropertyMap` --- .../CAGradientLayer+ICodable.swift | 6 +- .../IndirectlyCodable/CALayer+ICodable.swift | 8 +- .../CAScrollLayer+ICodable.swift | 6 +- .../CAShapeLayer+ICodable.swift | 10 +- .../CATextLayer+ICodable.swift | 8 +- .../IndirectlyCodable/CGColor+ICodable.swift | 2 +- .../IndirectlyCodable/CGPath+ICodable.swift | 2 +- Sources/SDCALayer/Model/JCGColor.swift | 2 +- Sources/SDCALayer/Model/JCGPath.swift | 2 +- .../Model/Layer/JCAGradientLayer.swift | 29 ++--- Sources/SDCALayer/Model/Layer/JCALayer.swift | 121 ++++++------------ .../Model/Layer/JCAScrollLayer.swift | 8 +- .../SDCALayer/Model/Layer/JCAShapeLayer.swift | 54 +++----- .../SDCALayer/Model/Layer/JCATextLayer.swift | 47 +++---- .../Protocol/CALayerConvertible.swift | 2 +- 15 files changed, 119 insertions(+), 188 deletions(-) diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CAGradientLayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CAGradientLayer+ICodable.swift index bb84c93..05c1b1d 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CAGradientLayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CAGradientLayer+ICodable.swift @@ -9,15 +9,15 @@ import QuartzCore extension CAGradientLayer { - public typealias Target = JCAGradientLayer + public typealias Model = JCAGradientLayer open override class var codableTypeName: String { - String(reflecting: Target.self) + String(reflecting: Model.self) } } extension CAGradientLayerType: IndirectlyCodable { - public typealias Target = JCAGradientLayerType + public typealias Model = JCAGradientLayerType public func codable() -> JCAGradientLayerType? { .init(with: self) diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift index 45e4c0b..c38dad4 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift @@ -9,7 +9,7 @@ import QuartzCore extension CALayer: IndirectlyCodable { - public typealias Target = JCALayer + public typealias Model = JCALayer public func codable() -> JCALayer? { guard let layerClass = NSClassFromString(Self.codableTypeName) as? JCALayer.Type else { @@ -21,12 +21,12 @@ extension CALayer: IndirectlyCodable { @objc open class var codableTypeName: String { - String(reflecting: Target.self) + String(reflecting: Model.self) } } extension CALayerCornerCurve: IndirectlyCodable { - public typealias Target = JCALayerCornerCurve + public typealias Model = JCALayerCornerCurve public func codable() -> JCALayerCornerCurve? { .init(with: self) @@ -34,7 +34,7 @@ extension CALayerCornerCurve: IndirectlyCodable { } extension CACornerMask: IndirectlyCodable { - public typealias Target = JCACornerMask + public typealias Model = JCACornerMask public func codable() -> JCACornerMask? { .init(with: self) diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CAScrollLayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CAScrollLayer+ICodable.swift index eacced6..221fd6f 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CAScrollLayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CAScrollLayer+ICodable.swift @@ -9,15 +9,15 @@ import QuartzCore extension CAScrollLayer { - public typealias Target = JCAScrollLayer + public typealias Model = JCAScrollLayer open override class var codableTypeName: String { - String(reflecting: Target.self) + String(reflecting: Model.self) } } extension CAScrollLayerScrollMode: IndirectlyCodable { - public typealias Target = JCAScrollLayerScrollMode + public typealias Model = JCAScrollLayerScrollMode public func codable() -> JCAScrollLayerScrollMode? { .init(with: self) diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CAShapeLayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CAShapeLayer+ICodable.swift index c274ddc..9a10d82 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CAShapeLayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CAShapeLayer+ICodable.swift @@ -9,15 +9,15 @@ import QuartzCore extension CAShapeLayer { - public typealias Target = JCAShapeLayer + public typealias Model = JCAShapeLayer open override class var codableTypeName: String { - String(reflecting: Target.self) + String(reflecting: Model.self) } } extension CAShapeLayerFillRule: IndirectlyCodable { - public typealias Target = JCAShapeLayerFillRule + public typealias Model = JCAShapeLayerFillRule public func codable() -> JCAShapeLayerFillRule? { .init(with: self) @@ -25,7 +25,7 @@ extension CAShapeLayerFillRule: IndirectlyCodable { } extension CAShapeLayerLineCap: IndirectlyCodable { - public typealias Target = JCAShapeLayerLineCap + public typealias Model = JCAShapeLayerLineCap public func codable() -> JCAShapeLayerLineCap? { .init(with: self) @@ -33,7 +33,7 @@ extension CAShapeLayerLineCap: IndirectlyCodable { } extension CAShapeLayerLineJoin: IndirectlyCodable { - public typealias Target = JCAShapeLayerLineJoin + public typealias Model = JCAShapeLayerLineJoin public func codable() -> JCAShapeLayerLineJoin? { .init(with: self) diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CATextLayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CATextLayer+ICodable.swift index 36d16e3..771261f 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CATextLayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CATextLayer+ICodable.swift @@ -9,15 +9,15 @@ import QuartzCore extension CATextLayer { - public typealias Target = JCATextLayer + public typealias Model = JCATextLayer open override class var codableTypeName: String { - String(reflecting: Target.self) + String(reflecting: Model.self) } } extension CATextLayerTruncationMode: IndirectlyCodable { - public typealias Target = JCATextLayerTruncationMode + public typealias Model = JCATextLayerTruncationMode public func codable() -> JCATextLayerTruncationMode? { .init(with: self) @@ -25,7 +25,7 @@ extension CATextLayerTruncationMode: IndirectlyCodable { } extension CATextLayerAlignmentMode: IndirectlyCodable { - public typealias Target = JCATextLayerAlignmentMode + public typealias Model = JCATextLayerAlignmentMode public func codable() -> JCATextLayerAlignmentMode? { .init(with: self) diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CGColor+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CGColor+ICodable.swift index c360c7d..5b1dabd 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CGColor+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CGColor+ICodable.swift @@ -9,7 +9,7 @@ import QuartzCore extension CGColor: IndirectlyCodable { - public typealias Target = JCGColor + public typealias Model = JCGColor public func applyProperties(to target: JCGColor) {} diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CGPath+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CGPath+ICodable.swift index 2dca2bb..c90b7a4 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CGPath+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CGPath+ICodable.swift @@ -9,7 +9,7 @@ import QuartzCore extension CGPath: IndirectlyCodable { - public typealias Target = JCGPath + public typealias Model = JCGPath public func codable() -> JCGPath? { .init(with: self) diff --git a/Sources/SDCALayer/Model/JCGColor.swift b/Sources/SDCALayer/Model/JCGColor.swift index 2c6fe2f..12c78e6 100644 --- a/Sources/SDCALayer/Model/JCGColor.swift +++ b/Sources/SDCALayer/Model/JCGColor.swift @@ -9,7 +9,7 @@ import Foundation import QuartzCore -public class JCGColor: ObjectConvertiblyCodable { +public class JCGColor: IndirectlyCodableModel { public typealias Target = CGColor public let code: String? diff --git a/Sources/SDCALayer/Model/JCGPath.swift b/Sources/SDCALayer/Model/JCGPath.swift index 66444a8..4473c05 100644 --- a/Sources/SDCALayer/Model/JCGPath.swift +++ b/Sources/SDCALayer/Model/JCGPath.swift @@ -8,7 +8,7 @@ import CoreGraphics -public class JCGPath: ObjectConvertiblyCodable { +public class JCGPath: IndirectlyCodableModel { public typealias Target = CGPath public var pathElements: [JCGPathElement]? diff --git a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift index 9d71113..f7df414 100644 --- a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift @@ -21,19 +21,12 @@ open class JCAGradientLayer: JCALayer { String(reflecting: Target.self) } - static private let propertyMap: PropertyMap = [ - \.colors: .init(\.colors), - \.startPoint: .init(\.startPoint), - \.endPoint: .init(\.endPoint), - \.type: .init(\.type) - ] - - static private let reversePropertyMap: PropertyMap = [ - \.colors: .init(\.colors), - \.startPoint: .init(\.startPoint), - \.endPoint: .init(\.endPoint), - \.type: .init(\.type) - ] + static private let propertyMap: PropertyMap = .init([ + .init(\.colors, \.colors), + .init(\.startPoint, \.startPoint), + .init(\.endPoint, \.endPoint), + .init(\.type, \.type) + ]) public var colors: [JCGColor]? public var locations: [Double]? @@ -64,7 +57,7 @@ open class JCAGradientLayer: JCALayer { public required convenience init(with object: CALayer) { self.init() - reverseApplyProperties(with: object) + applyProperties(with: object) } open override func encode(to encoder: Encoder) throws { @@ -91,12 +84,12 @@ open class JCAGradientLayer: JCALayer { target.locations = locations?.map { NSNumber(floatLiteral: $0) } } - open override func reverseApplyProperties(with target: CALayer) { - super.reverseApplyProperties(with: target) + open override func applyProperties(with target: CALayer) { + super.applyProperties(with: target) guard let target = target as? CAGradientLayer else { return } - Self.reversePropertyMap.apply(to: self, from: target) + Self.propertyMap.apply(to: self, from: target) self.locations = target.locations?.map { $0.doubleValue } } @@ -110,7 +103,7 @@ open class JCAGradientLayer: JCALayer { } } -public class JCAGradientLayerType: ObjectConvertiblyCodable { +public class JCAGradientLayerType: IndirectlyCodableModel { public typealias Target = CAGradientLayerType public var rawValue: String? diff --git a/Sources/SDCALayer/Model/Layer/JCALayer.swift b/Sources/SDCALayer/Model/Layer/JCALayer.swift index 121eef1..c1bf329 100644 --- a/Sources/SDCALayer/Model/Layer/JCALayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCALayer.swift @@ -1,9 +1,9 @@ // // JCALayer.swift -// +// // // Created by p-x9 on 2022/11/06. -// +// // import Foundation @@ -17,78 +17,41 @@ open class JCALayer: CALayerConvertible, Codable { String(reflecting: Target.self) } - static private let propertyMap: PropertyMap = [ - \.bounds: .init(\.bounds), - \.position: .init(\.position), - \.zPosition: .init(\.zPosition), - \.anchorPoint: .init(\.anchorPoint), - \.anchorPointZ: .init(\.anchorPointZ), - \.transform: .init(\.transform), - \.frame: .init(\.frame), - \.isHidden: .init(\.isHidden), - \.isDoubleSided: .init(\.isDoubleSided), - \.isGeometryFlipped: .init(\.isGeometryFlipped), - \.sublayerTransform: .init(\.sublayerTransform), - \.mask: .init(\.mask), - \.masksToBounds: .init(\.masksToBounds), - \.isOpaque: .init(\.isOpaque), - \.needsDisplayOnBoundsChange: .init(\.needsDisplayOnBoundsChange), - \.drawsAsynchronously: .init(\.drawsAsynchronously), - \.allowsEdgeAntialiasing: .init(\.allowsEdgeAntialiasing), - \.backgroundColor: .init(\.backgroundColor), - \.cornerRadius: .init(\.cornerRadius), - \.maskedCorners: .init(\.maskedCorners), - \.cornerCurve: .init(\.cornerCurve), - \.borderWidth: .init(\.borderWidth), - \.borderColor: .init(\.borderColor), - \.opacity: .init(\.opacity), - \.allowsGroupOpacity: .init(\.allowsGroupOpacity), - \.shouldRasterize: .init(\.shouldRasterize), - \.rasterizationScale: .init(\.rasterizationScale), - \.shadowColor: .init(\.shadowColor), - \.shadowOpacity: .init(\.shadowOpacity), - \.shadowOffset: .init(\.shadowOffset), - \.shadowRadius: .init(\.shadowRadius), - \.shadowPath: .init(\.shadowPath), - \.name: .init(\.name) - ] - - static private let reversePropertyMap: PropertyMap = [ - \.bounds: .init(\.bounds), - \.position: .init(\.position), - \.zPosition: .init(\.zPosition), - \.anchorPoint: .init(\.anchorPoint), - \.anchorPointZ: .init(\.anchorPointZ), - \.transform: .init(\.transform), - \.frame: .init(\.frame), - \.isHidden: .init(\.isHidden), - \.isDoubleSided: .init(\.isDoubleSided), - \.isGeometryFlipped: .init(\.isGeometryFlipped), - \.sublayerTransform: .init(\.sublayerTransform), - \.mask: .init(\.mask), - \.masksToBounds: .init(\.masksToBounds), - \.isOpaque: .init(\.isOpaque), - \.needsDisplayOnBoundsChange: .init(\.needsDisplayOnBoundsChange), - \.drawsAsynchronously: .init(\.drawsAsynchronously), - \.allowsEdgeAntialiasing: .init(\.allowsEdgeAntialiasing), - \.backgroundColor: .init(\.backgroundColor), - \.cornerRadius: .init(\.cornerRadius), - \.maskedCorners: .init(\.maskedCorners), - \.cornerCurve: .init(\.cornerCurve), - \.borderWidth: .init(\.borderWidth), - \.borderColor: .init(\.borderColor), - \.opacity: .init(\.opacity), - \.allowsGroupOpacity: .init(\.allowsGroupOpacity), - \.shouldRasterize: .init(\.shouldRasterize), - \.rasterizationScale: .init(\.rasterizationScale), - \.shadowColor: .init(\.shadowColor), - \.shadowOpacity: .init(\.shadowOpacity), - \.shadowOffset: .init(\.shadowOffset), - \.shadowRadius: .init(\.shadowRadius), - \.shadowPath: .init(\.shadowPath), - \.name: .init(\.name), - \.sublayers: .init(\.sublayers) - ] + static private let propertyMap: PropertyMap = .init([ + .init(\.bounds, \.bounds), + .init(\.position, \.position), + .init(\.zPosition, \.zPosition), + .init(\.anchorPoint, \.anchorPoint), + .init(\.anchorPointZ, \.anchorPointZ), + .init(\.transform, \.transform), + .init(\.frame, \.frame), + .init(\.isHidden, \.isHidden), + .init(\.isDoubleSided, \.isDoubleSided), + .init(\.isGeometryFlipped, \.isGeometryFlipped), + .init(\.sublayerTransform, \.sublayerTransform), + .init(\.mask, \.mask), + .init(\.masksToBounds, \.masksToBounds), + .init(\.isOpaque, \.isOpaque), + .init(\.needsDisplayOnBoundsChange, \.needsDisplayOnBoundsChange), + .init(\.drawsAsynchronously, \.drawsAsynchronously), + .init(\.allowsEdgeAntialiasing, \.allowsEdgeAntialiasing), + .init(\.backgroundColor, \.backgroundColor), + .init(\.cornerRadius, \.cornerRadius), + .init(\.maskedCorners, \.maskedCorners), + .init(\.cornerCurve, \.cornerCurve), + .init(\.borderWidth, \.borderWidth), + .init(\.borderColor, \.borderColor), + .init(\.opacity, \.opacity), + .init(\.allowsGroupOpacity, \.allowsGroupOpacity), + .init(\.shouldRasterize, \.shouldRasterize), + .init(\.rasterizationScale, \.rasterizationScale), + .init(\.shadowColor, \.shadowColor), + .init(\.shadowOpacity, \.shadowOpacity), + .init(\.shadowOffset, \.shadowOffset), + .init(\.shadowRadius, \.shadowRadius), + .init(\.shadowPath, \.shadowPath), + .init(\.name, \.name) + ]) public var bounds: CGRect? public var position: CGPoint? @@ -150,7 +113,7 @@ open class JCALayer: CALayerConvertible, Codable { public required convenience init(with object: CALayer) { self.init() - reverseApplyProperties(with: object) + applyProperties(with: object) } open func applyProperties(to target: CALayer) { @@ -162,8 +125,8 @@ open class JCALayer: CALayerConvertible, Codable { } } - open func reverseApplyProperties(with target: CALayer) { - Self.reversePropertyMap.apply(to: self, from: target) + open func applyProperties(with target: CALayer) { + Self.propertyMap.apply(to: self, from: target) self.mask = SDCALayer(model: target.mask?.codable()) self.sublayers = target.sublayers?.compactMap { @@ -180,7 +143,7 @@ open class JCALayer: CALayerConvertible, Codable { } } -public class JCALayerCornerCurve: ObjectConvertiblyCodable { +public class JCALayerCornerCurve: IndirectlyCodableModel { public typealias Target = CALayerCornerCurve public var rawValue: String? @@ -195,7 +158,7 @@ public class JCALayerCornerCurve: ObjectConvertiblyCodable { } } -public class JCACornerMask: ObjectConvertiblyCodable { +public class JCACornerMask: IndirectlyCodableModel { public typealias Target = CACornerMask public var rawValue: UInt? diff --git a/Sources/SDCALayer/Model/Layer/JCAScrollLayer.swift b/Sources/SDCALayer/Model/Layer/JCAScrollLayer.swift index 505a905..c997758 100644 --- a/Sources/SDCALayer/Model/Layer/JCAScrollLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAScrollLayer.swift @@ -38,7 +38,7 @@ open class JCAScrollLayer: JCALayer { public required convenience init(with object: CALayer) { self.init() - reverseApplyProperties(with: object) + applyProperties(with: object) } open override func encode(to encoder: Encoder) throws { @@ -59,8 +59,8 @@ open class JCAScrollLayer: JCALayer { } } - open override func reverseApplyProperties(with target: CALayer) { - super.reverseApplyProperties(with: target) + open override func applyProperties(with target: CALayer) { + super.applyProperties(with: target) guard let target = target as? CAScrollLayer else { return } @@ -76,7 +76,7 @@ open class JCAScrollLayer: JCALayer { } } -public class JCAScrollLayerScrollMode: ObjectConvertiblyCodable { +public class JCAScrollLayerScrollMode: IndirectlyCodableModel { public typealias Target = CAScrollLayerScrollMode public var rawValue: String? diff --git a/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift b/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift index e6f31e4..255ddc0 100644 --- a/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift @@ -21,33 +21,19 @@ open class JCAShapeLayer: JCALayer { String(reflecting: Target.self) } - static private let propertyMap: PropertyMap = [ - \.path: .init(\.path), - \.fillColor: .init(\.fillColor), - \.fillRule: .init(\.fillRule), - \.strokeColor: .init(\.strokeColor), - \.strokeStart: .init(\.strokeStart), - \.strokeEnd: .init(\.strokeEnd), - \.lineWidth: .init(\.lineWidth), - \.miterLimit: .init(\.miterLimit), - \.lineCap: .init(\.lineCap), - \.lineJoin: .init(\.lineJoin), - \.lineDashPhase: .init(\.lineDashPhase) - ] - - static private let reversePropertyMap: PropertyMap = [ - \.path: .init(\.path), - \.fillColor: .init(\.fillColor), - \.fillRule: .init(\.fillRule), - \.strokeColor: .init(\.strokeColor), - \.strokeStart: .init(\.strokeStart), - \.strokeEnd: .init(\.strokeEnd), - \.lineWidth: .init(\.lineWidth), - \.miterLimit: .init(\.miterLimit), - \.lineCap: .init(\.lineCap), - \.lineJoin: .init(\.lineJoin), - \.lineDashPhase: .init(\.lineDashPhase) - ] + static private let propertyMap: PropertyMap = .init([ + .init(\.path, \.path), + .init(\.fillColor, \.fillColor), + .init(\.fillRule, \.fillRule), + .init(\.strokeColor, \.strokeColor), + .init(\.strokeStart, \.strokeStart), + .init(\.strokeEnd, \.strokeEnd), + .init(\.lineWidth, \.lineWidth), + .init(\.miterLimit, \.miterLimit), + .init(\.lineCap, \.lineCap), + .init(\.lineJoin, \.lineJoin), + .init(\.lineDashPhase, \.lineDashPhase) + ]) public var path: JCGPath? @@ -103,7 +89,7 @@ open class JCAShapeLayer: JCALayer { public required convenience init(with object: CALayer) { self.init() - reverseApplyProperties(with: object) + applyProperties(with: object) } open override func encode(to encoder: Encoder) throws { @@ -142,12 +128,12 @@ open class JCAShapeLayer: JCALayer { target.lineDashPattern = lineDashPattern?.map { NSNumber(floatLiteral: $0) } } - open override func reverseApplyProperties(with target: CALayer) { - super.reverseApplyProperties(with: target) + open override func applyProperties(with target: CALayer) { + super.applyProperties(with: target) guard let target = target as? CAShapeLayer else { return } - Self.reversePropertyMap.apply(to: self, from: target) + Self.propertyMap.apply(to: self, from: target) self.lineDashPattern = target.lineDashPattern?.map { $0.doubleValue } } @@ -161,7 +147,7 @@ open class JCAShapeLayer: JCALayer { } } -public class JCAShapeLayerFillRule: ObjectConvertiblyCodable { +public class JCAShapeLayerFillRule: IndirectlyCodableModel { public typealias Target = CAShapeLayerFillRule public var rawValue: String? @@ -176,7 +162,7 @@ public class JCAShapeLayerFillRule: ObjectConvertiblyCodable { } } -public class JCAShapeLayerLineCap: ObjectConvertiblyCodable { +public class JCAShapeLayerLineCap: IndirectlyCodableModel { public typealias Target = CAShapeLayerLineCap public var rawValue: String? @@ -191,7 +177,7 @@ public class JCAShapeLayerLineCap: ObjectConvertiblyCodable { } } -public class JCAShapeLayerLineJoin: ObjectConvertiblyCodable { +public class JCAShapeLayerLineJoin: IndirectlyCodableModel { public typealias Target = CAShapeLayerLineJoin public var rawValue: String? diff --git a/Sources/SDCALayer/Model/Layer/JCATextLayer.swift b/Sources/SDCALayer/Model/Layer/JCATextLayer.swift index c24dff2..6c8f7e8 100644 --- a/Sources/SDCALayer/Model/Layer/JCATextLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCATextLayer.swift @@ -1,9 +1,9 @@ // // JCATextLayer.swift -// +// // // Created by p-x9 on 2022/11/14. -// +// // import Foundation @@ -22,27 +22,16 @@ open class JCATextLayer: JCALayer { String(reflecting: Target.self) } - static private let propertyMap: PropertyMap = [ - \.string: .init(\.string), - \.font: .init(\.font), - \.fontSize: .init(\.fontSize), - \.foregroundColor: .init(\.foregroundColor), - \.isWrapped: .init(\.isWrapped), - \.truncationMode: .init(\.truncationMode), - \.alignmentMode: .init(\.alignmentMode), - \.allowsFontSubpixelQuantization: .init(\.allowsFontSubpixelQuantization) - ] - - static private let reversePropertyMap: PropertyMap = [ - \.string: .init(\.string), -// \.font: .init(\.font), - \.fontSize: .init(\.fontSize), - \.foregroundColor: .init(\.foregroundColor), - \.isWrapped: .init(\.isWrapped), - \.truncationMode: .init(\.truncationMode), - \.alignmentMode: .init(\.alignmentMode), - \.allowsFontSubpixelQuantization: .init(\.allowsFontSubpixelQuantization) - ] + static private let propertyMap: PropertyMap = .init([ + .init(\.string, \.string), + .init(\.font, \.font), + .init(\.fontSize, \.fontSize), + .init(\.foregroundColor, \.foregroundColor), + .init(\.isWrapped, \.isWrapped), + .init(\.truncationMode, \.truncationMode), + .init(\.alignmentMode, \.alignmentMode), + .init(\.allowsFontSubpixelQuantization, \.allowsFontSubpixelQuantization) + ]) public var string: String? @@ -88,7 +77,7 @@ open class JCATextLayer: JCALayer { public required convenience init(with object: CALayer) { self.init() - reverseApplyProperties(with: object) + applyProperties(with: object) } open override func encode(to encoder: Encoder) throws { @@ -120,12 +109,12 @@ open class JCATextLayer: JCALayer { Self.propertyMap.apply(to: target, from: self) } - open override func reverseApplyProperties(with target: CALayer) { - super.reverseApplyProperties(with: target) + open override func applyProperties(with target: CALayer) { + super.applyProperties(with: target) guard let target = target as? CATextLayer else { return } - Self.reversePropertyMap.apply(to: self, from: target) + Self.propertyMap.apply(to: self, from: target) if let font = target[keyPath: \.font] { switch CFGetTypeID(font) { @@ -150,7 +139,7 @@ open class JCATextLayer: JCALayer { } } -public class JCATextLayerTruncationMode: ObjectConvertiblyCodable { +public class JCATextLayerTruncationMode: IndirectlyCodableModel { public typealias Target = CATextLayerTruncationMode public var rawValue: String? @@ -165,7 +154,7 @@ public class JCATextLayerTruncationMode: ObjectConvertiblyCodable { } } -public class JCATextLayerAlignmentMode: ObjectConvertiblyCodable { +public class JCATextLayerAlignmentMode: IndirectlyCodableModel { public typealias Target = CATextLayerAlignmentMode public var rawValue: String? diff --git a/Sources/SDCALayer/Protocol/CALayerConvertible.swift b/Sources/SDCALayer/Protocol/CALayerConvertible.swift index 2abd7e0..3868782 100644 --- a/Sources/SDCALayer/Protocol/CALayerConvertible.swift +++ b/Sources/SDCALayer/Protocol/CALayerConvertible.swift @@ -8,7 +8,7 @@ import Foundation -public protocol CALayerConvertible: ObjectConvertiblyCodable { +public protocol CALayerConvertible: IndirectlyCodableModel { func convertToLayer() -> Target? } From 9875823e3dde3df34fb7ea0e32561742e225cfc2 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 2 Apr 2024 15:35:26 +0900 Subject: [PATCH 08/18] add more property map elements --- .../Model/Layer/JCAGradientLayer.swift | 1 + Sources/SDCALayer/Model/Layer/JCALayer.swift | 19 ++++++++++++++++++- .../SDCALayer/Model/Layer/JCAShapeLayer.swift | 3 ++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift index f7df414..076e3ba 100644 --- a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift @@ -23,6 +23,7 @@ open class JCAGradientLayer: JCALayer { static private let propertyMap: PropertyMap = .init([ .init(\.colors, \.colors), +// .init(\.locations, \.locations), .init(\.startPoint, \.startPoint), .init(\.endPoint, \.endPoint), .init(\.type, \.type) diff --git a/Sources/SDCALayer/Model/Layer/JCALayer.swift b/Sources/SDCALayer/Model/Layer/JCALayer.swift index c1bf329..ff13a7c 100644 --- a/Sources/SDCALayer/Model/Layer/JCALayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCALayer.swift @@ -28,12 +28,24 @@ open class JCALayer: CALayerConvertible, Codable { .init(\.isHidden, \.isHidden), .init(\.isDoubleSided, \.isDoubleSided), .init(\.isGeometryFlipped, \.isGeometryFlipped), + .init(\.sublayers, \.sublayers), .init(\.sublayerTransform, \.sublayerTransform), .init(\.mask, \.mask), .init(\.masksToBounds, \.masksToBounds), +// .init(\.contents, \.contents), +// .init(\.contentsRect, \.contentsRect), +// .init(\.contentsGravity, \.contentsGravity), +// .init(\.contentsScale, \.contentsScale), +// .init(\.contentsCenter, \.contentsCenter), +// .init(\.contentsFormat, \.contentsFormat), +// .init(\.wantsExtendedDynamicRangeContent, \.wantsExtendedDynamicRangeContent), +// .init(\.minificationFilter, \.minificationFilter), +// .init(\.magnificationFilter, \.magnificationFilter), +// .init(\.minificationFilterBias, \.minificationFilterBias), .init(\.isOpaque, \.isOpaque), .init(\.needsDisplayOnBoundsChange, \.needsDisplayOnBoundsChange), .init(\.drawsAsynchronously, \.drawsAsynchronously), +// .init(\.edgeAntialiasingMask, \.edgeAntialiasingMask), .init(\.allowsEdgeAntialiasing, \.allowsEdgeAntialiasing), .init(\.backgroundColor, \.backgroundColor), .init(\.cornerRadius, \.cornerRadius), @@ -43,6 +55,9 @@ open class JCALayer: CALayerConvertible, Codable { .init(\.borderColor, \.borderColor), .init(\.opacity, \.opacity), .init(\.allowsGroupOpacity, \.allowsGroupOpacity), +// .init(\.compositingFilter, \.compositingFilter), +// .init(\.filters, \.filters), +// .init(\.backgroundFilters, \.backgroundFilters), .init(\.shouldRasterize, \.shouldRasterize), .init(\.rasterizationScale, \.rasterizationScale), .init(\.shadowColor, \.shadowColor), @@ -50,7 +65,9 @@ open class JCALayer: CALayerConvertible, Codable { .init(\.shadowOffset, \.shadowOffset), .init(\.shadowRadius, \.shadowRadius), .init(\.shadowPath, \.shadowPath), - .init(\.name, \.name) +// .init(\.actions, \.actions), + .init(\.name, \.name), +// .init(\.style, \.style) ]) public var bounds: CGRect? diff --git a/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift b/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift index 255ddc0..7562461 100644 --- a/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift @@ -32,7 +32,8 @@ open class JCAShapeLayer: JCALayer { .init(\.miterLimit, \.miterLimit), .init(\.lineCap, \.lineCap), .init(\.lineJoin, \.lineJoin), - .init(\.lineDashPhase, \.lineDashPhase) + .init(\.lineDashPhase, \.lineDashPhase), +// .init(\.lineDashPattern, \.lineDashPattern) ]) public var path: JCGPath? From 928357210b39ba00eca68174dd280ed3c977e0b6 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 2 Apr 2024 15:39:57 +0900 Subject: [PATCH 09/18] Warn if value assignment fails --- Sources/SDCALayer/Util/PropertyMap.swift | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Sources/SDCALayer/Util/PropertyMap.swift b/Sources/SDCALayer/Util/PropertyMap.swift index af2b97a..963f34d 100644 --- a/Sources/SDCALayer/Util/PropertyMap.swift +++ b/Sources/SDCALayer/Util/PropertyMap.swift @@ -101,7 +101,14 @@ extension PropertyMap { default: break } +#if DEBUG + let result = applier.apply(value, to: target) + if !result { + print("[⚠️SDCALayer] failed to assign value(\(keyPath)") + } +#else applier.apply(value, to: target) +#endif } } } @@ -126,7 +133,14 @@ extension PropertyMap { default: break } +#if DEBUG + let result = applier.apply(value, to: source) + if !result { + print("[⚠️SDCALayer] failed to assign value(\(keyPath)") + } +#else applier.apply(value, to: source) +#endif } } } @@ -157,7 +171,14 @@ extension PropertyMap { if let keyPath = keyPath._kvcKeyPathString, source.shouldArchiveValue(forKey: keyPath) { +#if DEBUG + let result = applier.apply(value, to: target) + if !result { + print("[⚠️SDCALayer] failed to assign value(\(keyPath)") + } +#else applier.apply(value, to: target) +#endif } } } From 43db025b25e30c726412abcab92d375f772a11c2 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Tue, 2 Apr 2024 17:56:04 +0900 Subject: [PATCH 10/18] fix log format --- Sources/SDCALayer/Model/JCGPath.swift | 2 +- Sources/SDCALayer/Util/PropertyMap.swift | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/SDCALayer/Model/JCGPath.swift b/Sources/SDCALayer/Model/JCGPath.swift index 4473c05..7a629ad 100644 --- a/Sources/SDCALayer/Model/JCGPath.swift +++ b/Sources/SDCALayer/Model/JCGPath.swift @@ -29,7 +29,7 @@ public class JCGPath: IndirectlyCodableModel { .filter { let isValid = $0.elementType != nil && $0.elementType!.numberOfPoints <= $0.points.count if !isValid { - print("⚠️[warning] SDCALayer invalid path element") + print("⚠️ [SDCALayer] invalid path element") } return isValid } diff --git a/Sources/SDCALayer/Util/PropertyMap.swift b/Sources/SDCALayer/Util/PropertyMap.swift index 963f34d..1370578 100644 --- a/Sources/SDCALayer/Util/PropertyMap.swift +++ b/Sources/SDCALayer/Util/PropertyMap.swift @@ -104,7 +104,7 @@ extension PropertyMap { #if DEBUG let result = applier.apply(value, to: target) if !result { - print("[⚠️SDCALayer] failed to assign value(\(keyPath)") + print("⚠️ [SDCALayer] failed to assign value(\(keyPath)") } #else applier.apply(value, to: target) @@ -136,7 +136,7 @@ extension PropertyMap { #if DEBUG let result = applier.apply(value, to: source) if !result { - print("[⚠️SDCALayer] failed to assign value(\(keyPath)") + print("⚠️ [SDCALayer] failed to assign value(\(keyPath)") } #else applier.apply(value, to: source) @@ -174,7 +174,7 @@ extension PropertyMap { #if DEBUG let result = applier.apply(value, to: target) if !result { - print("[⚠️SDCALayer] failed to assign value(\(keyPath)") + print("⚠️ [SDCALayer] failed to assign value(\(keyPath)") } #else applier.apply(value, to: target) From 2a9dcad2e105e1cc46c56f0cb285e9a4e0cdde72 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Wed, 3 Apr 2024 16:13:57 +0900 Subject: [PATCH 11/18] check if value is nil --- Sources/SDCALayer/Protocol/OptionalType.swift | 19 +++++++++++++++++++ Sources/SDCALayer/Util/PropertyMap.swift | 18 +++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 Sources/SDCALayer/Protocol/OptionalType.swift diff --git a/Sources/SDCALayer/Protocol/OptionalType.swift b/Sources/SDCALayer/Protocol/OptionalType.swift new file mode 100644 index 0000000..2f2d378 --- /dev/null +++ b/Sources/SDCALayer/Protocol/OptionalType.swift @@ -0,0 +1,19 @@ +// +// OptionalType.swift +// +// +// Created by p-x9 on 2024/04/03. +// +// + +import Foundation + +public protocol OptionalType { + associatedtype Wrapped + + var wrapped: Wrapped? { get } +} + +extension Optional: OptionalType { + public var wrapped: Wrapped? { self } +} diff --git a/Sources/SDCALayer/Util/PropertyMap.swift b/Sources/SDCALayer/Util/PropertyMap.swift index 1370578..e50dacc 100644 --- a/Sources/SDCALayer/Util/PropertyMap.swift +++ b/Sources/SDCALayer/Util/PropertyMap.swift @@ -104,7 +104,11 @@ extension PropertyMap { #if DEBUG let result = applier.apply(value, to: target) if !result { - print("⚠️ [SDCALayer] failed to assign value(\(keyPath)") + if let optional = (value as? (any OptionalType)), + optional.wrapped == nil { + return + } + print("⚠️ [SDCALayer] failed to assign value(\(keyPath))") } #else applier.apply(value, to: target) @@ -136,7 +140,11 @@ extension PropertyMap { #if DEBUG let result = applier.apply(value, to: source) if !result { - print("⚠️ [SDCALayer] failed to assign value(\(keyPath)") + if let optional = (value as? (any OptionalType)), + optional.wrapped == nil { + return + } + print("⚠️ [SDCALayer] failed to assign value(\(keyPath))") } #else applier.apply(value, to: source) @@ -174,7 +182,11 @@ extension PropertyMap { #if DEBUG let result = applier.apply(value, to: target) if !result { - print("⚠️ [SDCALayer] failed to assign value(\(keyPath)") + if let optional = (value as? (any OptionalType)), + optional.wrapped == nil { + return + } + print("⚠️ [SDCALayer] failed to assign value(\(keyPath))") } #else applier.apply(value, to: target) From 40cc65b8dd7f8a62380062fc86a10d2d2177853d Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Wed, 3 Apr 2024 17:45:56 +0900 Subject: [PATCH 12/18] Support additional properties of `CALayer` --- .../IndirectlyCodable/CALayer+ICodable.swift | 33 +++++++ Sources/SDCALayer/Model/Layer/JCALayer.swift | 91 +++++++++++++++++-- 2 files changed, 115 insertions(+), 9 deletions(-) diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift index c38dad4..9c48708 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift @@ -40,3 +40,36 @@ extension CACornerMask: IndirectlyCodable { .init(with: self) } } + +extension CALayerContentsGravity: IndirectlyCodable { + public typealias Model = JCALayerContentsGravity + + public func codable() -> Model? { + .init(with: self) + } +} + +extension CALayerContentsFormat: IndirectlyCodable { + public typealias Model = JCALayerContentsFormat + + public func codable() -> Model? { + .init(with: self) + } +} + +extension CALayerContentsFilter: IndirectlyCodable { + public typealias Model = JCALayerContentsFilter + + public func codable() -> Model? { + .init(with: self) + } +} + +extension CAEdgeAntialiasingMask: IndirectlyCodable { + public typealias Model = JCAEdgeAntialiasingMask + + public func codable() -> Model? { + .init(with: self) + } +} + diff --git a/Sources/SDCALayer/Model/Layer/JCALayer.swift b/Sources/SDCALayer/Model/Layer/JCALayer.swift index ff13a7c..dce8227 100644 --- a/Sources/SDCALayer/Model/Layer/JCALayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCALayer.swift @@ -33,19 +33,19 @@ open class JCALayer: CALayerConvertible, Codable { .init(\.mask, \.mask), .init(\.masksToBounds, \.masksToBounds), // .init(\.contents, \.contents), -// .init(\.contentsRect, \.contentsRect), -// .init(\.contentsGravity, \.contentsGravity), -// .init(\.contentsScale, \.contentsScale), -// .init(\.contentsCenter, \.contentsCenter), -// .init(\.contentsFormat, \.contentsFormat), + .init(\.contentsRect, \.contentsRect), + .init(\.contentsGravity, \.contentsGravity), + .init(\.contentsScale, \.contentsScale), + .init(\.contentsCenter, \.contentsCenter), + .init(\.contentsFormat, \.contentsFormat), // .init(\.wantsExtendedDynamicRangeContent, \.wantsExtendedDynamicRangeContent), -// .init(\.minificationFilter, \.minificationFilter), -// .init(\.magnificationFilter, \.magnificationFilter), -// .init(\.minificationFilterBias, \.minificationFilterBias), + .init(\.minificationFilter, \.minificationFilter), + .init(\.magnificationFilter, \.magnificationFilter), + .init(\.minificationFilterBias, \.minificationFilterBias), .init(\.isOpaque, \.isOpaque), .init(\.needsDisplayOnBoundsChange, \.needsDisplayOnBoundsChange), .init(\.drawsAsynchronously, \.drawsAsynchronously), -// .init(\.edgeAntialiasingMask, \.edgeAntialiasingMask), + .init(\.edgeAntialiasingMask, \.edgeAntialiasingMask), .init(\.allowsEdgeAntialiasing, \.allowsEdgeAntialiasing), .init(\.backgroundColor, \.backgroundColor), .init(\.cornerRadius, \.cornerRadius), @@ -92,12 +92,24 @@ open class JCALayer: CALayerConvertible, Codable { public var mask: SDCALayer? public var masksToBounds: Bool? + public var contentsRect: CGRect? + public var contentsGravity: JCALayerContentsGravity? + public var contentsScale: CGFloat? + public var contentsCenter: CGRect? + public var contentsFormat: JCALayerContentsFormat? + +// public var wantsExtendedDynamicRangeContent: Bool? + public var minificationFilter: JCALayerContentsFilter? + public var magnificationFilter: JCALayerContentsFilter? + public var minificationFilterBias: Float? + public var isOpaque: Bool? public var needsDisplayOnBoundsChange: Bool? public var drawsAsynchronously: Bool? + public var edgeAntialiasingMask: JCAEdgeAntialiasingMask? public var allowsEdgeAntialiasing: Bool? public var backgroundColor: JCGColor? @@ -189,3 +201,64 @@ public class JCACornerMask: IndirectlyCodableModel { return .init(rawValue: rawValue) } } + +public class JCALayerContentsGravity: IndirectlyCodableModel { + public typealias Target = CALayerContentsGravity + + public var rawValue: String? + + required public init(with object: Target) { + rawValue = object.rawValue + } + + public func converted() -> Target? { + guard let rawValue else { return nil } + return .init(rawValue: rawValue) + } +} + +public class JCALayerContentsFormat: IndirectlyCodableModel { + public typealias Target = CALayerContentsFormat + + public var rawValue: String? + + required public init(with object: Target) { + rawValue = object.rawValue + } + + public func converted() -> Target? { + guard let rawValue else { return nil } + return .init(rawValue: rawValue) + } +} + +public class JCALayerContentsFilter: IndirectlyCodableModel { + public typealias Target = CALayerContentsFilter + + public var rawValue: String? + + required public init(with object: Target) { + rawValue = object.rawValue + } + + public func converted() -> Target? { + guard let rawValue else { return nil } + return .init(rawValue: rawValue) + } +} + + +public class JCAEdgeAntialiasingMask: IndirectlyCodableModel { + public typealias Target = CAEdgeAntialiasingMask + + public var rawValue: UInt32? + + required public init(with object: Target) { + rawValue = object.rawValue + } + + public func converted() -> Target? { + guard let rawValue else { return nil } + return .init(rawValue: rawValue) + } +} From a471b1735bc077fd3fe7049b45d9276f8205e174 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Thu, 4 Apr 2024 15:14:16 +0900 Subject: [PATCH 13/18] fix property map of JCALayer --- Sources/SDCALayer/Model/Layer/JCALayer.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/SDCALayer/Model/Layer/JCALayer.swift b/Sources/SDCALayer/Model/Layer/JCALayer.swift index dce8227..ae4a3a4 100644 --- a/Sources/SDCALayer/Model/Layer/JCALayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCALayer.swift @@ -28,9 +28,9 @@ open class JCALayer: CALayerConvertible, Codable { .init(\.isHidden, \.isHidden), .init(\.isDoubleSided, \.isDoubleSided), .init(\.isGeometryFlipped, \.isGeometryFlipped), - .init(\.sublayers, \.sublayers), +// .init(\.sublayers, \.sublayers), // handle manually .init(\.sublayerTransform, \.sublayerTransform), - .init(\.mask, \.mask), +// .init(\.mask, \.mask), // handle manually .init(\.masksToBounds, \.masksToBounds), // .init(\.contents, \.contents), .init(\.contentsRect, \.contentsRect), @@ -148,6 +148,7 @@ open class JCALayer: CALayerConvertible, Codable { open func applyProperties(to target: CALayer) { Self.propertyMap.apply(to: target, from: self) + target.mask = mask?.convertToLayer() sublayers?.compactMap { $0.convertToLayer() } .forEach { target.addSublayer($0) From fbb7f157ab1b0b86ffba5802c054b7faf0313df6 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:11:14 +0900 Subject: [PATCH 14/18] improve type constraints of property map initializer --- .../Model/Layer/JCAGradientLayer.swift | 18 ++++++++++++++++-- .../SDCALayer/Model/Layer/JCATextLayer.swift | 8 ++++++-- Sources/SDCALayer/Util/PropertyMap.swift | 11 +++++------ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift index 076e3ba..6999879 100644 --- a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift @@ -22,8 +22,8 @@ open class JCAGradientLayer: JCALayer { } static private let propertyMap: PropertyMap = .init([ - .init(\.colors, \.colors), -// .init(\.locations, \.locations), +// .init(\.colors, \.colors), // handle manually +// .init(\.locations, \.locations), // handle manually .init(\.startPoint, \.startPoint), .init(\.endPoint, \.endPoint), .init(\.type, \.type) @@ -82,6 +82,10 @@ open class JCAGradientLayer: JCALayer { Self.propertyMap.apply(to: target, from: self) + target.colors = colors? + .compactMap { + $0.converted() + } target.locations = locations?.map { NSNumber(floatLiteral: $0) } } @@ -92,6 +96,16 @@ open class JCAGradientLayer: JCALayer { Self.propertyMap.apply(to: self, from: target) + self.colors = target.colors? + .filter { + CFGetTypeID($0 as AnyObject) == CGColor.typeID + } + .map { + $0 as! CGColor + } + .compactMap { + $0.codable() + } self.locations = target.locations?.map { $0.doubleValue } } diff --git a/Sources/SDCALayer/Model/Layer/JCATextLayer.swift b/Sources/SDCALayer/Model/Layer/JCATextLayer.swift index 6c8f7e8..a1cb00f 100644 --- a/Sources/SDCALayer/Model/Layer/JCATextLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCATextLayer.swift @@ -23,8 +23,8 @@ open class JCATextLayer: JCALayer { } static private let propertyMap: PropertyMap = .init([ - .init(\.string, \.string), - .init(\.font, \.font), +// .init(\.string, \.string), // handle manually +// .init(\.font, \.font), // handle manually .init(\.fontSize, \.fontSize), .init(\.foregroundColor, \.foregroundColor), .init(\.isWrapped, \.isWrapped), @@ -107,6 +107,9 @@ open class JCATextLayer: JCALayer { guard let target = target as? CATextLayer else { return } Self.propertyMap.apply(to: target, from: self) + + target.string = string + target.font = font as? CFTypeRef } open override func applyProperties(with target: CALayer) { @@ -116,6 +119,7 @@ open class JCATextLayer: JCALayer { Self.propertyMap.apply(to: self, from: target) + self.string = target.string as? String if let font = target[keyPath: \.font] { switch CFGetTypeID(font) { case CFStringGetTypeID(): diff --git a/Sources/SDCALayer/Util/PropertyMap.swift b/Sources/SDCALayer/Util/PropertyMap.swift index e50dacc..d2c5708 100644 --- a/Sources/SDCALayer/Util/PropertyMap.swift +++ b/Sources/SDCALayer/Util/PropertyMap.swift @@ -55,9 +55,9 @@ extension PropertyMap { extension PropertyMap.MappingElement { public init( - _ sourceKeyPath: ReferenceWritableKeyPath, + _ sourceKeyPath: ReferenceWritableKeyPath, _ destinationKeyPath: ReferenceWritableKeyPath - ) where SourceValue: IndirectlyCodable, DestinationValue: IndirectlyCodableModel { + ) where SourceValue: IndirectlyCodable, DestinationValue: IndirectlyCodableModel, SourceValue.Model == DestinationValue, SourceValue == DestinationValue.Target { forwardMapElement = (sourceKeyPath, .init(destinationKeyPath)) reverseMapElement = (destinationKeyPath, .init(sourceKeyPath)) } @@ -65,16 +65,15 @@ extension PropertyMap.MappingElement { public init( _ sourceKeyPath: ReferenceWritableKeyPath, _ destinationKeyPath: ReferenceWritableKeyPath - ) where SourceValue: Sequence, DestinationValue: Sequence, SourceValue.Element: IndirectlyCodable, DestinationValue.Element: IndirectlyCodableModel { + ) where SourceValue: IndirectlyCodable, DestinationValue: IndirectlyCodableModel, SourceValue.Model == DestinationValue, SourceValue == DestinationValue.Target { forwardMapElement = (sourceKeyPath, .init(destinationKeyPath)) reverseMapElement = (destinationKeyPath, .init(sourceKeyPath)) } - public init( _ sourceKeyPath: ReferenceWritableKeyPath, - _ destinationKeyPath: ReferenceWritableKeyPath - ) { + _ destinationKeyPath: ReferenceWritableKeyPath + ) where SourceValue: Sequence, DestinationValue: Sequence, SourceValue.Element: IndirectlyCodable, DestinationValue.Element: IndirectlyCodableModel, SourceValue.Element.Model == DestinationValue.Element, SourceValue.Element == DestinationValue.Element.Target { forwardMapElement = (sourceKeyPath, .init(destinationKeyPath)) reverseMapElement = (destinationKeyPath, .init(sourceKeyPath)) } From a20184a6a7585590e4b7a65383f01065ddd08b03 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Thu, 4 Apr 2024 21:15:17 +0900 Subject: [PATCH 15/18] implemented `RawIndirectlyCodable` --- .../Protocol/RawIndirectlyCodable.swift | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sources/SDCALayer/Protocol/RawIndirectlyCodable.swift diff --git a/Sources/SDCALayer/Protocol/RawIndirectlyCodable.swift b/Sources/SDCALayer/Protocol/RawIndirectlyCodable.swift new file mode 100644 index 0000000..1fd8e57 --- /dev/null +++ b/Sources/SDCALayer/Protocol/RawIndirectlyCodable.swift @@ -0,0 +1,40 @@ +// +// RawIndirectlyCodable.swift +// +// +// Created by p-x9 on 2024/04/04. +// +// + +import Foundation +import IndirectlyCodable + +public protocol RawIndirectlyCodable: RawRepresentable & IndirectlyCodable where RawValue: Codable, Model: RawIndirectlyCodableModel, Model.Target == Self {} + +public protocol RawIndirectlyCodableModel: IndirectlyCodableModel where Target: RawIndirectlyCodable, Target.Model == Self { + var rawValue: Target.RawValue { get } + init(rawValue: Target.RawValue) +} + +extension RawIndirectlyCodableModel { + public init(with target: Target) { + self.init(rawValue: target.rawValue) + } + + public func converted() -> Target? { + .init(rawValue: rawValue) + } +} +// +//extension RawIndirectlyCodableModel { +// public func encode(to encoder: Encoder) throws { +// var container = encoder.singleValueContainer() +// try container.encode(rawValue) +// } +// +// public init(from decoder: Decoder) throws { +// let container = try decoder.singleValueContainer() +// let rawValue = try container.decode(Target.RawValue.self) +// self.init(rawValue: rawValue) +// } +//} From 16027e20a119200bb8904e292ece81aee3b036ec Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Thu, 4 Apr 2024 21:17:00 +0900 Subject: [PATCH 16/18] fix to use `RawIndirectlyCodable` --- .../CAGradientLayer+ICodable.swift | 6 +- .../IndirectlyCodable/CALayer+ICodable.swift | 36 ++------ .../CAScrollLayer+ICodable.swift | 6 +- .../CAShapeLayer+ICodable.swift | 18 +--- .../CATextLayer+ICodable.swift | 12 +-- .../Model/Layer/JCAGradientLayer.swift | 14 +-- Sources/SDCALayer/Model/Layer/JCALayer.swift | 85 ++++++------------- .../Model/Layer/JCAScrollLayer.swift | 14 +-- .../SDCALayer/Model/Layer/JCAShapeLayer.swift | 42 +++------ .../SDCALayer/Model/Layer/JCATextLayer.swift | 28 ++---- 10 files changed, 65 insertions(+), 196 deletions(-) diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CAGradientLayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CAGradientLayer+ICodable.swift index 05c1b1d..56728d5 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CAGradientLayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CAGradientLayer+ICodable.swift @@ -16,10 +16,6 @@ extension CAGradientLayer { } } -extension CAGradientLayerType: IndirectlyCodable { +extension CAGradientLayerType: RawIndirectlyCodable { public typealias Model = JCAGradientLayerType - - public func codable() -> JCAGradientLayerType? { - .init(with: self) - } } diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift index 9c48708..156b3e5 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CALayer+ICodable.swift @@ -25,51 +25,27 @@ extension CALayer: IndirectlyCodable { } } -extension CALayerCornerCurve: IndirectlyCodable { +extension CALayerCornerCurve: RawIndirectlyCodable { public typealias Model = JCALayerCornerCurve - - public func codable() -> JCALayerCornerCurve? { - .init(with: self) - } } -extension CACornerMask: IndirectlyCodable { +extension CACornerMask: RawIndirectlyCodable { public typealias Model = JCACornerMask - - public func codable() -> JCACornerMask? { - .init(with: self) - } } -extension CALayerContentsGravity: IndirectlyCodable { +extension CALayerContentsGravity: RawIndirectlyCodable { public typealias Model = JCALayerContentsGravity - - public func codable() -> Model? { - .init(with: self) - } } -extension CALayerContentsFormat: IndirectlyCodable { +extension CALayerContentsFormat: RawIndirectlyCodable { public typealias Model = JCALayerContentsFormat - - public func codable() -> Model? { - .init(with: self) - } } -extension CALayerContentsFilter: IndirectlyCodable { +extension CALayerContentsFilter: RawIndirectlyCodable { public typealias Model = JCALayerContentsFilter - - public func codable() -> Model? { - .init(with: self) - } } -extension CAEdgeAntialiasingMask: IndirectlyCodable { +extension CAEdgeAntialiasingMask: RawIndirectlyCodable { public typealias Model = JCAEdgeAntialiasingMask - - public func codable() -> Model? { - .init(with: self) - } } diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CAScrollLayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CAScrollLayer+ICodable.swift index 221fd6f..c2160b8 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CAScrollLayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CAScrollLayer+ICodable.swift @@ -16,10 +16,6 @@ extension CAScrollLayer { } } -extension CAScrollLayerScrollMode: IndirectlyCodable { +extension CAScrollLayerScrollMode: RawIndirectlyCodable { public typealias Model = JCAScrollLayerScrollMode - - public func codable() -> JCAScrollLayerScrollMode? { - .init(with: self) - } } diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CAShapeLayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CAShapeLayer+ICodable.swift index 9a10d82..2e18e58 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CAShapeLayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CAShapeLayer+ICodable.swift @@ -16,26 +16,14 @@ extension CAShapeLayer { } } -extension CAShapeLayerFillRule: IndirectlyCodable { +extension CAShapeLayerFillRule: RawIndirectlyCodable { public typealias Model = JCAShapeLayerFillRule - - public func codable() -> JCAShapeLayerFillRule? { - .init(with: self) - } } -extension CAShapeLayerLineCap: IndirectlyCodable { +extension CAShapeLayerLineCap: RawIndirectlyCodable { public typealias Model = JCAShapeLayerLineCap - - public func codable() -> JCAShapeLayerLineCap? { - .init(with: self) - } } -extension CAShapeLayerLineJoin: IndirectlyCodable { +extension CAShapeLayerLineJoin: RawIndirectlyCodable { public typealias Model = JCAShapeLayerLineJoin - - public func codable() -> JCAShapeLayerLineJoin? { - .init(with: self) - } } diff --git a/Sources/SDCALayer/Extension/IndirectlyCodable/CATextLayer+ICodable.swift b/Sources/SDCALayer/Extension/IndirectlyCodable/CATextLayer+ICodable.swift index 771261f..84f5750 100644 --- a/Sources/SDCALayer/Extension/IndirectlyCodable/CATextLayer+ICodable.swift +++ b/Sources/SDCALayer/Extension/IndirectlyCodable/CATextLayer+ICodable.swift @@ -16,18 +16,10 @@ extension CATextLayer { } } -extension CATextLayerTruncationMode: IndirectlyCodable { +extension CATextLayerTruncationMode: RawIndirectlyCodable { public typealias Model = JCATextLayerTruncationMode - - public func codable() -> JCATextLayerTruncationMode? { - .init(with: self) - } } -extension CATextLayerAlignmentMode: IndirectlyCodable { +extension CATextLayerAlignmentMode: RawIndirectlyCodable { public typealias Model = JCATextLayerAlignmentMode - - public func codable() -> JCATextLayerAlignmentMode? { - .init(with: self) - } } diff --git a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift index 6999879..5b85203 100644 --- a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift @@ -118,17 +118,11 @@ open class JCAGradientLayer: JCALayer { } } -public class JCAGradientLayerType: IndirectlyCodableModel { +public final class JCAGradientLayerType: RawIndirectlyCodableModel { public typealias Target = CAGradientLayerType - public var rawValue: String? - - required public init(with object: CAGradientLayerType) { - rawValue = object.rawValue - } - - public func converted() -> CAGradientLayerType? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } diff --git a/Sources/SDCALayer/Model/Layer/JCALayer.swift b/Sources/SDCALayer/Model/Layer/JCALayer.swift index ae4a3a4..8aa2e4d 100644 --- a/Sources/SDCALayer/Model/Layer/JCALayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCALayer.swift @@ -173,93 +173,56 @@ open class JCALayer: CALayerConvertible, Codable { } } -public class JCALayerCornerCurve: IndirectlyCodableModel { +public final class JCALayerCornerCurve: RawIndirectlyCodableModel { public typealias Target = CALayerCornerCurve - public var rawValue: String? - - required public init(with object: CALayerCornerCurve) { - rawValue = object.rawValue - } - - public func converted() -> CALayerCornerCurve? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } -public class JCACornerMask: IndirectlyCodableModel { +public final class JCACornerMask: RawIndirectlyCodableModel { public typealias Target = CACornerMask - public var rawValue: UInt? - - required public init(with object: CACornerMask) { - rawValue = object.rawValue - } - - public func converted() -> CACornerMask? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } -public class JCALayerContentsGravity: IndirectlyCodableModel { +public final class JCALayerContentsGravity: RawIndirectlyCodableModel { public typealias Target = CALayerContentsGravity - public var rawValue: String? - - required public init(with object: Target) { - rawValue = object.rawValue - } - - public func converted() -> Target? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } -public class JCALayerContentsFormat: IndirectlyCodableModel { +public final class JCALayerContentsFormat: RawIndirectlyCodableModel { public typealias Target = CALayerContentsFormat - public var rawValue: String? - - required public init(with object: Target) { - rawValue = object.rawValue - } - - public func converted() -> Target? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } -public class JCALayerContentsFilter: IndirectlyCodableModel { +public final class JCALayerContentsFilter: RawIndirectlyCodableModel { public typealias Target = CALayerContentsFilter - public var rawValue: String? - - required public init(with object: Target) { - rawValue = object.rawValue - } - - public func converted() -> Target? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } - -public class JCAEdgeAntialiasingMask: IndirectlyCodableModel { +public final class JCAEdgeAntialiasingMask: RawIndirectlyCodableModel { public typealias Target = CAEdgeAntialiasingMask - public var rawValue: UInt32? - - required public init(with object: Target) { - rawValue = object.rawValue - } - - public func converted() -> Target? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } diff --git a/Sources/SDCALayer/Model/Layer/JCAScrollLayer.swift b/Sources/SDCALayer/Model/Layer/JCAScrollLayer.swift index c997758..5a6ab6f 100644 --- a/Sources/SDCALayer/Model/Layer/JCAScrollLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAScrollLayer.swift @@ -76,17 +76,11 @@ open class JCAScrollLayer: JCALayer { } } -public class JCAScrollLayerScrollMode: IndirectlyCodableModel { +public final class JCAScrollLayerScrollMode: RawIndirectlyCodableModel { public typealias Target = CAScrollLayerScrollMode - public var rawValue: String? - - required public init(with object: CAScrollLayerScrollMode) { - rawValue = object.rawValue - } - - public func converted() -> CAScrollLayerScrollMode? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } diff --git a/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift b/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift index 7562461..3085eaf 100644 --- a/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift @@ -148,47 +148,29 @@ open class JCAShapeLayer: JCALayer { } } -public class JCAShapeLayerFillRule: IndirectlyCodableModel { +public final class JCAShapeLayerFillRule: RawIndirectlyCodableModel { public typealias Target = CAShapeLayerFillRule - public var rawValue: String? - - required public init(with object: CAShapeLayerFillRule) { - rawValue = object.rawValue - } - - public func converted() -> CAShapeLayerFillRule? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } -public class JCAShapeLayerLineCap: IndirectlyCodableModel { +public final class JCAShapeLayerLineCap: RawIndirectlyCodableModel { public typealias Target = CAShapeLayerLineCap - public var rawValue: String? - - required public init(with object: CAShapeLayerLineCap) { - rawValue = object.rawValue - } - - public func converted() -> CAShapeLayerLineCap? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } -public class JCAShapeLayerLineJoin: IndirectlyCodableModel { +public final class JCAShapeLayerLineJoin: RawIndirectlyCodableModel { public typealias Target = CAShapeLayerLineJoin - public var rawValue: String? - - required public init(with object: CAShapeLayerLineJoin) { - rawValue = object.rawValue - } - - public func converted() -> CAShapeLayerLineJoin? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } diff --git a/Sources/SDCALayer/Model/Layer/JCATextLayer.swift b/Sources/SDCALayer/Model/Layer/JCATextLayer.swift index a1cb00f..c487847 100644 --- a/Sources/SDCALayer/Model/Layer/JCATextLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCATextLayer.swift @@ -143,32 +143,20 @@ open class JCATextLayer: JCALayer { } } -public class JCATextLayerTruncationMode: IndirectlyCodableModel { +public final class JCATextLayerTruncationMode: RawIndirectlyCodableModel { public typealias Target = CATextLayerTruncationMode - public var rawValue: String? - - required public init(with object: CATextLayerTruncationMode) { - rawValue = object.rawValue - } - - public func converted() -> CATextLayerTruncationMode? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } -public class JCATextLayerAlignmentMode: IndirectlyCodableModel { +public final class JCATextLayerAlignmentMode: RawIndirectlyCodableModel { public typealias Target = CATextLayerAlignmentMode - public var rawValue: String? - - required public init(with object: CATextLayerAlignmentMode) { - rawValue = object.rawValue - } - - public func converted() -> CATextLayerAlignmentMode? { - guard let rawValue else { return nil } - return .init(rawValue: rawValue) + public var rawValue: Target.RawValue + public required init(rawValue: Target.RawValue) { + self.rawValue = rawValue } } From 4627a6133b7e9d1868200df1b5e9b6bab83f7b71 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Thu, 4 Apr 2024 21:28:28 +0900 Subject: [PATCH 17/18] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e53c570..f5b16c5 100644 --- a/README.md +++ b/README.md @@ -145,8 +145,8 @@ class JAALayer: JCALayer { // apply properties to model from target // targe -> model - public override func reverseApplyProperties(with target: CALayer) { - super.reverseApplyProperties(with: target) + public override func applyProperties(with target: CALayer) { + super.applyProperties(with: target) guard let target = target as? AALayer else { return } From 8c8d493c8d88887da39e65274587b2b679f0d3c8 Mon Sep 17 00:00:00 2001 From: p-x9 <50244599+p-x9@users.noreply.github.com> Date: Thu, 4 Apr 2024 22:09:33 +0900 Subject: [PATCH 18/18] Refactor CodingKeys --- .../SDCALayer/Model/Layer/JCAGradientLayer.swift | 6 +++++- Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift | 13 ++++++++++++- Sources/SDCALayer/Model/Layer/JCATextLayer.swift | 9 ++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift index 5b85203..cb6bec9 100644 --- a/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAGradientLayer.swift @@ -14,7 +14,11 @@ open class JCAGradientLayer: JCALayer { public typealias Target = CAGradientLayer private enum CodingKeys: String, CodingKey { - case colors, locations, startPoint, endPoint, type + case colors + case locations + case startPoint + case endPoint + case type } open override class var targetTypeName: String { diff --git a/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift b/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift index 3085eaf..2c0f392 100644 --- a/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCAShapeLayer.swift @@ -14,7 +14,18 @@ open class JCAShapeLayer: JCALayer { typealias Target = CAShapeLayer private enum CodingKeys: String, CodingKey { - case path, fillColor, fillRule, strokeColor, strokeStart, strokeEnd, lineWidth, miterLimit, lineCap, lineJoin, lineDashPhase, lineDashPattern + case path + case fillColor + case fillRule + case strokeColor + case strokeStart + case strokeEnd + case lineWidth + case miterLimit + case lineCap + case lineJoin + case lineDashPhase + case lineDashPattern } open override class var targetTypeName: String { diff --git a/Sources/SDCALayer/Model/Layer/JCATextLayer.swift b/Sources/SDCALayer/Model/Layer/JCATextLayer.swift index c487847..dea63f4 100644 --- a/Sources/SDCALayer/Model/Layer/JCATextLayer.swift +++ b/Sources/SDCALayer/Model/Layer/JCATextLayer.swift @@ -15,7 +15,14 @@ open class JCATextLayer: JCALayer { typealias Target = CATextLayer private enum CodingKeys: String, CodingKey { - case string, font, fontSize, foregroundColor, isWrapped, truncationMode, alignmentMode, allowsFontSubpixelQuantization + case string + case font + case fontSize + case foregroundColor + case isWrapped + case truncationMode + case alignmentMode + case allowsFontSubpixelQuantization } open override class var targetTypeName: String {