99import UIKit
1010import 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.
1420public 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