react-native-sheet2 1.2.6 → 6.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +51 -32
  3. package/{react-native-sheet.podspec → Sheet.podspec} +7 -6
  4. package/android/build.gradle +43 -25
  5. package/android/generated/java/com/facebook/react/viewmanagers/SheetViewManagerDelegate.java +67 -0
  6. package/android/generated/java/com/facebook/react/viewmanagers/SheetViewManagerInterface.java +27 -0
  7. package/android/generated/java/com/sheet/NativeSheetSpec.java +78 -0
  8. package/android/generated/jni/CMakeLists.txt +36 -0
  9. package/android/generated/jni/RNSheetViewSpec-generated.cpp +50 -0
  10. package/android/generated/jni/RNSheetViewSpec.h +31 -0
  11. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/ComponentDescriptors.cpp +22 -0
  12. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/ComponentDescriptors.h +24 -0
  13. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/EventEmitters.cpp +24 -0
  14. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/EventEmitters.h +25 -0
  15. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/Props.cpp +33 -0
  16. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/Props.h +36 -0
  17. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/RNSheetViewSpecJSI-generated.cpp +46 -0
  18. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/RNSheetViewSpecJSI.h +98 -0
  19. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/ShadowNodes.cpp +17 -0
  20. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/ShadowNodes.h +32 -0
  21. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/States.cpp +16 -0
  22. package/android/generated/jni/react/renderer/components/RNSheetViewSpec/States.h +29 -0
  23. package/android/gradle.properties +4 -5
  24. package/android/src/main/AndroidManifest.xml +0 -9
  25. package/android/src/main/AndroidManifestNew.xml +2 -0
  26. package/android/src/main/java/com/behavior/BottomSheetBehavior.java +10 -17
  27. package/android/src/main/java/com/sheet/AppFittedSheet.kt +107 -39
  28. package/android/src/main/java/com/sheet/BaseRNView.kt +81 -33
  29. package/android/src/main/java/com/sheet/CustomBottomSheetDialog.java +8 -0
  30. package/android/src/main/java/com/sheet/DialogRootViewGroup.kt +44 -33
  31. package/android/src/main/java/com/sheet/FragmentModalBottomSheet.kt +12 -2
  32. package/android/src/main/java/com/sheet/SafeAreaUtils.kt +90 -0
  33. package/android/src/main/java/com/sheet/SheetDismissEvent.kt +20 -0
  34. package/android/src/main/java/com/sheet/SheetModule.kt +37 -0
  35. package/android/src/main/java/com/sheet/SheetPackage.kt +32 -6
  36. package/android/src/main/java/com/sheet/SheetView.kt +5 -0
  37. package/android/src/main/java/com/sheet/SheetViewManager.kt +77 -56
  38. package/android/src/main/java/com/sheet/utils.kt +2 -10
  39. package/android/src/main/res/layout/dialog_bottom_sheet.xml +2 -2
  40. package/android/src/main/res/values/styles.xml +9 -0
  41. package/ios/FittedSheets/SheetContentViewController.swift +4 -4
  42. package/ios/FittedSheets/SheetViewController.swift +29 -100
  43. package/ios/HostFittedSheet.swift +271 -0
  44. package/ios/Sheet-Bridging-Header.h +1 -0
  45. package/ios/SheetModule.h +10 -0
  46. package/ios/SheetModule.mm +67 -0
  47. package/ios/SheetView.h +14 -0
  48. package/ios/SheetView.mm +129 -0
  49. package/ios/generated/RNSheetViewSpec/ComponentDescriptors.cpp +22 -0
  50. package/ios/generated/RNSheetViewSpec/ComponentDescriptors.h +24 -0
  51. package/ios/generated/RNSheetViewSpec/EventEmitters.cpp +24 -0
  52. package/ios/generated/RNSheetViewSpec/EventEmitters.h +25 -0
  53. package/ios/generated/RNSheetViewSpec/Props.cpp +33 -0
  54. package/ios/generated/RNSheetViewSpec/Props.h +36 -0
  55. package/ios/generated/RNSheetViewSpec/RCTComponentViewHelpers.h +44 -0
  56. package/ios/generated/RNSheetViewSpec/RNSheetViewSpec-generated.mm +60 -0
  57. package/ios/generated/RNSheetViewSpec/RNSheetViewSpec.h +100 -0
  58. package/ios/generated/RNSheetViewSpec/ShadowNodes.cpp +17 -0
  59. package/ios/generated/RNSheetViewSpec/ShadowNodes.h +32 -0
  60. package/ios/generated/RNSheetViewSpec/States.cpp +16 -0
  61. package/ios/generated/RNSheetViewSpec/States.h +29 -0
  62. package/ios/generated/RNSheetViewSpecJSI-generated.cpp +46 -0
  63. package/ios/generated/RNSheetViewSpecJSI.h +98 -0
  64. package/lib/module/FittedSheet.js +153 -0
  65. package/lib/module/FittedSheet.js.map +1 -0
  66. package/lib/module/NativeSheet.ts +17 -0
  67. package/lib/module/PublicSheetView.js +48 -0
  68. package/lib/module/PublicSheetView.js.map +1 -0
  69. package/lib/module/SheetViewNativeComponent.ts +31 -0
  70. package/lib/module/index.js +13 -0
  71. package/lib/module/index.js.map +1 -0
  72. package/lib/module/package.json +1 -0
  73. package/lib/typescript/package.json +1 -0
  74. package/lib/typescript/src/FittedSheet.d.ts +51 -0
  75. package/lib/typescript/src/FittedSheet.d.ts.map +1 -0
  76. package/lib/typescript/src/NativeSheet.d.ts +16 -0
  77. package/lib/typescript/src/NativeSheet.d.ts.map +1 -0
  78. package/lib/typescript/src/PublicSheetView.d.ts +12 -0
  79. package/lib/typescript/src/PublicSheetView.d.ts.map +1 -0
  80. package/lib/typescript/src/SheetViewNativeComponent.d.ts +22 -0
  81. package/lib/typescript/src/SheetViewNativeComponent.d.ts.map +1 -0
  82. package/lib/typescript/src/index.d.ts +8 -0
  83. package/lib/typescript/src/index.d.ts.map +1 -0
  84. package/package.json +109 -42
  85. package/react-native.config.js +12 -0
  86. package/src/FittedSheet.tsx +118 -95
  87. package/src/NativeSheet.ts +17 -0
  88. package/src/PublicSheetView.tsx +66 -0
  89. package/src/SheetViewNativeComponent.ts +31 -0
  90. package/src/index.tsx +26 -8
  91. package/android/src/main/java/com/modal/FullScreenDialog.kt +0 -100
  92. package/android/src/main/java/com/modal/TopModalView.kt +0 -77
  93. package/android/src/main/java/com/modal/TopModalViewManager.kt +0 -83
  94. package/android/src/main/java/com/modal/TopModalViewModule.kt +0 -40
  95. package/android/src/main/java/com/sheet/SheetViewModule.kt +0 -41
  96. package/ios/LBBottomSheet/BottomSheetConstant.swift +0 -48
  97. package/ios/LBBottomSheet/Model/BottomSheetController+Behavior.swift +0 -262
  98. package/ios/LBBottomSheet/Model/BottomSheetController+Theme.swift +0 -156
  99. package/ios/LBBottomSheet/Protocols/BottomSheetInteractionDelegate.swift +0 -29
  100. package/ios/LBBottomSheet/Protocols/BottomSheetPositionDelegate.swift +0 -31
  101. package/ios/LBBottomSheet/UI/BottomSheet.storyboard +0 -105
  102. package/ios/LBBottomSheet/UI/BottomSheetController.swift +0 -601
  103. package/ios/LBBottomSheet/Utils/Components/ForwardingEventsView.swift +0 -60
  104. package/ios/LBBottomSheet/Utils/Extensions/CACornerMask+Extension.swift +0 -33
  105. package/ios/LBBottomSheet/Utils/Extensions/Mirror+Extension.swift +0 -47
  106. package/ios/LBBottomSheet/Utils/Extensions/UIApplication+Extension.swift +0 -35
  107. package/ios/LBBottomSheet/Utils/Extensions/UIColor+Extension.swift +0 -61
  108. package/ios/LBBottomSheet/Utils/Extensions/UIGestureRecognizer+Extensions.swift +0 -28
  109. package/ios/LBBottomSheet/Utils/Extensions/UIScreen+Extension.swift +0 -28
  110. package/ios/LBBottomSheet/Utils/Extensions/UIScrollView+Extension.swift +0 -25
  111. package/ios/LBBottomSheet/Utils/Extensions/UIView+Extension.swift +0 -52
  112. package/ios/LBBottomSheet/Utils/Extensions/UIViewController+Extension.swift +0 -104
  113. package/ios/Sheet.xcodeproj/project.pbxproj +0 -377
  114. package/ios/SheetViewManager.m +0 -25
  115. package/ios/SheetViewManager.swift +0 -324
  116. package/ios/TopModalViewManager.swift +0 -196
  117. package/src/TopModal.tsx +0 -94
@@ -1,601 +0,0 @@
1
- // Copyright © 2021 Lunabee Studio
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
- //
15
- // BottomSheetController.swift
16
- // LBBottomSheet
17
- //
18
- // Created by Lunabee Studio / Date - 12/10/2021 - for the LBBottomSheet Swift Package.
19
- //
20
-
21
- import UIKit
22
-
23
- /// This class is the main component of this package.
24
- /// It is presented modally and embeds the controller you want to present as a bottom sheet.
25
- public final class BottomSheetController: UIViewController {
26
- override public var modalPresentationStyle: UIModalPresentationStyle {
27
- get { .overFullScreen }
28
- set { }
29
- }
30
-
31
- /// This is the height of the grabber "zone".
32
- /// ![TopInset](TopInset)
33
- ///
34
- /// Here is it's implementation:
35
- /// ```swift
36
- /// public var topInset: CGFloat { (theme.grabber?.topMargin ?? 0.0) * 2.0 + (theme.grabber?.size.height ?? 0.0) }
37
- /// ```
38
- /// You can have a look at <doc:/LBBottomSheet/BottomSheetController/Theme-swift.struct/Grabber-swift.struct> to check the <doc:/LBBottomSheet/BottomSheetController/Theme-swift.struct/Grabber-swift.struct/topMargin>.
39
- public var topInset: CGFloat { (theme.grabber?.topMargin ?? 0.0) * 2.0 + (theme.grabber?.size.height ?? 0.0) }
40
-
41
- /// The delegate to get the bottom sheet position updates if the presenting controller needs to update its content bottom inset.
42
- public weak var bottomSheetPositionDelegate: BottomSheetPositionDelegate? {
43
- didSet { notifyBottomSheetPositionUpdate() }
44
- }
45
- /// The delegate to get bottom sheet interactions events.
46
- public weak var bottomSheetInteractionDelegate: BottomSheetInteractionDelegate?
47
-
48
- @IBOutlet private var mainDismissButton: UIButton!
49
- @IBOutlet private var grabberView: UIView!
50
- @IBOutlet private var gestureView: UIView!
51
- @IBOutlet private var topGrabberConstraint: NSLayoutConstraint!
52
- @IBOutlet private var widthGrabberConstraint: NSLayoutConstraint!
53
- @IBOutlet private var heightGrabberConstraint: NSLayoutConstraint!
54
- @IBOutlet private var bottomContainerView: UIView!
55
- @IBOutlet private var bottomContainerInnerView: UIView!
56
- @IBOutlet private var bottomContainerInnerViewTranslucentTopConstraint: NSLayoutConstraint!
57
- @IBOutlet private var bottomContainerInnerViewTopConstraint: NSLayoutConstraint!
58
- @IBOutlet private var bottomContainerHeightConstraint: NSLayoutConstraint!
59
- @IBOutlet private var bottomContainerBottomConstraint: NSLayoutConstraint!
60
- @IBOutlet private var bottomContainerLeadingConstraint: NSLayoutConstraint!
61
- @IBOutlet private var bottomContainerTrailingConstraint: NSLayoutConstraint!
62
-
63
- public private(set) var theme: Theme = Theme()
64
- public private(set) var behavior: Behavior = Behavior()
65
- private let minTopMargin: CGFloat = 40.0
66
- private var didAlreadyStartAppearing: Bool = false
67
-
68
- private var panGesture: UIPanGestureRecognizer?
69
- private var tapGesture: UITapGestureRecognizer?
70
- private var isGestureBeingActivated: Bool = false
71
- private var lastHeightAtPanGestureStart: CGFloat = 0.0
72
- private var lastChildHeightAtPanGestureStart: CGFloat = 0.0
73
- private var bottomSheetChild: UIViewController!
74
- private var isChildAlreadyVisible: Bool = false
75
- private var childHeight: CGFloat {
76
- if let heightDeclaringController = bottomSheetChild.lbbsFindControllerDeclaringPreferredHeightInBottomSheet() {
77
- var height: CGFloat = heightDeclaringController.value(forKey: BottomSheetConstant.preferredHeightVariableName) as? CGFloat ?? 0.0
78
- if let grabber = theme.grabber, !grabber.background.isTranslucent { height += topInset }
79
- return height
80
- } else {
81
- print("⚠️ [LBBottomSheet] ⚠️: If you use the \"fitContent\" heightMode, you can declare the following variable in the controller you want to present: \"@objc var preferredHeightInBottomSheet: CGFloat\" returning the customized height you want the bottom sheet to have.")
82
- let childView: UIView? = bottomSheetChild?.view
83
- let defaultFirstScrollView: UIScrollView? = childView?.lbbsGetFirstTableOrCollectionView()
84
- let defaultHeight: CGFloat = defaultFirstScrollView?.lbbsContentHeight ?? childView?.frame.height ?? 0.0
85
- if defaultHeight == 0.0 {
86
- print("👉 [LBBottomSheet]: The default calculated height is 0.0 so the applied height is by default 75% of the screen height.")
87
- return UIScreen.main.bounds.height * 0.75
88
- } else {
89
- print("👉 [LBBottomSheet]: The default calculated height is \(defaultHeight) based on your layout. You can have a look at the previous warning log to customize this behavior.")
90
- let bottomSafeArea: CGFloat = UIApplication.shared.lbbsKeySceneWindow?.safeAreaInsets.bottom ?? 0.0
91
- var height: CGFloat = defaultHeight + bottomSafeArea
92
- if let grabber = theme.grabber, !grabber.background.isTranslucent { height += topInset }
93
- return height
94
- }
95
- }
96
- }
97
- private var isFirstLoad: Bool = true
98
-
99
- /// Overriden to customize the way the controller is initialized.
100
- public override func viewDidLoad() {
101
- super.viewDidLoad()
102
- initUI()
103
- initGrabberBackgroundView()
104
- addGesture()
105
- addObservers()
106
- }
107
-
108
- /// Overriden to customize the way the controller is appearing.
109
- public override func viewDidAppear(_ animated: Bool) {
110
- super.viewDidAppear(animated)
111
- guard isFirstLoad else { return }
112
- isFirstLoad = false
113
- preferredHeightInBottomSheetDidUpdate()
114
- setInitialPosition()
115
- setupDimmingBackground()
116
- makeAppearing()
117
- }
118
-
119
- /// Call this function to tell the bottom sheet the embedded controller height did change.
120
- /// This way, this controller will calculate the new needed height and the bottom sheet layout will be updated.
121
- public func preferredHeightInBottomSheetDidUpdate() {
122
- panGesture?.lbbsCancel()
123
- let newHeight: CGFloat = calculateExpectedHeight()
124
- guard newHeight != bottomContainerHeightConstraint.constant else { return }
125
- bottomContainerHeightConstraint.constant = newHeight
126
- if didAlreadyStartAppearing {
127
- animate { self.view.layoutIfNeeded() }
128
- } else {
129
- view.layoutIfNeeded()
130
- }
131
- }
132
-
133
- /// Overriden to update the shadow color in case of light/dark mode change.
134
- public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
135
- super.traitCollectionDidChange(previousTraitCollection)
136
- updateShadowColors()
137
- }
138
-
139
- /// Overriden to call the position delegate update method after a layout calculation.
140
- public override func viewDidLayoutSubviews() {
141
- super.viewDidLayoutSubviews()
142
- notifyBottomSheetPositionUpdate()
143
- }
144
-
145
- /// Use this function to dismiss the bottom sheet. This will animate the disappearing based on the behavior configuration.
146
- public func dismiss(_ completion: (() -> Void)? = nil) {
147
- makeDisappearing {
148
- super.dismiss(animated: false, completion: completion)
149
- }
150
- }
151
-
152
- @available(swift, obsoleted: 5.0, message: "Use the bottom sheet provided dismiss function instead of the default one.", renamed: "dismiss(_:)")
153
- public override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
154
- super.dismiss(animated: flag, completion: completion)
155
- }
156
-
157
- public func grow(toMaximumHeight: Bool = false) {
158
- switch behavior.heightMode {
159
- case .specific:
160
- let childHeight: CGFloat = childHeight
161
- let screenHeight: CGFloat = UIScreen.main.bounds.height
162
- let maximumHeight: CGFloat = behavior.heightMode.maximumHeight(with: childHeight, screenHeight: screenHeight, from: self)
163
- let nextHeight: CGFloat? = behavior.heightMode.nextHeight(with: childHeight,
164
- screenHeight: maximumHeight,
165
- from: self,
166
- originHeight: bottomContainerHeightConstraint.constant,
167
- goingUp: true)
168
- guard let destinationHeight = toMaximumHeight ? maximumHeight : nextHeight else { return }
169
- bottomContainerHeightConstraint.constant = destinationHeight
170
- bottomContainerBottomConstraint.constant = 0.0
171
- animate {
172
- self.updateCornerRadiusFor(destinationHeight: destinationHeight)
173
- self.view.layoutIfNeeded()
174
- }
175
- default:
176
- print("⚠️ [LBBottomSheet] ⚠️: Call to grow(toMaximumHeight:) doesn't have any effect as the current HeightMode is not `.specific`")
177
- break
178
- }
179
- }
180
-
181
- public func reduce(toMinimumHeight: Bool = false) {
182
- switch behavior.heightMode {
183
- case .specific:
184
- let childHeight: CGFloat = childHeight
185
- let screenHeight: CGFloat = UIScreen.main.bounds.height
186
- let minimumHeight: CGFloat = behavior.heightMode.minimumHeight(with: childHeight, screenHeight: screenHeight)
187
- let nextHeight: CGFloat? = behavior.heightMode.nextHeight(with: childHeight,
188
- screenHeight: UIScreen.main.bounds.height,
189
- from: self,
190
- originHeight: bottomContainerHeightConstraint.constant,
191
- goingUp: false)
192
- guard let destinationHeight = toMinimumHeight ? minimumHeight : nextHeight else { return }
193
- bottomContainerHeightConstraint.constant = destinationHeight
194
- bottomContainerBottomConstraint.constant = 0.0
195
- animate {
196
- self.updateCornerRadiusFor(destinationHeight: destinationHeight)
197
- self.view.layoutIfNeeded()
198
- }
199
- default:
200
- print("⚠️ [LBBottomSheet] ⚠️: Call to reduce(toMinimumHeight:) doesn't have any effect as the current HeightMode is not `.specific`")
201
- break
202
- }
203
- }
204
- }
205
-
206
- // MARK: - Controller instantiation -
207
- internal extension BottomSheetController {
208
- static func controller(bottomSheetChild: UIViewController,
209
- bottomSheetPositionDelegate: BottomSheetPositionDelegate? = nil,
210
- bottomSheetInteractionDelegate: BottomSheetInteractionDelegate? = nil,
211
- theme: Theme = Theme(),
212
- behavior: Behavior = Behavior()) -> BottomSheetController {
213
- let bottomController: BottomSheetController = UIStoryboard(name: "BottomSheet", bundle: Bundle(for: BottomSheetController.self)).instantiateInitialViewController() as! BottomSheetController
214
- bottomController.bottomSheetChild = bottomSheetChild
215
- bottomController.bottomSheetPositionDelegate = bottomSheetPositionDelegate
216
- bottomController.bottomSheetInteractionDelegate = bottomSheetInteractionDelegate
217
- bottomController.theme = theme
218
- bottomController.behavior = behavior
219
- return bottomController
220
- }
221
- }
222
-
223
- // MARK: - UI configuration -
224
- private extension BottomSheetController {
225
- func initUI() {
226
- view.backgroundColor = .clear
227
- bottomContainerView.backgroundColor = .clear
228
- bottomContainerView.layer.cornerRadius = theme.cornerRadius
229
- bottomContainerView.layer.maskedCorners = theme.maskedCorners
230
- bottomContainerView.layer.masksToBounds = true
231
- bottomContainerView.alpha = 0.0
232
-
233
- if let grabber = theme.grabber {
234
- topGrabberConstraint.constant = grabber.topMargin
235
- widthGrabberConstraint.constant = grabber.size.width
236
- heightGrabberConstraint.constant = grabber.size.height
237
- grabberView.backgroundColor = grabber.color
238
- grabberView.layer.maskedCorners = grabber.maskedCorners
239
- grabberView.layer.masksToBounds = true
240
- switch grabber.cornerRadiusType {
241
- case .rounded:
242
- grabberView.layer.cornerRadius = grabber.size.height / 2.0
243
- case let .fixed(value):
244
- grabberView.layer.cornerRadius = value
245
- }
246
- } else {
247
- grabberView.isHidden = true
248
- }
249
-
250
- if let shadow = theme.shadow {
251
- updateShadowColors()
252
- view.layer.shadowOffset = shadow.offset
253
- view.layer.shadowOpacity = shadow.opacity
254
- view.layer.shadowRadius = shadow.radius
255
- view.layer.shouldRasterize = true
256
- view.layer.rasterizationScale = UIScreen.main.scale
257
- }
258
-
259
- lbbsAddChildViewController(bottomSheetChild, containerView: bottomContainerInnerView)
260
- bottomContainerView.backgroundColor = bottomSheetChild.view.backgroundColor
261
- bottomContainerLeadingConstraint.constant = theme.leadingMargin
262
- bottomContainerTrailingConstraint.constant = theme.trailingMargin
263
- }
264
-
265
- func initGrabberBackgroundView() {
266
- guard let background = theme.grabber?.background else { return }
267
- switch background {
268
- case let .color(color, isTranslucent):
269
- let coloredView: UIView = UIView()
270
- coloredView.backgroundColor = color
271
- gestureView.lbbsAddFullSubview(coloredView, belowSubview: grabberView)
272
- updateBottomChildContainerTopConstraint(isGrabberBackgroundTranslucent: isTranslucent)
273
- case let .view(view, isTranslucent):
274
- gestureView.lbbsAddFullSubview(view, belowSubview: grabberView)
275
- updateBottomChildContainerTopConstraint(isGrabberBackgroundTranslucent: isTranslucent)
276
- }
277
- }
278
-
279
- func updateShadowColors() {
280
- guard let color = theme.shadow?.color.cgColor else { return }
281
- view.layer.shadowColor = color
282
- }
283
-
284
- func updateBottomChildContainerTopConstraint(isGrabberBackgroundTranslucent: Bool) {
285
- bottomContainerInnerViewTranslucentTopConstraint.isActive = isGrabberBackgroundTranslucent
286
- bottomContainerInnerViewTopConstraint.isActive = !isGrabberBackgroundTranslucent
287
- }
288
-
289
- func setInitialPosition() {
290
- bottomContainerBottomConstraint.constant = -calculateExpectedHeight()
291
- bottomContainerView.alpha = 1.0
292
- view.layoutIfNeeded()
293
- }
294
-
295
- func setupDimmingBackground() {
296
- if behavior.forwardEventsToRearController {
297
- mainDismissButton.isUserInteractionEnabled = false
298
- if let rearView = presentingViewController?.view {
299
- let view: ForwardingEventsView = self.view as! ForwardingEventsView
300
- view.destinationView = rearView
301
- view.excludedParentView = bottomContainerView
302
- }
303
- } else {
304
- mainDismissButton.isUserInteractionEnabled = true
305
- }
306
- }
307
-
308
- func addGesture() {
309
- guard behavior.swipeMode != .none else { return }
310
- panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerHandler(_:)))
311
- panGesture?.delegate = self
312
- view.addGestureRecognizer(panGesture!)
313
- if theme.grabber?.canTouchToDismiss == true {
314
- tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapGestureRecognizerHandler(_:)))
315
- tapGesture?.delegate = self
316
- view.addGestureRecognizer(tapGesture!)
317
- }
318
- }
319
- }
320
-
321
- // MARK: - Observers -
322
- private extension BottomSheetController {
323
- func addObservers() {
324
- guard behavior.updateHeightOnContentSizeCategoryChange else { return }
325
- NotificationCenter.default.addObserver(forName: UIContentSizeCategory.didChangeNotification, object: nil, queue: .main) { [weak self] _ in
326
- self?.bottomContainerView?.layoutIfNeeded()
327
- self?.preferredHeightInBottomSheetDidUpdate()
328
- }
329
- }
330
- }
331
-
332
- // MARK: - Appearing/Disappearing animations -
333
- private extension BottomSheetController {
334
- func makeAppearing() {
335
- didAlreadyStartAppearing = true
336
- bottomContainerHeightConstraint.constant = calculateExpectedHeight()
337
- bottomContainerBottomConstraint.constant = 0.0
338
- animateAppearing {
339
- self.view.backgroundColor = self.theme.dimmingBackgroundColor
340
- self.view.layoutIfNeeded()
341
- }
342
- }
343
-
344
- func makeDisappearing(_ completion: @escaping () -> ()) {
345
- bottomContainerBottomConstraint.constant = -bottomContainerHeightConstraint.constant
346
- animateDisappearing {
347
- self.view.backgroundColor = .clear
348
- self.view.layoutIfNeeded()
349
- } completion: {
350
- completion()
351
- }
352
- }
353
- }
354
-
355
- // MARK: - BottomSheet position update -
356
- private extension BottomSheetController {
357
- func notifyBottomSheetPositionUpdate() {
358
- guard isViewLoaded else { return }
359
- bottomSheetPositionDelegate?.bottomSheetPositionDidUpdate(y: UIScreen.main.bounds.height - bottomContainerHeightConstraint.constant - bottomContainerBottomConstraint.constant)
360
- }
361
- }
362
-
363
- // MARK: - Pan gesture management -
364
- private extension BottomSheetController {
365
- @objc func panGestureRecognizerHandler(_ gesture: UIPanGestureRecognizer) {
366
- switch gesture.state {
367
- case .began:
368
- processPanGestureBegan(gesture)
369
- fallthrough
370
- case .changed:
371
- processPanGestureChanged(gesture)
372
- case .ended, .cancelled, .failed:
373
- processPanGestureEnded(gesture)
374
- default:
375
- break
376
- }
377
- }
378
-
379
- func processPanGestureBegan(_ gesture: UIPanGestureRecognizer) {
380
- tapGesture?.lbbsCancel()
381
- isGestureBeingActivated = true
382
- lastHeightAtPanGestureStart = bottomContainerHeightConstraint.constant
383
- lastChildHeightAtPanGestureStart = childHeight
384
- }
385
-
386
- func processPanGestureChanged(_ gesture: UIPanGestureRecognizer) {
387
- let yTranslation: CGFloat = gesture.translation(in: bottomContainerView).y
388
- let destinationHeight: CGFloat = lastHeightAtPanGestureStart - yTranslation
389
- let childHeight: CGFloat = lastChildHeightAtPanGestureStart
390
- let newHeight: CGFloat
391
- let newBottom: CGFloat
392
- let minHeight: CGFloat = behavior.heightMode.minimumHeight(with: childHeight, screenHeight: UIScreen.main.bounds.height)
393
- let maxHeight: CGFloat = behavior.heightMode.maximumHeight(with: childHeight, screenHeight: UIScreen.main.bounds.height, from: self)
394
- if destinationHeight > maxHeight {
395
- newHeight = maxHeight + behavior.elasticityFunction(destinationHeight - maxHeight)
396
- newBottom = 0.0
397
- } else if destinationHeight < minHeight {
398
- newHeight = minHeight
399
- newBottom = behavior.allowsSwipeToDismiss ? destinationHeight - minHeight : -behavior.elasticityFunction(abs(destinationHeight - minHeight))
400
- } else {
401
- newHeight = destinationHeight
402
- newBottom = 0.0
403
- }
404
- bottomContainerHeightConstraint.constant = newHeight
405
- bottomContainerBottomConstraint.constant = newBottom
406
- view.layoutIfNeeded()
407
- }
408
-
409
- func processPanGestureEnded(_ gesture: UIPanGestureRecognizer) {
410
- let yTranslation: CGFloat = gesture.translation(in: bottomContainerView).y
411
- let yVelocity: CGFloat = gesture.velocity(in: bottomContainerView).y
412
- if (yTranslation > lastHeightAtPanGestureStart * behavior.heightPercentageThresholdToDismiss || yVelocity > behavior.velocityThresholdToDismiss) {
413
- switch behavior.heightMode {
414
- case .specific:
415
- if behavior.allowsSwipeToDismiss {
416
- dismiss()
417
- } else {
418
- bottomContainerHeightConstraint.constant = behavior.heightMode.minimumHeight(with: lastChildHeightAtPanGestureStart,
419
- screenHeight: UIScreen.main.bounds.height)
420
- bottomContainerBottomConstraint.constant = 0.0
421
- animate { self.view.layoutIfNeeded() }
422
- }
423
- default:
424
- if behavior.allowsSwipeToDismiss {
425
- dismiss()
426
- } else {
427
- bottomContainerHeightConstraint.constant = behavior.heightMode.minimumHeight(with: lastChildHeightAtPanGestureStart,
428
- screenHeight: UIScreen.main.bounds.height)
429
- bottomContainerBottomConstraint.constant = 0.0
430
- animate { self.view.layoutIfNeeded() }
431
- }
432
- }
433
- } else {
434
- switch behavior.heightMode {
435
- case .specific:
436
- let isFastSwipeUpGestureDetected: Bool = yVelocity < -behavior.velocityThresholdToOpenAtMaxHeight
437
- let maximumHeight: CGFloat = behavior.heightMode.maximumHeight(with: lastChildHeightAtPanGestureStart,
438
- screenHeight: UIScreen.main.bounds.height,
439
- from: self)
440
- let expectedHeight: CGFloat = calculateExpectedHeight(lastChildHeightAtPanGestureStart)
441
-
442
- let destinationHeight: CGFloat = isFastSwipeUpGestureDetected ? maximumHeight : expectedHeight
443
- bottomContainerHeightConstraint.constant = destinationHeight
444
- bottomContainerBottomConstraint.constant = 0.0
445
- animate {
446
- self.updateCornerRadiusFor(destinationHeight: destinationHeight)
447
- self.view.layoutIfNeeded()
448
- }
449
- default:
450
- let destinationHeight: CGFloat = calculateExpectedHeight(lastChildHeightAtPanGestureStart)
451
- bottomContainerHeightConstraint.constant = destinationHeight
452
- bottomContainerBottomConstraint.constant = 0.0
453
- animate { self.view.layoutIfNeeded() }
454
- }
455
- }
456
- isGestureBeingActivated = false
457
- }
458
-
459
- func calculateExpectedHeight(_ givenChildHeight: CGFloat? = nil) -> CGFloat {
460
- let childHeight: CGFloat = givenChildHeight ?? self.childHeight
461
- switch behavior.heightMode {
462
- case let .fitContent(heightLimit):
463
- return min(childHeight, heightLimit.value(from: self, screenHeight: UIScreen.main.bounds.height))
464
- case let .free(minHeight, maxHeight, heightLimit):
465
- let maxHeightLimit: CGFloat = heightLimit.value(from: self, screenHeight: UIScreen.main.bounds.height)
466
- return min(max(bottomContainerHeightConstraint.constant, minHeight ?? 0.0), min(maxHeight ?? maxHeightLimit, maxHeightLimit))
467
- case let .specific(values, heightLimit):
468
- let maxHeightLimit: CGFloat = heightLimit.value(from: self, screenHeight: UIScreen.main.bounds.height)
469
- let heightValues: [CGFloat] = values.sortedPointValues(screenHeight: UIScreen.main.bounds.height, childHeight: childHeight)
470
- .map { min($0, maxHeightLimit) }
471
- return min(heightValues.min { abs($0 - bottomContainerHeightConstraint.constant) < abs($1 - bottomContainerHeightConstraint.constant) } ?? 0.0, maxHeightLimit)
472
- }
473
- }
474
-
475
- func updateCornerRadiusFor(destinationHeight: CGFloat) {
476
- bottomContainerView.layer.cornerRadius = destinationHeight == UIScreen.main.bounds.height ? UIScreen.main.lbbsCornerRadius : theme.cornerRadius
477
- }
478
- }
479
-
480
- // MARK: - Tap gesture management -
481
- private extension BottomSheetController {
482
- @objc func tapGestureRecognizerHandler(_ gesture: UITapGestureRecognizer) {
483
- switch gesture.state {
484
- case .ended:
485
- dismiss()
486
- default:
487
- break
488
- }
489
- }
490
- }
491
-
492
- // MARK: - Gestures delegates -
493
- extension BottomSheetController: UIGestureRecognizerDelegate {
494
- public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
495
- if gestureRecognizer === tapGesture {
496
- return gestureView.bounds.contains(gestureRecognizer.location(in: gestureView))
497
- } else if gestureRecognizer === panGesture {
498
- switch behavior.swipeMode {
499
- case .top:
500
- return gestureView.bounds.contains(gestureRecognizer.location(in: gestureView))
501
- case .full:
502
- return bottomContainerView!.bounds.contains(gestureRecognizer.location(in: bottomContainerView))
503
- default:
504
- return false
505
- }
506
- } else {
507
- return true
508
- }
509
- }
510
-
511
- public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
512
- if let panGesture = panGesture, gestureRecognizer === panGesture {
513
- if let scrollView = otherGestureRecognizer.view as? UIScrollView {
514
- // In the case the user is swiping down, we have to check if we let the contained scrollView scrolling or if we have
515
- // to cancel the scrollView gesture to let the bottomSheet itself being swiped.
516
- let isSwipingFromGestureView: Bool = gestureView.frame.contains(gestureRecognizer.location(in: gestureView))
517
- let isTheMainEmbeddedViewAScrollView: Bool = scrollView.superview?.subviews.count == 1
518
- let isScrollViewAtTheTop: Bool = scrollView.contentOffset.y <= 0.0
519
- let isUserSwipingDown: Bool = panGesture.translation(in: view).y >= 0.0
520
- if isSwipingFromGestureView || (isTheMainEmbeddedViewAScrollView && isScrollViewAtTheTop && isUserSwipingDown && behavior.swipeMode == .full) {
521
- otherGestureRecognizer.lbbsCancel()
522
- scrollView.setContentOffset(.zero, animated: true)
523
- return true
524
- } else {
525
- return false
526
- }
527
- } else {
528
- return true
529
- }
530
- } else {
531
- return true
532
- }
533
- }
534
- }
535
-
536
- // MARK: - Animation -
537
- private extension BottomSheetController {
538
- func animate(_ block: @escaping () -> ()) {
539
- if let animationConfiguration = behavior.bottomSheetAnimationConfiguration {
540
- UIView.animate(withDuration: animationConfiguration.duration,
541
- delay: 0.0,
542
- usingSpringWithDamping: animationConfiguration.damping,
543
- initialSpringVelocity: animationConfiguration.velocity,
544
- options: [.curveEaseInOut]) {
545
- block()
546
- }
547
- } else {
548
- UIView.animate(withDuration: 0.3) { block() }
549
- }
550
- }
551
-
552
- func animateAppearing(_ block: @escaping () -> ()) {
553
- if let animationConfiguration = behavior.appearingAnimationConfiguration {
554
- UIView.animate(withDuration: animationConfiguration.duration,
555
- delay: 0.0,
556
- usingSpringWithDamping: animationConfiguration.damping,
557
- initialSpringVelocity: animationConfiguration.velocity,
558
- options: [.curveEaseInOut],
559
- animations: { block() })
560
- } else {
561
- UIView.animate(withDuration: 0.5,
562
- delay: 0.0,
563
- usingSpringWithDamping: 1.0,
564
- initialSpringVelocity: 0.1,
565
- options: [.curveEaseOut],
566
- animations: { block() })
567
- }
568
- }
569
-
570
- func animateDisappearing(_ animationBlock: @escaping () -> (), completion: @escaping () -> ()) {
571
- if let animationConfiguration = behavior.disappearingAnimationConfiguration {
572
- UIView.animate(withDuration: animationConfiguration.duration,
573
- delay: 0.0,
574
- usingSpringWithDamping: animationConfiguration.damping,
575
- initialSpringVelocity: animationConfiguration.velocity,
576
- options: [.curveEaseInOut],
577
- animations: { animationBlock() }) { _ in
578
- completion()
579
- }
580
- } else {
581
- UIView.animate(withDuration: 0.5,
582
- delay: 0.0,
583
- usingSpringWithDamping: 1.0,
584
- initialSpringVelocity: 0.1,
585
- options: [.curveEaseOut],
586
- animations: { animationBlock() }) { _ in
587
- completion()
588
- }
589
- }
590
- }
591
- }
592
-
593
- // MARK: - Actions -
594
- private extension BottomSheetController {
595
- @IBAction func dismissButtonPressed(_ sender: Any) {
596
- if behavior.canTouchDimmingBackgroundToDismiss {
597
- dismiss()
598
- bottomSheetInteractionDelegate?.bottomSheetInteractionDidTapOutside()
599
- }
600
- }
601
- }
@@ -1,60 +0,0 @@
1
- // Copyright © 2021 Lunabee Studio
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
- //
15
- // ForwardingEventsView.swift
16
- // LBBottomSheet
17
- //
18
- // Created by Lunabee Studio / Date - 12/10/2021 - for the LBBottomSheet Swift Package.
19
- //
20
-
21
- import UIKit
22
-
23
- /// This view implementation makes us able to forward events not happening on the bottom sheet itself to the controller behind the bottom sheet.
24
- final class ForwardingEventsView: UIView {
25
-
26
- /// The view which must be the destination of the detected touch events.
27
- var destinationView: UIView?
28
- /// This is the view for which (children included) we must not forward the touch event to the view behind it.
29
- var excludedParentView: UIView?
30
-
31
- override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
32
- // Here we get the original result of the hit test.
33
- let hitTest: UIView? = super.hitTest(point, with: event)
34
-
35
- // We will recursivly go up along the hitTest result superviews to detect if the touched view is the excluded one or contained in the excluded one.
36
- // Recursively, each superview will be stored in currentView.
37
- var currentView: UIView? = hitTest
38
- var isChildOfExcludedParentView: Bool = hitTest == excludedParentView
39
-
40
- // Now we will go up along the superviews until the root one to check if the touched view is not contained in the excluded one.
41
- while let superview = currentView?.superview {
42
- currentView = superview
43
- if currentView == excludedParentView {
44
- isChildOfExcludedParentView = true
45
- break
46
- }
47
- }
48
-
49
- if isChildOfExcludedParentView {
50
- // In the case the touch event was made on the excluded view or on one of its children, we don't forward the touch event to the view behind, so we keep the initial hit test result.
51
- return hitTest
52
- } else {
53
- // If we didn't find the excluded view in the touched view hierarchy, we forward the hitTest to the destination view which can be, for example, the view of the controller being behind the bottom sheet.
54
- // If no destination view was defined, we just return the initial hit test result.
55
- let destinationPoint: CGPoint = convert(point, to: destinationView)
56
- return destinationView?.hitTest(destinationPoint, with: event) ?? hitTest
57
- }
58
- }
59
-
60
- }