Skip to content

Commit

Permalink
support accessibilityPerformEscape
Browse files Browse the repository at this point in the history
  • Loading branch information
e-sung committed Mar 17, 2022
1 parent b2f5bd7 commit 3261089
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions PanModal/View/PanContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,34 @@ import UIKit
having to do those changes directly on the view
*/
class PanContainerView: UIView {

private weak var presentedViewController: UIViewController?

init(presentedView: UIView, frame: CGRect) {
super.init(frame: frame)
addSubview(presentedView)
presentedViewController = presentedView.parentViewController
}

@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func accessibilityPerformEscape() -> Bool {
var shouldPerforEscape: Bool = true
if let panModalPresentable = presentedViewController as? PanModalPresentable {
shouldPerforEscape = panModalPresentable.allowsDragToDismiss || panModalPresentable.allowsTapToDismiss
presentedViewController?.dismiss(animated: true, completion: nil)
}
return shouldPerforEscape
}
}

private extension UIResponder {
var parentViewController: UIViewController? {
return next as? UIViewController ?? next?.parentViewController
}
}

extension UIView {
Expand Down
43 changes: 43 additions & 0 deletions Tests/UIAccessiblityPerformEscapeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// UIAccessiblityPerformEscapeTests.swift
// PanModalTests
//
// Created by Sungdoo on 2022/03/17.
// Copyright © 2022 Detail. All rights reserved.
//

import XCTest
import PanModal

class UIAccessiblityPerformEscapeTests: XCTestCase {

class MockViewController: UIViewController, PanModalPresentable {
var panScrollable: UIScrollView? { return nil }
}

func testAccessibilityPerformEscape() throws {

let presenterViewController = UIApplication.shared.keyWindow?.rootViewController
let panModal: UIViewController & PanModalPresentable = MockViewController()

presenterViewController?.presentPanModal(panModal)
XCTAssertNotNil(presenterViewController?.presentedViewController, "panModal should have been presented")

let presentDidFisnish = XCTestExpectation()
let dismissDidFinish = XCTestExpectation()

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {

let panContainerView = panModal.view.superview
presentDidFisnish.fulfill()

panContainerView?.accessibilityPerformEscape()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
XCTAssertNil(presenterViewController?.presentedViewController, "panModal should have been dismissed")
dismissDidFinish.fulfill()
}
}

wait(for: [presentDidFisnish, dismissDidFinish], timeout: 10)
}
}

0 comments on commit 3261089

Please sign in to comment.