Iām new to Swift, so please go simple on me š
Iām attempting to create a view representing a Meal with, on the high, the sum of the carbs, fat and proteins of every Ingredient. The substances are introduced as a listing beneath. In every line, the contribution in vitamins for that ingredient is reported and its grams could be modified. I would love for the sums on the high to vary instantaneously when the grams are.
In the mean time, essentially the most environment friendly factor I used to be in a position to do is to immediately change solely the vitamins in the identical line. To replace the data on high, Iāve to get out of the view and re-enter. Is there a option to keep away from this, and get an instantaneous replace?
Iām utilizing Swift 5.
That is my code (redacted for brevity):
struct MealPrepView: View {
let MyColor = Shade(UIColor(named: "MyColor") ?? UIColor.black)
@Surroundings(.presentationMode) var presentationMode: Binding<PresentationMode>
@Surroundings(.managedObjectContext) personal var viewContext
@EnvironmentObject var mealHolder: MealHolder
@EnvironmentObject var ingredientHolder: IngredientHolder
@State var selectedMeal: MealItem?
@State var identify: String
@State var notes: String
@State var substances: [IngredientItem]
var carbs: Float {
substances.map({$0.carbs}).cut back(0,+)
}
var proteins: Float{
substances.map({$0.proteins}).cut back(0,+)
}
var fat: Float{
substances.map({$0.fat}).cut back(0,+)
}
// complete grams / grams in 1 block
var carbsblocks: Float {
carbs / 9
}
var proteinsblocks: Float {
proteins / 7
}
var fatsblocks: Float {
fat / 3
}
@State personal var showAlert: Bool = false
@State personal var showNameInput:Bool = false
init(passedMeal: MealItem? = nil){
if let meal = passedMeal{
// there's a meal handed by -> edit mode
_selectedMeal = State(initialValue: meal)
_name = State(initialValue: meal.identify)
_notes = State(initialValue: meal.notes ?? "")
_ingredients = State(initialValue: meal.substances ?? [])
}
else{
_name = State(initialValue: "")
_notes = State(initialValue: "")
_ingredients = State(initialValue: [])
}
}
var physique: some View {
VStack{
HStack{
Spacer()
VStack{
Textual content(String(format: "C: %.1f", carbsblocks))
Textual content(String(format: " %.1f g", carbs)).foregroundColor(.grey)
}
Spacer()
VStack{
Textual content(String(format: "P: %.1f", proteinsblocks))
Textual content(String(format: " %.1f g", proteins)).foregroundColor(.grey)
}
Spacer()
VStack{
Textual content(String(format: "F: %.1f", fatsblocks))
Textual content(String(format: " %.1f g", fat)).foregroundColor(.grey)
}
Spacer()
}
.navigationBarTitle("Create meal")
.padding(.high, 20)
Spacer()
Record{
ForEach($substances){
$ingredient in IngredientCellEditQ(passedIngredient: ingredient)
.environmentObject(ingredientHolder).foregroundColor(MyColor)
}.onDelete(carry out: deleteItems)
}
}
}
and the view known as within the Record:
struct IngredientCellEditQ: View{
@ObservedObject var passedIngredient: IngredientItem
@EnvironmentObject var ingredientHolder: IngredientHolder
@EnvironmentObject var temporaryMealIngredients: TemporaryMealIngredients
@State personal var newQuantity: Float
personal let copiedIngredient: IngredientItem
init(passedIngredient: IngredientItem){
self.passedIngredient = passedIngredient
self._newQuantity = State<Float>(initialValue: passedIngredient.amount)
self.copiedIngredient = IngredientItem(identify: passedIngredient.identify, carbs: passedIngredient.carbs, proteins: passedIngredient.proteins, fat: passedIngredient.fat, amount: passedIngredient.amount, unit: passedIngredient.unit, notes: passedIngredient.notes, nosave: true)
}
var physique: some View
{
HStack
{
VStack(alignment: .main){
Textual content(String(passedIngredient.identify))
Spacer().body(peak: 7)
HStack{
Textual content(String(format: "C: %.1f", passedIngredient.carbs) + String(" g"))
.foregroundColor(.grey).font(.system(dimension: 10))
Textual content(String(format: " P: %.1f", passedIngredient.proteins) + String(" g"))
.foregroundColor(.grey).font(.system(dimension: 10))
Textual content(String(format: " F: %.1f", passedIngredient.fat) + String(" g"))
.foregroundColor(.grey).font(.system(dimension: 10))
}
}
Spacer()
TextField("100", worth: $newQuantity.onChange(updateInfo), format: .quantity)
.keyboardType(.numberPad)
.body(width: 40)
.textFieldStyle(PlainTextFieldStyle())
.multilineTextAlignment(.trailing)
Textual content(String(passedIngredient.unit))
}
.onTapGesture {
hideKeyboard()
}
}
personal func updateInfo(_ newQuantity: Float) -> Void {
passedIngredient.carbs = copiedIngredient.carbs / copiedIngredient.amount * newQuantity
passedIngredient.proteins = copiedIngredient.proteins / copiedIngredient.amount * newQuantity
passedIngredient.fat = copiedIngredient.fat / copiedIngredient.amount * newQuantity
passedIngredient.amount = newQuantity
}
}