diff --git a/WorldTrotter.xcodeproj/project.pbxproj b/WorldTrotter.xcodeproj/project.pbxproj index 5301521..88883ed 100644 --- a/WorldTrotter.xcodeproj/project.pbxproj +++ b/WorldTrotter.xcodeproj/project.pbxproj @@ -9,13 +9,13 @@ /* Begin PBXBuildFile section */ 653254AF295FBF4F001CD284 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653254AE295FBF4F001CD284 /* AppDelegate.swift */; }; 653254B1295FBF4F001CD284 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653254B0295FBF4F001CD284 /* SceneDelegate.swift */; }; - 653254B3295FBF4F001CD284 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653254B2295FBF4F001CD284 /* ViewController.swift */; }; 653254B6295FBF4F001CD284 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 653254B4295FBF4F001CD284 /* Main.storyboard */; }; 653254B8295FBF50001CD284 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 653254B7295FBF50001CD284 /* Assets.xcassets */; }; 653254BB295FBF50001CD284 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 653254B9295FBF50001CD284 /* LaunchScreen.storyboard */; }; 653254C6295FBF51001CD284 /* WorldTrotterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653254C5295FBF51001CD284 /* WorldTrotterTests.swift */; }; 653254D0295FBF51001CD284 /* WorldTrotterUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653254CF295FBF51001CD284 /* WorldTrotterUITests.swift */; }; 653254D2295FBF51001CD284 /* WorldTrotterUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653254D1295FBF51001CD284 /* WorldTrotterUITestsLaunchTests.swift */; }; + 653254DF295FD0AE001CD284 /* ConversionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653254DE295FD0AE001CD284 /* ConversionViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -39,7 +39,6 @@ 653254AB295FBF4F001CD284 /* WorldTrotter.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WorldTrotter.app; sourceTree = BUILT_PRODUCTS_DIR; }; 653254AE295FBF4F001CD284 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 653254B0295FBF4F001CD284 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; - 653254B2295FBF4F001CD284 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 653254B5295FBF4F001CD284 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 653254B7295FBF50001CD284 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 653254BA295FBF50001CD284 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; @@ -49,6 +48,7 @@ 653254CB295FBF51001CD284 /* WorldTrotterUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = WorldTrotterUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 653254CF295FBF51001CD284 /* WorldTrotterUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorldTrotterUITests.swift; sourceTree = ""; }; 653254D1295FBF51001CD284 /* WorldTrotterUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorldTrotterUITestsLaunchTests.swift; sourceTree = ""; }; + 653254DE295FD0AE001CD284 /* ConversionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversionViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -101,8 +101,8 @@ children = ( 653254AE295FBF4F001CD284 /* AppDelegate.swift */, 653254B0295FBF4F001CD284 /* SceneDelegate.swift */, - 653254B2295FBF4F001CD284 /* ViewController.swift */, 653254B4295FBF4F001CD284 /* Main.storyboard */, + 653254DE295FD0AE001CD284 /* ConversionViewController.swift */, 653254B7295FBF50001CD284 /* Assets.xcassets */, 653254B9295FBF50001CD284 /* LaunchScreen.storyboard */, 653254BC295FBF50001CD284 /* Info.plist */, @@ -258,8 +258,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 653254B3295FBF4F001CD284 /* ViewController.swift in Sources */, 653254AF295FBF4F001CD284 /* AppDelegate.swift in Sources */, + 653254DF295FD0AE001CD284 /* ConversionViewController.swift in Sources */, 653254B1295FBF4F001CD284 /* SceneDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/WorldTrotter/Base.lproj/Main.storyboard b/WorldTrotter/Base.lproj/Main.storyboard index 25a7638..ea5a560 100644 --- a/WorldTrotter/Base.lproj/Main.storyboard +++ b/WorldTrotter/Base.lproj/Main.storyboard @@ -1,24 +1,97 @@ - + + - + + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/WorldTrotter/ConversionViewController.swift b/WorldTrotter/ConversionViewController.swift new file mode 100644 index 0000000..424f8ee --- /dev/null +++ b/WorldTrotter/ConversionViewController.swift @@ -0,0 +1,75 @@ +// +// ConversionViewController.swift +// WorldTrotter +// +// Created by Xiang Xiao on 12/30/22. +// + +import UIKit + +class ConversionViewController: UIViewController, UITextFieldDelegate { + + @IBOutlet var celsiusLabel: UILabel! + @IBOutlet var textField: UITextField! + var fahrenheitValue: Measurement? { + didSet { + updateCelsiusLabel() + } + } + + var celsiusValue: Measurement? { + if let fahrenheitValue = fahrenheitValue { + return fahrenheitValue.converted(to: .celsius) + } else { + return nil + } + } + + @IBAction func fahrenheitFieldEditingChanged(_ textField: UITextField) { + if let text = textField.text, let value = Double(text) { + fahrenheitValue = Measurement(value: value, unit: .fahrenheit) + } else { + fahrenheitValue = nil + } + } + + @IBAction func dismissKeyboard(_ sender: UITapGestureRecognizer) { + textField.resignFirstResponder() + } + + func updateCelsiusLabel() { + if let celsiusValue = celsiusValue { + celsiusLabel.text = + numberFormatter.string(from: NSNumber(value: celsiusValue.value)) + } else { + celsiusLabel.text = "???" + } + } + + override func viewDidLoad() { + super.viewDidLoad() + + updateCelsiusLabel() + } + + let numberFormatter: NumberFormatter = { + let nf = NumberFormatter() + nf.numberStyle = .decimal + nf.minimumFractionDigits = 0 + nf.maximumFractionDigits = 1 + return nf + }() + + func textField(_ textField: UITextField, + shouldChangeCharactersIn range: NSRange, + replacementString string: String) -> Bool { + let existingTextHasDecimalSeparator = textField.text?.range(of: ".") + let replacementTextHasDecimalSeparator = string.range(of: ".") + + if existingTextHasDecimalSeparator != nil, + replacementTextHasDecimalSeparator != nil { + return false + } + return true + } +} diff --git a/WorldTrotter/ViewController.swift b/WorldTrotter/ViewController.swift deleted file mode 100644 index c7dc888..0000000 --- a/WorldTrotter/ViewController.swift +++ /dev/null @@ -1,19 +0,0 @@ -// -// ViewController.swift -// WorldTrotter -// -// Created by Xiang Xiao on 12/30/22. -// - -import UIKit - -class ViewController: UIViewController { - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view. - } - - -} -