Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Ratios/CalculatorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class CalculatorViewController: UIViewController {
self.stackViewWidthConstraint?.isActive = true
}

func updateBackgroundColor(_ ratio: Int, duration: TimeInterval = 0.15) {
func updateBackgroundColor(_ ratio: Double, duration: TimeInterval = 0.15) {
UIView.animate(withDuration: duration, delay: 0, options: [.curveEaseInOut, .allowUserInteraction], animations: {
let newBackgroundColor = BrewStrengthColor.colorFor(ratio)
self.view.backgroundColor = newBackgroundColor
Expand All @@ -157,7 +157,7 @@ class CalculatorViewController: UIViewController {
}

@objc func handleFieldValueChange(_ sender: AnyObject) {
let ratio = Int(self.ratioInputView.textField.text ?? "16") ?? 16
let ratio = Double(self.ratioInputView.textField.text ?? "16") ?? 16
var total = Double(self.totalInputView.textField.text ?? "0") ?? 0
var water = Double(self.waterInputView.textField.text ?? "0") ?? 0
var grounds = Double(self.groundsInputView.textField.text ?? "0") ?? 0
Expand Down
6 changes: 3 additions & 3 deletions Ratios/Helpers/Calculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import Foundation

struct Calculator {

static func calculateGrounds(brew: Double, ratio: Int) -> Double {
static func calculateGrounds(brew: Double, ratio: Double) -> Double {
guard ratio > 2 else {
return 0
}
return brew / Double(ratio - 2)
}

static func calculateGrounds(water: Double, ratio: Int) -> Double {
static func calculateGrounds(water: Double, ratio: Double) -> Double {
guard ratio > 0 else {
return 0
}
return water / Double(ratio)
}

static func calculateWater(grounds: Double, ratio: Int) -> Double {
static func calculateWater(grounds: Double, ratio: Double) -> Double {
return grounds * Double(ratio)
}

Expand Down
10 changes: 5 additions & 5 deletions Ratios/Helpers/PersistenceStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
import Foundation

protocol PersistenceStore {
func save(grounds: Double, ratio: Int)
func getValues() -> (grounds: Double, ratio: Int)
func save(grounds: Double, ratio: Double)
func getValues() -> (grounds: Double, ratio: Double)
}

extension UserDefaults: PersistenceStore {

private static let ratiosPersistenceGroundsKey = "co.brushedtype.ratios.grounds"
private static let ratiosPersistenceRatioKey = "co.brushedtype.ratios.ratio"

func save(grounds: Double, ratio: Int) {
func save(grounds: Double, ratio: Double) {
self.set(grounds, forKey: UserDefaults.ratiosPersistenceGroundsKey)
self.set(ratio, forKey: UserDefaults.ratiosPersistenceRatioKey)
}

func getValues() -> (grounds: Double, ratio: Int) {
func getValues() -> (grounds: Double, ratio: Double) {
let grounds = self.double(forKey: UserDefaults.ratiosPersistenceGroundsKey)
let ratio = self.integer(forKey: UserDefaults.ratiosPersistenceRatioKey)
let ratio = self.double(forKey: UserDefaults.ratiosPersistenceRatioKey)

let groundsOrFallback = grounds > 0 ? grounds : 12
let ratioOrFallback = ratio > 0 ? ratio : 16
Expand Down