Skip to content

Commit

Permalink
[Chapter 3-4] WorldTrotter
Browse files Browse the repository at this point in the history
  • Loading branch information
xxiao23 committed Dec 31, 2022
1 parent e254bf1 commit 74b5223
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 29 deletions.
8 changes: 4 additions & 4 deletions WorldTrotter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = "<group>"; };
653254B0295FBF4F001CD284 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
653254B2295FBF4F001CD284 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
653254B5295FBF4F001CD284 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
653254B7295FBF50001CD284 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
653254BA295FBF50001CD284 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
Expand All @@ -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 = "<group>"; };
653254D1295FBF51001CD284 /* WorldTrotterUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorldTrotterUITestsLaunchTests.swift; sourceTree = "<group>"; };
653254DE295FD0AE001CD284 /* ConversionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversionViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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;
Expand Down
85 changes: 79 additions & 6 deletions WorldTrotter/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,24 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<!--Conversion View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="BYZ-38-t0r" customClass="ConversionViewController" customModule="WorldTrotter" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<subviews>
<textField opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="248" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="value" textAlignment="center" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="1Lc-Bd-aZ7">
<rect key="frame" x="71.666666666666686" y="67" width="250" height="85.333333333333314"/>
<constraints>
<constraint firstAttribute="width" constant="250" id="M0J-TX-VzK"/>
</constraints>
<color key="textColor" systemColor="systemOrangeColor"/>
<fontDescription key="fontDescription" type="system" pointSize="70"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no" keyboardType="decimalPad"/>
<connections>
<action selector="fahrenheitFieldEditingChanged:" destination="BYZ-38-t0r" eventType="editingChanged" id="rn4-5I-55p"/>
<outlet property="delegate" destination="BYZ-38-t0r" id="8q4-sa-PF0"/>
</connections>
</textField>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="degrees Fahrenheit" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ogG-ut-M6e">
<rect key="frame" x="49" y="160.33333333333334" width="295" height="43"/>
<fontDescription key="fontDescription" type="system" pointSize="36"/>
<color key="textColor" red="0.88235294117647056" green="0.34509803921568627" blue="0.16078431372549018" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="is really" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Bdo-9x-GJX">
<rect key="frame" x="139.66666666666666" y="211.33333333333334" width="113.66666666666666" height="43"/>
<fontDescription key="fontDescription" type="system" pointSize="36"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="100" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="nkh-sF-v9D">
<rect key="frame" x="138" y="262.33333333333331" width="117" height="84"/>
<fontDescription key="fontDescription" type="system" pointSize="70"/>
<color key="textColor" red="0.88235294117647056" green="0.34509803921568627" blue="0.16078431372549018" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="degrees Celsius" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="oeK-CR-4n8">
<rect key="frame" x="73.666666666666671" y="354.33333333333331" width="245.66666666666663" height="43"/>
<fontDescription key="fontDescription" type="system" pointSize="36"/>
<color key="textColor" red="0.88235294117647056" green="0.34509803921568627" blue="0.16078431372549018" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" red="0.96078431372549022" green="0.95686274509803915" blue="0.94509803921568625" alpha="1" colorSpace="calibratedRGB"/>
<gestureRecognizers/>
<constraints>
<constraint firstItem="1Lc-Bd-aZ7" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="2qN-7X-jGg"/>
<constraint firstItem="oeK-CR-4n8" firstAttribute="top" secondItem="nkh-sF-v9D" secondAttribute="bottom" constant="8" id="7Vn-Ni-ODE"/>
<constraint firstItem="ogG-ut-M6e" firstAttribute="top" secondItem="1Lc-Bd-aZ7" secondAttribute="bottom" constant="8" id="H8s-IL-D4u"/>
<constraint firstItem="oeK-CR-4n8" firstAttribute="centerX" secondItem="nkh-sF-v9D" secondAttribute="centerX" id="HVy-5e-dLm"/>
<constraint firstItem="ogG-ut-M6e" firstAttribute="centerX" secondItem="1Lc-Bd-aZ7" secondAttribute="centerX" id="Idr-Nu-DMC"/>
<constraint firstItem="Bdo-9x-GJX" firstAttribute="top" secondItem="ogG-ut-M6e" secondAttribute="bottom" constant="8" id="KFc-DK-NFL"/>
<constraint firstItem="oeK-CR-4n8" firstAttribute="centerX" secondItem="ogG-ut-M6e" secondAttribute="centerX" id="QX7-ua-maY"/>
<constraint firstItem="Bdo-9x-GJX" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="139.66666666666666" id="abW-3C-I8U"/>
<constraint firstItem="1Lc-Bd-aZ7" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="8" id="e5x-UB-qKA"/>
<constraint firstItem="nkh-sF-v9D" firstAttribute="top" secondItem="Bdo-9x-GJX" secondAttribute="bottom" constant="8" id="gKp-dK-G7t"/>
<constraint firstItem="oeK-CR-4n8" firstAttribute="centerX" secondItem="Bdo-9x-GJX" secondAttribute="centerX" id="uF4-yM-fbQ"/>
</constraints>
<connections>
<outletCollection property="gestureRecognizers" destination="U0a-uQ-sJm" appends="YES" id="POv-Qh-9Eq"/>
</connections>
</view>
<connections>
<outlet property="celsiusLabel" destination="nkh-sF-v9D" id="WQU-xD-wcy"/>
<outlet property="textField" destination="1Lc-Bd-aZ7" id="huu-fR-3KY"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
<tapGestureRecognizer id="U0a-uQ-sJm">
<connections>
<action selector="dismissKeyboard:" destination="BYZ-38-t0r" id="yJg-Fd-Z1x"/>
</connections>
</tapGestureRecognizer>
</objects>
<point key="canvasLocation" x="61.832061068702288" y="3.5211267605633805"/>
</scene>
</scenes>
<resources>
<systemColor name="systemOrangeColor">
<color red="1" green="0.58431372549019611" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>
75 changes: 75 additions & 0 deletions WorldTrotter/ConversionViewController.swift
Original file line number Diff line number Diff line change
@@ -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<UnitTemperature>? {
didSet {
updateCelsiusLabel()
}
}

var celsiusValue: Measurement<UnitTemperature>? {
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
}
}
19 changes: 0 additions & 19 deletions WorldTrotter/ViewController.swift

This file was deleted.

0 comments on commit 74b5223

Please sign in to comment.