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

Respect the swipe actions configuration #254

Merged
merged 1 commit into from
Jan 20, 2021
Merged
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
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