Bugfixes
This commit is contained in:
Binary file not shown.
@@ -367,14 +367,20 @@ fileprivate struct RecipeIngredientSection: View {
|
||||
}
|
||||
Spacer()
|
||||
Button {
|
||||
withAnimation {
|
||||
if groceryList.containsRecipe(recipeDetail.id) {
|
||||
groceryList.deleteGroceryRecipe(recipeDetail.id)
|
||||
} else {
|
||||
groceryList.addItems(recipeDetail.recipeIngredient, toRecipe: recipeDetail.id, recipeName: recipeDetail.name)
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Image(systemName: "storefront")
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
.padding(4)
|
||||
@@ -403,6 +409,7 @@ fileprivate struct RecipeToolSection: View {
|
||||
fileprivate struct IngredientListItem: View {
|
||||
@EnvironmentObject var groceryList: GroceryList
|
||||
@State var ingredient: String
|
||||
@State var recipeId: String
|
||||
let addToGroceryListAction: () -> Void
|
||||
@State var isSelected: Bool = false
|
||||
@State private var dragOffset: CGFloat = 0
|
||||
@@ -410,6 +417,10 @@ fileprivate struct IngredientListItem: View {
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment: .top) {
|
||||
if groceryList.containsItem(at: recipeId, item: ingredient) {
|
||||
Image(systemName: "storefront")
|
||||
.foregroundStyle(Color.green)
|
||||
}
|
||||
if isSelected {
|
||||
Image(systemName: "checkmark.circle")
|
||||
} else {
|
||||
@@ -435,26 +446,21 @@ fileprivate struct IngredientListItem: View {
|
||||
self.dragOffset = max(0, min(dragAmount, maxDragDistance + pow(dragAmount - maxDragDistance, 0.7)))
|
||||
}
|
||||
.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()
|
||||
}
|
||||
}
|
||||
}
|
||||
// Animate back to original position
|
||||
withAnimation {
|
||||
self.dragOffset = 0
|
||||
}
|
||||
}
|
||||
)
|
||||
.background {
|
||||
if dragOffset > 0 {
|
||||
HStack {
|
||||
Image(systemName: "storefront")
|
||||
.foregroundStyle(Color.green)
|
||||
.opacity((dragOffset - 10)/(maxDragDistance-10))
|
||||
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -180,12 +180,14 @@ class GroceryList: ObservableObject {
|
||||
groceryDict.removeValue(forKey: recipeId)
|
||||
}
|
||||
save()
|
||||
objectWillChange.send()
|
||||
}
|
||||
|
||||
func deleteGroceryRecipe(_ recipeId: String) {
|
||||
print("Deleting grocery recipe with id \(recipeId)")
|
||||
groceryDict.removeValue(forKey: recipeId)
|
||||
save()
|
||||
objectWillChange.send()
|
||||
}
|
||||
|
||||
func deleteAll() {
|
||||
@@ -200,6 +202,18 @@ class GroceryList: ObservableObject {
|
||||
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() {
|
||||
Task {
|
||||
await dataStore.save(data: groceryDict, toPath: "grocery_list.data")
|
||||
|
||||
Reference in New Issue
Block a user