Skip to content

Commit

Permalink
Allow Pager respond to move commands
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoya committed Nov 15, 2021
1 parent 1686143 commit c493133
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/SwiftUIPager/Pager+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ extension Pager: Buildable {
public func onDraggingEnded(_ callback: (() -> Void)?) -> Self {
mutating(keyPath: \.onDraggingEnded, value: callback)
}

#else

/// Sets the explicit animation to be used. Defaults to `.standard`
///
/// - Parameter value: animation to use while dragging and to page
///
/// - Warning: `spring` animations don't work well. Avoid high responses while dragging as the animation should be short
public func draggingAnimation(_ value: DraggingAnimation) -> Self {
mutating(keyPath: \.draggingAnimation, value: value)
}

#endif

Expand Down
11 changes: 11 additions & 0 deletions Sources/SwiftUIPager/PagerContent+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ extension Pager.PagerContent: Buildable {
func onDraggingEnded(_ callback: (() -> Void)?) -> Self {
mutating(keyPath: \.onDraggingEnded, value: callback)
}

#else

/// Sets the explicit animation to be used. Defaults to `.standard`
///
/// - Parameter value: animation to use while dragging and to page
///
/// - Warning: `spring` animations don't work well. Avoid high responses while dragging as the animation should be short
public func draggingAnimation(_ value: DraggingAnimation) -> Self {
mutating(keyPath: \.draggingAnimation, value: value)
}

#endif

Expand Down
24 changes: 24 additions & 0 deletions Sources/SwiftUIPager/PagerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ extension Pager {
wrappedView = AnyView(wrappedView.gesture(allowsDragging ? swipeGesture : nil, priority: gesturePriority))
#else
let wrappedView = stack
.focusable()
.onMoveCommand(perform: self.onMoveCommandSent)
#endif

#if os(macOS)
wrappedView = wrappedView
.focusable()
.onMoveCommand(perform: self.onMoveCommandSent)
#endif

var resultView = wrappedView
Expand Down Expand Up @@ -220,6 +228,22 @@ extension Pager {

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension Pager.PagerContent {

#if os(tvOS) || os(macOS)
func onMoveCommandSent(_ command: MoveCommandDirection) {
let animation = self.draggingAnimation.animation ?? .default
switch command {
case .left:
withAnimation(animation) { self.pagerModel.update(.previous) }
case .right:
withAnimation(animation) { self.pagerModel.update(.next) }
case .up, .down:
break
@unknown default:
break
}
}
#endif

/// `DragGesture` customized to work with `Pager`
#if !os(tvOS)
Expand Down

0 comments on commit c493133

Please sign in to comment.