From 20f017b1ec956af6bfb590c7f3f358e330db8acd Mon Sep 17 00:00:00 2001 From: Peter Vanhoef Date: Sun, 11 Jun 2017 20:33:17 +0200 Subject: [PATCH] Implements extra credit 6 (see #18) --- .../Calculator/Base.lproj/Main.storyboard | 6 +-- Calculator/Calculator/CalculatorBrain.swift | 34 +++++++++++++++ .../Calculator/CalculatorViewController.swift | 41 ++++++++++++++++++- Calculator/Calculator/Keys.swift | 1 + 4 files changed, 78 insertions(+), 4 deletions(-) diff --git a/Calculator/Calculator/Base.lproj/Main.storyboard b/Calculator/Calculator/Base.lproj/Main.storyboard index 7ef522f..6516c6d 100755 --- a/Calculator/Calculator/Base.lproj/Main.storyboard +++ b/Calculator/Calculator/Base.lproj/Main.storyboard @@ -1,11 +1,11 @@ - + - + @@ -529,6 +529,6 @@ - + diff --git a/Calculator/Calculator/CalculatorBrain.swift b/Calculator/Calculator/CalculatorBrain.swift index a98083e..f32ed53 100755 --- a/Calculator/Calculator/CalculatorBrain.swift +++ b/Calculator/Calculator/CalculatorBrain.swift @@ -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 + } + } } diff --git a/Calculator/Calculator/CalculatorViewController.swift b/Calculator/Calculator/CalculatorViewController.swift index 35f6364..4d5d97a 100755 --- a/Calculator/Calculator/CalculatorViewController.swift +++ b/Calculator/Calculator/CalculatorViewController.swift @@ -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 diff --git a/Calculator/Calculator/Keys.swift b/Calculator/Calculator/Keys.swift index a40487a..c254faa 100644 --- a/Calculator/Calculator/Keys.swift +++ b/Calculator/Calculator/Keys.swift @@ -11,4 +11,5 @@ import Foundation struct Keys { static let scale = "scale" static let origin = "origin" + static let savedSequence = "savedSequence" }