-
Notifications
You must be signed in to change notification settings - Fork 39
/
TemperatureConverter.swift
75 lines (66 loc) · 1.79 KB
/
TemperatureConverter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// TemperatureConverter.swift
// Few
//
// Created by Josh Abernathy on 3/10/15.
// Copyright (c) 2015 Josh Abernathy. All rights reserved.
//
import Foundation
import Few
struct ConverterState {
static let defaultFahrenheit: CGFloat = 32
let fahrenheit: CGFloat
let celcius: CGFloat
init(fahrenheit: CGFloat = defaultFahrenheit, celcius: CGFloat = f2c(defaultFahrenheit)) {
self.fahrenheit = fahrenheit
self.celcius = celcius
}
}
private func c2f(c: CGFloat) -> CGFloat {
return (c * 9/5) + 32
}
private func f2c(f: CGFloat) -> CGFloat {
return (f - 32) * 5/9
}
private func renderLabeledInput(label: String, value: String, autofocus: Bool, fn: String -> ()) -> Element {
return Element()
.direction(.Row)
.padding(Edges(bottom: 4))
.children([
Label(label).width(75),
Input(
text: value,
placeholder: label,
action: fn)
.autofocus(autofocus)
.width(100),
])
}
private func parseFloat(str: String) -> CGFloat? {
let numberFormatter = NSNumberFormatter()
return (numberFormatter.numberFromString(str)?.doubleValue).map { CGFloat($0) }
}
typealias TemperatureConverter = TemperatureConverter_<ConverterState>
class TemperatureConverter_<LOL>: Component<ConverterState> {
init() {
super.init(initialState: ConverterState());
}
override func render() -> Element {
return Element()
.justification(.Center)
.childAlignment(.Center)
.direction(.Column)
.children([
renderLabeledInput("Fahrenheit", "\(state.fahrenheit)", true) {
if let f = parseFloat($0) {
self.updateState { _ in ConverterState(fahrenheit: f, celcius: f2c(f)) }
}
},
renderLabeledInput("Celcius", "\(state.celcius)", false) {
if let c = parseFloat($0) {
self.updateState { _ in ConverterState(fahrenheit: c2f(c), celcius: c) }
}
},
])
}
}