Skip to content

Commit

Permalink
⭐️ Impl: RNIComputableValue
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed May 19, 2023
1 parent 065d09e commit 6596f25
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 1 deletion.
111 changes: 111 additions & 0 deletions ios/src_library/React Native/RNIComputable/RNIComputableValue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// RNIComputableValue.swift
// swift-programmatic-modal
//
// Created by Dominic Go on 5/19/23.
//

import Foundation

public struct RNIComputableValue {

// MARK: - Properties
// ------------------

public let mode: RNIComputableValueMode;

public let offset: RNIComputableOffset?;

public let minValue: CGFloat?;
public let maxValue: CGFloat?;


// MARK: - Internal Functions
// --------------------------


func valueWithOffsets(forValue value: CGFloat) -> CGFloat {
return self.offset?.compute(withValue: value) ?? value;
};

func valueWithClamp(forValue value: CGFloat) -> CGFloat {
return value.clamped(
min: self.minValue,
max: self.maxValue
);
};

// MARK: - Functions
// -----------------

public func computeRaw(
withTargetValue targetValue: CGFloat,
currentValue: CGFloat
) -> CGFloat {
switch self.mode {
case .current:
return currentValue;

case .stretch:
return targetValue;

case let .constant(constantValue):
return constantValue;

case let .percent(percentValue):
return percentValue * targetValue;
};
};

public func compute(
withTargetValue targetValue: CGFloat,
currentValue: CGFloat
) -> CGFloat {
let rawValue = self.computeRaw(
withTargetValue: targetValue,
currentValue: currentValue
);

let clampedValue = self.valueWithClamp(forValue: rawValue);
return self.valueWithOffsets(forValue: clampedValue);
};
};

extension RNIComputableValue {
public init?(fromDict dict: NSDictionary){
guard let mode = RNIComputableValueMode(fromDict: dict)
else { return nil };

self.mode = mode;

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

return offset;
}();

self.minValue =
Self.getDoubleValue(forDict: dict, withKey: "minValue");

self.maxValue =
Self.getDoubleValue(forDict: dict, withKey: "maxValue");
};

public init(mode: RNIComputableValueMode){
self.mode = mode;

self.offset = nil;
self.minValue = nil;
self.maxValue = nil;
};

static private func getDoubleValue(
forDict dict: NSDictionary,
withKey key: String
) -> CGFloat? {
guard let number = dict[key] as? NSNumber else { return nil };
return number.doubleValue;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// RNIComputableValueMode.swift
// swift-programmatic-modal
//
// Created by Dominic Go on 5/19/23.
//

import Foundation


public enum RNIComputableValueMode {
case current;
case stretch;
case constant(constantValue: Double);
case percent(percentValue: Double);
};

extension RNIComputableValueMode {
public 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 value = dict["constantValue"] as? NSNumber
else { return nil };

self = .constant(
constantValue: value.doubleValue
);

case "percent":
guard let value = dict["percentValue"] as? NSNumber
else { return nil };

self = .percent(percentValue: value.doubleValue);

default:
return nil;
};
};
};

Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public class RNIModalViewController: UIViewController {
);

default:
self.preferredContentSize = computableSize.computeWithOffsets(
self.preferredContentSize = computableSize.compute(
withTargetSize: targetSize, currentSize: .zero
);
};
Expand Down
27 changes: 27 additions & 0 deletions src/types/RNIComputable/RNIComputableValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable prettier/prettier */

import type { RNIComputableOffset } from './RNIComputableOffset';


type RNIComputableValueShared = {
offset?: RNIComputableOffset;
minValue?: number;
maxValue?: number;
};

type RNIComputableValueModeBase = {
mode: 'current';
} | {
mode: 'stretch';
} | {
mode: 'constant';
constantValue: number;
} | {
mode: 'percent';
percentValue: number;
};


export type RNIComputableValue =
| RNIComputableValueModeBase
& RNIComputableValueShared;
1 change: 1 addition & 0 deletions src/types/RNIComputable/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './RNIComputableOffset';
export * from './RNIComputableSize';
export * from './RNIComputableValue';

0 comments on commit 6596f25

Please sign in to comment.