Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support UIAccessibilityPerformEscape #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions PanModal/View/PanContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,36 @@ 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
if shouldPerforEscape {
presentedViewController?.dismiss(animated: true, completion: nil)
}
}
return shouldPerforEscape
}
}

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

extension UIView {
Expand Down
4 changes: 4 additions & 0 deletions PanModalDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0800056627E406A200EF6459 /* UIAccessiblityPerformEscapeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0800056527E406A200EF6459 /* UIAccessiblityPerformEscapeTests.swift */; };
0F2A2C552239C119003BDB2F /* PanModal.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2A2C532239C119003BDB2F /* PanModal.h */; settings = {ATTRIBUTES = (Public, ); }; };
0F2A2C582239C119003BDB2F /* PanModal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0F2A2C512239C119003BDB2F /* PanModal.framework */; };
0F2A2C592239C119003BDB2F /* PanModal.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 0F2A2C512239C119003BDB2F /* PanModal.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
Expand Down Expand Up @@ -90,6 +91,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0800056527E406A200EF6459 /* UIAccessiblityPerformEscapeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIAccessiblityPerformEscapeTests.swift; sourceTree = "<group>"; };
0F2A2C512239C119003BDB2F /* PanModal.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PanModal.framework; sourceTree = BUILT_PRODUCTS_DIR; };
0F2A2C532239C119003BDB2F /* PanModal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PanModal.h; sourceTree = "<group>"; };
0F2A2C542239C119003BDB2F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -207,6 +209,7 @@
743CABC52226171500634A5A /* Tests */ = {
isa = PBXGroup;
children = (
0800056527E406A200EF6459 /* UIAccessiblityPerformEscapeTests.swift */,
743CABC62226171500634A5A /* PanModalTests.swift */,
743CABC82226171500634A5A /* Info.plist */,
);
Expand Down Expand Up @@ -552,6 +555,7 @@
buildActionMask = 2147483647;
files = (
743CABC72226171500634A5A /* PanModalTests.swift in Sources */,
0800056627E406A200EF6459 /* UIAccessiblityPerformEscapeTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
75 changes: 75 additions & 0 deletions Tests/UIAccessiblityPerformEscapeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// 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? { nil }
}

class UnDismissablePanModalViewController: UIViewController, PanModalPresentable {
var panScrollable: UIScrollView? { nil }
var allowsTapToDismiss: Bool { false }
var allowsDragToDismiss: Bool { false }
}

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)
}

func testAccessibilityPerformEscapeOnDismissDisabledModal() throws {

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

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) {
XCTAssertNotNil(presenterViewController?.presentedViewController, "panModal shouldn't have been dismissed")
dismissDidFinish.fulfill()
}
}

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