Start 100DaysOfSwiftUI from 2020.Mar.18th.
Day 16, 17, 18, 19: Project 1, part one, two and three (including challenges)
done with 75min.
New findings: many findings
“preview content” is resource for previewing. I just ignored until now…
“.keyboardType” is new findings. I have never used, but looks convenient for user.
SegmentedPickerStyle has great UI style.
specifier: is also very interesting.
maybe I miss more convenient modifier of SwiftUI components.
Here is the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
// // ContentView.swift // WeSplit // // Created by Tomoaki Yagishita on 2020/03/28. // Copyright © 2020 SmallDeskSoftware. All rights reserved. // import SwiftUI struct ContentView: View { @State private var checkAmount = "" @State private var numberOfPeople = "2" @State private var tipPercentage = 2 let tipPercentages = [10, 15, 20, 25, 0] var totalAmountForCheck: Double { let tipSelection = Double(tipPercentages[tipPercentage]) let orderAmount = Double(checkAmount) ?? 0 let tipValue = orderAmount / 100 * tipSelection let grandTotal = orderAmount + tipValue return grandTotal } var totalPerPerson: Double { let peopleCount = Double(numberOfPeople) ?? 0 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 body: some View { NavigationView { Form { Section { TextField("Amount", text: $checkAmount) .keyboardType(.decimalPad) TextField("Number of People", text: $numberOfPeople) .keyboardType(.decimalPad) } Section(header: Text("How much tip do you want to leave?")) { Picker("Tip percentage", selection: $tipPercentage) { ForEach(0 ..< tipPercentages.count) { Text("\(self.tipPercentages[$0])%") } } .pickerStyle(SegmentedPickerStyle()) } Section(header: Text("Total amount for the check")) { Text("$\(totalAmountForCheck, specifier: "%.2f")") } Section(header: Text("Amount per person")) { Text("$\(totalPerPerson, specifier: "%.2f")") } }.navigationBarTitle("WeSplit") } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } |
Sponsor Link