Skip to content

Commit

Permalink
Respect the swipe actions configuration (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebshr authored Jan 20, 2021
1 parent e4d56d1 commit 65bd5f5
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions ListableUI/Sources/Internal/ItemCell.ContentViewContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ extension ItemCell {
final class ContentContainerView : UIView {

let contentView : Content.ContentView

private var swipeConfiguration: SwipeConfiguration?

private var swipeState: SwipeActionState = .closed {
didSet {
if oldValue != swipeState {
Expand All @@ -30,7 +30,7 @@ extension ItemCell {
self.contentView = Content.createReusableContentView(frame: bounds)

super.init(frame: frame)

self.addSubview(self.contentView)
}

Expand Down Expand Up @@ -112,7 +112,8 @@ extension ItemCell {
swipeConfiguration = SwipeConfiguration(
panGestureRecognizer: panGestureRecognizer,
swipeView: swipeView,
numberOfActions: actions.actions.count
numberOfActions: actions.actions.count,
performsFirstActionWithFullSwipe: actions.performsFirstActionWithFullSwipe
)
}

Expand All @@ -124,21 +125,22 @@ extension ItemCell {
set(state: .closed)
}
}

private weak var listView : ListView? = nil

@objc private func handlePan(sender: UIPanGestureRecognizer) {

if self.listView == nil {
self.listView = self.firstSuperview(ofType: ListView.self)
}

guard let configuration = swipeConfiguration else { return }

let offsetMultiplier = configuration.numberOfActions == 1 ? 0.5 : 0.7
let performActionOffset = frame.width * CGFloat(offsetMultiplier)
let currentSwipeOffset = -contentView.frame.origin.x
let willPerformAction = currentSwipeOffset > performActionOffset
&& configuration.performsFirstActionWithFullSwipe

if sender.state == .began {
self.listView?.liveCells.perform {
Expand Down Expand Up @@ -194,20 +196,20 @@ extension ItemCell {
}

private func didPerformAction(expandActions: Bool) {

if expandActions {
self.set(state: .expandActions, animated: true)
} else {
self.set(state: .closed, animated: true)
}
}

func performAnimatedClose() {
self.set(state: .closed, animated: true)
}

private func set(state: SwipeActionState, animated: Bool = false) {

swipeState = state

if animated {
Expand Down Expand Up @@ -236,6 +238,7 @@ extension ItemCell {
let panGestureRecognizer: UIPanGestureRecognizer
let swipeView: Content.SwipeActionsView
var numberOfActions: Int
var performsFirstActionWithFullSwipe: Bool
}
}

Expand Down

0 comments on commit 65bd5f5

Please sign in to comment.