In this code, I input two values, load1 and load2 and the rate is calculated by adding load1 and load2. The gross pay gp is calculated by adding the rates on the list. But, when I delete any load, on the list, it should subtract the same load from the gross pay. Instead, every time it subtracts the same amount of load that I deleted at the first. So when you delete it in using the edit button, it only deletes the load from the list. However, the subtraction of amount from the gross pay is not right.
Can someone help me with this please?
struct Calculation: View {
@State var load1 = Float()
@State var load2 = Float()
@State var gp : Float = 0
@State var rate: Float = 0
@ObservedObject var taskStore = TaskStore()
func addNewToDo() {
taskStore.tasks.append(Task(id: String(taskStore.tasks.count + 1), toDoItem: " Earning: = \(rate.description)" ))
}
var body: some View {
NavigationView {
VStack {
List {
Section(header:Text("load 2"))
{
TextField("Enter value of load 1", value: $load1, format: .number)
TextField("Enter value of load 1", value: $load2, format: .number)
}
HStack {
Button(String(format: "Add Load"), action:
{
print("pay for the shift is ")
print(Rocky(mypay: rate))
gp += rate
})
Button(action: {addNewToDo(); Rocky(mypay: rate) }, label: {
Text(" ")
})
}
ForEach(self.taskStore.tasks) {
task in
Text(task.toDoItem)
}.onMove(perform : self.move)
.onDelete(perform : self.delete) //For each
}.navigationBarTitle("SHIFTS")
.navigationBarItems(trailing: EditButton()) //List
Text("Gross Pay = $\(gp) ")
}.onAppear()
} }
func Rocky(mypay: Float)
{ rate = load1 + load2
print("Sus \(gp)")
}
func move(from source : IndexSet, to destination : Int)
{
taskStore.tasks.move(fromOffsets: source, toOffset: destination)
}
func delete(at offsets : IndexSet) {
taskStore.tasks.remove(atOffsets: offsets)
gp -= rate
}
}
struct BlueTwoView_Previews: PreviewProvider {
static var previews: some View {
Calculation()
}
}
This is on another file
struct Task : Identifiable {
var id = String()
var toDoItem = String()
}
class TaskStore : ObservableObject {
@Published var tasks = [Task]()
}