Skip to content

Commit

Permalink
⭐️ Impl: RNIComputableSize - Min/Max Size
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed May 19, 2023
1 parent f96c970 commit 065d09e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 9 deletions.
24 changes: 24 additions & 0 deletions ios/src_library/Extensions/FloatingPoint+Clamping.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// FloatingPoint+Helpers.swift
// swift-programmatic-modal
//
// Created by Dominic Go on 5/19/23.
//

import Foundation

extension FloatingPoint {
public func clamped(min lowerBound: Self?, max upperBound: Self?) -> Self {
var clampedValue = self;

if let upperBound = upperBound {
clampedValue = min(clampedValue, upperBound);
};

if let lowerBound = lowerBound {
clampedValue = max(clampedValue, lowerBound);
};

return clampedValue;
};
};
67 changes: 60 additions & 7 deletions ios/src_library/React Native/RNIComputable/RNIComputableSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@
//

import Foundation
import JavaScriptCore


public struct RNIComputableSize {

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

public let mode: RNIComputableSizeMode;

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

public func computeOffsets(withSize size: CGSize) -> CGSize {
public let minWidth: CGFloat?;
public let minHeight: CGFloat?;

public let maxWidth: CGFloat?;
public let maxHeight: CGFloat?;

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

func sizeWithOffsets(forSize size: CGSize) -> CGSize {
let offsetWidth =
self.offsetWidth?.compute(withValue: size.width);

Expand All @@ -28,7 +39,23 @@ public struct RNIComputableSize {
);
};

public func compute(
func sizeWithClamp(forSize size: CGSize) -> CGSize {
return CGSize(
width: size.width.clamped(
min: self.minWidth,
max: self.maxWidth
),
height: size.height.clamped(
min: self.minHeight,
max: self.maxHeight
)
);
};

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

public func computeRaw(
withTargetSize targetSize: CGSize,
currentSize: CGSize
) -> CGSize {
Expand All @@ -50,16 +77,17 @@ public struct RNIComputableSize {
};
};

public func computeWithOffsets(
public func compute(
withTargetSize targetSize: CGSize,
currentSize: CGSize
) -> CGSize {
let computedSize = self.compute(
let rawSize = self.computeRaw(
withTargetSize: targetSize,
currentSize: currentSize
);

return self.computeOffsets(withSize: computedSize);
let clampedSize = self.sizeWithClamp(forSize: rawSize);
return self.sizeWithOffsets(forSize: clampedSize);
};
};

Expand All @@ -85,11 +113,36 @@ extension RNIComputableSize {

return offset;
}();

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

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

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

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

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

self.offsetWidth = nil;
self.offsetHeight = nil;
self.minWidth = nil;
self.minHeight = nil;
self.maxWidth = nil;
self.maxHeight = 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
Expand Up @@ -10,8 +10,16 @@ import Foundation
public enum RNIComputableSizeMode {
case current;
case stretch;
case constant(constantWidth: Double, constantHeight: Double);
case percent(percentWidth: Double, percentHeight: Double);

case constant(
constantWidth: Double,
constantHeight: Double
);

case percent(
percentWidth: Double,
percentHeight: Double
);
};

extension RNIComputableSizeMode {
Expand Down
6 changes: 6 additions & 0 deletions src/types/RNIComputable/RNIComputableSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import type { RNIComputableOffset } from './RNIComputableOffset';
type RNIComputableSizeShared = {
offsetWidth?: RNIComputableOffset;
offsetHeight?: RNIComputableOffset;

minWidth?: number;
minHeight?: number;

maxWidth?: number;
maxHeight?: number;
};

type RNIComputableSizeModeBase = {
Expand Down

0 comments on commit 065d09e

Please sign in to comment.