Skip to content

Commit

Permalink
Fix #148 by handling cancelled drag gestures (#149, thx @juhieta)
Browse files Browse the repository at this point in the history
In addition to handling drag gesture changes & ending, also handle cancelled drag gesture. Fixes #148
  • Loading branch information
juhieta authored Feb 21, 2024
1 parent 720ddbd commit 7af4bb2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
20 changes: 6 additions & 14 deletions Sources/BottomSheet/BottomSheetView/BottomSheetView+Gestures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal extension BottomSheetView {
func dragGesture(with geometry: GeometryProxy) -> some Gesture {
DragGesture()
.onChanged { value in
self.lastDragValue = value

// Perform custom onChanged action
self.configuration.onDragChanged(value)

Expand All @@ -19,20 +21,10 @@ internal extension BottomSheetView {
// Dismiss the keyboard on drag
self.endEditing()
}
.onEnded { value in
// Perform custom onEnded action
self.configuration.onDragEnded(value)

// Switch the position based on the translation and screen height
self.dragPositionSwitch(
with: geometry,
value: value
)

// Reset translation, because the dragging ended
self.translation = 0
// Dismiss the keyboard after drag
self.endEditing()
// Set isDragging flag to true while user is dragging
// The value is reset to false when dragging is stopped or cancelled
.updating($isDragging) { (value, gestureState, transaction) in

Check warning on line 26 in Sources/BottomSheet/BottomSheetView/BottomSheetView+Gestures.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Unused Closure Parameter Violation: Unused parameter in a closure should be replaced with _ (unused_closure_parameter)

Check warning on line 26 in Sources/BottomSheet/BottomSheetView/BottomSheetView+Gestures.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Unused Closure Parameter Violation: Unused parameter in a closure should be replaced with _ (unused_closure_parameter)
gestureState = true
}
}

Expand Down
24 changes: 23 additions & 1 deletion Sources/BottomSheet/BottomSheetView/BottomSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import SwiftUI

internal struct BottomSheetView<HContent: View, MContent: View>: View {

@GestureState var isDragging: Bool = false
@State var lastDragValue: DragGesture.Value?

// For iPhone landscape and iPad support
#if !os(macOS)
@Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass?
Expand Down Expand Up @@ -60,6 +62,26 @@ internal struct BottomSheetView<HContent: View, MContent: View>: View {
self.bottomSheet(with: geometry)
}
}
// Handle drag ended or cancelled
// Drag cancellation can happen e.g. when user drags from bottom of the screen to show app switcher
.valueChanged(value: isDragging, onChange: { isDragging in
if lastDragValue != nil && !isDragging {
// Perform custom onEnded action
self.configuration.onDragEnded(lastDragValue!)

// Switch the position based on the translation and screen height
self.dragPositionSwitch(
with: geometry,
value: lastDragValue!
)

// Reset translation and last drag value, because the dragging ended
self.translation = 0
self.lastDragValue = nil
// Dismiss the keyboard after drag
self.endEditing()
}
})
// Animate value changes
#if !os(macOS)
.animation(
Expand Down
24 changes: 24 additions & 0 deletions Sources/BottomSheet/Helper/Extensions/ViewExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// ViewExtension.swift
//
//
// Created by Jukka Hietanen on 12.12.2023.
//

import Foundation

import SwiftUI
import Combine

internal extension View {
/// A backwards compatible wrapper for iOS 14 `onChange`
@ViewBuilder func valueChanged<T: Equatable>(value: T, onChange: @escaping (T) -> Void) -> some View {
if #available(iOS 14.0, macOS 11.0, *) {
self.onChange(of: value, perform: onChange)
} else {
self.onReceive(Just(value)) { (value) in
onChange(value)
}
}
}
}

0 comments on commit 7af4bb2

Please sign in to comment.