Skip to content

Commit d93b63c

Browse files
committed
Add unlimited undo functionality
1 parent 5e49332 commit d93b63c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

KSSwipeStack.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'KSSwipeStack'
11-
s.version = '0.3.1'
11+
s.version = '0.4.0'
1212
s.summary = 'A Swift card swiping library created by Kicksort Consulting AB'
1313

1414
# This description is used to generate tags and improve search results.
@@ -28,7 +28,7 @@ DOCS: https://github.com/Kicksort/KSSwipeStack
2828
DESC
2929

3030
s.homepage = 'https://github.com/Kicksort/KSSwipeStack'
31-
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
31+
s.screenshots = 'https://camo.githubusercontent.com/0c98618dc067d31bf4a0126d408082f3911d4181/68747470733a2f2f6d656469612e67697068792e636f6d2f6d656469612f7738426e6d736a634a79464b552f67697068792e676966'
3232
s.license = { :type => 'MIT', :file => 'LICENSE' }
3333
s.author = { 'Simon Arneson' => 'arneson@kicksort.se' }
3434
s.source = { :git => 'https://github.com/kicksort/KSSwipeStack.git', :tag => s.version.to_s }

KSSwipeStack/Classes/SwipeView.swift

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
import UIKit
1010
import RxSwift
1111

12+
13+
struct SwipeHistoryItem {
14+
let data: SwipableData
15+
let origin: CGPoint
16+
}
17+
1218
/// Represents a swipable view to be rendered in the swipe stack.
1319
/// The visual representation of a SwipeData object.
1420
public class SwipeView: UIView {
@@ -18,7 +24,7 @@ public class SwipeView: UIView {
1824

1925
fileprivate var dataset: [SwipableData] = []
2026
fileprivate var renderedCards: [SwipableView] = []
21-
fileprivate var previousCard: SwipableView?
27+
fileprivate var swipeHistory: [SwipeHistoryItem] = []
2228
fileprivate var options = SwipeOptions()
2329

2430
fileprivate var swipeDelegate: SwipeDelegate?
@@ -88,6 +94,21 @@ public class SwipeView: UIView {
8894
bringSubview(toFront: renderedCard)
8995
}
9096

97+
/**
98+
Adds a card to the top of the stack and calls notifyDatasetUpdated to make sure it is rendered if needed.
99+
- parameter data: The data the new card represents.
100+
*/
101+
public func addCardToTop(_ data: SwipableData, from origin: CGPoint) {
102+
let cardFrame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
103+
let renderedCard = renderCard(data.getView(with: cardFrame))
104+
renderedCard.frame.origin = origin
105+
renderedCards.insert(renderedCard, at: 0)
106+
addSubview(renderedCard)
107+
swipeHelper.transformCard(renderedCard)
108+
bringSubview(toFront: renderedCard)
109+
snapBack()
110+
}
111+
91112
/**
92113
Get swipe events generated by the SwipeView
93114
- returns: RxObservable firing once for each swipe
@@ -112,6 +133,17 @@ public class SwipeView: UIView {
112133
return needsRefill()
113134
}
114135

136+
// Undoes last swipe
137+
public func undoSwipe() {
138+
guard options.allowUndo else {
139+
return
140+
}
141+
142+
if let cardToUndo = swipeHistory.popLast() {
143+
addCardToTop(cardToUndo.data, from: cardToUndo.origin)
144+
}
145+
}
146+
115147
fileprivate func setupSwipeCards() {
116148

117149
}
@@ -267,8 +299,10 @@ public class SwipeView: UIView {
267299
// TODO
268300
return
269301
}
270-
271-
previousCard = card.isUndoable() ? card : nil
302+
303+
if card.isUndoable(), let data = card.getData() {
304+
swipeHistory.append(SwipeHistoryItem(data: data, origin: card.frame.origin))
305+
}
272306

273307
dismissCard(direction: direction, gesture: gesture, completion: { [weak self] in
274308
let swipe = Swipe(direction: direction, data: card.getData())

0 commit comments

Comments
 (0)