End of code
Spacer() // Leaves a gap between the Enter & Detail buttons, and the destructive Delete button
Button {
delete(costa: &costa, tipa: &tipa, number: &number)
} label: {
Text("Delete")
.font(.title2)
.padding(10)
.background(Color.red)
.foregroundStyle(Color.white)
.cornerRadius(12)
}
.padding(.trailing, 10) // Keeps it off the edge of the screen
}
Text(text1) // Just to show it is updated DO NOT KEEP THIS
Divider() // Nice little divider
.padding(.vertical, 20)
}
VStack {
Grid(alignment: .center, horizontalSpacing: 6, verticalSpacing: 6) {
GridRow {
Text("Avg. Tip")
.padding(.horizontal, 20)
Text("Tip %")
.padding(.horizontal, 20)
Text("#")
.padding(.horizontal, 20)
}
.gridColumnAlignment(.center)
.font(.title3)
GridRow {
Text(tipaa)
.padding(.horizontal, 20)
Text(tipcc)
.padding(.horizontal, 20)
Text(numbc)
.padding(.horizontal, 20)
}
.gridColumnAlignment(.center)
.font(.system(size: 22, weight: .bold))
.foregroundStyle(Color.blue)
}
Spacer()
}
VStack {
Grid(alignment: .center, horizontalSpacing: 6, verticalSpacing: 6) {
GridRow {
Text("Total Cost")
.padding(.horizontal, 10)
Text("Total Tips")
.padding(.horizontal, 10)
Text("Total Cash")
.padding(.horizontal, 10)
}
.gridColumnAlignment(.center)
.font(.title3)
GridRow {
Text(totalCost)
.padding(.horizontal, 10)
Text(totalTips)
.padding(.horizontal, 10)
Text(totalCash)
.padding(.horizontal, 10)
}
.gridColumnAlignment(.center)
.font(.system(size: 22, weight: .bold))
.foregroundStyle(Color.blue)
}
Spacer()
}
}
}
}
func enterPayment() {
var ttips = 0.0
var tcost = 0.0
var tipo = 0.00
costt = Double(cost)
paidt = Double(paid)
errorMsg = ""
if(costt != nil && paidt != nil) {
tipo = Double(paid)! - Double(cost)!
tipa.append(tipo)
costa.append(Double(cost)!)
number += 1
numbc = String(number)
ttips = (tipa.reduce(0, +))
tcost = (costa.reduce(0, +))
tipaa = (ttips / Double(number)).formatted(.currency(code: "USD"))
totalTips = ttips.formatted(.currency(code: "USD"))
totalCost = tcost.formatted(.currency(code: "USD"))
tipp = tipo.formatted(.currency(code: "USD"))
tipc = String(tipo / Double(cost)! * 100)
tipc = String(format: "%3.0f%%", Double(tipc)!)
tipcc = String(ttips / tcost * 100)
tipcc = String(format: "%3.0f%%", Double(tipcc)!)
totalCash = (tcost + ttips).formatted(.currency(code: "USD"))
} else {
errorMsg = "Enter numbers and 1 decimal point only."
}
}
}
struct ViewDetail: View {
@Binding var text1InDetail: String // Binding here = "" Could be an Array of 44 String
@State var tip1: String = ""
@State var text23: String = ""
@State var tip23: String = ""
var body: some View {
HStack {
Text(text1InDetail)
Text(tip1)
Text(text23)
Text(tip23)
}
.onAppear {
text1InDetail = "125"// If array: text1InDetail[0] = 125
}
}
func detailLine(costa: inout [Double], tipa: inout [Double]) { // NEVER Called
print(costa, tipa)
text1InDetail = "125"
print("detail")
}
}
func delete(costa: inout [Double], tipa: inout [Double], number: inout Int) {
print(costa, tipa)
if !tipa.isEmpty && !costa.isEmpty {
tipa.removeLast()
costa.removeLast()
number -= 1
}
print(costa,tipa)
}