Skip to content

Commit

Permalink
💫 Update: Exp - AdaptiveModal
Browse files Browse the repository at this point in the history
Summary: Update experiment/test - `swift-programmatic-modal/AdaptiveModal`.
  • Loading branch information
dominicstop committed Jun 18, 2023
1 parent a0ed2cc commit da3199d
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,21 @@ struct AdaptiveModalInterpolationPoint: Equatable {
};

func apply(toModalBackgroundView modalBgView: UIView?){
modalBgView?.alpha = self.modalBackgroundOpacity;
modalBgView?.backgroundColor = self.modalBackgroundColor;
guard let modalBgView = modalBgView else { return };

modalBgView.alpha = self.modalBackgroundOpacity;
modalBgView.backgroundColor = self.modalBackgroundColor;
};

func apply(toModalBackgroundEffectView effectView: UIVisualEffectView?){
effectView?.alpha = self.modalBackgroundVisualEffectOpacity;
};

func apply(toBackgroundView bgView: UIView?){
bgView?.alpha = self.backgroundOpacity;
guard let bgView = bgView else { return };

bgView.alpha = self.backgroundOpacity;
bgView.backgroundColor = self.backgroundColor;
};

func apply(toBackgroundVisualEffectView effectView: UIView?){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,9 @@ class AdaptiveModalManager: NSObject {
+ "\n - currentInterpolationIndex: \(self.currentInterpolationIndex)"
+ "\n - modalView gestureRecognizers: \(self.modalView?.gestureRecognizers.debugDescription ?? "N/A")"
+ "\n - interpolationSteps.computedRect: \(self.interpolationSteps.map({ $0.computedRect }))"
+ "\n - interpolationSteps.percent: \(self.interpolationSteps.map({ $0.percent }))"
+ "\n - interpolationSteps.backgroundVisualEffectIntensity: \(self.interpolationSteps.map({ $0.backgroundVisualEffectIntensity }))"
+ "\n - interpolationSteps.backgroundVisualEffect: \(self.interpolationSteps.map({ $0.backgroundVisualEffect }))"
+ "\n"
);
};
Expand Down Expand Up @@ -1433,6 +1436,8 @@ class AdaptiveModalManager: NSObject {
if shouldDismiss {
self.notifyOnModalDidHide();
};

self.debug();
};

private func notifyOnModalWillHide(){
Expand Down
114 changes: 69 additions & 45 deletions experiments/swift-programmatic-modal/RNILayout/RNILayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public struct RNILayout {
public let width : RNILayoutValue;
public let height: RNILayoutValue;

public let marginLeft : RNILayoutValueMode?;
public let marginRight : RNILayoutValueMode?;
public let marginTop : RNILayoutValueMode?;
public let marginBottom: RNILayoutValueMode?;
public let marginLeft : RNILayoutValue?;
public let marginRight : RNILayoutValue?;
public let marginTop : RNILayoutValue?;
public let marginBottom: RNILayoutValue?;

// MARK: - Init
// ------------
Expand All @@ -41,10 +41,10 @@ public struct RNILayout {
width : RNILayoutValue,
height: RNILayoutValue,

marginLeft : RNILayoutValueMode? = nil,
marginRight : RNILayoutValueMode? = nil,
marginTop : RNILayoutValueMode? = nil,
marginBottom: RNILayoutValueMode? = nil
marginLeft : RNILayoutValue? = nil,
marginRight : RNILayoutValue? = nil,
marginTop : RNILayoutValue? = nil,
marginBottom: RNILayoutValue? = nil
) {
self.horizontalAlignment = horizontalAlignment;
self.verticalAlignment = verticalAlignment;
Expand All @@ -66,10 +66,10 @@ public struct RNILayout {
width : RNILayoutValue? = nil,
height: RNILayoutValue? = nil,

marginLeft : RNILayoutValueMode? = nil,
marginRight : RNILayoutValueMode? = nil,
marginTop : RNILayoutValueMode? = nil,
marginBottom: RNILayoutValueMode? = nil
marginLeft : RNILayoutValue? = nil,
marginRight : RNILayoutValue? = nil,
marginTop : RNILayoutValue? = nil,
marginBottom: RNILayoutValue? = nil
) {
self.horizontalAlignment = horizontalAlignment ?? prev.horizontalAlignment;
self.verticalAlignment = verticalAlignment ?? prev.verticalAlignment;
Expand Down Expand Up @@ -156,6 +156,54 @@ public struct RNILayout {
return rect;
};

public func computeMargins(
usingLayoutValueContext context: RNILayoutValueContext
) -> (
left : CGFloat?,
right : CGFloat?,
top : CGFloat?,
bottom: CGFloat?,

horizontal: CGFloat,
vertical : CGFloat
) {
let computedMarginLeft = self.marginLeft?.computeValue(
usingLayoutValueContext: context,
preferredSizeKey: \.width
);

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

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

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

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

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

return (
left : computedMarginLeft ?? 0,
right : computedMarginRight ?? 0,
top : computedMarginTop ?? 0,
bottom: computedMarginBottom ?? 0,

horizontal: computedMarginHorizontal,
vertical : computedMarginVertical
);
};

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

Expand All @@ -182,40 +230,16 @@ public struct RNILayout {

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 computedMarginVertical =
(computedMarginTop ?? 0) + (computedMarginBottom ?? 0);
let computedMargins = self.computeMargins(usingLayoutValueContext: context);

// Margin - X-Axis
switch self.horizontalAlignment {
case .left:
guard let marginLeft = computedMarginLeft else { break };
guard let marginLeft = computedMargins.left else { break };
rect.origin.x = rect.origin.x + marginLeft;

case .right:
guard let marginRight = computedMarginRight else { break };
guard let marginRight = computedMargins.right else { break };
rect.origin.x = rect.origin.x - marginRight;

case .center:
Expand All @@ -225,11 +249,11 @@ public struct RNILayout {
// Margin - Y-Axis
switch self.verticalAlignment {
case .top:
guard let marginTop = computedMarginTop else { break };
guard let marginTop = computedMargins.top else { break };
rect.origin.y = rect.origin.y + marginTop;

case .bottom:
guard let marginBottom = computedMarginBottom else { break };
guard let marginBottom = computedMargins.bottom else { break };
rect.origin.y = rect.origin.y - marginBottom;

case .center:
Expand All @@ -238,24 +262,24 @@ public struct RNILayout {

let shouldRecomputeXAxis: Bool = {
switch self.width.mode {
case .stretch: return abs(computedMarginHorizontal) < rect.width;
case .stretch: return abs(computedMargins.horizontal) < rect.width;
default: return false;
};
}();

let shouldRecomputeYAxis: Bool = {
switch self.height.mode {
case .stretch: return abs(computedMarginVertical) < rect.height;
case .stretch: return abs(computedMargins.vertical) < rect.height;
default: return false;
};
}();

if shouldRecomputeXAxis {
rect.size.width = rect.size.width - computedMarginHorizontal;
rect.size.width = rect.size.width - computedMargins.horizontal;
};

if shouldRecomputeYAxis {
rect.size.height = rect.size.height - computedMarginVertical;
rect.size.height = rect.size.height - computedMargins.vertical;
};

if shouldRecomputeXAxis || shouldRecomputeYAxis {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ enum RNILayoutPreset {
marginLeft: .constant(0),
marginRight: .percent(
relativeTo: .currentWidth,
percentValue: 1
percentValue: -1
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public struct RNILayoutValue {
preferredSizeKey: nil
);

let computedMaxValue = self.minValue?.compute(
let computedMaxValue = self.maxValue?.compute(
usingLayoutValueContext: context,
preferredSizeKey: nil
);
Expand Down
Loading

0 comments on commit da3199d

Please sign in to comment.