Claude31,
Thanks for the quick reply! I appreciate it. Here's the ContentView code in question:
//---------------------------------------------------------
//
// ContentView.swift
// SplitTheTab
//
// Created by Dale Puckett on 12/21/19.
// Copyright © 2019 Dale Puckett. All rights reserved.
//
import SwiftUI
extension UIApplication {
func endEditing() {
sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
/* extension UIApplication {
func resumeEditing() {
sendAction(#selector(UIResponder.becomeFirstResponder), to: nil, from: nil, for: onTapGesture)
}
} */
struct ContentView: View {
@State private var checkAmount = ""
@State private var tipPercentage = 2
let tipPercentages = [10, 15, 20, 25, 0]
@State private var numberOfPeople = 0
let numPayees = [1, 2, 3, 4, 5, 6, 7, 8, 9]
var totalPerPerson: Double {
// calculate the total per person here
let peopleCount = Double(numberOfPeople + 1)
let tipSelection = Double(tipPercentages[tipPercentage])
let orderAmount = Double(checkAmount) ?? 0
let tipValue = orderAmount / 100 * tipSelection
let grandTotal = orderAmount + tipValue
let amountPerPerson = grandTotal / peopleCount
return amountPerPerson
}
var amountOfTip: Double {
let mealAmount = Double(checkAmount) ?? 0
let tipPicked = Double(tipPercentages[tipPercentage])
let tipTotal = Double(mealAmount / 100 * tipPicked)
return tipTotal
}
var mealCostWithTip: Double {
// calculate the total cost of the meal here
let tabAmount = Double(checkAmount) ?? 0
let tipSelected = Double(tipPercentages[tipPercentage])
let tipTotal = Double(tabAmount / 100 * tipSelected)
let totalTab = Double(tabAmount + tipTotal)
return totalTab
}
var body: some View {
UITableView.appearance().backgroundColor = .clear
return
NavigationView{
Form {
Section (header: Text("What is amount of check without the tip?").foregroundColor(.white)){
TextField("Amount", text: self.$checkAmount){
self.endEditing()
}// End TextField
.font(.callout)
.background(Color (.clear)).keyboardType(.decimalPad)
} // End Section
.font(.callout)
// Let the user pick the percentage of check for tip
Section (header: Text("How much tip do you want to leave?").foregroundColor(.white)){
// self.resumeEditing()
Picker("Tip percentage", selection: self.$tipPercentage){
ForEach(0 ..< self.tipPercentages.count) {
Text("\(self.tipPercentages[$0])%")
}
} // End Picker
} // End Section
.pickerStyle(SegmentedPickerStyle())
.font(.callout)
// Let user pick number of payors here
Section (header: Text("How many people are paying?").foregroundColor(.white)){
Picker("Number Paying", selection: self.$numberOfPeople) {
ForEach(0 ..< self.numPayees.count) {
Text("\(self.numPayees[$0])")
}
} // End Picker
} // End Section
.pickerStyle(SegmentedPickerStyle())
.font(.callout)
// Reveal tip amount
Section (header: Text("Amount of Tip").foregroundColor(.white)){
Text("$\(self.amountOfTip, specifier: "%.2f")")
} // End Section
.font(.callout)
// Reveal cost of meal with tip selected
Section (header: Text("Cost of Meal with Tip").foregroundColor(.white)){
Text("$\(self.mealCostWithTip, specifier: "%.2f")")
} // End Section
.font(.callout)
// Show the amount each person owes
Section (header: Text("Each Person Owes!").foregroundColor(.white)){
Text("$\(self.totalPerPerson, specifier: "%.2f")").bold()
.font(.title)
} // End Section
.font(.callout)
} // End Form
.navigationBarTitle("Split The Tab").background(Color (.blue)).edgesIgnoringSafeArea(.bottom)
} // End Navigation View
.onTapGesture {
self.endEditing()
}
} // End body View
private func endEditing() {
UIApplication.shared.endEditing()
}
/* private func resumeEditing() {
UIApplication.shared.resumeEditing()
} */
} // End ContentView
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}