From ef648787531f466d5eeab5b4cbae9f7ceed5c2bf Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Thu, 27 Apr 2023 01:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=20Refactor:=20Split=20`RNIIdentifi?= =?UTF-8?q?able`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Extract types in `ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable` into their own files. --- .../RNIIdentifiable+Default.swift | 37 ++++++++++ .../RNIIdentifiable/RNIIdentifiable.swift | 74 ------------------- .../RNIIdentifiable/RNIObjectIdentifier.swift | 54 ++++++++++++++ 3 files changed, 91 insertions(+), 74 deletions(-) create mode 100644 ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable+Default.swift create mode 100644 ios/src_library/React Native/RNIIdentifiable/RNIObjectIdentifier.swift diff --git a/ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable+Default.swift b/ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable+Default.swift new file mode 100644 index 00000000..946bef9d --- /dev/null +++ b/ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable+Default.swift @@ -0,0 +1,37 @@ +// +// RNIIdentifiable+Default.swift +// react-native-ios-modal +// +// Created by Dominic Go on 4/27/23. +// + +import Foundation + +extension RNIIdentifiable { + public static var synthesizedIdPrefix: String { + String(describing: Self.self) + "-"; + }; + + public var synthesizedIdentifier: RNIObjectIdentifier { + if let identifier = self.metadata { + return identifier; + }; + + let identifier = RNIObjectIdentifier(type: Self.self); + self.metadata = identifier; + + return identifier; + }; + + public var synthesizedID: Int { + self.synthesizedIdentifier.id; + }; + + public var synthesizedStringID: String { + Self.synthesizedIdPrefix + "\(self.synthesizedID)"; + }; + + public var synthesizedUUID: UUID { + self.synthesizedIdentifier.uuid; + }; +}; diff --git a/ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable.swift b/ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable.swift index 8a372778..d19bcca8 100644 --- a/ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable.swift +++ b/ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable.swift @@ -7,52 +7,6 @@ import Foundation - -fileprivate class Counter { - static var typeToCounterMap: Dictionary = [:]; - - static func getTypeString(ofType _type: Any) -> String { - return String(describing: type(of: _type)); - }; - - static func set(forType type: Any, counter: Int) { - let typeString = Self.getTypeString(ofType: type); - Self.typeToCounterMap[typeString] = counter; - }; - - static func set(forType typeString: String, counter: Int) { - Self.typeToCounterMap[typeString] = counter; - }; - - static func get(forType type: Any) -> Int { - let typeString = Self.getTypeString(ofType: type); - - guard let counter = Self.typeToCounterMap[typeString] else { - Self.set(forType: typeString, counter: -1); - return -1; - }; - - return counter; - }; - - static func getAndIncrement(forType type: Any) -> Int { - let prevCount = Self.get(forType: type); - let nextCount = prevCount + 1; - - Self.set(forType: type, counter: nextCount); - return nextCount; - }; -}; - -public final class RNIObjectIdentifier { - public let id: Int; - public let uuid = UUID(); - - public init(type: Any) { - self.id = Counter.getAndIncrement(forType: type); - }; -}; - public protocol RNIIdentifiable: AnyObject, RNIObjectMetadata where T == RNIObjectIdentifier { @@ -64,31 +18,3 @@ public protocol RNIIdentifiable: }; -extension RNIIdentifiable { - public static var synthesizedIdPrefix: String { - String(describing: Self.self) + "-"; - }; - - public var synthesizedIdentifier: RNIObjectIdentifier { - if let identifier = self.metadata { - return identifier; - }; - - let identifier = RNIObjectIdentifier(type: Self.self); - self.metadata = identifier; - - return identifier; - }; - - public var synthesizedID: Int { - self.synthesizedIdentifier.id; - }; - - public var synthesizedStringID: String { - Self.synthesizedIdPrefix + "\(self.synthesizedID)"; - }; - - public var synthesizedUUID: UUID { - self.synthesizedIdentifier.uuid; - }; -}; diff --git a/ios/src_library/React Native/RNIIdentifiable/RNIObjectIdentifier.swift b/ios/src_library/React Native/RNIIdentifiable/RNIObjectIdentifier.swift new file mode 100644 index 00000000..a2da669a --- /dev/null +++ b/ios/src_library/React Native/RNIIdentifiable/RNIObjectIdentifier.swift @@ -0,0 +1,54 @@ +// +// RNIObjectIdentifier.swift +// react-native-ios-modal +// +// Created by Dominic Go on 4/27/23. +// + +import Foundation + +fileprivate class Counter { + static var typeToCounterMap: Dictionary = [:]; + + static func getTypeString(ofType _type: Any) -> String { + return String(describing: type(of: _type)); + }; + + static func set(forType type: Any, counter: Int) { + let typeString = Self.getTypeString(ofType: type); + Self.typeToCounterMap[typeString] = counter; + }; + + static func set(forType typeString: String, counter: Int) { + Self.typeToCounterMap[typeString] = counter; + }; + + static func get(forType type: Any) -> Int { + let typeString = Self.getTypeString(ofType: type); + + guard let counter = Self.typeToCounterMap[typeString] else { + Self.set(forType: typeString, counter: -1); + return -1; + }; + + return counter; + }; + + static func getAndIncrement(forType type: Any) -> Int { + let prevCount = Self.get(forType: type); + let nextCount = prevCount + 1; + + Self.set(forType: type, counter: nextCount); + return nextCount; + }; +}; + +public final class RNIObjectIdentifier { + + public let id: Int; + public let uuid = UUID(); + + public init(type: Any) { + self.id = Counter.getAndIncrement(forType: type); + }; +};