From 2b433514ed072b0dbcdf6c7874d8943ed244489c Mon Sep 17 00:00:00 2001 From: Dominic Go Date: Sun, 6 Oct 2024 09:37:11 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Impl:=20`RNIContentSizingM?= =?UTF-8?q?ode`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ios/Temp/RNIContentSizingMode.swift | 131 ++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 ios/Temp/RNIContentSizingMode.swift diff --git a/ios/Temp/RNIContentSizingMode.swift b/ios/Temp/RNIContentSizingMode.swift new file mode 100644 index 0000000..f76c167 --- /dev/null +++ b/ios/Temp/RNIContentSizingMode.swift @@ -0,0 +1,131 @@ +// +// RNIContentSizingMode.swift +// react-native-ios-modal +// +// Created by Dominic Go on 10/6/24. +// + +import Foundation +import DGSwiftUtilities + + +/// Should this really be an enum? +/// Wouldn't it be better if this was an `OptionSet`? +/// +public enum RNIContentSizingMode: String { + case sizingFromReact; + case sizingFromNative; + + case sizingWidthFromReactAndHeightFromNative; + case sizingHeightFromReactAndWidthFromNative; + + // MARK: Computed Properties + // ------------------------- + + public var isSizingHeightFromNative: Bool { + switch self { + case .sizingFromNative, .sizingWidthFromReactAndHeightFromNative: + return true; + + default: + return false; + }; + }; + + public var isSizingWidthFromNative: Bool { + switch self { + case .sizingFromNative, .sizingHeightFromReactAndWidthFromNative: + return true; + + default: + return false; + }; + }; + + public var isSizingHeightFromReact: Bool { + switch self { + case .sizingFromReact, .sizingHeightFromReactAndWidthFromNative: + return true; + + default: + return false; + }; + }; + + public var isSizingWidthFromReact: Bool { + switch self { + case .sizingFromReact, .sizingWidthFromReactAndHeightFromNative: + return true; + + default: + return false; + }; + }; + + // MARK: Functions + // --------------- + + public func derivedSizingMode( + isSizingWidthFromNative: Bool, + isSizingHeightFromNative: Bool + ) -> Self { + + switch (isSizingWidthFromNative, isSizingHeightFromNative) { + case (true, true): + return .sizingFromNative; + + case (false, true): + return .sizingWidthFromReactAndHeightFromNative; + + case (true, false): + return .sizingHeightFromReactAndWidthFromNative; + + case (false, false): + return .sizingFromReact; + }; + }; + + public func derivedSizingMode( + fromHorizontalViewPosition horizontalViewPosition: ViewPositionHorizontal, + isSizingHeightFromNative: Bool + ) -> Self { + + self.derivedSizingMode( + isSizingWidthFromNative: horizontalViewPosition.willSatisfyWidthConstraint, + isSizingHeightFromNative: isSizingHeightFromNative + ); + }; + + public func derivedSizingMode( + fromHorizontalAlignmentPosition horizontalAlignmentPosition: HorizontalAlignmentPosition, + isSizingHeightFromNative: Bool + ) -> Self { + + self.derivedSizingMode( + isSizingWidthFromNative: horizontalAlignmentPosition.isStretching, + isSizingHeightFromNative: isSizingHeightFromNative + ); + }; + + public func derivedSizingMode( + fromVerticalAlignmentPosition verticalAlignmentPosition: VerticalAlignmentPosition, + isSizingWidthFromNative: Bool + ) -> Self { + + self.derivedSizingMode( + isSizingWidthFromNative: isSizingWidthFromNative, + isSizingHeightFromNative: verticalAlignmentPosition.isStretching + ); + }; + + public func derivedSizingMode( + fromHorizontalAlignmentPosition horizontalAlignmentPosition: HorizontalAlignmentPosition, + verticalAlignmentPosition: VerticalAlignmentPosition + ) -> Self { + + self.derivedSizingMode( + isSizingWidthFromNative: horizontalAlignmentPosition.isStretching, + isSizingHeightFromNative: verticalAlignmentPosition.isStretching + ); + }; +};