Skip to content

Commit

Permalink
[CM-1261] Comments addressed.
Browse files Browse the repository at this point in the history
  • Loading branch information
devyml committed Mar 22, 2023
1 parent b6bf97a commit 3a213b5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
14 changes: 8 additions & 6 deletions Sources/YBottomSheet/BottomSheetController+Appearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ 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

/// Whether the sheet can be dismissed by swiping down or tapping on the dimmer. Default is `true`.
///
/// The user can always dismiss the sheet from the close button if it is visible.
public var isDismissAllowed: Bool

/// Default appearance (fixed size sheet)
public static let `default` = Appearance()
/// Default appearance for a resizable sheet
Expand All @@ -51,7 +53,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.
/// - isDismissAllowed: Whether the sheet can be dismissed by swiping down or tapping on the dimmer.
public init(
indicatorAppearance: DragIndicatorView.Appearance? = nil,
headerAppearance: SheetHeaderView.Appearance? = .default,
Expand All @@ -62,7 +64,7 @@ extension BottomSheetController {
presentAnimationCurve: UIView.AnimationOptions = .curveEaseIn,
dismissAnimationCurve: UIView.AnimationOptions = .curveEaseOut,
minimumContentHeight: CGFloat? = nil,
allowDismiss: Bool = true
isDismissAllowed: Bool = true
) {
self.indicatorAppearance = indicatorAppearance
self.headerAppearance = headerAppearance
Expand All @@ -73,7 +75,7 @@ extension BottomSheetController {
self.presentAnimationCurve = presentAnimationCurve
self.dismissAnimationCurve = dismissAnimationCurve
self.minimumContentHeight = minimumContentHeight
self.allowDismiss = allowDismiss
self.isDismissAllowed = isDismissAllowed
}
}
}
16 changes: 9 additions & 7 deletions Sources/YBottomSheet/BottomSheetController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ public class BottomSheetController: UIViewController {
return true
}

/// Dismiss bottom sheet.
/// - Parameter isCloseButton: whether close button is tapped or not.
func didDismiss(isCloseButton: Bool = false) {
if appearance.allowDismiss || isCloseButton {
/// Dismisses the bottom sheet if allowed.
///
/// This method is not called when the header's close button is tapped.
func didDismiss() {
if appearance.isDismissAllowed {
onDismiss()
}
}
Expand Down Expand Up @@ -316,8 +317,9 @@ private extension BottomSheetController {

extension BottomSheetController: SheetHeaderViewDelegate {
@objc
func didCloseTapped() {
didDismiss(isCloseButton: true)
func didTapCloseButton() {
// Directly dismiss the sheet without considering `isDismissAllowed`.
onDismiss()
}
}

Expand Down Expand Up @@ -382,6 +384,6 @@ internal extension BottomSheetController {

@objc
func simulateDismiss() {
didCloseTapped()
didTapCloseButton()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
import Foundation

internal protocol SheetHeaderViewDelegate: AnyObject {
func didCloseTapped()
func didTapCloseButton()
}
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?.didCloseTapped()
delegate?.didTapCloseButton()
}

// For unit testing
Expand Down
20 changes: 13 additions & 7 deletions Tests/YBottomSheetTests/BottomSheetControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import YMatterType

// OK to have lots of test cases
// swiftlint:disable file_length
// swiftlint:disable type_body_length

final class BottomSheetControllerTests: XCTestCase {
var window: UIWindow!
Expand Down Expand Up @@ -288,9 +289,7 @@ final class BottomSheetControllerTests: XCTestCase {
let sut = makeSUT(viewController: UINavigationController(rootViewController: UIViewController()))
XCTAssertFalse(sut.hasHeader)
}
}

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

Expand Down Expand Up @@ -327,16 +326,18 @@ extension BottomSheetControllerTests {

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

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

sut.simulateOnDimmerTap()
sut.simulateOnSwipeDown()

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

Expand Down Expand Up @@ -383,24 +384,29 @@ final class SpyBottomSheetController: BottomSheetController {
var onSwipeDown = false
var onDimmerTapped = false
var onDragging = false

override func simulateDismiss() {
super.simulateDismiss()
isDismissed = true
}

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

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

override func simulateOnDimmerTap() {
super.simulateOnDimmerTap()
if appearance.allowDismiss {
if appearance.isDismissAllowed {
onDimmerTapped = true
}
}
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 didCloseTapped() {
func didTapCloseButton() {
isDismissed = true
}
}

0 comments on commit 3a213b5

Please sign in to comment.