This commit is contained in:
VincentMeilinger
2024-01-24 20:54:50 +01:00
parent 48d3da1964
commit fbcdfbff12
3 changed files with 35 additions and 15 deletions

View File

@@ -367,14 +367,20 @@ fileprivate struct RecipeIngredientSection: View {
} }
Spacer() Spacer()
Button { Button {
withAnimation {
if groceryList.containsRecipe(recipeDetail.id) {
groceryList.deleteGroceryRecipe(recipeDetail.id)
} else {
groceryList.addItems(recipeDetail.recipeIngredient, toRecipe: recipeDetail.id, recipeName: recipeDetail.name) groceryList.addItems(recipeDetail.recipeIngredient, toRecipe: recipeDetail.id, recipeName: recipeDetail.name)
}
}
} label: { } label: {
Image(systemName: "storefront") Image(systemName: "storefront")
} }
} }
ForEach(recipeDetail.recipeIngredient, id: \.self) { ingredient in ForEach(recipeDetail.recipeIngredient, id: \.self) { ingredient in
IngredientListItem(ingredient: ingredient) { IngredientListItem(ingredient: ingredient, recipeId: recipeDetail.id) {
groceryList.addItem(ingredient, toRecipe: recipeDetail.id, recipeName: recipeDetail.name) groceryList.addItem(ingredient, toRecipe: recipeDetail.id, recipeName: recipeDetail.name)
} }
.padding(4) .padding(4)
@@ -403,6 +409,7 @@ fileprivate struct RecipeToolSection: View {
fileprivate struct IngredientListItem: View { fileprivate struct IngredientListItem: View {
@EnvironmentObject var groceryList: GroceryList @EnvironmentObject var groceryList: GroceryList
@State var ingredient: String @State var ingredient: String
@State var recipeId: String
let addToGroceryListAction: () -> Void let addToGroceryListAction: () -> Void
@State var isSelected: Bool = false @State var isSelected: Bool = false
@State private var dragOffset: CGFloat = 0 @State private var dragOffset: CGFloat = 0
@@ -410,6 +417,10 @@ fileprivate struct IngredientListItem: View {
var body: some View { var body: some View {
HStack(alignment: .top) { HStack(alignment: .top) {
if groceryList.containsItem(at: recipeId, item: ingredient) {
Image(systemName: "storefront")
.foregroundStyle(Color.green)
}
if isSelected { if isSelected {
Image(systemName: "checkmark.circle") Image(systemName: "checkmark.circle")
} else { } else {
@@ -435,26 +446,21 @@ fileprivate struct IngredientListItem: View {
self.dragOffset = max(0, min(dragAmount, maxDragDistance + pow(dragAmount - maxDragDistance, 0.7))) self.dragOffset = max(0, min(dragAmount, maxDragDistance + pow(dragAmount - maxDragDistance, 0.7)))
} }
.onEnded { gesture in .onEnded { gesture in
if gesture.translation.width > maxDragDistance * 0.8 { // Swipe right threshold if gesture.translation.width > maxDragDistance * 0.8 { // Swipe threshold
withAnimation {
if groceryList.containsItem(at: recipeId, item: ingredient) {
groceryList.deleteItem(ingredient, fromRecipe: recipeId)
} else {
addToGroceryListAction() addToGroceryListAction()
} }
}
}
// Animate back to original position // Animate back to original position
withAnimation { withAnimation {
self.dragOffset = 0 self.dragOffset = 0
} }
} }
) )
.background {
if dragOffset > 0 {
HStack {
Image(systemName: "storefront")
.foregroundStyle(Color.green)
.opacity((dragOffset - 10)/(maxDragDistance-10))
Spacer()
}
}
}
} }
} }

View File

@@ -180,12 +180,14 @@ class GroceryList: ObservableObject {
groceryDict.removeValue(forKey: recipeId) groceryDict.removeValue(forKey: recipeId)
} }
save() save()
objectWillChange.send()
} }
func deleteGroceryRecipe(_ recipeId: String) { func deleteGroceryRecipe(_ recipeId: String) {
print("Deleting grocery recipe with id \(recipeId)") print("Deleting grocery recipe with id \(recipeId)")
groceryDict.removeValue(forKey: recipeId) groceryDict.removeValue(forKey: recipeId)
save() save()
objectWillChange.send()
} }
func deleteAll() { func deleteAll() {
@@ -200,6 +202,18 @@ class GroceryList: ObservableObject {
save() save()
} }
func containsItem(at recipeId: String, item: String) -> Bool {
guard let recipe = groceryDict[recipeId] else { return false }
if recipe.items.contains(where: { $0.name == item }) {
return true
}
return false
}
func containsRecipe(_ recipeId: String) -> Bool {
return groceryDict[recipeId] != nil
}
func save() { func save() {
Task { Task {
await dataStore.save(data: groceryDict, toPath: "grocery_list.data") await dataStore.save(data: groceryDict, toPath: "grocery_list.data")