Skip to content

Commit

Permalink
Implement Undo Action
Browse files Browse the repository at this point in the history
  • Loading branch information
omarthamri committed Dec 1, 2023
1 parent ce662e8 commit 9d40690
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 4 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Example/Tests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.7.3</string>
<string>1.8</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion MyTinderSwipingAnimation.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'MyTinderSwipingAnimation'
s.version = '1.7.3'
s.version = '1.8'
s.summary = 'TinderSwipingAnimation is an easy to use library written using SwiftUI to simplify the implementation of the tinder swipe animation.'

# This description is used to generate tags and improve search results.
Expand Down
2 changes: 2 additions & 0 deletions Sources/TinderSwipingAnimation/Components/CustomButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct CustomButton: View {
viewModel.goTop = true
case .goBottom:
viewModel.goBottom = true
case .undo:
viewModel.undo = true
}
}) {
button.image
Expand Down
1 change: 1 addition & 0 deletions Sources/TinderSwipingAnimation/Models/ButtonModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ public struct ButtonModel {
case goLeft
case goTop
case goBottom
case undo
}
}
2 changes: 2 additions & 0 deletions Sources/TinderSwipingAnimation/Models/Direction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public enum Direction {
case left
case top
case bottom
case undo
public var description: String {
switch self {
case .right: return "right"
case .left: return "left"
case .top: return "top"
case .bottom: return "bottom"
case .undo: return "undo"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ class TinderViewModel: ObservableObject {
@Published var goLeft: Bool = false
@Published var goTop: Bool = false
@Published var goBottom: Bool = false
@Published var undo: Bool = false
}
18 changes: 17 additions & 1 deletion Sources/TinderSwipingAnimation/Views/CardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ struct CardView: View {
viewModel.$goBottom.sink { _ in
}
.store(in: &subscriptions)
viewModel.$undo.sink { _ in
}
.store(in: &subscriptions)
}
var body: some View {
ZStack {
ForEach(0..<cards.count,id: \.self) { i in
Card(card: cards[i], buttons: buttons, viewModel: viewModel, orientation: orientation, backgroundColor: backgroundColor)
.offset(x: self.x[i],y: y[i])
.rotationEffect(.init(degrees: degree[i]))
.onChange(of: viewModel.goRight || viewModel.goLeft || viewModel.goBottom || viewModel.goTop) {
.onChange(of: viewModel.goRight || viewModel.goLeft || viewModel.goBottom || viewModel.goTop || viewModel.undo) {
if viewModel.goRight && abs(degree[i]) != 12 && abs(degree[i]) != 1 {
withAnimation(.default) {
viewModel.goRight = false
Expand Down Expand Up @@ -86,6 +89,19 @@ struct CardView: View {
viewModel.cards[i].thumbsUpOpacity = 0
viewModel.cards[i].thumbsDownOpacity = 0
}
} else if viewModel.undo {
viewModel.undo = false
if let lastIndex = degree.firstIndex(where: {abs($0) == 1 || abs($0) == 12}) {
withAnimation(.default) {
self.x[lastIndex] = 0
self.y[lastIndex] = 0
self.offset = 0
self.degree[lastIndex] = 0
viewModel.cardSwiped = (cards[i],Direction.undo)
viewModel.cards[i].thumbsUpOpacity = 0
viewModel.cards[i].thumbsDownOpacity = 0
}
}
}
}
.gesture(DragGesture()
Expand Down

0 comments on commit 9d40690

Please sign in to comment.