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-13] Add option to disable dismiss UI. #14

Merged
merged 4 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions Sources/YBottomSheet/BottomSheetController+Animation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// BottomSheetController+Animation.swift
// YBottomSheet
//
// Created by Dev Karan on 22/03/23.
// Copyright © 2023 Y Media Labs. All rights reserved.
//

import UIKit

extension BottomSheetController: UIViewControllerTransitioningDelegate {
/// Returns the animator for presenting a bottom sheet
public func animationController(
forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
BottomSheetPresentAnimator(sheetViewController: self)
}

/// Returns the animator for dismissing a bottom sheet
public func animationController(
forDismissed dismissed: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
BottomSheetDismissAnimator(sheetViewController: self)
}
}
9 changes: 7 additions & 2 deletions Sources/YBottomSheet/BottomSheetController+Appearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ extension BottomSheetController {
///
/// Only applicable for resizable sheets. `nil` means to use the content view's intrinsic height as the minimum.
public var minimumContentHeight: CGFloat?

/// Whether the sheet is dismissible or not. Default is `true`.
public var allowDismiss: Bool
mpospese marked this conversation as resolved.
Show resolved Hide resolved

/// Default appearance (fixed size sheet)
public static let `default` = Appearance()
/// Default appearance for a resizable sheet
Expand All @@ -49,6 +51,7 @@ extension BottomSheetController {
/// - presentAnimationCurve: Animaiton during presenting.
/// - dismissAnimationCurve: Animation during dismiss.
/// - minimumContentHeight: Optional) Minimum content view height.
/// - allowDismiss: Whether the sheet is dismissible or not.
mpospese marked this conversation as resolved.
Show resolved Hide resolved
public init(
indicatorAppearance: DragIndicatorView.Appearance? = nil,
headerAppearance: SheetHeaderView.Appearance? = .default,
Expand All @@ -58,7 +61,8 @@ extension BottomSheetController {
animationDuration: TimeInterval = 0.3,
presentAnimationCurve: UIView.AnimationOptions = .curveEaseIn,
dismissAnimationCurve: UIView.AnimationOptions = .curveEaseOut,
minimumContentHeight: CGFloat? = nil
minimumContentHeight: CGFloat? = nil,
allowDismiss: Bool = true
mpospese marked this conversation as resolved.
Show resolved Hide resolved
) {
self.indicatorAppearance = indicatorAppearance
self.headerAppearance = headerAppearance
Expand All @@ -69,6 +73,7 @@ extension BottomSheetController {
self.presentAnimationCurve = presentAnimationCurve
self.dismissAnimationCurve = dismissAnimationCurve
self.minimumContentHeight = minimumContentHeight
self.allowDismiss = allowDismiss
}
}
}
32 changes: 11 additions & 21 deletions Sources/YBottomSheet/BottomSheetController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ public class BottomSheetController: UIViewController {
didDismiss()
return true
}

/// Dismiss bottom sheet.
/// - Parameter isCloseButton: whether close button is tapped or not.
func didDismiss(isCloseButton: Bool = false) {
if appearance.allowDismiss || isCloseButton {
onDismiss()
}
}
mpospese marked this conversation as resolved.
Show resolved Hide resolved
}

private extension BottomSheetController {
Expand Down Expand Up @@ -308,8 +316,8 @@ private extension BottomSheetController {

extension BottomSheetController: SheetHeaderViewDelegate {
@objc
func didDismiss() {
onDismiss()
func didCloseTapped() {
didDismiss(isCloseButton: true)
}
}

Expand Down Expand Up @@ -352,24 +360,6 @@ private extension BottomSheetController {
}
}

extension BottomSheetController: UIViewControllerTransitioningDelegate {
/// Returns the animator for presenting a bottom sheet
public func animationController(
forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
BottomSheetPresentAnimator(sheetViewController: self)
}

/// Returns the animator for dismissing a bottom sheet
public func animationController(
forDismissed dismissed: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
BottomSheetDismissAnimator(sheetViewController: self)
}
}

// Methods for unit testing
internal extension BottomSheetController {
@objc
Expand All @@ -392,6 +382,6 @@ internal extension BottomSheetController {

@objc
func simulateDismiss() {
mpospese marked this conversation as resolved.
Show resolved Hide resolved
didDismiss()
didCloseTapped()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
import Foundation

internal protocol SheetHeaderViewDelegate: AnyObject {
func didDismiss()
func didCloseTapped()
mpospese marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion Sources/YBottomSheet/SheetHeaderView/SheetHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ open class SheetHeaderView: UIView {
required public init?(coder: NSCoder) { nil }

@objc private func closeButtonAction() {
delegate?.didDismiss()
delegate?.didCloseTapped()
}

// For unit testing
Expand Down
98 changes: 60 additions & 38 deletions Tests/YBottomSheetTests/BottomSheetControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,40 +130,6 @@ final class BottomSheetControllerTests: XCTestCase {
XCTAssertTrue(sut.indicatorContainer.isHidden)
XCTAssertFalse(sut.isResizable)
}

func test_onDimmer() {
let sut = SpyBottomSheetController(title: "", childView: UIView())

XCTAssertFalse(sut.onDimmerTapped)
XCTAssertFalse(sut.isDismissed)

sut.simulateOnDimmerTap()

XCTAssertTrue(sut.onDimmerTapped)
XCTAssertTrue(sut.isDismissed)
}

func test_onSwipeDown() {
let sut = SpyBottomSheetController(title: "", childView: UIView())

XCTAssertFalse(sut.onSwipeDown)
XCTAssertFalse(sut.isDismissed)

sut.simulateOnSwipeDown()

XCTAssertTrue(sut.onSwipeDown)
XCTAssertTrue(sut.isDismissed)
}

func test_onDidDismiss() {
let sut = SpyBottomSheetController(title: "", childView: UIView())

XCTAssertFalse(sut.isDismissed)

sut.simulateDismiss()

XCTAssertTrue(sut.isDismissed)
}

func test_dragging_worksIfResizable() {
let sut = SpyBottomSheetController(title: "", childView: ChildView(), appearance: .defaultResizable)
Expand Down Expand Up @@ -324,6 +290,56 @@ final class BottomSheetControllerTests: XCTestCase {
}
}

extension BottomSheetControllerTests {
mpospese marked this conversation as resolved.
Show resolved Hide resolved
func test_onDimmer() {
let sut = SpyBottomSheetController(title: "", childView: UIView())

XCTAssertFalse(sut.onDimmerTapped)
XCTAssertFalse(sut.isDismissed)

sut.simulateOnDimmerTap()

XCTAssertTrue(sut.onDimmerTapped)
XCTAssertTrue(sut.isDismissed)
}

func test_onSwipeDown() {
let sut = SpyBottomSheetController(title: "", childView: UIView())

XCTAssertFalse(sut.onSwipeDown)
XCTAssertFalse(sut.isDismissed)

sut.simulateOnSwipeDown()

XCTAssertTrue(sut.onSwipeDown)
XCTAssertTrue(sut.isDismissed)
}

func test_onDidDismiss() {
let sut = SpyBottomSheetController(title: "", childView: UIView())

XCTAssertFalse(sut.isDismissed)

sut.simulateDismiss()

XCTAssertTrue(sut.isDismissed)
}

func test_forbidDismiss() {
let sut = SpyBottomSheetController(title: "", childView: UIView())
sut.appearance.allowDismiss = false

XCTAssertFalse(sut.onSwipeDown)
XCTAssertFalse(sut.onDimmerTapped)

sut.simulateOnDimmerTap()
sut.simulateOnSwipeDown()
mpospese marked this conversation as resolved.
Show resolved Hide resolved

XCTAssertFalse(sut.onSwipeDown)
XCTAssertFalse(sut.onDimmerTapped)
}
}

private extension BottomSheetControllerTests {
func makeSUT(
viewController: UIViewController,
Expand Down Expand Up @@ -368,19 +384,25 @@ final class SpyBottomSheetController: BottomSheetController {
var onDimmerTapped = false
var onDragging = false

override func didDismiss() {
override func didDismiss(isCloseButton: Bool) {
super.didDismiss()
isDismissed = true
if isCloseButton || appearance.allowDismiss {
isDismissed = true
}
}

override func simulateOnSwipeDown() {
super.simulateOnSwipeDown()
onSwipeDown = true
if appearance.allowDismiss {
onSwipeDown = true
}
}

override func simulateOnDimmerTap() {
super.simulateOnDimmerTap()
onDimmerTapped = true
if appearance.allowDismiss {
onDimmerTapped = true
}
}

@discardableResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private extension SheetHeaderViewTests {
}

extension SheetHeaderViewTests: SheetHeaderViewDelegate {
func didDismiss() {
func didCloseTapped() {
isDismissed = true
}
}