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

In addition to handling drag gesture changes & ending, also handle ca… #149

Merged
merged 3 commits into from
Feb 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
// 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)
}
}
}
}
Loading