Compare commits

..

No commits in common. "56b32c39abb243ae5b992aa44c7aae6c2bd21c5c" and "ed1377d9c5e3855f7494ad3bedcaf29af1676430" have entirely different histories.

1 changed files with 51 additions and 139 deletions

View File

@ -243,21 +243,6 @@ struct ContentView: View {
@State var dragOffset = CGFloat(0)
enum SwipeStartState {
case center
case right
case left
}
enum SwipeStartDir {
case left
case right
}
// @State var curSwipeState: SwipeState = .start
@State var startSwipeState: SwipeStartState?
@State var startSwipeDir: SwipeStartDir?
@Environment(\.appDatabase) private var appDatabase
@Query(RibbonRequest()) private var ribbons: [Ribbon]
@ -366,145 +351,77 @@ struct ContentView: View {
}
.offset(x: 20, y: 0)
.offset(x: pulledOut.width)
.offset(x: mainSwipe.width)
.offset(x: mainSwipe.width, y: mainSwipe.height)
.gesture(
DragGesture()
.onChanged { value in
// Calculate the offset
.onChanged { gesture in
let margin = CGFloat(30)
let rightSwipeLimit = CGFloat(110)
let leftSwipeLimit = CGFloat(-200)
var newOffset = value.translation.width
if startSwipeState == nil {
if pulledOut.width == 0 {
startSwipeState = .center
} else if pulledOut.width < 0 {
startSwipeState = .left
} else {
startSwipeState = .right
print("mao mainSwipe :\(mainSwipe.width)")
print(pulledOut.width)
var swipeWidth = gesture.translation.width
var swipeActual = gesture.translation.width + pulledOut.width
// swiping right
if swipeWidth > 0 {
if swipeActual > rightSwipeLimit {
swipeWidth = rightSwipeLimit + pow(swipeActual - rightSwipeLimit, 0.8)
}
print("start swipe meow: \(startSwipeState)")
mainSwipe.width = swipeWidth - pulledOut.width
// swiping right
} else {
if swipeActual < leftSwipeLimit {
swipeWidth = leftSwipeLimit - pow(-swipeActual + leftSwipeLimit, 0.8)
}
mainSwipe.width = swipeWidth - pulledOut.width
}
if newOffset > 0 {
startSwipeDir = .right
} else {
startSwipeDir = .left
}
// Apply resistance if out of bounds
var maxOffsetRight: CGFloat = 110
var maxOffsetLeft: CGFloat = 200
if startSwipeState == .right {
if startSwipeDir == .right {
maxOffsetRight = .zero
maxOffsetLeft = .zero
} else if startSwipeDir == .left {
maxOffsetRight = CGFloat(110)
maxOffsetLeft = CGFloat(-30)
}
} else if startSwipeState == .left {
if startSwipeDir == .left {
maxOffsetLeft = .zero
maxOffsetRight = .zero
} else {
maxOffsetLeft = CGFloat(200)
maxOffsetRight = .zero
}
}
if newOffset + pulledOut.width < -maxOffsetLeft {
if startSwipeState == .right && startSwipeDir == .left {
print("meow here")
newOffset = -maxOffsetLeft + rubberBandEffect(newOffset + maxOffsetLeft) - pulledOut.width
} else {
print("meow here")
newOffset = -maxOffsetLeft + rubberBandEffect(newOffset + maxOffsetLeft)
}
} else if newOffset + pulledOut.width > maxOffsetRight {
newOffset = maxOffsetRight + rubberBandEffect(newOffset - maxOffsetRight)
}
// if newOffset.height < -maxOffset {
// newOffset.height = -maxOffset + rubberBandEffect(newOffset.height + maxOffset)
// } else if newOffset.height > maxOffset {
// newOffset.height = maxOffset + rubberBandEffect(newOffset.height - maxOffset)
// }
self.mainSwipe.width = newOffset
// dragOffset is what is used to make the text be readable
// with the right pane being visible
// if mainSwipe.width < -margin && pulledOut.width <= 0 {
// if mainSwipe.width < -margin && pulledOut.width <= 0 {
// dragOffset = margin + mainSwipe.width + pulledOut.width
// }
// if mainSwipe.width > 0 && pulledOut.width < 0 {
// dragOffset = margin + mainSwipe.width + pulledOut.width
// }
}
.onEnded { _ in
var finalSwipe = CGFloat(0)
let swipeLeftFinal = CGFloat(-200)
let swipeRightFinal = CGFloat(110)
// let margin = CGFloat(30)
if startSwipeState == .center {
if startSwipeDir == .right {
finalSwipe = swipeRightFinal
} else {
finalSwipe = swipeLeftFinal
}
} else if startSwipeState == .right {
finalSwipe = .zero
if startSwipeDir == .left {
finalSwipe = .zero
} else {
finalSwipe = swipeRightFinal
}
} else if startSwipeState == .left {
if startSwipeDir == .left {
finalSwipe = swipeLeftFinal
} else {
finalSwipe = .zero
}
if mainSwipe.width < -margin && pulledOut.width <= 0 {
dragOffset = margin + mainSwipe.width + pulledOut.width
}
startSwipeState = nil
startSwipeDir = nil
if mainSwipe.width > 0 && pulledOut.width < 0 {
dragOffset = margin + mainSwipe.width + pulledOut.width
}
}
.onEnded { _ in
let pulledOutRight = CGFloat(-200)
let pulledOutLeft = CGFloat(110)
let margin = CGFloat(30)
// var setDragOffset = CGFloat(0)
var setPulledOutWith = CGFloat(0)
var setDragOffset = CGFloat(0)
// if mainSwipe.width < 0 && pulledOut.width > 0 {
// setPulledOutWith = CGFloat(0)
// } else if mainSwipe.width > 0 && pulledOut.width < 0 {
// setPulledOutWith = CGFloat(0)
if mainSwipe.width < 0 && pulledOut.width > 0 {
setPulledOutWith = CGFloat(0)
} else if mainSwipe.width > 0 && pulledOut.width < 0 {
setPulledOutWith = CGFloat(0)
// } else if (mainSwipe.width < 0 && pulledOut.width < 0) ||
// (mainSwipe.width < 0 && pulledOut.width == 0) {
// setPulledOutWith = pulledOutRight
// setDragOffset = margin + setPulledOutWith
// } else if abs(mainSwipe.width + pulledOut.width) > 30 {
// setPulledOutWith = pulledOutLeft
// }
} else if (mainSwipe.width < 0 && pulledOut.width < 0) ||
(mainSwipe.width < 0 && pulledOut.width == 0) {
setPulledOutWith = pulledOutRight
setDragOffset = margin + setPulledOutWith
} else if abs(mainSwipe.width + pulledOut.width) > 30 {
setPulledOutWith = pulledOutLeft
}
withAnimation(.spring(response: 0.2)) {
pulledOut.width = finalSwipe
// dragOffset = setDragOffset
mainSwipe = .zero
pulledOut.width = setPulledOutWith
dragOffset = setDragOffset
mainSwipe = .zero
}
}
)
@ -513,11 +430,6 @@ struct ContentView: View {
.background(Color(red: 0.1, green: 0.1, blue: 0.1))
}
func handleVisibilityChanged2(_: String, change _: VisibilityChange, tracker _: VisibilityTracker<String>) {}
func rubberBandEffect(_ offset: CGFloat) -> CGFloat {
let resistance: CGFloat = 0.55
return resistance * pow(abs(offset), 0.7) * (offset < 0 ? -1 : 1)
}
}
private let itemFormatter: DateFormatter = {