Skip to content

Instantly share code, notes, and snippets.

@nooglersoon
Last active June 16, 2020 13:22
Show Gist options
  • Save nooglersoon/7801f587eb66d9cf107b2cc8e0166ea4 to your computer and use it in GitHub Desktop.
Save nooglersoon/7801f587eb66d9cf107b2cc8e0166ea4 to your computer and use it in GitHub Desktop.
Medium Session: 100 Days of SwitUI - We Split
//
// ContentView.swift
// WeSplit
//
// Created by Fauzi Achmad Bangsa Diria on 14/06/20.
// Copyright © 2020 nooglersoon. All rights reserved.
//
import SwiftUI
struct ContentView: View {
@State private var checkAmount = ""
@State private var numberOfPeople = ""
@State private var tipPercentage = 4
var totalPerPerson: Double {
let peopleCount = Double(numberOfPeople) ?? 0
let peopleCounts = peopleCount
let tipSelection = Double(tipPercentages[tipPercentage])
let orderAmount = Double(checkAmount) ?? 0
let tipValue = orderAmount / 100 * tipSelection
let grandTotal = orderAmount + tipValue
let amountPerPerson = grandTotal / peopleCounts
if peopleCounts == 0 {
return Double(0)
}else{
return amountPerPerson
}
}
var totalAmount: Double {
let peopleCount = Double(numberOfPeople) ?? 0
let peopleCounts = peopleCount
return totalPerPerson * peopleCounts
}
let tipPercentages = [10, 15, 20, 25, 0]
var body: some View {
NavigationView {
Form {
Section {
TextField("Amount", text: $checkAmount)
.keyboardType(.decimalPad)
TextField("Number of People (2 by default) ", 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("The total amount is")) {
Text("$\(totalAmount, specifier: "%.2f")")
}
Section(header: Text("Amount Per Person")) {
Text("$\(totalPerPerson, specifier: "%.2f")")
}
} .navigationBarTitle("WeSplit")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
//.environment(\.colorScheme, .dark)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment