| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import SwiftUI
- struct CurrentGlucoseView: View {
- @Binding var recentGlucose: BloodGlucose?
- @Binding var delta: Int?
- let units: GlucoseUnits
- private var glucoseFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 0
- if units == .mgdL {
- formatter.minimumFractionDigits = 1
- formatter.maximumFractionDigits = 1
- }
- return formatter
- }
- private var deltaFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- formatter.positivePrefix = "+"
- return formatter
- }
- private var dateFormatter: DateFormatter {
- let formatter = DateFormatter()
- formatter.timeStyle = .short
- return formatter
- }
- var body: some View {
- HStack {
- VStack {
- Text(
- recentGlucose?.glucose
- .map { glucoseFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)! } ??
- "--"
- )
- .font(.largeTitle)
- Spacer()
- Text(
- recentGlucose.map { dateFormatter.string(from: $0.dateString) } ?? "--"
- ).font(.caption)
- }
- VStack {
- Spacer()
- image.padding(.bottom, 2)
- Text(
- delta
- .map { deltaFormatter.string(from: Double(units == .mmolL ? $0.asMmolL : Decimal($0)) as NSNumber)!
- } ??
- "--"
- ).font(.caption)
- Text("\(units.rawValue)").font(.caption2)
- }
- }
- }
- var image: Image {
- guard let direction = recentGlucose?.direction else {
- return Image(systemName: "arrow.left.and.right")
- }
- switch direction {
- case .doubleUp,
- .singleUp,
- .tripleUp:
- return Image(systemName: "arrow.up")
- case .fortyFiveUp:
- return Image(systemName: "arrow.up.right")
- case .flat:
- return Image(systemName: "arrow.forward")
- case .fortyFiveDown:
- return Image(systemName: "arrow.down.forward")
- case .doubleDown,
- .singleDown,
- .tripleDown:
- return Image(systemName: "arrow.down")
- case .none,
- .notComputable,
- .rateOutOfRange:
- return Image(systemName: "arrow.left.and.right")
- }
- }
- }
|