Skip to content

Commit

Permalink
Implements extra credit 6 (see #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Vanhoef authored and Peter Vanhoef committed Jun 11, 2017
1 parent 038e89e commit 20f017b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Calculator/Calculator/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="fcY-c8-IEH">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="fcY-c8-IEH">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -529,6 +529,6 @@
</scene>
</scenes>
<inferredMetricsTieBreakers>
<segue reference="Zbq-q6-BA9"/>
<segue reference="jyr-Pc-8eS"/>
</inferredMetricsTieBreakers>
</document>
34 changes: 34 additions & 0 deletions Calculator/Calculator/CalculatorBrain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,38 @@ struct CalculatorBrain {
}

}

var savedSequence: [AnyObject] {
get {
var encodedSequence = [AnyObject]()
for item in sequence {
switch item {
case .operand(let operand):
encodedSequence.append(operand as AnyObject)
case .operation(let operation):
encodedSequence.append(operation as AnyObject)
case .variable(let variable):
encodedSequence.append(variable as AnyObject)
}
}
return encodedSequence
}
set {
var decodedSequence = [ExpressionLiteral]()
for item in newValue {
if let operand = item as? Double {
decodedSequence.append(.operand(operand))
} else {
if let name = item as? String {
if operations[name] != nil {
decodedSequence.append(.operation(name))
} else {
decodedSequence.append(.variable(name))
}
}
}
}
sequence = decodedSequence
}
}
}
41 changes: 40 additions & 1 deletion Calculator/Calculator/CalculatorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,46 @@ class CalculatorViewController: UIViewController {
brain.setOperand(variable: symbol)
displays = brain.evaluateWithErrorReport(using: dictionary)
}


private let defaults = UserDefaults.standard

private var savedSequence: [AnyObject]? {
get {
return defaults.value(forKey: Keys.savedSequence) as? [AnyObject]
}
set {
defaults.set(newValue, forKey: Keys.savedSequence)
}
}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
if savedSequence != nil {
brain.savedSequence = savedSequence!
displays = brain.evaluateWithErrorReport(using: dictionary)

var viewController = splitViewController?.viewControllers.last
if let navigationController = viewController as? UINavigationController {
viewController = navigationController.visibleViewController
}
if let graphingViewController = viewController as? GraphingViewController {
graphingViewController.unaryFunction = { [weak weakSelf = self] operand in
let graphingDictionary: [String: Double] = ["M": operand]
return weakSelf?.brain.evaluate(using: graphingDictionary).result }
graphingViewController.navigationItem.title = self.brain.evaluate().description
}
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

if !brain.evaluate(using: dictionary).isPending {
savedSequence = brain.savedSequence
}
}

// MARK: - Navigation

Expand Down
1 change: 1 addition & 0 deletions Calculator/Calculator/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ import Foundation
struct Keys {
static let scale = "scale"
static let origin = "origin"
static let savedSequence = "savedSequence"
}

0 comments on commit 20f017b

Please sign in to comment.