| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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
- @Binding var boluses: [PumpHistoryEvent]
- @State var state: Home.StateModel
- @State var totalBolus: Decimal = 0
- 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
- }
- var body: some View {
- HStack {
- Text("IOB").font(.callout).foregroundColor(.secondary)
- Text(
- (numberFormatter.string(from: (state.suggestion?.iob ?? 0) as NSNumber) ?? "0") +
- NSLocalizedString(" U", comment: "Insulin unit")
- )
- .font(.callout).fontWeight(.bold)
- Spacer()
- Text("COB").font(.callout).foregroundColor(.secondary)
- Text(
- (numberFormatter.string(from: (state.suggestion?.cob ?? 0) as NSNumber) ?? "0") +
- NSLocalizedString(" g", comment: "gram of carbs")
- )
- .font(.callout).fontWeight(.bold)
- 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(.callout).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(.callout)
- .fontWeight(.bold)
- Text(calculateTINS())
- .font(.callout).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)
- }
- }
- }
- }
- func calculateTINS() -> String {
- let date = Date()
- let calendar = Calendar.current
- let startTime = calendar.startOfDay(for: date)
- let endTime = calendar.date(bySettingHour: 23, minute: 59, second: 59, of: date)
- let bolusesForCurrentDay = boluses.filter { $0.timestamp >= startTime && $0.type == .bolus }
- let totalBolus = bolusesForCurrentDay.map { $0.amount ?? 0 }.reduce(0, +)
- let roundedTotalBolus = Decimal(round(100 * Double(totalBolus)) / 100)
- return "\(roundedTotalBolus) U"
- }
- // func calculateTINS() -> String {
- // let date = Date()
- // let calendar = Calendar.current
- // let startTime = calendar.startOfDay(for: date)
- // let endTime = calendar.date(bySettingHour: 23, minute: 59, second: 59, of: date)
- //
- // let bolusesForCurrentDay = boluses.filter { $0.timestamp >= startTime && $0.type == .bolus}
- // print("****************Boluses for current day: \(bolusesForCurrentDay)")
- //
- // let totalBolus = bolusesForCurrentDay.map { $0.value as? Decimal ?? 0 }.reduce(0, +)
- // print("****************Total Bolus: \(totalBolus)")
- //
- // return "\(totalBolus) U"
- // }
- // func calculateTINS() {
- // DispatchQueue.main.async { [weak self] in
- // guard let self = self else { return }
- //
- // // start of day
- // let startOfDay = Calendar.current.startOfDay(for: Date())
- //
- // // filtering for day
- //// let bolusesForCurrentDay = self.provider.pumpHistory()
- //// .filter { $0.type == .bolus && $0.timestamp >= startOfDay }
- //
- // // adding all together
- //// let totalBolus = bolusesForCurrentDay.map { $0.amount ?? 0 }.reduce(0, +)
- //
- // let totalBolus = boluses.map {}
- //
- // self.totalBolus = totalBolus
- // }
- // }
- //
- 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)
- }
- }
|