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]()
}
CodePudding user response:
If you store a Float
in your Task
, rather than just a String
value, you can use that to add/subtract from your gp
. The most basic change would look like this (but, be sure to check out the second half of the answer before implementing it this way):
struct Task : Identifiable {
var id = String()
var toDoItem = String()
var amount : Float = 0 //<-- Here
}
class TaskStore : ObservableObject {
@Published var tasks = [Task]()
}
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)", amount: rate))
}
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) {
if let index = offsets.first { //<-- Here
let task = taskStore.tasks[index]
gp -= task.amount
}
taskStore.tasks.remove(atOffsets: offsets)
}
}
Furthermore, you could refactor your code so that gp
isn't @State
, but rather a computed property. Then, it just reflects the total amounts in the Task
list -- no need to do manual addition/calculations when the list changes. Similarly, the String
toDoItem
could be a computed property on your Task
.
struct Task : Identifiable {
var id = String()
var amount : Float = 0
}
extension Task {
var toDoItem: String {
"Earning: = \(amount)"
}
}
class TaskStore : ObservableObject {
@Published var tasks = [Task]()
}
struct Calculation: View {
@State var load1 = Float()
@State var load2 = Float()
@State var rate: Float = 0
@ObservedObject var taskStore = TaskStore()
var gp : Float {
taskStore.tasks.reduce(0) { acc, item in
acc item.amount
}
}
func addNewToDo() {
taskStore.tasks.append(Task(id: String(taskStore.tasks.count 1), amount: rate))
}
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))
})
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)
}
}