-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added recipe list, create views and schema
- Loading branch information
1 parent
d07af55
commit 0261f7a
Showing
10 changed files
with
200 additions
and
6 deletions.
There are no files selected for viewing
Binary file modified
BIN
+34.7 KB
(150%)
....xcodeproj/project.xcworkspace/xcuserdata/matt.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// | ||
// RecipeCreate.swift | ||
// SwiftFood | ||
// | ||
// Created by Matthew Caballero on 12/11/24. | ||
// | ||
|
||
import SwiftUI | ||
import SwiftData | ||
|
||
struct RecipeCreate: View { | ||
@Environment(\.modelContext) private var context | ||
@Environment(\.dismiss) private var dismiss | ||
|
||
@Query private var myRecipes: [Recipe] | ||
|
||
@State private var title: String = "" | ||
@State private var time_required: Float = 0.0 | ||
@State private var servings_amount: Int = 0 | ||
@State private var instructions: String = "" | ||
|
||
// Used below for the number field, to show decimal correctly | ||
private var decimalFormatter: NumberFormatter { | ||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .decimal | ||
formatter.minimumFractionDigits = 0 | ||
formatter.maximumFractionDigits = 2 | ||
return formatter | ||
} | ||
|
||
var body: some View { | ||
VStack { | ||
Text("My Ingredients") | ||
.font(.headline) | ||
|
||
// Text input fields | ||
// Uses accessibility identifiers, easy to access during testing | ||
VStack(alignment: .leading, spacing: 10) { | ||
HStack { | ||
Text("Title: ") | ||
TextField("Recipe Title", text: $title) | ||
.accessibilityIdentifier("CreateRecipeTitle") | ||
.textFieldStyle(.roundedBorder) | ||
.frame(width: 110) | ||
} | ||
HStack { | ||
Text("Time Required:") | ||
TextField("Time Required", value: $time_required, formatter: decimalFormatter) | ||
.accessibilityIdentifier("CreateRecipeTimeRequired") | ||
.textFieldStyle(.roundedBorder) | ||
.keyboardType(.decimalPad) | ||
.frame(width: 40) | ||
Text("hours") | ||
} | ||
HStack { | ||
Text("Number of Servings: ") | ||
TextField("Number of Servings", value: $servings_amount, formatter: NumberFormatter()) | ||
.accessibilityIdentifier("CreateRecipeServingsAmount") | ||
.textFieldStyle(.roundedBorder) | ||
.keyboardType(.numberPad) | ||
.frame(width: 40) | ||
Text("servings") | ||
} | ||
Text("Instructions: ") | ||
ScrollView { | ||
TextEditor(text: $instructions) | ||
.accessibilityIdentifier("CreateRecipeInstructions") | ||
.frame(height: 150) // Fixed height | ||
.cornerRadius(8) | ||
.overlay(RoundedRectangle(cornerRadius: 8).stroke(Color.gray)) | ||
} | ||
} | ||
// Add button | ||
Button("Add Recipe") { | ||
// Trigger add recipe function | ||
addRecipe() | ||
// Close current view / go back in the nav stack | ||
dismiss() | ||
} | ||
.accessibilityIdentifier("AddRecipeButton") | ||
.buttonStyle(.borderedProminent) | ||
} | ||
} | ||
|
||
func addRecipe() { | ||
guard !title.isEmpty, time_required > 0, servings_amount > 0, !instructions.isEmpty else { return } // No empty inputs | ||
let recipe = Recipe( | ||
title: title, | ||
time_required: time_required, | ||
servings_amount: servings_amount, | ||
instructions: instructions | ||
) | ||
context.insert(recipe) | ||
|
||
// Resetting input fields | ||
title = "" | ||
time_required = 0.0 | ||
servings_amount = 0 | ||
instructions = "" | ||
} | ||
} | ||
|
||
#Preview { | ||
ContentView() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// RecipeList.swift | ||
// SwiftFood | ||
// | ||
// Created by Matthew Caballero on 12/11/24. | ||
// | ||
|
||
import SwiftUI | ||
import SwiftData | ||
|
||
struct RecipeList: View { | ||
@Environment(\.modelContext) private var context | ||
@Query private var myRecipes: [Recipe] | ||
|
||
var body: some View { | ||
VStack { | ||
Text("My Recipes") | ||
.font(.headline) | ||
|
||
List { | ||
// For each recipe in recipes context | ||
ForEach(myRecipes) { recipe in | ||
HStack { | ||
VStack(alignment: .leading) { | ||
Text(recipe.title) | ||
.font(.headline) | ||
// Formatting to show the decimal correctly | ||
Text("\(String(format: "%.1f", recipe.time_required)) hours") | ||
Text("\(recipe.servings_amount) servings") | ||
} | ||
Spacer() | ||
|
||
// Delete Button | ||
Button(action: { | ||
deleteItem(recipe) | ||
}) { | ||
Image(systemName: "trash") | ||
.foregroundColor(.red) | ||
} | ||
.buttonStyle(.borderless) | ||
} | ||
} | ||
} | ||
.cornerRadius(20) | ||
NavigationLink("Create new recipe!") { | ||
RecipeCreate() | ||
} | ||
.accessibilityIdentifier("ToRecipeCreate") | ||
.buttonStyle(.borderedProminent) | ||
.padding(.bottom) | ||
} | ||
.padding() | ||
} | ||
|
||
func deleteItem(_ recipe: Recipe) { | ||
context.delete(recipe) | ||
} | ||
} | ||
|
||
#Preview { | ||
ContentView() | ||
} |