-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐️ Impl:
ModalSheetView.presentModal
- Loading branch information
1 parent
cd488bf
commit 1f05e96
Showing
10 changed files
with
216 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
ios/RNIModalSheetView/RNIModalSheetViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// | ||
// RNIModalSheetViewController.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 9/26/24. | ||
// | ||
|
||
import UIKit | ||
import react_native_ios_utilities | ||
|
||
#if !RCT_NEW_ARCH_ENABLED | ||
import React | ||
#endif | ||
|
||
|
||
open class RNIModalSheetViewController: UIViewController { | ||
|
||
public var shouldTriggerDefaultCleanup = true; | ||
|
||
public weak var mainSheetContentParent: RNIContentViewParentDelegate?; | ||
private(set) public weak var mainSheetContent: RNIWrapperViewContent?; | ||
|
||
public var positionConfigForMainSheetContent: AlignmentPositionConfig = .default; | ||
|
||
// MARK: - View Controller Lifecycle | ||
// --------------------------------- | ||
|
||
public override func viewDidLoad() { | ||
|
||
guard let mainSheetContentParent = self.mainSheetContentParent, | ||
let mainSheetContent = mainSheetContentParent.contentDelegate as? RNIWrapperViewContent | ||
else { | ||
return; | ||
}; | ||
|
||
self.mainSheetContent = mainSheetContent; | ||
mainSheetContentParent.reactViewLifecycleDelegates.add(self); | ||
|
||
// MARK: Setup Constraints | ||
#if !RCT_NEW_ARCH_ENABLED | ||
rootReactView.removeAllAncestorConstraints(); | ||
#endif | ||
|
||
self.view.addSubview(mainSheetContent); | ||
mainSheetContent.translatesAutoresizingMaskIntoConstraints = false; | ||
|
||
let constraints = self.positionConfigForMainSheetContent.createConstraints( | ||
forView: mainSheetContent, | ||
attachingTo: self.view, | ||
enclosingView: self.view | ||
); | ||
|
||
NSLayoutConstraint.activate(constraints); | ||
|
||
// MARK: Set Initial Size | ||
let hasValidSize = !self.view.bounds.size.isZero; | ||
if hasValidSize { | ||
self.positionConfigForMainSheetContent.setIntrinsicContentSizeOverrideIfNeeded( | ||
forRootReactView: mainSheetContentParent, | ||
withSize: self.view.bounds.size | ||
); | ||
}; | ||
|
||
let shouldSetSize = | ||
hasValidSize | ||
&& self.positionConfigForMainSheetContent.isStretchingOnBothAxis; | ||
|
||
if shouldSetSize { | ||
self.positionConfigForMainSheetContent.applySize( | ||
toRootReactView: mainSheetContentParent, | ||
attachingTo: self.view | ||
); | ||
}; | ||
}; | ||
|
||
public override func viewDidLayoutSubviews() { | ||
guard let mainSheetContentParent = self.mainSheetContentParent else { | ||
return; | ||
}; | ||
|
||
self.positionConfigForMainSheetContent.setIntrinsicContentSizeOverrideIfNeeded( | ||
forRootReactView: mainSheetContentParent, | ||
withSize: self.view.bounds.size | ||
); | ||
|
||
self.positionConfigForMainSheetContent.applySize( | ||
toRootReactView: mainSheetContentParent, | ||
attachingTo: self.view | ||
); | ||
}; | ||
|
||
// MARK: Methods | ||
// -------------- | ||
}; | ||
|
||
extension RNIModalSheetViewController: RNIViewLifecycle { | ||
|
||
public func notifyOnRequestForCleanup(sender: RNIContentViewParentDelegate) { | ||
guard self.shouldTriggerDefaultCleanup, | ||
self.view.window != nil | ||
else { | ||
return; | ||
}; | ||
|
||
if self.presentingViewController != nil { | ||
self.dismiss(animated: true); | ||
|
||
} else if self.parent != nil { | ||
self.willMove(toParent: nil); | ||
self.view.removeFromSuperview(); | ||
self.removeFromParent(); | ||
|
||
} else { | ||
self.view.removeFromSuperview(); | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
|
||
export const MODAL_SHEET_VIEW_CONTENT_NATIVE_ID_KEYS = Object.freeze({ | ||
mainSheetContent: 'mainSheetContent', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters