Skip to content

Commit

Permalink
⭐️ Impl: RNIComputable
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-04-26-07-30-15` - Impl. `RNIComputable`.
  • Loading branch information
dominicstop committed Apr 28, 2023
1 parent 1810e29 commit e0314ce
Show file tree
Hide file tree
Showing 7 changed files with 530 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// RNIComputableCommandArguments.swift
// react-native-ios-modal
//
// Created by Dominic Go on 4/27/23.
//

import Foundation

public class RNIComputableCommandArguments {
public struct GetView: RNIDictionarySynthesizable {

public let tag: Int?;
public let reactTag: NSNumber?;
public let nativeID: String?;

public init(fromDict dict: NSDictionary) {
self.tag = {
guard let tag = dict["tag"] as? NSNumber else { return nil };
return tag.intValue;
}();

self.reactTag = dict["reactTag"] as? NSNumber;
self.nativeID = dict["nativeID"] as? String;
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// RNIComputableOffset.swift
// react-native-ios-modal
//
// Created by Dominic Go on 4/28/23.
//

import Foundation

public struct RNIComputableOffset {

public enum OffsetOperation: String {
case multiply, divide, add, subtract;

func compute(a: Double, b: Double) -> Double {
switch self {
case .add:
return a + b;

case .divide:
return a / b;

case .multiply:
return a * b;

case .subtract:
return a - b;
};
};
};

public var offset: Double;
public var offsetOperation: OffsetOperation;

func compute(withValue value: Double) -> Double {
return self.offsetOperation.compute(a: self.offset, b: value);
};
};

extension RNIComputableOffset {

init?(fromDict dict: NSDictionary){
guard let offset = dict["offset"] as? NSNumber,
let offsetOperationRaw = dict["offsetOperation"] as? String,
let offsetOperation = OffsetOperation(rawValue: offsetOperationRaw)
else { return nil };

self.offset = offset.doubleValue;
self.offsetOperation = offsetOperation;
};
};
61 changes: 61 additions & 0 deletions ios/src_library/React Native/RNIComputable/RNIComputableSize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// RNIComputableValue.swift
// react-native-ios-modal
//
// Created by Dominic Go on 4/26/23.
//

import Foundation
import JavaScriptCore


public struct RNIComputableSize {
let mode: RNIComputableSizeMode;

let offsetWidth: RNIComputableOffset?;
let offsetHeight: RNIComputableOffset?;

func computeOffsets(withSize size: CGSize) -> CGSize {
let offsetWidth =
self.offsetWidth?.compute(withValue: size.width) ?? 0;

let offsetHeight =
self.offsetHeight?.compute(withValue: size.height) ?? 0;

return CGSize(
width: size.width + offsetWidth,
height: size.height + offsetHeight
);
};
};

extension RNIComputableSize {
init?(fromDict dict: NSDictionary){
guard let mode = RNIComputableSizeMode(fromDict: dict)
else { return nil };

self.mode = mode;

self.offsetWidth = {
guard let offsetRaw = dict["offsetWidth"] as? NSDictionary,
let offset = RNIComputableOffset(fromDict: offsetRaw)
else { return nil };

return offset;
}();

self.offsetHeight = {
guard let offsetRaw = dict["offsetHeight"] as? NSDictionary,
let offset = RNIComputableOffset(fromDict: offsetRaw)
else { return nil };

return offset;
}();
};

init(mode: RNIComputableSizeMode){
self.mode = mode;
self.offsetWidth = nil;
self.offsetHeight = nil;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// RNIComputableSizeEvaluator.swift
// react-native-ios-modal
//
// Created by Dominic Go on 4/28/23.
//

import Foundation

public class RNIComputableSizeEvaluator {

var computableSize = RNIComputableSize(mode: .stretch);
var targetSize: Double?;

lazy var computableValueEvaluator = RNIComputableValueEvaluator();

func evaluate(
withTargetSize targetSize: CGSize?,
currentSize: CGSize?,
rootView: UIView?,
targetView: UIView?
) -> CGSize? {
let computedSize: CGSize? = {
switch self.computableSize.mode {
case .current:
guard let targetSize = targetSize else { return nil };
return targetSize;

case .stretch:
guard let currentSize = targetSize else { return nil };
return currentSize;

case let .constant(constantWidth, constantHeight):
return CGSize(
width: constantWidth,
height: constantHeight
);

case let .percent(percentWidth, percentHeight):
guard let targetSize = targetSize else { return nil };

return CGSize(
width: targetSize.width * percentWidth,
height: targetSize.height * percentHeight
);

case let .function(valueFunction):
let evaluator = self.computableValueEvaluator;

evaluator.rootView = rootView;
evaluator.targetView = targetView;
evaluator.jsString = valueFunction;

guard let resultRaw = evaluator.computedValue,
let resultDict = resultRaw.toDictionary(),
let resultSize = CGSize(fromDict: resultDict as NSDictionary)
else { return nil };

return resultSize;
};
}();

guard let computedSize = computedSize else { return nil };
return self.computableSize.computeOffsets(withSize: computedSize);
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// RNIComputableSizeOffset.swift
// react-native-ios-modal
//
// Created by Dominic Go on 4/28/23.
//

import Foundation

public enum RNIComputableSizeMode {
case current;
case stretch;
case constant(constantWidth: Double, constantHeight: Double);
case percent(percentWidth: Double, percentHeight: Double);
case function(valueFunction: String);
};

extension RNIComputableSizeMode {
init?(fromDict dict: NSDictionary){
guard let mode = dict["mode"] as? String else { return nil };

switch mode {
case "current":
self = .current;

case "stretch":
self = .stretch;

case "constant":
guard let width = dict["constantWidth"] as? NSNumber,
let height = dict["constantHeight"] as? NSNumber
else { return nil };

self = .constant(
constantWidth: width.doubleValue,
constantHeight: height.doubleValue
);

case "percent":
guard let width = dict["percentWidth"] as? NSNumber,
let height = dict["percentHeight"] as? NSNumber
else { return nil };

self = .percent(
percentWidth: width.doubleValue,
percentHeight: height.doubleValue
);

case "function":
guard let value = dict["valueFunction"] as? String else { return nil };
self = .function(valueFunction: value);

default:
return nil;
};
};
};
Loading

0 comments on commit e0314ce

Please sign in to comment.