From 2b1db684b2ad1f2bd5d0923b53bb669a3d9aec5e Mon Sep 17 00:00:00 2001 From: Matt Digel Date: Sun, 20 Dec 2020 15:55:52 -0800 Subject: [PATCH] Created bill split with tip calculator segueing to results modal --- Tipsy.xcodeproj/project.pbxproj | 20 +++- Tipsy/Base.lproj/Main.storyboard | 98 ++++++++++++------- .../CalculatorViewController.swift | 89 +++++++++++++++++ Tipsy/Controllers/ResultsViewController.swift | 42 ++++++++ Tipsy/ViewController.swift | 20 ---- 5 files changed, 212 insertions(+), 57 deletions(-) create mode 100644 Tipsy/Controllers/CalculatorViewController.swift create mode 100644 Tipsy/Controllers/ResultsViewController.swift delete mode 100644 Tipsy/ViewController.swift diff --git a/Tipsy.xcodeproj/project.pbxproj b/Tipsy.xcodeproj/project.pbxproj index 794ee36..9420692 100644 --- a/Tipsy.xcodeproj/project.pbxproj +++ b/Tipsy.xcodeproj/project.pbxproj @@ -7,19 +7,21 @@ objects = { /* Begin PBXBuildFile section */ + 718D8A2B258FD40E00F3C5EE /* ResultsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 718D8A2A258FD40E00F3C5EE /* ResultsViewController.swift */; }; ADB689532326708D006D4E2B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADB689522326708D006D4E2B /* AppDelegate.swift */; }; ADB689552326708D006D4E2B /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADB689542326708D006D4E2B /* SceneDelegate.swift */; }; - ADB689572326708D006D4E2B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADB689562326708D006D4E2B /* ViewController.swift */; }; + ADB689572326708D006D4E2B /* CalculatorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADB689562326708D006D4E2B /* CalculatorViewController.swift */; }; ADB6895A2326708D006D4E2B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = ADB689582326708D006D4E2B /* Main.storyboard */; }; ADB6895C2326708E006D4E2B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = ADB6895B2326708E006D4E2B /* Assets.xcassets */; }; ADB6895F2326708E006D4E2B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = ADB6895D2326708E006D4E2B /* LaunchScreen.storyboard */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 718D8A2A258FD40E00F3C5EE /* ResultsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultsViewController.swift; sourceTree = ""; }; ADB6894F2326708D006D4E2B /* Tipsy.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Tipsy.app; sourceTree = BUILT_PRODUCTS_DIR; }; ADB689522326708D006D4E2B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; ADB689542326708D006D4E2B /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; - ADB689562326708D006D4E2B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + ADB689562326708D006D4E2B /* CalculatorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculatorViewController.swift; sourceTree = ""; }; ADB689592326708D006D4E2B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; ADB6895B2326708E006D4E2B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; ADB6895E2326708E006D4E2B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; @@ -37,6 +39,15 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 718D8A2D258FD42400F3C5EE /* Controllers */ = { + isa = PBXGroup; + children = ( + ADB689562326708D006D4E2B /* CalculatorViewController.swift */, + 718D8A2A258FD40E00F3C5EE /* ResultsViewController.swift */, + ); + path = Controllers; + sourceTree = ""; + }; ADB689462326708D006D4E2B = { isa = PBXGroup; children = ( @@ -58,7 +69,7 @@ children = ( ADB689522326708D006D4E2B /* AppDelegate.swift */, ADB689542326708D006D4E2B /* SceneDelegate.swift */, - ADB689562326708D006D4E2B /* ViewController.swift */, + 718D8A2D258FD42400F3C5EE /* Controllers */, ADB689582326708D006D4E2B /* Main.storyboard */, ADB6895B2326708E006D4E2B /* Assets.xcassets */, ADB6895D2326708E006D4E2B /* LaunchScreen.storyboard */, @@ -138,9 +149,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - ADB689572326708D006D4E2B /* ViewController.swift in Sources */, + ADB689572326708D006D4E2B /* CalculatorViewController.swift in Sources */, ADB689532326708D006D4E2B /* AppDelegate.swift in Sources */, ADB689552326708D006D4E2B /* SceneDelegate.swift in Sources */, + 718D8A2B258FD40E00F3C5EE /* ResultsViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Tipsy/Base.lproj/Main.storyboard b/Tipsy/Base.lproj/Main.storyboard index 938fe27..ea67bb2 100644 --- a/Tipsy/Base.lproj/Main.storyboard +++ b/Tipsy/Base.lproj/Main.storyboard @@ -1,27 +1,29 @@ - + + - + + - + - + - + - + - + - + - - - @@ -72,7 +83,7 @@ - + @@ -122,10 +136,10 @@ - + - + @@ -151,8 +165,8 @@ - + @@ -179,26 +197,33 @@ - + + + + + + + + - + - + - + - + - + @@ -263,8 +292,11 @@ - + + + + diff --git a/Tipsy/Controllers/CalculatorViewController.swift b/Tipsy/Controllers/CalculatorViewController.swift new file mode 100644 index 0000000..abe0fb3 --- /dev/null +++ b/Tipsy/Controllers/CalculatorViewController.swift @@ -0,0 +1,89 @@ +// +// ViewController.swift +// Tipsy +// +// Created by Angela Yu on 09/09/2019. +// Copyright © 2019 The App Brewery. All rights reserved. +// + +import UIKit + +class CalculatorViewController: UIViewController { + @IBOutlet weak var billTextField: UITextField! + @IBOutlet weak var zeroPctButton: UIButton! + @IBOutlet weak var tenPctButton: UIButton! + @IBOutlet weak var twentyPctButton: UIButton! + @IBOutlet weak var splitNumberLabel: UILabel! + + var tipSelectedDecimal: Double = 0.10 + var numberOfPeople: Int = 2 + var result2Decimals: String = "" + + + + @IBAction func tipChanged(_ sender: UIButton) { + //Dismiss keyboard + billTextField.endEditing(true) + + let tipAmountString = sender.currentTitle! + tenPctButton.isSelected = false + twentyPctButton.isSelected = false + zeroPctButton.isSelected = false + + sender.isSelected = true + + + tipSelectedDecimal = Double(tipAmountString.replacingOccurrences(of: "%", with: ""))! / 100 + + // Another way to do it: + // String(tipAmountString.dropLast()) + + } + + @IBAction func stepperValueChanged(_ sender: UIStepper) { + + splitNumberLabel.text = String(Int(sender.value)) + // Another wayt to do it: + // splitNumberLabel.text = String(format: "%.0f", sender.value) + + numberOfPeople = Int(sender.value) + + } + + @IBAction func calculatePressed(_ sender: Any) { + print("Calculate the following:") + print(tipSelectedDecimal) + print(numberOfPeople) + print(billTextField.text!) + + let bill = billTextField.text! + + if bill != "" { + let billTotal = Double(bill)! + + let result = billTotal * (1 + tipSelectedDecimal) / Double(numberOfPeople) + + result2Decimals = String(format: "%.2f", result) + + print(result2Decimals) + } + + self.performSegue(withIdentifier: "goToResults", sender: self) + + } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + // Get the new view controller using segue.destination. + // Pass the selected object to the new view controller. + + let resultsVC = segue.destination as! ResultsViewController + + if segue.identifier == "goToResults" { + resultsVC.splitTotal = result2Decimals + resultsVC.settingsText = "Split between \(numberOfPeople) with \(tipSelectedDecimal*100)% tip" + + } + } + +} + diff --git a/Tipsy/Controllers/ResultsViewController.swift b/Tipsy/Controllers/ResultsViewController.swift new file mode 100644 index 0000000..5b1a5d3 --- /dev/null +++ b/Tipsy/Controllers/ResultsViewController.swift @@ -0,0 +1,42 @@ +// +// ResultsViewController.swift +// Tipsy +// +// Created by Matthew Digel on 12/20/20. +// Copyright © 2020 The App Brewery. All rights reserved. +// + +import UIKit + +class ResultsViewController: UIViewController { + @IBOutlet weak var totalLabel: UILabel! + @IBOutlet weak var settingsLabel: UILabel! + + var splitTotal: String = "" + var settingsText: String = "" + + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + + totalLabel.text = splitTotal + settingsLabel.text = settingsText + } + + @IBAction func recalculatePressed(_ sender: UIButton) { + self.dismiss(animated: true, completion: nil) + } + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + // Get the new view controller using segue.destination. + // Pass the selected object to the new view controller. + } + */ + +} diff --git a/Tipsy/ViewController.swift b/Tipsy/ViewController.swift deleted file mode 100644 index c716da4..0000000 --- a/Tipsy/ViewController.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// ViewController.swift -// Tipsy -// -// Created by Angela Yu on 09/09/2019. -// Copyright © 2019 The App Brewery. All rights reserved. -// - -import UIKit - -class ViewController: UIViewController { - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view. - } - - -} -