Skip to content

Commit

Permalink
🛠 Refactor: Split RNIIdentifiable
Browse files Browse the repository at this point in the history
Summary: Extract types in `ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable` into their own files.
  • Loading branch information
dominicstop committed Apr 26, 2023
1 parent 737047b commit ef64878
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -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;
};
};
74 changes: 0 additions & 74 deletions ios/src_library/React Native/RNIIdentifiable/RNIIdentifiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,6 @@

import Foundation


fileprivate class Counter {
static var typeToCounterMap: Dictionary<String, Int> = [:];

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 {

Expand All @@ -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;
};
};
Original file line number Diff line number Diff line change
@@ -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<String, Int> = [:];

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

0 comments on commit ef64878

Please sign in to comment.