forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlucoseEntryTableViewController.swift
More file actions
67 lines (55 loc) · 2.07 KB
/
GlucoseEntryTableViewController.swift
File metadata and controls
67 lines (55 loc) · 2.07 KB
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
//
// GlucoseEntryTableViewController.swift
// LoopKitUI
//
// Created by Michael Pangburn on 11/24/18.
// Copyright © 2018 LoopKit Authors. All rights reserved.
//
import UIKit
import HealthKit
import LoopKit
public protocol GlucoseEntryTableViewControllerDelegate: AnyObject {
func glucoseEntryTableViewControllerDidChangeGlucose(_ controller: GlucoseEntryTableViewController)
}
public class GlucoseEntryTableViewController: TextFieldTableViewController {
let glucoseUnit: HKUnit
private lazy var glucoseFormatter: NumberFormatter = {
let quantityFormatter = QuantityFormatter(for: glucoseUnit)
return quantityFormatter.numberFormatter
}()
public var glucose: HKQuantity? {
get {
guard let value = value, let doubleValue = Double(value) else {
return nil
}
return HKQuantity(unit: glucoseUnit, doubleValue: doubleValue)
}
set {
if let newValue = newValue {
value = glucoseFormatter.string(from: newValue.doubleValue(for: glucoseUnit))
} else {
value = nil
}
}
}
public weak var glucoseEntryDelegate: GlucoseEntryTableViewControllerDelegate?
public init(glucoseUnit: HKUnit) {
self.glucoseUnit = glucoseUnit
super.init(style: .grouped)
unit = glucoseUnit.shortLocalizedUnitString()
keyboardType = .decimalPad
placeholder = "Enter glucose value"
delegate = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension GlucoseEntryTableViewController: TextFieldTableViewControllerDelegate {
public func textFieldTableViewControllerDidEndEditing(_ controller: TextFieldTableViewController) {
glucoseEntryDelegate?.glucoseEntryTableViewControllerDidChangeGlucose(self)
}
public func textFieldTableViewControllerDidReturn(_ controller: TextFieldTableViewController) {
glucoseEntryDelegate?.glucoseEntryTableViewControllerDidChangeGlucose(self)
}
}