Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BMI Calculator complete with dynamic state management based on variou… #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions BMI Calculator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@
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 */; };
AD64359A2312D0BC00E22B00 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = AD6435992312D0BC00E22B00 /* README.md */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
718D8A00258E9FCC00F3C5EE /* ResultsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultsViewController.swift; sourceTree = "<group>"; };
718D8A22258ECE0900F3C5EE /* CalculatorBrain.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CalculatorBrain.swift; sourceTree = "<group>"; };
718D8A25258ECE1F00F3C5EE /* BMI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BMI.swift; sourceTree = "<group>"; };
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 = "<group>"; };
AD0B9F7B230D61800065EFD9 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
AD0B9F7D230D61800065EFD9 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
AD0B9F7D230D61800065EFD9 /* CalculateViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculateViewController.swift; sourceTree = "<group>"; };
AD0B9F80230D61800065EFD9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
AD0B9F82230D61840065EFD9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
AD0B9F85230D61840065EFD9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
Expand All @@ -39,6 +45,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
718D8A21258ECDC600F3C5EE /* Models */ = {
isa = PBXGroup;
children = (
718D8A22258ECE0900F3C5EE /* CalculatorBrain.swift */,
718D8A25258ECE1F00F3C5EE /* BMI.swift */,
);
path = Models;
sourceTree = "<group>";
};
AD0B9F6D230D61800065EFD9 = {
isa = PBXGroup;
children = (
Expand All @@ -61,7 +76,7 @@
children = (
AD0B9F79230D61800065EFD9 /* AppDelegate.swift */,
AD0B9F7B230D61800065EFD9 /* SceneDelegate.swift */,
AD0B9F8F230D63EA0065EFD9 /* Models */,
718D8A21258ECDC600F3C5EE /* Models */,
AD0B9F8E230D63E10065EFD9 /* Views */,
AD0B9F8D230D63D70065EFD9 /* Controllers */,
AD0B9F82230D61840065EFD9 /* Assets.xcassets */,
Expand All @@ -74,7 +89,8 @@
AD0B9F8D230D63D70065EFD9 /* Controllers */ = {
isa = PBXGroup;
children = (
AD0B9F7D230D61800065EFD9 /* ViewController.swift */,
AD0B9F7D230D61800065EFD9 /* CalculateViewController.swift */,
718D8A00258E9FCC00F3C5EE /* ResultsViewController.swift */,
);
path = Controllers;
sourceTree = "<group>";
Expand All @@ -87,13 +103,6 @@
path = Views;
sourceTree = "<group>";
};
AD0B9F8F230D63EA0065EFD9 /* Models */ = {
isa = PBXGroup;
children = (
);
path = Models;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -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;
};
Expand Down
73 changes: 73 additions & 0 deletions BMI Calculator/Controllers/CalculateViewController.swift
Original file line number Diff line number Diff line change
@@ -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!)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commented print statement can be removed

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whaaat?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clueless

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi


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()

}
}

}

47 changes: 47 additions & 0 deletions BMI Calculator/Controllers/ResultsViewController.swift
Original file line number Diff line number Diff line change
@@ -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.
}
*/

}
33 changes: 31 additions & 2 deletions BMI Calculator/Controllers/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}

15 changes: 15 additions & 0 deletions BMI Calculator/Models/BMI.swift
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 36 additions & 0 deletions BMI Calculator/Models/CalculatorBrain.swift
Original file line number Diff line number Diff line change
@@ -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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commented print statement can be removed

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
}
}
Loading