Skip to content

Commit 95ca312

Browse files
authored
Fix: Allow dismiss in code before presentation is complete (#60)
1 parent d6182e8 commit 95ca312

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

.circleci/config.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ version: 2
22
jobs:
33
build-and-test:
44
macos:
5-
xcode: "11.2.1"
5+
xcode: "13.2.0"
66
shell: /bin/bash --login -o pipefail
77
steps:
88
- checkout
9-
- run: xcodebuild -project BottomSheet.xcodeproj -scheme "Demo" -sdk iphonesimulator -destination 'platform=iOS Simulator,OS=13.2.2,name=iPhone 11 Pro' build test | xcpretty
9+
- run: xcodebuild -project BottomSheet.xcodeproj -scheme "Demo" -sdk iphonesimulator -destination 'platform=iOS Simulator,OS=15.2,name=iPhone 13 Pro' build test | xcpretty
1010

1111
swiftlint:
1212
docker:

Demo/DemoViewController.swift

+28-16
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,12 @@ final class DemoViewController: UIViewController {
2323
private func setup() {
2424
view.backgroundColor = .white
2525

26-
let buttonA = UIButton(type: .system)
27-
buttonA.setTitle("Navigation View Controller", for: .normal)
28-
buttonA.titleLabel?.font = .systemFont(ofSize: 18)
29-
buttonA.addTarget(self, action: #selector(presentNavigationViewController), for: .touchUpInside)
30-
31-
let buttonB = UIButton(type: .system)
32-
buttonB.setTitle("View Controller", for: .normal)
33-
buttonB.titleLabel?.font = .systemFont(ofSize: 18)
34-
buttonB.addTarget(self, action: #selector(presentViewController), for: .touchUpInside)
35-
36-
let buttonC = UIButton(type: .system)
37-
buttonC.setTitle("View", for: .normal)
38-
buttonC.titleLabel?.font = .systemFont(ofSize: 18)
39-
buttonC.addTarget(self, action: #selector(presentView), for: .touchUpInside)
40-
41-
let stackView = UIStackView(arrangedSubviews: [buttonA, buttonB, buttonC])
26+
let stackView = UIStackView(arrangedSubviews: [
27+
createButton(title: "Navigation View Controller", selector: #selector(presentNavigationViewController)),
28+
createButton(title: "View Controller", selector: #selector(presentViewController)),
29+
createButton(title: "View", selector: #selector(presentView)),
30+
createButton(title: "Automatic dismiss after 0.05s", selector: #selector(presentAutomaticDismiss)),
31+
])
4232
stackView.translatesAutoresizingMaskIntoConstraints = false
4333
stackView.axis = .vertical
4434

@@ -50,6 +40,14 @@ final class DemoViewController: UIViewController {
5040
])
5141
}
5242

43+
private func createButton(title: String, selector: Selector) -> UIButton {
44+
let button = UIButton(type: .system)
45+
button.setTitle(title, for: .normal)
46+
button.titleLabel?.font = .systemFont(ofSize: 18)
47+
button.addTarget(self, action: selector, for: .touchUpInside)
48+
return button
49+
}
50+
5351
// MARK: - Presentation logic
5452

5553
@objc private func presentNavigationViewController() {
@@ -62,6 +60,20 @@ final class DemoViewController: UIViewController {
6260
present(navigationController, animated: true)
6361
}
6462

63+
@objc private func presentAutomaticDismiss() {
64+
let viewController = ViewController(withNavigationButton: true, contentHeight: 400)
65+
viewController.title = "Step 1"
66+
67+
let navigationController = BottomSheetNavigationController(rootViewController: viewController)
68+
navigationController.navigationBar.isTranslucent = false
69+
70+
present(navigationController, animated: true)
71+
72+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05, execute: { [weak navigationController] in
73+
navigationController?.dismiss(animated: true)
74+
})
75+
}
76+
6577
// MARK: - Presentation logic
6678

6779
@objc private func presentViewController() {

Sources/BottomSheetPresentationController.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,14 @@ extension BottomSheetPresentationController: UIViewControllerAnimatedTransitioni
190190
self.transitionContext = transitionContext
191191

192192
let completion = { [weak self] (didComplete: Bool) in
193-
self?.transitionContext?.completeTransition(didComplete)
194-
self?.transitionState = nil
193+
guard let self = self else { return }
194+
let previousTransitionState = self.transitionState
195+
196+
self.transitionContext?.completeTransition(didComplete)
197+
198+
if previousTransitionState == self.transitionState {
199+
self.transitionState = nil
200+
}
195201
}
196202

197203
switch transitionState {

0 commit comments

Comments
 (0)