| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- import SwiftUI
- struct PumpView: View {
- @Binding var reservoir: Decimal?
- @Binding var battery: Battery?
- @Binding var name: String
- @Binding var expiresAtDate: Date?
- @Binding var timerDate: Date
- @State var state: Home.StateModel
- @Environment(\.colorScheme) var colorScheme
- private var reservoirFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 0
- return formatter
- }
- private var batteryFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .percent
- return formatter
- }
- private var numberFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- return formatter
- }
- private var dateFormatter: DateFormatter {
- let dateFormatter = DateFormatter()
- dateFormatter.timeStyle = .short
- return dateFormatter
- }
- var body: some View {
- HStack {
- Text("IOB").font(.caption).foregroundColor(.secondary)
- Text(
- (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
- NSLocalizedString(" U", comment: "Insulin unit")
- )
- .font(.caption).fontWeight(.bold)
- Spacer()
- Text("COB").font(.caption).foregroundColor(.secondary)
- Text(
- (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
- NSLocalizedString(" g", comment: "gram of carbs")
- )
- .font(.caption).fontWeight(.bold)
- Spacer()
- LoopView(
- suggestion: $state.suggestion,
- enactedSuggestion: $state.enactedSuggestion,
- closedLoop: $state.closedLoop,
- timerDate: $state.timerDate,
- isLooping: $state.isLooping,
- lastLoopDate: $state.lastLoopDate,
- manualTempBasal: $state.manualTempBasal
- ).onTapGesture {
- state.isStatusPopupPresented = true
- }.onLongPressGesture {
- let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
- impactHeavy.impactOccurred()
- state.runLoop()
- }
- Spacer()
- if let reservoir = reservoir {
- HStack {
- Image(systemName: "drop.fill")
- .resizable()
- .aspectRatio(contentMode: .fit)
- .frame(maxHeight: 15)
- .foregroundColor(reservoirColor)
- if reservoir == 0xDEAD_BEEF {
- Text("50+ " + NSLocalizedString("U", comment: "Insulin unit")).font(.callout)
- .fontWeight(.bold)
- } else {
- Text(
- reservoirFormatter
- .string(from: reservoir as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
- )
- .font(.caption).fontWeight(.bold)
- }
- }
- }
- Spacer()
- if let battery = battery, battery.display ?? false, expiresAtDate == nil {
- HStack {
- Image(systemName: "battery.100")
- .resizable()
- .aspectRatio(contentMode: .fit)
- .frame(maxHeight: 15)
- .foregroundColor(batteryColor)
- Text("\(Int(battery.percent ?? 100)) %").font(.caption)
- .fontWeight(.bold)
- }
- }
- if let date = expiresAtDate {
- HStack {
- Image(systemName: "stopwatch.fill")
- .resizable()
- .aspectRatio(contentMode: .fit)
- .frame(maxHeight: 15)
- .foregroundColor(timerColor)
- Text(remainingTimeString(time: date.timeIntervalSince(timerDate))).font(.callout)
- .fontWeight(.bold)
- }
- }
- }
- }
- private func remainingTimeString(time: TimeInterval) -> String {
- guard time > 0 else {
- return NSLocalizedString("Replace pod", comment: "View/Header when pod expired")
- }
- var time = time
- let days = Int(time / 1.days.timeInterval)
- time -= days.days.timeInterval
- let hours = Int(time / 1.hours.timeInterval)
- time -= hours.hours.timeInterval
- let minutes = Int(time / 1.minutes.timeInterval)
- if days >= 1 {
- return "\(days)" + NSLocalizedString("d", comment: "abbreviation for days") + " \(hours)" +
- NSLocalizedString("h", comment: "abbreviation for hours")
- }
- if hours >= 1 {
- return "\(hours)" + NSLocalizedString("h", comment: "abbreviation for hours")
- }
- return "\(minutes)" + NSLocalizedString("m", comment: "abbreviation for minutes")
- }
- private var batteryColor: Color {
- guard let battery = battery, let percent = battery.percent else {
- return .gray
- }
- switch percent {
- case ...10:
- return .red
- case ...20:
- return .yellow
- default:
- return .green
- }
- }
- private var reservoirColor: Color {
- guard let reservoir = reservoir else {
- return .gray
- }
- switch reservoir {
- case ...10:
- return .red
- case ...30:
- return .yellow
- default:
- return .blue
- }
- }
- private var timerColor: Color {
- guard let expisesAt = expiresAtDate else {
- return .gray
- }
- let time = expisesAt.timeIntervalSince(timerDate)
- switch time {
- case ...8.hours.timeInterval:
- return .red
- case ...1.days.timeInterval:
- return .yellow
- default:
- return .green
- }
- }
- }
- struct Hairline: View {
- let color: Color
- var body: some View {
- Rectangle()
- .fill(color)
- .frame(width: UIScreen.main.bounds.width / 1.3, height: 1)
- .opacity(0.5)
- }
- }
|