Skip to content

Commit

Permalink
Fix: Allow dismiss in code before presentation is complete (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
bstien authored Mar 2, 2022
1 parent d6182e8 commit 95ca312
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ version: 2
jobs:
build-and-test:
macos:
xcode: "11.2.1"
xcode: "13.2.0"
shell: /bin/bash --login -o pipefail
steps:
- checkout
- run: xcodebuild -project BottomSheet.xcodeproj -scheme "Demo" -sdk iphonesimulator -destination 'platform=iOS Simulator,OS=13.2.2,name=iPhone 11 Pro' build test | xcpretty
- run: xcodebuild -project BottomSheet.xcodeproj -scheme "Demo" -sdk iphonesimulator -destination 'platform=iOS Simulator,OS=15.2,name=iPhone 13 Pro' build test | xcpretty

swiftlint:
docker:
Expand Down
44 changes: 28 additions & 16 deletions Demo/DemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,12 @@ final class DemoViewController: UIViewController {
private func setup() {
view.backgroundColor = .white

let buttonA = UIButton(type: .system)
buttonA.setTitle("Navigation View Controller", for: .normal)
buttonA.titleLabel?.font = .systemFont(ofSize: 18)
buttonA.addTarget(self, action: #selector(presentNavigationViewController), for: .touchUpInside)

let buttonB = UIButton(type: .system)
buttonB.setTitle("View Controller", for: .normal)
buttonB.titleLabel?.font = .systemFont(ofSize: 18)
buttonB.addTarget(self, action: #selector(presentViewController), for: .touchUpInside)

let buttonC = UIButton(type: .system)
buttonC.setTitle("View", for: .normal)
buttonC.titleLabel?.font = .systemFont(ofSize: 18)
buttonC.addTarget(self, action: #selector(presentView), for: .touchUpInside)

let stackView = UIStackView(arrangedSubviews: [buttonA, buttonB, buttonC])
let stackView = UIStackView(arrangedSubviews: [
createButton(title: "Navigation View Controller", selector: #selector(presentNavigationViewController)),
createButton(title: "View Controller", selector: #selector(presentViewController)),
createButton(title: "View", selector: #selector(presentView)),
createButton(title: "Automatic dismiss after 0.05s", selector: #selector(presentAutomaticDismiss)),
])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical

Expand All @@ -50,6 +40,14 @@ final class DemoViewController: UIViewController {
])
}

private func createButton(title: String, selector: Selector) -> UIButton {
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 18)
button.addTarget(self, action: selector, for: .touchUpInside)
return button
}

// MARK: - Presentation logic

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

@objc private func presentAutomaticDismiss() {
let viewController = ViewController(withNavigationButton: true, contentHeight: 400)
viewController.title = "Step 1"

let navigationController = BottomSheetNavigationController(rootViewController: viewController)
navigationController.navigationBar.isTranslucent = false

present(navigationController, animated: true)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.05, execute: { [weak navigationController] in
navigationController?.dismiss(animated: true)
})
}

// MARK: - Presentation logic

@objc private func presentViewController() {
Expand Down
10 changes: 8 additions & 2 deletions Sources/BottomSheetPresentationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,14 @@ extension BottomSheetPresentationController: UIViewControllerAnimatedTransitioni
self.transitionContext = transitionContext

let completion = { [weak self] (didComplete: Bool) in
self?.transitionContext?.completeTransition(didComplete)
self?.transitionState = nil
guard let self = self else { return }
let previousTransitionState = self.transitionState

self.transitionContext?.completeTransition(didComplete)

if previousTransitionState == self.transitionState {
self.transitionState = nil
}
}

switch transitionState {
Expand Down

0 comments on commit 95ca312

Please sign in to comment.