forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlucoseRangePicker.swift
More file actions
161 lines (142 loc) · 5.18 KB
/
GlucoseRangePicker.swift
File metadata and controls
161 lines (142 loc) · 5.18 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// GlucoseRangePicker.swift
// LoopKitUI
//
// Created by Michael Pangburn on 5/14/20.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import SwiftUI
import HealthKit
import LoopKit
public struct GlucoseRangePicker: View {
public enum UsageContext: Equatable {
/// This picker is one component of a larger multi-component picker (e.g. a schedule item picker).
case component(availableWidth: CGFloat)
/// This picker operates independently.
case independent
}
@Binding var lowerBound: HKQuantity
@Binding var upperBound: HKQuantity
var unit: HKUnit
var minValue: HKQuantity?
var maxValue: HKQuantity?
var guardrail: Guardrail<HKQuantity>
var formatter: NumberFormatter
var usageContext: UsageContext
public init(
range: Binding<ClosedRange<HKQuantity>>,
unit: HKUnit,
minValue: HKQuantity?,
maxValue: HKQuantity? = nil,
guardrail: Guardrail<HKQuantity>,
usageContext: UsageContext = .independent
) {
self._lowerBound = Binding(
get: { range.wrappedValue.lowerBound },
set: {
if $0 > range.wrappedValue.upperBound {
// Prevent crash if picker gets into state where "lower bound" > "upper bound"
range.wrappedValue = $0...$0
}
range.wrappedValue = $0...range.wrappedValue.upperBound
}
)
self._upperBound = Binding(
get: { range.wrappedValue.upperBound },
set: {
if range.wrappedValue.lowerBound > $0 {
// Prevent crash if picker gets into state where "lower bound" > "upper bound"
range.wrappedValue = range.wrappedValue.lowerBound...range.wrappedValue.lowerBound
} else {
range.wrappedValue = range.wrappedValue.lowerBound...$0
}
}
)
self.unit = unit
self.minValue = minValue
self.maxValue = maxValue
self.guardrail = guardrail
self.formatter = {
let quantityFormatter = QuantityFormatter(for: unit)
return quantityFormatter.numberFormatter
}()
self.usageContext = usageContext
}
public var body: some View {
switch usageContext {
case .component(availableWidth: let availableWidth):
body(availableWidth: availableWidth)
case .independent:
GeometryReader { geometry in
HStack(spacing: 0) {
Spacer()
self.body(availableWidth: geometry.size.width)
Spacer()
}
}
.frame(height: 216)
}
}
private func body(availableWidth: CGFloat) -> some View {
HStack(spacing: 0) {
GlucoseValuePicker(
value: $lowerBound,
unit: unit,
guardrail: guardrail,
bounds: lowerBoundRange,
isUnitLabelVisible: false
)
.frame(width: availableWidth / 3)
.overlay(
Text(separator)
.foregroundColor(Color(.secondaryLabel))
.offset(x: spacing + separatorWidth),
alignment: .trailing
)
.padding(.leading, usageContext == .independent ? unitLabelWidth : 0)
.padding(.trailing, spacing + separatorWidth + spacing)
.clipped()
.compositingGroup()
.accessibility(identifier: "min_glucose_picker")
GlucoseValuePicker(
value: $upperBound,
unit: unit,
guardrail: guardrail,
bounds: upperBoundRange
)
.frame(width: availableWidth / 3)
.padding(.trailing, unitLabelWidth)
.clipped()
.compositingGroup()
.accessibility(identifier: "max_glucose_picker")
}
}
var separator: String { "–" }
var separatorWidth: CGFloat {
let attributedSeparator = NSAttributedString(
string: separator,
attributes: [.font: UIFont.preferredFont(forTextStyle: .body)]
)
return attributedSeparator.size().width
}
var spacing: CGFloat { 4 }
var unitLabelWidth: CGFloat {
let attributedUnitString = NSAttributedString(
string: unit.shortLocalizedUnitString(),
attributes: [.font: UIFont.preferredFont(forTextStyle: .body)]
)
return attributedUnitString.size().width
}
var lowerBoundRange: ClosedRange<HKQuantity> {
let min = minValue.map { Swift.max(guardrail.absoluteBounds.lowerBound, $0) }
?? guardrail.absoluteBounds.lowerBound
let max = Swift.min(guardrail.absoluteBounds.upperBound, upperBound)
return min...max
}
var upperBoundRange: ClosedRange<HKQuantity> {
let min = max(guardrail.absoluteBounds.lowerBound, lowerBound)
let max = maxValue.map { Swift.min(guardrail.absoluteBounds.upperBound, $0) }
?? guardrail.absoluteBounds.upperBound
return min...max
}
}