Skip to content

Commit

Permalink
💫 Update: Exp - AdaptiveModal
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-06-04-23-45-31` - Impl: Adaptive Modal - `LayoutComputableValue`.

Summary: Update experiment/test - `swift-programmatic-modal/AdaptiveModal`.
  • Loading branch information
dominicstop committed Jun 8, 2023
1 parent 546b292 commit d4ee43c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 72 deletions.
117 changes: 52 additions & 65 deletions experiments/swift-programmatic-modal/RNILayout/RNILayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public struct RNILayout {
usingLayoutValueContext: context,
preferredSizeKey: \.height
);

print(
"computeRawRectSize"
+ "\n - computedWidth: \(computedWidth)"
+ "\n - computedHeight: \(computedHeight)"
);

return CGSize(
width : computedWidth ?? 0,
Expand All @@ -115,43 +121,41 @@ public struct RNILayout {
///
public func computeRawRectOrigin(
usingLayoutValueContext context: RNILayoutValueContext,
withTargetRect targetRect: CGRect,
forRect rect: CGRect? = nil,
withSize size: CGSize? = nil,
ignoreXAxis: Bool = false,
ignoreYAxis: Bool = false
) -> CGRect {

let origin = rect?.origin ?? .zero;
let size = rect?.size ?? size ?? .zero;
let size = rect?.size ?? context.currentSize ?? .zero;

var rect = CGRect(origin: origin, size: size);

if !ignoreXAxis {
// Compute Origin - X
switch self.horizontalAlignment {
case .center:
rect.setPoint(midX: targetRect.midX);
rect.setPoint(midX: context.targetRect.midX);

case .left:
rect.setPoint(minX: targetRect.minX);
rect.setPoint(minX: context.targetRect.minX);

case .right:
rect.setPoint(maxX: targetRect.maxX);
rect.setPoint(maxX: context.targetRect.maxX);
};
};

if !ignoreYAxis {
// Compute Origin - Y
switch self.verticalAlignment {
case .center:
rect.setPoint(midY: targetRect.midY);
rect.setPoint(midY: context.targetRect.midY);

case .top:
rect.setPoint(minY: targetRect.minY);
rect.setPoint(minY: context.targetRect.minY);

case .bottom:
rect.setPoint(maxY: targetRect.maxY);
rect.setPoint(maxY: context.targetRect.maxY);
};
};

Expand All @@ -176,60 +180,68 @@ public struct RNILayout {
let computedSize = self.computeRawRectSize(
usingLayoutValueContext: baseContext
);

var rect = self.computeRawRectOrigin(
usingLayoutValueContext: baseContext,
withTargetRect: baseContext.targetRect
);

let context = RNILayoutValueContext(
derivedFrom: baseContext,
currentSize: computedSize
);

var rect = self.computeRawRectOrigin(usingLayoutValueContext: context);

let computedMarginLeft = self.marginLeft?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.width
);

let computedMarginRight = self.marginRight?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.width
);

let computedMarginTop = self.marginTop?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.height
);

let computedMarginBottom = self.marginBottom?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.height
);

let computedMarginHorizontal =
(computedMarginLeft ?? 0) + (computedMarginRight ?? 0);

let computedMargiVertical =
(computedMarginTop ?? 0) + (computedMarginBottom ?? 0);

print(
"computeRect"
+ "\n - computedMarginLeft: \(computedMarginLeft)"
+ "\n - computedMarginRight: \(computedMarginRight)"
+ "\n - computedMarginTop: \(computedMarginTop)"
+ "\n - computedMarginBottom: \(computedMarginBottom)"
+ "\n - computedMarginHorizontal: \(computedMarginHorizontal)"
+ "\n - computedMargiVertical: \(computedMargiVertical)"
);


// Margin - X-Axis
switch self.horizontalAlignment {
case .left:
let computedMarginLeft =
self.marginLeft?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.width
);

guard let marginLeft = computedMarginLeft else { break };
rect.origin.x = rect.origin.x + marginLeft;

case .right:
let computedMarginRight = self.marginRight?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.width
);

guard let marginRight = computedMarginRight else { break };
rect.origin.x = rect.origin.x - marginRight;

case .center:
if case .stretch = self.width.mode {
let computedMarginHorizontal: CGFloat = {
let computedMarginLeft = self.marginLeft?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.width
);

let computedMarginRight = self.marginRight?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.width
);

return (computedMarginLeft ?? 0) + (computedMarginRight ?? 0);
}();

rect.size.width = rect.size.width - computedMarginHorizontal;

// re-compute origin
rect = self.computeRawRectOrigin(
usingLayoutValueContext: context,
withTargetRect: context.targetRect,
forRect: rect,
ignoreYAxis: true
);
Expand All @@ -239,45 +251,20 @@ public struct RNILayout {
// Margin - Y-Axis
switch self.verticalAlignment {
case .top:
let computedMarginTop = self.marginTop?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.height
);

guard let marginTop = computedMarginTop else { break };
rect.origin.y = rect.origin.y + marginTop;

case .bottom:
let computedMarginBottom = self.marginBottom?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.height
);

guard let marginBottom = computedMarginBottom else { break };
rect.origin.y = rect.origin.y - marginBottom;

case .center:
if case .stretch = self.height.mode {
let computedMargiVertical: CGFloat = {
let computedMarginTop = self.marginTop?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.height
);

let computedMarginBottom = self.marginBottom?.compute(
usingLayoutValueContext: context,
preferredSizeKey: \.height
);

return (computedMarginTop ?? 0) + (computedMarginBottom ?? 0);
}();

rect.size.height = rect.size.height - computedMargiVertical;

// re-compute origin
rect = self.computeRawRectOrigin(
usingLayoutValueContext: context,
withTargetRect: context.targetRect,
forRect: rect,
ignoreXAxis: true
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public struct RNILayoutValue {
usingLayoutValueContext context: RNILayoutValueContext,
toValue value: CGFloat
) -> CGFloat? {
guard let offsetValue = self.offsetValue else { return nil };
guard let offsetValue = self.offsetValue else { return value };

let computedOffsetValue = offsetValue.compute(
usingLayoutValueContext: context,
preferredSizeKey: nil
);

guard let computedOffsetValue = computedOffsetValue else { return nil };
guard let computedOffsetValue = computedOffsetValue else { return value };

let computableOffset = RNIComputableOffset(
offset: computedOffsetValue,
Expand All @@ -80,7 +80,9 @@ public struct RNILayoutValue {
preferredSizeKey: nil
);

return value.clamped(min: computedMinValue, max: computedMaxValue);
let clamped = value.clamped(min: computedMinValue, max: computedMaxValue);

return clamped;
};

public func computeRawValue(
Expand Down Expand Up @@ -111,6 +113,12 @@ public struct RNILayoutValue {
toValue: computedValueRaw ?? 0
);

print(
"RNILayoutValue.computeValue"
+ "\n - computedValueRaw: \(computedValueRaw)"
+ "\n - computedValueWithOffsets: \(computedValueWithOffsets)"
);

return self.clampValue(
usingLayoutValueContext: context,
forValue: computedValueWithOffsets ?? 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ class RNILayoutTestViewController : UIViewController {
self.view.addSubview(floatingView);

self.view = view;
self.updateFloatingView();
};

override func viewDidLayoutSubviews() {
self.applyRadiusMaskFor();
self.updateFloatingView();
// self.applyRadiusMaskFor();
};

func updateFloatingView(){
Expand All @@ -381,11 +381,16 @@ class RNILayoutTestViewController : UIViewController {
usingLayoutValueContext: layoutValueContext
);

print(
"updateFloatingView"
+ "\n - computedRect: \(computedRect)"
);

let floatingView = self.floatingView;
floatingView.frame = computedRect;

self.floatingViewLabel.text = "\(self.layoutConfigIndex)";
self.applyRadiusMaskFor();
//self.applyRadiusMaskFor();
};

@objc func onPressFloatingView(_ sender: UITapGestureRecognizer){
Expand Down
2 changes: 1 addition & 1 deletion experiments/swift-programmatic-modal/Test/TestRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import UIKit

enum TestRoutes {
static let rootRouteKey: Self = .RNIDraggableTest;
static let rootRouteKey: Self = .RNILayoutTest;

case RNILayoutTest;
case RNIDraggableTest;
Expand Down

0 comments on commit d4ee43c

Please sign in to comment.