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); + }; +};