Minor fixes

This commit is contained in:
VincentMeilinger
2024-01-25 16:31:01 +01:00
parent fbcdfbff12
commit f6f9ca553a
2 changed files with 23 additions and 15 deletions

View File

@@ -412,16 +412,18 @@ fileprivate struct IngredientListItem: View {
@State var recipeId: String @State var recipeId: String
let addToGroceryListAction: () -> Void let addToGroceryListAction: () -> Void
@State var isSelected: Bool = false @State var isSelected: Bool = false
// Drag animation
@State private var dragOffset: CGFloat = 0 @State private var dragOffset: CGFloat = 0
let maxDragDistance = 30.0 @State private var animationStartOffset: CGFloat = 0
let maxDragDistance = 50.0
var body: some View { var body: some View {
HStack(alignment: .top) { HStack(alignment: .top) {
if groceryList.containsItem(at: recipeId, item: ingredient) { if groceryList.containsItem(at: recipeId, item: ingredient) {
Image(systemName: "storefront") Image(systemName: "storefront")
.foregroundStyle(Color.green) .foregroundStyle(Color.green)
} } else if isSelected {
if isSelected {
Image(systemName: "checkmark.circle") Image(systemName: "checkmark.circle")
} else { } else {
Image(systemName: "circle") Image(systemName: "circle")
@@ -436,28 +438,34 @@ fileprivate struct IngredientListItem: View {
.onTapGesture { .onTapGesture {
isSelected.toggle() isSelected.toggle()
} }
.animation(.easeInOut, value: isSelected)
.offset(x: dragOffset, y: 0) .offset(x: dragOffset, y: 0)
.animation(.easeInOut, value: isSelected)
.gesture( .gesture(
DragGesture() DragGesture()
.onChanged { gesture in .onChanged { gesture in
// Update drag offset as the user drags // Update drag offset as the user drags
if animationStartOffset == 0 {
animationStartOffset = gesture.translation.width
}
let dragAmount = gesture.translation.width let dragAmount = gesture.translation.width
self.dragOffset = max(0, min(dragAmount, maxDragDistance + pow(dragAmount - maxDragDistance, 0.7))) let offset = min(dragAmount, maxDragDistance + pow(dragAmount - maxDragDistance, 0.7)) - animationStartOffset
self.dragOffset = max(0, offset)
} }
.onEnded { gesture in .onEnded { gesture in
if gesture.translation.width > maxDragDistance * 0.8 { // Swipe threshold
withAnimation {
if groceryList.containsItem(at: recipeId, item: ingredient) {
groceryList.deleteItem(ingredient, fromRecipe: recipeId)
} else {
addToGroceryListAction()
}
}
}
// Animate back to original position
withAnimation { withAnimation {
if dragOffset > maxDragDistance * 0.3 { // Swipe threshold
if groceryList.containsItem(at: recipeId, item: ingredient) {
groceryList.deleteItem(ingredient, fromRecipe: recipeId)
} else {
addToGroceryListAction()
}
}
// Animate back to original position
self.dragOffset = 0 self.dragOffset = 0
self.animationStartOffset = 0
} }
} }
) )