diff --git a/BMI Calculator.xcodeproj/project.pbxproj b/BMI Calculator.xcodeproj/project.pbxproj index 195990f2..85095d14 100644 --- a/BMI Calculator.xcodeproj/project.pbxproj +++ b/BMI Calculator.xcodeproj/project.pbxproj @@ -7,9 +7,12 @@ objects = { /* Begin PBXBuildFile section */ + 718D8A01258E9FCC00F3C5EE /* ResultsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 718D8A00258E9FCC00F3C5EE /* ResultsViewController.swift */; }; + 718D8A23258ECE0900F3C5EE /* CalculatorBrain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 718D8A22258ECE0900F3C5EE /* CalculatorBrain.swift */; }; + 718D8A26258ECE1F00F3C5EE /* BMI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 718D8A25258ECE1F00F3C5EE /* BMI.swift */; }; AD0B9F7A230D61800065EFD9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD0B9F79230D61800065EFD9 /* AppDelegate.swift */; }; AD0B9F7C230D61800065EFD9 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD0B9F7B230D61800065EFD9 /* SceneDelegate.swift */; }; - AD0B9F7E230D61800065EFD9 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD0B9F7D230D61800065EFD9 /* ViewController.swift */; }; + AD0B9F7E230D61800065EFD9 /* CalculateViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD0B9F7D230D61800065EFD9 /* CalculateViewController.swift */; }; AD0B9F81230D61800065EFD9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AD0B9F7F230D61800065EFD9 /* Main.storyboard */; }; AD0B9F83230D61840065EFD9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AD0B9F82230D61840065EFD9 /* Assets.xcassets */; }; AD0B9F86230D61840065EFD9 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AD0B9F84230D61840065EFD9 /* LaunchScreen.storyboard */; }; @@ -17,10 +20,13 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 718D8A00258E9FCC00F3C5EE /* ResultsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultsViewController.swift; sourceTree = ""; }; + 718D8A22258ECE0900F3C5EE /* CalculatorBrain.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CalculatorBrain.swift; sourceTree = ""; }; + 718D8A25258ECE1F00F3C5EE /* BMI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BMI.swift; sourceTree = ""; }; AD0B9F76230D61800065EFD9 /* BMI Calculator.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "BMI Calculator.app"; sourceTree = BUILT_PRODUCTS_DIR; }; AD0B9F79230D61800065EFD9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; AD0B9F7B230D61800065EFD9 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; - AD0B9F7D230D61800065EFD9 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + AD0B9F7D230D61800065EFD9 /* CalculateViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculateViewController.swift; sourceTree = ""; }; AD0B9F80230D61800065EFD9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; AD0B9F82230D61840065EFD9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; AD0B9F85230D61840065EFD9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; @@ -39,6 +45,15 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 718D8A21258ECDC600F3C5EE /* Models */ = { + isa = PBXGroup; + children = ( + 718D8A22258ECE0900F3C5EE /* CalculatorBrain.swift */, + 718D8A25258ECE1F00F3C5EE /* BMI.swift */, + ); + path = Models; + sourceTree = ""; + }; AD0B9F6D230D61800065EFD9 = { isa = PBXGroup; children = ( @@ -61,7 +76,7 @@ children = ( AD0B9F79230D61800065EFD9 /* AppDelegate.swift */, AD0B9F7B230D61800065EFD9 /* SceneDelegate.swift */, - AD0B9F8F230D63EA0065EFD9 /* Models */, + 718D8A21258ECDC600F3C5EE /* Models */, AD0B9F8E230D63E10065EFD9 /* Views */, AD0B9F8D230D63D70065EFD9 /* Controllers */, AD0B9F82230D61840065EFD9 /* Assets.xcassets */, @@ -74,7 +89,8 @@ AD0B9F8D230D63D70065EFD9 /* Controllers */ = { isa = PBXGroup; children = ( - AD0B9F7D230D61800065EFD9 /* ViewController.swift */, + AD0B9F7D230D61800065EFD9 /* CalculateViewController.swift */, + 718D8A00258E9FCC00F3C5EE /* ResultsViewController.swift */, ); path = Controllers; sourceTree = ""; @@ -87,13 +103,6 @@ path = Views; sourceTree = ""; }; - AD0B9F8F230D63EA0065EFD9 /* Models */ = { - isa = PBXGroup; - children = ( - ); - path = Models; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -166,9 +175,12 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - AD0B9F7E230D61800065EFD9 /* ViewController.swift in Sources */, + 718D8A23258ECE0900F3C5EE /* CalculatorBrain.swift in Sources */, + AD0B9F7E230D61800065EFD9 /* CalculateViewController.swift in Sources */, AD0B9F7A230D61800065EFD9 /* AppDelegate.swift in Sources */, AD0B9F7C230D61800065EFD9 /* SceneDelegate.swift in Sources */, + 718D8A26258ECE1F00F3C5EE /* BMI.swift in Sources */, + 718D8A01258E9FCC00F3C5EE /* ResultsViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/BMI Calculator/Controllers/CalculateViewController.swift b/BMI Calculator/Controllers/CalculateViewController.swift new file mode 100644 index 00000000..6225365a --- /dev/null +++ b/BMI Calculator/Controllers/CalculateViewController.swift @@ -0,0 +1,73 @@ +// +// ViewController.swift +// BMI Calculator +// +// Created by Angela Yu on 21/08/2019. +// Copyright © 2019 Angela Yu. All rights reserved. +// + +import UIKit + +class CalculateViewController: UIViewController { + @IBOutlet weak var heightSlider: UISlider! + @IBOutlet weak var weightSlider: UISlider! + @IBOutlet weak var heightLabel: UILabel! + @IBOutlet weak var weightLabel: UILabel! + +// var bmiValue = "0.0" + var calculatorBrain = CalculatorBrain() + + + override func viewDidLoad() { + super.viewDidLoad() + // Do any additional setup after loading the view. + } + + @IBAction func heightSliderChanged(_ sender: UISlider) { + let currentValue = sender.value + let roundedValue = (currentValue*100).rounded()/100 + print(roundedValue) + heightLabel.text = "\(roundedValue)m" + } + + @IBAction func weightSliderChanged(_ sender: UISlider) { + let currentValue = Int(sender.value) + print(currentValue) + weightLabel.text = "\(currentValue)Kg" + + } + + @IBAction func calculatePressed(_ sender: UIButton) { + let height = heightSlider.value + let weight = weightSlider.value + + calculatorBrain.calculateBMI(height: height, weight: weight) + + + // Manual way to create modal pop up on a custom class +// let secondVC = SecondViewController() +// secondVC.bmiValue = String(format: "%.1f", bmi) +// self.present(secondVC, animated: true, completion: nil) + + + // for navigating btw screens that have a segue created on Main.storyboard + self.performSegue(withIdentifier: "goToResults", sender: self) + + + } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + //print("sender", sender.bmi!) + + if segue.identifier == "goToResults" { + // narrow down the data type and specify the exact type the desination will be + let destinationVC = segue.destination as! ResultsViewController + destinationVC.bmiValue = calculatorBrain.getBMIValue() + destinationVC.advice = calculatorBrain.getAdvice() + destinationVC.color = calculatorBrain.getColor() + + } + } + +} + diff --git a/BMI Calculator/Controllers/ResultsViewController.swift b/BMI Calculator/Controllers/ResultsViewController.swift new file mode 100644 index 00000000..37760761 --- /dev/null +++ b/BMI Calculator/Controllers/ResultsViewController.swift @@ -0,0 +1,47 @@ +// +// ResultsViewController.swift +// BMI Calculator +// +// Created by Matthew Digel on 12/19/20. +// Copyright © 2020 Angela Yu. All rights reserved. +// + +import UIKit + +class ResultsViewController: UIViewController { + @IBOutlet weak var bmiLabel: UILabel! + @IBOutlet weak var adviceLabel: UILabel! + @IBOutlet weak var backgroundView: UIImageView! + + var bmiValue: String? + var advice: String? + var color: UIColor? + + override func viewDidLoad() { + super.viewDidLoad() + bmiLabel.text = bmiValue + adviceLabel.text = advice + view.backgroundColor = color + + // Could also do background Color this way: +// backgroundView.backgroundColor = color + } + + @IBAction func recalculatePressed(_ sender: UIButton) { + // can use self too + // self.dismiss(animated: true, completion: nil) + 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/BMI Calculator/Controllers/ViewController.swift b/BMI Calculator/Controllers/ViewController.swift index 6ff2105b..5a8504b6 100644 --- a/BMI Calculator/Controllers/ViewController.swift +++ b/BMI Calculator/Controllers/ViewController.swift @@ -8,13 +8,42 @@ import UIKit -class ViewController: UIViewController { - +class CalculateViewController: UIViewController { + @IBOutlet weak var heightSlider: UISlider! + @IBOutlet weak var weightSlider: UISlider! + @IBOutlet weak var heightLabel: UILabel! + @IBOutlet weak var weightLabel: UILabel! + override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } + @IBAction func heightSliderChanged(_ sender: UISlider) { + let currentValue = sender.value + let roundedValue = (currentValue*100).rounded()/100 + print(roundedValue) + heightLabel.text = "\(roundedValue)m" + } + + @IBAction func weightSliderChanged(_ sender: UISlider) { + let currentValue = Int(sender.value) + print(currentValue) + weightLabel.text = "\(currentValue)Kg" + } + + @IBAction func calculatePressed(_ sender: UIButton) { + let height = heightSlider.value + let weight = weightSlider.value + + let bmi = weight / pow(height, 2) + + let secondVC = SecondViewController() + secondVC.bmiValue = String(format: "%.1f", bmi) + + self.present(secondVC, animated: true, completion: nil) + } + } diff --git a/BMI Calculator/Models/BMI.swift b/BMI Calculator/Models/BMI.swift new file mode 100644 index 00000000..92b44a90 --- /dev/null +++ b/BMI Calculator/Models/BMI.swift @@ -0,0 +1,15 @@ +// +// BMI.swift +// BMI Calculator +// +// Created by Matthew Digel on 12/19/20. +// Copyright © 2020 Angela Yu. All rights reserved. +// + +import UIKit + +struct BMI { + let value: Float + let advice: String + let color: UIColor +} diff --git a/BMI Calculator/Models/CalculatorBrain.swift b/BMI Calculator/Models/CalculatorBrain.swift new file mode 100644 index 00000000..c0f29763 --- /dev/null +++ b/BMI Calculator/Models/CalculatorBrain.swift @@ -0,0 +1,36 @@ +import UIKit + +struct CalculatorBrain { + var bmi: BMI? + + mutating func calculateBMI(height: Float, weight: Float){ + let bmiValue = weight / pow(height, 2) + if bmiValue < 18.5 { + // print("Underweight") + bmi = BMI(value: bmiValue, advice: "Eat More Pies", color: #colorLiteral(red: 0.2884203767, green: 0.749411387, blue: 1, alpha: 1)) + + } else if bmiValue < 24.9 { + print("Normal") + bmi = BMI(value: bmiValue, advice: "You are fit", color: #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)) + + } else { + print("Overweight") + bmi = BMI(value: bmiValue, advice: "Eat Less Pies", color: #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)) + } + + } + + func getBMIValue() -> String { + let BMIValue = bmi?.value + + return String(format: "%.1f", BMIValue ?? "0.0") + } + + func getAdvice () -> String { + return bmi?.advice ?? "No advice" + } + + func getColor () -> UIColor { + return bmi?.color ?? UIColor.white + } +} diff --git a/BMI Calculator/Views/Base.lproj/Main.storyboard b/BMI Calculator/Views/Base.lproj/Main.storyboard index e03f2fb0..ea074959 100644 --- a/BMI Calculator/Views/Base.lproj/Main.storyboard +++ b/BMI Calculator/Views/Base.lproj/Main.storyboard @@ -1,16 +1,17 @@ - + - + + - + - + @@ -54,6 +55,9 @@ + + + @@ -82,8 +86,11 @@ + + + - + @@ -114,49 +125,55 @@ - + + + + + + + - + - + - + - + - + - + @@ -185,8 +206,12 @@ - + + + + +