Skip to content

Commit

Permalink
end of lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
kingst committed Oct 15, 2018
1 parent 216537f commit 3cc2786
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 4 additions & 0 deletions StopWatch/Base.lproj/Main.storyboard
Expand Up @@ -50,6 +50,9 @@
<state key="normal" title="Lap">
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
<connections>
<action selector="lapButtonPressed" destination="BYZ-38-t0r" eventType="touchUpInside" id="xjX-oc-v0f"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="0.00" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="XnN-ry-xnq">
<rect key="frame" x="16" y="84" width="382" height="51"/>
Expand Down Expand Up @@ -89,6 +92,7 @@
</view>
<connections>
<outlet property="startButton" destination="oec-PD-a3v" id="Uhx-Cw-W0U"/>
<outlet property="timeDisplayLabel" destination="XnN-ry-xnq" id="eIn-nt-Wvp"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
Expand Down
20 changes: 20 additions & 0 deletions StopWatch/TimerModel.swift
Expand Up @@ -10,12 +10,16 @@ import Foundation

protocol UpdateTime {
func timeUpdated(_ interval: TimeInterval)
func lapsUpdated(_ laps: [TimeInterval])
}

class TimerModel {
var updateDelegate: UpdateTime?
var startTime: Date?
var timer: Timer?
var lapTime: Date?

var laps: [TimeInterval] = []

init() {
}
Expand All @@ -29,6 +33,22 @@ class TimerModel {
return Date().timeIntervalSince(startTime)
}

func lap() -> Void {
guard let startTime = self.startTime else {
return
}

let now = Date()
let baseTime = self.lapTime ?? startTime
let interval = now.timeIntervalSince(baseTime)
self.laps.append(interval)

// we'll talk about this in detail later
DispatchQueue.main.async {
self.updateDelegate?.lapsUpdated(self.laps)
}
}

func startTimer() -> Void {
self.startTime = Date()
// don't worry about runloop details, we'll talk about it later
Expand Down
48 changes: 46 additions & 2 deletions StopWatch/ViewController.swift
Expand Up @@ -8,9 +8,49 @@

import UIKit

class ViewController: UIViewController, UpdateTime {
class ViewController: UIViewController, UpdateTime, UITableViewDataSource, UITableViewDelegate {

func lapsUpdated(_ laps: [TimeInterval]) {
var displayLaps: [LapDisplay] = []
for (index, lap) in laps.reversed().enumerated() {
displayLaps.append(LapDisplay(lapNumber: laps.count - index, lapTime: lap))
}
self.laps = displayLaps
}


struct LapDisplay {
let lapNumber: Int
let lapTime: TimeInterval
}

@IBOutlet weak var timeDisplayLabel: UILabel!
var laps: [LapDisplay]?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// by default there is one section for each tableview
return self.laps?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let laps = self.laps else {
return UITableViewCell()
}

let lap = laps[indexPath.row]

// the identifier is like the type of the cell
let cell = tableView.dequeueReusableCell(withIdentifier: "lapCell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "lapCell")

// IMPORTANT: make sure to set all fields!
cell.textLabel?.text = "Lap \(lap.lapNumber)"
cell.detailTextLabel?.text = String(format: "%0.02f", lap.lapTime)

return cell
}

func timeUpdated(_ interval: TimeInterval) {
print(interval)
self.timeDisplayLabel.text = String(format: "%0.02f", interval)
}


Expand All @@ -23,6 +63,10 @@ class ViewController: UIViewController, UpdateTime {
self.timer.startTimer()
}

@IBAction func lapButtonPressed() {
self.timer.lap()
}

func toggleButton(withTitles titles: (String, String), on button: UIButton) {
let title = button.currentTitle == titles.0 ? titles.1 : titles.0
button.setTitle(title, for: .normal)
Expand Down

0 comments on commit 3cc2786

Please sign in to comment.