Referencing variables in a separate HStack within the same struct

113 Views Asked by At

I am almost done with this view, but am stuck on the final Total Calories row at the bottom. I cannot make it sum the protein/carb/fat calorie numbers. I believe this is because they are defined and changed out of scope (in the other HStack), so I tried to define the GramTotals at the top of the struct.

That got rid of the errors, but it seems the Total Calories HStack will not recognize the changes being made to the values in the other HStack. Any ideas for how to fix this? I'll owe you one!

Full Code is below:

import SwiftUI

struct ContentView: View {
    
    init() { // this can be done in `onAppear` modifier if you need to restore the appereance later on `onDisappear`
        UITableView.appearance().contentInset.top = -35
        }
 
    @State var food1 = ""
    @State var food2 = ""
    @State var food3 = ""
    @State var food4 = ""
    @State var food5 = ""
    @State var food6 = ""
    @State var food7 = ""
    @State var food8 = ""
    @State var food9 = ""
    @State var food10 = ""
    
    @State var protein1 = ""
    @State var protein2 = ""
    @State var protein3 = ""
    @State var protein4 = ""
    @State var protein5 = ""
    @State var protein6 = ""
    @State var protein7 = ""
    @State var protein8 = ""
    @State var protein9 = ""
    @State var protein10 = ""
    
    @State var proteinGramTotal = 0
    @State var carbsGramTotal = 0
    @State var fatGramTotal = 0
    
    @State var carbs1 = ""
    @State var carbs2 = ""
    @State var carbs3 = ""
    @State var carbs4 = ""
    @State var carbs5 = ""
    @State var carbs6 = ""
    @State var carbs7 = ""
    @State var carbs8 = ""
    @State var carbs9 = ""
    @State var carbs10 = ""
    
    @State var fat1 = ""
    @State var fat2 = ""
    @State var fat3 = ""
    @State var fat4 = ""
    @State var fat5 = ""
    @State var fat6 = ""
    @State var fat7 = ""
    @State var fat8 = ""
    @State var fat9 = ""
    @State var fat10 = ""
    
    @State var fiber1 = ""
    @State var fiber2 = ""
    @State var fiber3 = ""
    @State var fiber4 = ""
    @State var fiber5 = ""
    @State var fiber6 = ""
    @State var fiber7 = ""
    @State var fiber8 = ""
    @State var fiber9 = ""
    @State var fiber10 = ""
    
    var body: some View {
        NavigationView {
            VStack {
                HStack(alignment: .top) {
                    VStack(alignment: .leading) {
                        Text("Food")
                        Form {
                                Section {
                                    TextField("Food 1", text: $food1)
                                    TextField("Food 2", text: $food2)
                                    TextField("Food 3", text: $food3)
                                    TextField("Food 4", text: $food4)
                                    TextField("Food 5", text: $food5)
                                    TextField("Food 6", text: $food6)
                                    TextField("Food 7", text: $food7)
                                    TextField("Food 8", text: $food8)
                                    TextField("Food 9", text: $food9)
                                    TextField("Food 10", text: $food10)
                                }
                        }
                        .padding([.leading], -16)
                    }
                    .frame(width: 100, height: 470)
                    .padding(.leading)
                    VStack {
                        Text("Protein")
                        Form {
                            Section {
                                TextField("Protein 1", text: $protein1)
                                TextField("Protein 2", text: $protein2)
                                TextField("Protein 3", text: $protein3)
                                TextField("Protein 4", text: $protein4)
                                TextField("Protein 5", text: $protein5)
                                TextField("Protein 6", text: $protein6)
                                TextField("Protein 7", text: $protein7)
                                TextField("Protein 8", text: $protein8)
                                TextField("Protein 9", text: $protein9)
                                TextField("Protein 10", text: $protein10)
                            }
                        }
                    }
                    .frame(height: 470)
                    VStack {
                        Text("Carbs")
                        Form {
                        
                            Section {
                                    TextField("Carbs 1", text: $carbs1)
                                    TextField("Carbs 2", text: $carbs2)
                                    TextField("Carbs 3", text: $carbs3)
                                    TextField("Carbs 4", text: $carbs4)
                                    TextField("Carbs 5", text: $carbs5)
                                    TextField("Carbs 6", text: $carbs6)
                                    TextField("Carbs 7", text: $carbs7)
                                    TextField("Carbs 8", text: $carbs8)
                                    TextField("Carbs 9", text: $carbs9)
                                    TextField("Carbs 10", text: $carbs10)
                            }
                            
                        }
                    }
                    .frame(height: 470)
                    VStack {
                        Text("Fat")
                        Form {
                       
                                Section {
                                    TextField("Fat 1", text: $fat1)
                                    TextField("Fat 2", text: $fat2)
                                    TextField("Fat 3", text: $fat3)
                                    TextField("Fat 4", text: $fat4)
                                    TextField("Fat 5", text: $fat5)
                                    TextField("Fat 6", text: $fat6)
                                    TextField("Fat 7", text: $fat7)
                                    TextField("Fat 8", text: $fat8)
                                    TextField("Fat 9", text: $fat9)
                                    TextField("Fat 10", text: $fat10)
                                }
                           
                        }
                    }
                    .frame(height: 470)
                    VStack {
                        Text("Fiber")
                        Form {
                           
                                Section {
                                    TextField("Fiber 1", text: $fiber1)
                                    TextField("Fiber 2", text: $fiber2)
                                    TextField("Fiber 3", text: $fiber3)
                                    TextField("Fiber 4", text: $fiber4)
                                    TextField("Fiber 5", text: $fiber5)
                                    TextField("Fiber 6", text: $fiber6)
                                    TextField("Fiber 7", text: $fiber7)
                                    TextField("Fiber 8", text: $fiber8)
                                    TextField("Fiber 9", text: $fiber9)
                                    TextField("Fiber 10", text: $fiber10)
                                }
                           
                        }
                    }
                    .frame(height: 470)
                    .navigationBarTitle("Today's Menu")
                    
                    
                }
                .padding(.top)
              
                HStack {
                    VStack(alignment: .leading) {
                        Text("Grams")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
                            .padding()
                            .foregroundColor(.white)
                        Text("Calories")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
                            .padding()
                            .foregroundColor(.white)
                    }
                    .frame(width: 114)
                    
                    VStack {
                        let proteinEntry1 = (Int(protein1) ?? 0)
                        let proteinEntry2 = (Int(protein2) ?? 0)
                        let proteinEntry3 = (Int(protein3) ?? 0)
                        let proteinEntry4 = (Int(protein4) ?? 0)
                        let proteinEntry5 = (Int(protein5) ?? 0)
                        let proteinEntry6 = (Int(protein6) ?? 0)
                        let proteinEntry7 = (Int(protein7) ?? 0)
                        let proteinEntry8 = (Int(protein8) ?? 0)
                        let proteinEntry9 = (Int(protein9) ?? 0)
                        let proteinEntry10 = (Int(protein10) ?? 0)
                        
                        var proteinChunk1 = proteinEntry1 + proteinEntry2
                        var proteinChunk2 = proteinEntry3 + proteinEntry4
                        var proteinChunk3 = proteinEntry5 + proteinEntry6
                        var proteinChunk4 = proteinEntry7 + proteinEntry8
                        var proteinChunk5 = proteinEntry9 + proteinEntry10
                        
                        var proteinGramTotal = (proteinChunk1 + proteinChunk2 + proteinChunk3 + proteinChunk4 + proteinChunk5)
                        
                        Text("\(proteinGramTotal)")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                            .foregroundColor(.white)
                        Text("\((proteinGramTotal) * 4)")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                            .foregroundColor(.white)
                    }
                    
                    VStack {
                        let carbsEntry1 = (Int(carbs1) ?? 0)
                        let carbsEntry2 = (Int(carbs2) ?? 0)
                        let carbsEntry3 = (Int(carbs3) ?? 0)
                        let carbsEntry4 = (Int(carbs4) ?? 0)
                        let carbsEntry5 = (Int(carbs5) ?? 0)
                        let carbsEntry6 = (Int(carbs6) ?? 0)
                        let carbsEntry7 = (Int(carbs7) ?? 0)
                        let carbsEntry8 = (Int(carbs8) ?? 0)
                        let carbsEntry9 = (Int(carbs9) ?? 0)
                        let carbsEntry10 = (Int(carbs10) ?? 0)
                        
                        var carbsChunk1 = carbsEntry1 + carbsEntry2
                        var carbsChunk2 = carbsEntry3 + carbsEntry4
                        var carbsChunk3 = carbsEntry5 + carbsEntry6
                        var carbsChunk4 = carbsEntry7 + carbsEntry8
                        var carbsChunk5 = carbsEntry9 + carbsEntry10
                        
                        var carbsGramTotal = (carbsChunk1 + carbsChunk2 + carbsChunk3 + carbsChunk4 + carbsChunk5)
                        
                        Text("\(carbsGramTotal)")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                            .foregroundColor(.white)
                        Text("\((carbsGramTotal) * 4)")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                            .foregroundColor(.white)
                    }
               
                    VStack {
                        let fatEntry1 = (Int(fat1) ?? 0)
                        let fatEntry2 = (Int(fat2) ?? 0)
                        let fatEntry3 = (Int(fat3) ?? 0)
                        let fatEntry4 = (Int(fat4) ?? 0)
                        let fatEntry5 = (Int(fat5) ?? 0)
                        let fatEntry6 = (Int(fat6) ?? 0)
                        let fatEntry7 = (Int(fat7) ?? 0)
                        let fatEntry8 = (Int(fat8) ?? 0)
                        let fatEntry9 = (Int(fat9) ?? 0)
                        let fatEntry10 = (Int(fat10) ?? 0)
                        
                        var fatChunk1 = fatEntry1 + fatEntry2
                        var fatChunk2 = fatEntry3 + fatEntry4
                        var fatChunk3 = fatEntry5 + fatEntry6
                        var fatChunk4 = fatEntry7 + fatEntry8
                        var fatChunk5 = fatEntry9 + fatEntry10
                        
                        var fatGramTotal = (fatChunk1 + fatChunk2 + fatChunk3 + fatChunk4 + fatChunk5)
                        
                        Text("\(fatGramTotal)")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                            .foregroundColor(.white)
                        Text("\((fatGramTotal) * 9)")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                            .foregroundColor(.white)
                    }
                  
                    VStack {
                        let fiberEntry1 = (Int(fiber1) ?? 0)
                        let fiberEntry2 = (Int(fiber4) ?? 0)
                        let fiberEntry3 = (Int(fiber7) ?? 0)
                        let fiberEntry4 = (Int(fiber10) ?? 0)
                        let fiberEntry5 = (Int(fiber3) ?? 0)
                        let fiberEntry6 = (Int(fiber3) ?? 0)
                        let fiberEntry7 = (Int(fiber3) ?? 0)
                        let fiberEntry8 = (Int(fiber3) ?? 0)
                        let fiberEntry9 = (Int(fiber3) ?? 0)
                        let fiberEntry10 = (Int(fiber3) ?? 0)
                        
                        var fiberChunk1 = fiberEntry1 + fiberEntry2
                        var fiberChunk2 = fiberEntry3 + fiberEntry4
                        var fiberChunk3 = fiberEntry5 + fiberEntry6
                        var fiberChunk4 = fiberEntry7 + fiberEntry8
                        var fiberChunk5 = fiberEntry9 + fiberEntry10
                        
                        var fiberGramTotal = (fiberChunk1 + fiberChunk2 + fiberChunk3 + fiberChunk4 + fiberChunk5)
                        
                        Text("\(fiberGramTotal)")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                            .foregroundColor(.white)
                        Text("\((fiberGramTotal) * 4)")
                            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                            .foregroundColor(.white)
                    }
                }
                .background(Color.accentColor)
                HStack {
                    Text("Total Calories")
                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
                        .padding()
                        .foregroundColor(.white)
                    
                    var proteinCalories = ((self.proteinGramTotal) * 4)
                    
                    var carbsCalories = ((self.carbsGramTotal) * 4)
                    
                    var fatCalories = ((self.fatGramTotal) * 9)
                    
                    Text("\(proteinCalories + carbsCalories + fatCalories)")
                        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
                        .foregroundColor(.white)
                }
                .background(Color.accentColor)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
0

There are 0 best solutions below