Nextcloud Login refactoring
This commit is contained in:
@@ -7,18 +7,24 @@
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
// MARK: - RecipeView Ingredients Section
|
||||
/*
|
||||
|
||||
struct RecipeIngredientSection: View {
|
||||
@Environment(CookbookState.self) var cookbookState
|
||||
@State var viewModel: RecipeView.ViewModel
|
||||
@Environment(\.modelContext) var modelContext
|
||||
@Bindable var recipe: Recipe
|
||||
@Binding var editMode: Bool
|
||||
@Binding var presentIngredientEditView: Bool
|
||||
@State var recipeGroceries: RecipeGroceries? = nil
|
||||
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
HStack {
|
||||
Button {
|
||||
withAnimation {
|
||||
/*
|
||||
if cookbookState.groceryList.containsRecipe(viewModel.recipe.id) {
|
||||
cookbookState.groceryList.deleteGroceryRecipe(viewModel.recipe.id)
|
||||
} else {
|
||||
@@ -28,6 +34,7 @@ struct RecipeIngredientSection: View {
|
||||
recipeName: viewModel.recipe.name
|
||||
)
|
||||
}
|
||||
*/
|
||||
}
|
||||
} label: {
|
||||
if #available(iOS 17.0, *) {
|
||||
@@ -35,7 +42,7 @@ struct RecipeIngredientSection: View {
|
||||
} else {
|
||||
Image(systemName: "heart.text.square")
|
||||
}
|
||||
}.disabled(viewModel.editMode)
|
||||
}.disabled(editMode)
|
||||
|
||||
SecondaryLabel(text: LocalizedStringKey("Ingredients"))
|
||||
|
||||
@@ -45,26 +52,30 @@ struct RecipeIngredientSection: View {
|
||||
.foregroundStyle(.secondary)
|
||||
.bold()
|
||||
|
||||
ServingPickerView(selectedServingSize: $viewModel.recipe.ingredientMultiplier)
|
||||
ServingPickerView(selectedServingSize: $recipe.ingredientMultiplier)
|
||||
}
|
||||
|
||||
ForEach(0..<viewModel.recipe.recipeIngredient.count, id: \.self) { ix in
|
||||
IngredientListItem(
|
||||
ingredient: $viewModel.recipe.recipeIngredient[ix],
|
||||
servings: $viewModel.recipe.ingredientMultiplier,
|
||||
recipeYield: Double(viewModel.recipe.recipeYield),
|
||||
recipeId: viewModel.recipe.id
|
||||
|
||||
|
||||
ForEach(0..<recipe.ingredients.count, id: \.self) { ix in
|
||||
/*IngredientListItem(
|
||||
ingredient: $recipe.recipeIngredient[ix],
|
||||
servings: $recipe.ingredientMultiplier,
|
||||
recipeYield: Double(recipe.recipeYield),
|
||||
recipeId: recipe.id
|
||||
) {
|
||||
/*
|
||||
cookbookState.groceryList.addItem(
|
||||
viewModel.recipe.recipeIngredient[ix],
|
||||
toRecipe: viewModel.recipe.id,
|
||||
recipeName: viewModel.recipe.name
|
||||
)
|
||||
recipe.recipeIngredient[ix],
|
||||
toRecipe: recipe.id,
|
||||
recipeName: recipe.name
|
||||
)*/
|
||||
}
|
||||
.padding(4)
|
||||
.padding(4)*/
|
||||
Text(recipe.ingredients[ix])
|
||||
}
|
||||
|
||||
if viewModel.recipe.ingredientMultiplier != Double(viewModel.recipe.recipeYield) {
|
||||
if recipe.ingredientMultiplier != Double(recipe.yield) {
|
||||
HStack() {
|
||||
Image(systemName: "exclamationmark.triangle.fill")
|
||||
.foregroundStyle(.secondary)
|
||||
@@ -73,9 +84,9 @@ struct RecipeIngredientSection: View {
|
||||
}.padding(.top)
|
||||
}
|
||||
|
||||
if viewModel.editMode {
|
||||
if editMode {
|
||||
Button {
|
||||
viewModel.presentIngredientEditView.toggle()
|
||||
presentIngredientEditView.toggle()
|
||||
} label: {
|
||||
Text("Edit")
|
||||
}
|
||||
@@ -83,19 +94,80 @@ struct RecipeIngredientSection: View {
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.animation(.easeInOut, value: viewModel.recipe.ingredientMultiplier)
|
||||
.animation(.easeInOut, value: recipe.ingredientMultiplier)
|
||||
}
|
||||
|
||||
func toggleAllGroceryItems(_ itemNames: [String], inCategory categoryId: String, named name: String) {
|
||||
do {
|
||||
// Find or create the target category
|
||||
let categoryPredicate = #Predicate<RecipeGroceries> { $0.id == categoryId }
|
||||
let fetchDescriptor = FetchDescriptor<RecipeGroceries>(predicate: categoryPredicate)
|
||||
|
||||
if let existingCategory = try modelContext.fetch(fetchDescriptor).first {
|
||||
// Delete category if it exists
|
||||
modelContext.delete(existingCategory)
|
||||
} else {
|
||||
// Create the category if it doesn't exist
|
||||
let newCategory = RecipeGroceries(id: categoryId, name: name)
|
||||
modelContext.insert(newCategory)
|
||||
|
||||
// Add new GroceryItems to the category
|
||||
for itemName in itemNames {
|
||||
let newItem = GroceryItem(name: itemName, isChecked: false)
|
||||
newCategory.items.append(newItem)
|
||||
}
|
||||
|
||||
try modelContext.save()
|
||||
}
|
||||
} catch {
|
||||
print("Error adding grocery items: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
|
||||
func toggleGroceryItem(_ itemName: String, inCategory categoryId: String, named name: String) {
|
||||
do {
|
||||
// Find or create the target category
|
||||
let categoryPredicate = #Predicate<RecipeGroceries> { $0.id == categoryId }
|
||||
let fetchDescriptor = FetchDescriptor<RecipeGroceries>(predicate: categoryPredicate)
|
||||
|
||||
if let existingCategory = try modelContext.fetch(fetchDescriptor).first {
|
||||
// Delete item if it exists
|
||||
if existingCategory.items.contains(where: { $0.name == itemName }) {
|
||||
existingCategory.items.removeAll { $0.name == itemName }
|
||||
|
||||
// Delete category if empty
|
||||
if existingCategory.items.isEmpty {
|
||||
modelContext.delete(existingCategory)
|
||||
}
|
||||
} else {
|
||||
existingCategory.items.append(GroceryItem(name: itemName, isChecked: false))
|
||||
}
|
||||
} else {
|
||||
// Add the category if it doesn't exist
|
||||
let newCategory = RecipeGroceries(id: categoryId, name: name)
|
||||
modelContext.insert(newCategory)
|
||||
|
||||
// Add the item to the new category
|
||||
newCategory.items.append(GroceryItem(name: itemName, isChecked: false))
|
||||
}
|
||||
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
print("Error adding grocery items: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - RecipeIngredientSection List Item
|
||||
|
||||
/*
|
||||
fileprivate struct IngredientListItem: View {
|
||||
@Environment(CookbookState.self) var cookbookState
|
||||
@Environment(\.modelContext) var modelContext
|
||||
@Bindable var recipeGroceries: RecipeGroceries
|
||||
@Binding var ingredient: String
|
||||
@Binding var servings: Double
|
||||
@State var recipeYield: Double
|
||||
@State var recipeId: String
|
||||
let addToGroceryListAction: () -> Void
|
||||
|
||||
|
||||
@State var modifiedIngredient: AttributedString = ""
|
||||
@State var isSelected: Bool = false
|
||||
@@ -110,7 +182,7 @@ fileprivate struct IngredientListItem: View {
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment: .top) {
|
||||
if cookbookState.groceryList.containsItem(at: recipeId, item: ingredient) {
|
||||
if recipeGroceries.items.contains(ingredient) {
|
||||
if #available(iOS 17.0, *) {
|
||||
Image(systemName: "storefront")
|
||||
.foregroundStyle(Color.green)
|
||||
@@ -168,7 +240,7 @@ fileprivate struct IngredientListItem: View {
|
||||
.onEnded { gesture in
|
||||
withAnimation {
|
||||
if dragOffset > maxDragDistance * 0.3 { // Swipe threshold
|
||||
if cookbookState.groceryList.containsItem(at: recipeId, item: ingredient) {
|
||||
if recipeGroceries.items.contains(ingredient) {
|
||||
cookbookState.groceryList.deleteItem(ingredient, fromRecipe: recipeId)
|
||||
} else {
|
||||
addToGroceryListAction()
|
||||
@@ -182,7 +254,7 @@ fileprivate struct IngredientListItem: View {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
struct ServingPickerView: View {
|
||||
@@ -217,4 +289,4 @@ struct ServingPickerView: View {
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user