From 3aa9980ba57defa2c18296ddaa6e223505066922 Mon Sep 17 00:00:00 2001 From: Hailey Date: Wed, 9 Oct 2024 19:20:48 -0700 Subject: [PATCH] cleanup tweaks --- modules/bottom-sheet/ios/SheetView.swift | 42 +++++++++++-------- .../ios/SheetViewController.swift | 6 +-- modules/bottom-sheet/ios/Util.swift | 4 +- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/modules/bottom-sheet/ios/SheetView.swift b/modules/bottom-sheet/ios/SheetView.swift index 2223a5a8ca..e03adb4c9b 100644 --- a/modules/bottom-sheet/ios/SheetView.swift +++ b/modules/bottom-sheet/ios/SheetView.swift @@ -3,9 +3,14 @@ import UIKit class SheetView: ExpoView, UISheetPresentationControllerDelegate { // Views - private var sheetVc: SheetViewController? + private var sheetVc: SheetViewController! private var innerView: UIView? - private var touchHandler: RCTTouchHandler? + + // Scroll view + private var scrollView: RCTScrollView? + + // Touch handler + private var touchHandler: RCTTouchHandler! // Events private let onAttemptDismiss = EventDispatcher() @@ -26,9 +31,9 @@ class SheetView: ExpoView, UISheetPresentationControllerDelegate { var preventExpansion = false var cornerRadius: CGFloat? var minHeight = 0.0 - var maxHeight: CGFloat! { + var maxHeight: CGFloat = Util.getScreenHeight() { didSet { - let screenHeight = Util.getScreenHeight() ?? 0 + let screenHeight = Util.getScreenHeight() if maxHeight > screenHeight { maxHeight = screenHeight } @@ -66,14 +71,22 @@ class SheetView: ExpoView, UISheetPresentationControllerDelegate { } } } + private var prevLayoutDetentIdentifier: UISheetPresentationController.Detent.Identifier? // MARK: - Lifecycle required init (appContext: AppContext? = nil) { super.init(appContext: appContext) - self.maxHeight = Util.getScreenHeight() - self.touchHandler = RCTTouchHandler(bridge: appContext?.reactBridge) + + self.touchHandler = RCTTouchHandler(bridge: self.appContext?.reactBridge) + + self.sheetVc = SheetViewController() + self.touchHandler.attach(to: self.sheetVc.view) + if let sheet = self.sheetVc.sheetPresentationController { + sheet.delegate = self + } + SheetManager.shared.add(self) } @@ -84,7 +97,6 @@ class SheetView: ExpoView, UISheetPresentationControllerDelegate { // We don't want this view to actually get added to the tree, so we'll simply store it for adding // to the SheetViewController override func insertReactSubview(_ subview: UIView!, at atIndex: Int) { - self.touchHandler?.attach(to: subview) self.innerView = subview } @@ -106,10 +118,7 @@ class SheetView: ExpoView, UISheetPresentationControllerDelegate { private func destroy() { self.isClosing = false self.isOpen = false - self.sheetVc = nil self.touchHandler?.detach(from: self.innerView) - self.touchHandler = nil - self.innerView = nil SheetManager.shared.remove(self) } @@ -125,16 +134,13 @@ class SheetView: ExpoView, UISheetPresentationControllerDelegate { return } - let sheetVc = SheetViewController() - sheetVc.setDetents(contentHeight: self.clampHeight(contentHeight), preventExpansion: self.preventExpansion) + self.sheetVc.view.addSubview(innerView) + self.sheetVc.setDetents(contentHeight: self.clampHeight(contentHeight), preventExpansion: self.preventExpansion) if let sheet = sheetVc.sheetPresentationController { - sheet.delegate = self sheet.preferredCornerRadius = self.cornerRadius self.selectedDetentIdentifier = sheet.selectedDetentIdentifier } - sheetVc.view.addSubview(innerView) - self.sheetVc = sheetVc self.isOpening = true rvc.present(sheetVc, animated: true) { [weak self] in @@ -146,16 +152,16 @@ class SheetView: ExpoView, UISheetPresentationControllerDelegate { func updateLayout() { if self.prevLayoutDetentIdentifier == self.selectedDetentIdentifier, let contentHeight = self.innerView?.subviews.first?.frame.size.height { - self.sheetVc?.updateDetents(contentHeight: self.clampHeight(contentHeight), + self.sheetVc.updateDetents(contentHeight: self.clampHeight(contentHeight), preventExpansion: self.preventExpansion) - self.selectedDetentIdentifier = self.sheetVc?.getCurrentDetentIdentifier() + self.selectedDetentIdentifier = self.sheetVc.getCurrentDetentIdentifier() } self.prevLayoutDetentIdentifier = self.selectedDetentIdentifier } func dismiss() { self.isClosing = true - self.sheetVc?.dismiss(animated: true) { [weak self] in + self.sheetVc.dismiss(animated: true) { [weak self] in self?.destroy() } } diff --git a/modules/bottom-sheet/ios/SheetViewController.swift b/modules/bottom-sheet/ios/SheetViewController.swift index 7b6606980d..f79f358c42 100644 --- a/modules/bottom-sheet/ios/SheetViewController.swift +++ b/modules/bottom-sheet/ios/SheetViewController.swift @@ -21,13 +21,11 @@ class SheetViewController: UIViewController { } func setDetents(contentHeight: CGFloat, preventExpansion: Bool) { - guard let sheet = self.sheetPresentationController, - let screenHeight = Util.getScreenHeight() - else { + guard let sheet = self.sheetPresentationController else { return } - if contentHeight > screenHeight - 100 { + if contentHeight > Util.getScreenHeight() - 100 { sheet.detents = [ .large() ] diff --git a/modules/bottom-sheet/ios/Util.swift b/modules/bottom-sheet/ios/Util.swift index c654596a74..b098c15080 100644 --- a/modules/bottom-sheet/ios/Util.swift +++ b/modules/bottom-sheet/ios/Util.swift @@ -6,13 +6,13 @@ // class Util { - static func getScreenHeight() -> CGFloat? { + static func getScreenHeight() -> CGFloat { if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, let window = windowScene.windows.first { let safeAreaInsets = window.safeAreaInsets let fullScreenHeight = UIScreen.main.bounds.height return fullScreenHeight - (safeAreaInsets.top + safeAreaInsets.bottom) } - return nil + return 0 } }