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

[Issue-11] Make dimmer a Close button in VO mode. #23

Merged
merged 9 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/run_linter_and_unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ jobs:
- name: Set Xcode version
run: |
ls -l /Applications | grep 'Xcode'
sudo xcode-select -s /Applications/Xcode_14.0.1.app
sudo xcode-select -s /Applications/Xcode_14.2.app

- name: Lint code using SwiftLint
run: swiftlint --strict --reporter github-actions-logging

- name: Build iOS
run: |
xcodebuild -scheme YBottomSheet -sdk iphonesimulator16.0 -destination 'platform=iOS Simulator,name=iPhone 14' build-for-testing
xcodebuild -scheme YBottomSheet -sdk iphonesimulator16.2 -destination 'platform=iOS Simulator,name=iPhone 14' build-for-testing

- name: Run tests iOS
run: |
xcodebuild -scheme YBottomSheet -sdk iphonesimulator16.0 -destination 'platform=iOS Simulator,name=iPhone 14' test-without-building
xcodebuild -scheme YBottomSheet -sdk iphonesimulator16.2 -destination 'platform=iOS Simulator,name=iPhone 14' test-without-building
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/yml-org/YCoreUI.git",
from: "1.5.0"
from: "1.7.0"
),
.package(
url: "https://github.com/yml-org/YMatterType.git",
Expand Down
50 changes: 26 additions & 24 deletions Sources/YBottomSheet/BottomSheetController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,26 @@ import UIKit
public class BottomSheetController: UIViewController {
// Holds the sheet content until the view is loaded
private let content: Content
private enum Content {
case view(title: String, view: UIView)
case controller(_: UIViewController)
}

private var shadowSize: CGSize = .zero
private let minimumTopOffset: CGFloat = 44
private let minimumContentHeight: CGFloat = 88
private var topAnchor: NSLayoutConstraint?
private var indicatorTopAnchor: NSLayoutConstraint?
private var childHeightAnchor: NSLayoutConstraint?
private var panGesture: UIPanGestureRecognizer?
internal lazy var lastYOffset: CGFloat = {
sheetView.frame.origin.y
}()
internal lazy var lastYOffset: CGFloat = { sheetView.frame.origin.y }()

/// Minimum downward velocity beyond which we interpret a pan gesture as a downward swipe.
public var dismissThresholdVelocity: CGFloat = 1000

/// Priorities for various non-required constraints.
enum Priorities {
static let panGesture = UILayoutPriority(775)
static let sheetContentHugging = UILayoutPriority(751)
static let sheetCompressionResistanceLow = UILayoutPriority.defaultLow
static let sheetCompressionResistanceHigh = UILayoutPriority(800)
}

/// Dimmer tap view
let dimmerTapView: UIView = {
let view = UIView()
view.accessibilityTraits = .button
view.accessibilityLabel = BottomSheetController.Strings.closeButton.localized
view.accessibilityIdentifier = AccessibilityIdentifiers.dimmerId
return view
}()
/// Dimmer view.
let dimmerView = UIView()
/// Bottom sheet view.
Expand Down Expand Up @@ -133,7 +126,14 @@ public class BottomSheetController: UIViewController {
super.viewDidLoad()
build()
}


/// :nodoc:
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// set initial VO focus to the sheet not the dimmer
UIAccessibility.post(notification: .screenChanged, argument: sheetView)
}

/// :nodoc:
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
Expand Down Expand Up @@ -204,13 +204,13 @@ private extension BottomSheetController {
func buildSheet() {
buildViews()
buildConstraints()
view.layoutIfNeeded()
updateViewAppearance()
addGestures()
}

func buildViews() {
view.addSubview(dimmerView)
view.addSubview(dimmerTapView)
view.addSubview(sheetView)
sheetView.addSubview(stackView)
stackView.addArrangedSubview(indicatorContainer)
Expand All @@ -220,8 +220,10 @@ private extension BottomSheetController {

func buildConstraints() {
dimmerView.constrainEdges()
dimmerTapView.constrainEdges(.notBottom)
dimmerTapView.constrain(.bottomAnchor, to: sheetView.topAnchor)

sheetView.constrainEdges(.notTop)

sheetView.constrain(
.topAnchor,
to: view.safeAreaLayoutGuide.topAnchor,
Expand All @@ -246,6 +248,7 @@ private extension BottomSheetController {
}

func updateViewAppearance() {
dimmerTapView.isAccessibilityElement = appearance.isDismissAllowed
sheetView.layer.cornerRadius = appearance.layout.cornerRadius
updateShadow()
dimmerView.backgroundColor = appearance.dimmerColor
Expand Down Expand Up @@ -316,7 +319,7 @@ private extension BottomSheetController {
swipeGesture.direction = .down
view.addGestureRecognizer(swipeGesture)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onDimmerTap))
dimmerView.addGestureRecognizer(tapGesture)
dimmerTapView.addGestureRecognizer(tapGesture)
}

func onDismiss() {
Expand Down Expand Up @@ -344,7 +347,7 @@ private extension BottomSheetController {
}

@objc
private func handlePan(_ gesture: UIPanGestureRecognizer) {
func handlePan(_ gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .began, .changed:
let translation = gesture.translation(in: sheetView)
Expand Down Expand Up @@ -383,8 +386,7 @@ internal extension BottomSheetController {
onDimmerTap(sender: UITapGestureRecognizer())
}

@objc
@discardableResult
@objc @discardableResult
func simulateDragging(_ gesture: UIPanGestureRecognizer) -> Bool {
guard isResizable else { return false }
handlePan(gesture)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// BottomSheetController+AccessibilityIdentifiers.swift
// YBottomSheet
//
// Created by Dev Karan on 25/04/23.
// Copyright © 2023 Y Media Labs. All rights reserved.
//

extension BottomSheetController {
/// Accessibility Identifiers
enum AccessibilityIdentifiers {
/// Button ID
static let buttonId = "sheet.button.close"
/// Dimmer ID
static let dimmerId = "sheet.dimmer.close"
}
}
26 changes: 26 additions & 0 deletions Sources/YBottomSheet/Enums/BottomSheetController+Enums.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// BottomSheetController+Enums.swift
// YBottomSheet
//
// Created by Dev Karan on 25/04/23.
// Copyright © 2023 Y Media Labs. All rights reserved.
//

import Foundation
import UIKit

internal extension BottomSheetController {
/// Priorities for various non-required constraints.
enum Priorities {
static let panGesture = UILayoutPriority(775)
static let sheetContentHugging = UILayoutPriority(751)
static let sheetCompressionResistanceLow = UILayoutPriority.defaultLow
static let sheetCompressionResistanceHigh = UILayoutPriority(800)
}

/// Types of content that can populate a bottom sheet
enum Content {
case view(title: String, view: UIView)
case controller(_: UIViewController)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// YBottomSheet+Strings.swift
// BottomSheetController+Strings.swift
// YBottomSheet
//
// Created by Dev Karan on 15/02/23.
Expand Down
1 change: 1 addition & 0 deletions Sources/YBottomSheet/SheetHeaderView/SheetHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private extension SheetHeaderView {
buildConstraints()
closeButton.addTarget(self, action: #selector(closeButtonAction), for: .touchUpInside)
closeButton.accessibilityLabel = BottomSheetController.Strings.closeButton.localized
closeButton.accessibilityIdentifier = BottomSheetController.AccessibilityIdentifiers.buttonId
}

func buildViews() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// YBottomSheet+StringsTests.swift
// BottomSheetController+StringsTests.swift
// YBottomSheet
//
// Created by Dev Karan on 15/02/23.
Expand All @@ -10,7 +10,7 @@ import XCTest
import YCoreUI
@testable import YBottomSheet

final class YBottomSheetStringsTests: XCTestCase {
final class BottomSheetControllerStringsTests: XCTestCase {
func testLoad() {
BottomSheetController.Strings.allCases.forEach {
// Given a localized string constant
Expand Down