Skip to content

Commit

Permalink
⭐️ Impl: modalContentPreferredContentSize Prop
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-04-20-23-58-24` - Impl. sheet-related props.
* `TODO:2023-04-21-00-31-46` - Impl. modal content prop  `modalContentPreferredContentSize`.
  • Loading branch information
dominicstop committed Apr 28, 2023
1 parent 56ae8ae commit ccf7326
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 13 deletions.
22 changes: 19 additions & 3 deletions ios/src_library/React Native/RNIModalView/RNIModalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class RNIModalView:
var modalContentWrapper: RNIWrapperView?;
public var modalVC: RNIModalViewController?;


public var sheetDetentStringCurrent: String?;
public var sheetDetentStringPrevious: String?;

Expand Down Expand Up @@ -101,6 +100,12 @@ public class RNIModalView:
/// user-provided identifier for this modal
@objc var modalID: NSString? = nil;

@objc var modalContentPreferredContentSize: NSDictionary? {
didSet {
self.modalVC?.setPreferredContentSize();
}
};

// MARK: - Properties: React Props - BG-Related
// --------------------------------------------

Expand Down Expand Up @@ -282,6 +287,16 @@ public class RNIModalView:
// MARK: - Properties: Synthesized From Props
// ------------------------------------------

public var synthesizedModalContentPreferredContentSize: RNIComputableSize {
guard let dict = self.modalContentPreferredContentSize,
let computableSize = RNIComputableSize(fromDict: dict)
else {
return RNIComputableSize(mode: .unspecified)
};

return computableSize;
};

public var synthesizedModalPresentationStyle: UIModalPresentationStyle {
let defaultStyle: UIModalPresentationStyle = {
guard #available(iOS 13.0, *) else { return .overFullScreen };
Expand Down Expand Up @@ -736,7 +751,8 @@ public class RNIModalView:
///
switch self.synthesizedModalPresentationStyle {
case .overFullScreen,
.fullScreen:
.fullScreen,
.formSheet:
break;

default:
Expand All @@ -748,7 +764,7 @@ public class RNIModalView:

if #available(iOS 15.0, *),
let sheetController = self.sheetPresentationController {

sheetController.delegate = self;
self.applyModalSheetProps(to: sheetController);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ public class RNIModalViewController: UIViewController {
// MARK: - Properties
// ------------------

private(set) public var prevBounds: CGRect?;

weak var lifecycleDelegate: RNIViewControllerLifeCycleNotifiable?;

weak var modalViewRef: RNIModalView?;
weak var lifecycleDelegate: RNIViewControllerLifeCycleNotifiable?;

var isBGTransparent: Bool = true {
didSet {
Expand All @@ -26,14 +23,17 @@ public class RNIModalViewController: UIViewController {
self.updateBackgroundBlur();
}
};

var isBGBlurred: Bool = true {
didSet {
guard oldValue != self.isBGBlurred else { return };
self.updateBackgroundBlur();
}
};

private(set) public var prevBounds: CGRect?;

public lazy var computableSizeEvaluator = RNIComputableSizeEvaluator();

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

Expand All @@ -45,6 +45,18 @@ public class RNIModalViewController: UIViewController {
self.modalViewRef?.modalContentWrapper;
};

var computablePreferredContentSize: RNIComputableSize? {
self.modalViewRef?.synthesizedModalContentPreferredContentSize;
};

var shouldUpdateContentSize: Bool {
guard let computableSize = self.computablePreferredContentSize,
case .current = computableSize.mode
else { return true };

return false;
};

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

Expand Down Expand Up @@ -106,7 +118,7 @@ public class RNIModalViewController: UIViewController {
guard didChangeBounds,
let modalContentWrapper = self.modalContentWrapper
else { return };

let nextBounds = self.view.bounds;

let prevBounds = self.prevBounds;
Expand All @@ -120,10 +132,12 @@ public class RNIModalViewController: UIViewController {
+ " - nextBounds: \(nextBounds)"
);
#endif

modalContentWrapper.notifyForBoundsChange(size: nextBounds.size);
modalContentWrapper.center = self.view.center;

if self.shouldUpdateContentSize {
modalContentWrapper.notifyForBoundsChange(size: nextBounds.size);
modalContentWrapper.center = self.view.center;
};

self.lifecycleDelegate?.viewDidLayoutSubviews(sender: self);
};

Expand All @@ -132,7 +146,9 @@ public class RNIModalViewController: UIViewController {

self.lifecycleDelegate?
.viewWillAppear(sender: self, animated: animated);


self.setPreferredContentSize();

#if DEBUG
print(
"Log - RNIModalViewController.viewWillAppear"
Expand Down Expand Up @@ -225,6 +241,40 @@ public class RNIModalViewController: UIViewController {
#endif
};

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

func setPreferredContentSize(){
guard let computableSize = self.computablePreferredContentSize
else { return };

switch computableSize.mode {
case .unspecified:
return;

case .current:
guard let modalContentWrapper = self.modalContentWrapper
else { return };

self.preferredContentSize = self.view.systemLayoutSizeFitting(
modalContentWrapper.bounds.size
);

default:
self.computableSizeEvaluator.computableSize = computableSize;

let computedSize = self.computableSizeEvaluator.evaluate(
withTargetSize: self.view.bounds.size,
currentSize: self.modalContentWrapper?.bounds.size,
rootView: self.view.window?.rootViewController?.view,
targetView: self.view
);

guard let computedSize = computedSize else { return };
self.preferredContentSize = computedSize;
};
};

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ @interface RCT_EXTERN_MODULE(RNIModalViewManager, RCTViewManager)
// -----------------------------

RCT_EXPORT_VIEW_PROPERTY(modalID, NSString);
RCT_EXPORT_VIEW_PROPERTY(modalContentPreferredContentSize, NSDictionary);

// MARK: - Value Props - BG-Related
// --------------------------------
Expand Down
6 changes: 6 additions & 0 deletions src/components/ModalView/ModalView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ import {
NATIVE_ID_KEYS,
VirtualizedListContext,
} from './ModalViewConstants';

import { ModalViewEmitterEventsDeprecated } from './ModalViewEmitterDeprecated';
import { parseRNIComputableSize } from '../../types/RNIComputable';

// prettier-ignore
export class ModalView extends
Expand Down Expand Up @@ -95,6 +97,7 @@ export class ModalView extends
const {
// Native Props - General
modalID,
modalContentPreferredContentSize,
shouldEnableAggressiveCleanup,

// Native Props - BG-Related
Expand Down Expand Up @@ -193,6 +196,9 @@ export class ModalView extends
shouldEnableAggressiveCleanup: (
shouldEnableAggressiveCleanup ?? true
),
modalContentPreferredContentSize: (
parseRNIComputableSize(modalContentPreferredContentSize)
),

// B - Pass down...
containerStyle,
Expand Down
4 changes: 4 additions & 0 deletions src/components/ModalView/ModalViewProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type {
DeprecatedOnModalDidDismissEvent,
} from 'src/native_components/RNIModalView';

import type { RNIComputableSize } from '../../types/RNIComputable';

export type ModalViewBaseProps = Partial<
Pick<
RNIModalViewProps,
Expand Down Expand Up @@ -62,6 +64,8 @@ export type ModalViewBaseProps = Partial<
| 'onModalDidChangeSelectedDetentIdentifier'
>
> & {
modalContentPreferredContentSize?: RNIComputableSize;

// TODO: See TODO:2023-03-04-13-02-45 - Refactor: Rename to
// shouldAutoCloseOnUnmount
autoCloseOnUnmount?: boolean;
Expand Down
3 changes: 3 additions & 0 deletions src/native_components/RNIModalView/RNIModalViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ import type {
OnModalDetentDidComputeEvent,
OnModalDidChangeSelectedDetentIdentifierEvent,
} from './RNIModalViewEvents';

import type { UnionWithAutoComplete } from 'src/types/UtilityTypes';
import type { RNIComputableSizeNative } from 'src/types/RNIComputable';

export type RNIModalViewBaseProps = {
// Props - General
// ---------------

modalID?: string;
modalContentPreferredContentSize?: RNIComputableSizeNative;

// Props - BG-Related
// ------------------
Expand Down

0 comments on commit ccf7326

Please sign in to comment.