| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- import Foundation
- import SwiftUI
- enum DataTable {
- enum Config {}
- enum Mode: String, Hashable, Identifiable, CaseIterable {
- case treatments
- case glucose
- var id: String { rawValue }
- var name: String {
- var name: String = ""
- switch self {
- case .treatments:
- name = "Treatments"
- case .glucose:
- name = "Glucose"
- }
- return NSLocalizedString(name, comment: "History Mode")
- }
- }
- enum DataType: String, Equatable {
- case carbs
- case fpus
- case bolus
- case tempBasal
- case tempTarget
- case suspend
- case resume
- var name: String {
- var name: String = ""
- switch self {
- case .carbs:
- name = "Carbs"
- case .fpus:
- name = "Protein / Fat"
- case .bolus:
- name = "Bolus"
- case .tempBasal:
- name = "Temp Basal"
- case .tempTarget:
- name = "Temp Target"
- case .suspend:
- name = "Suspend"
- case .resume:
- name = "Resume"
- }
- return NSLocalizedString(name, comment: "Treatment type")
- }
- }
- class Treatment: Identifiable, Hashable, Equatable {
- let id: String
- let idPumpEvent: String?
- let units: GlucoseUnits
- let type: DataType
- let date: Date
- let amount: Decimal?
- let secondAmount: Decimal?
- let duration: Decimal?
- let isFPU: Bool?
- let fpuID: String?
- let note: String?
- let isSMB: Bool?
- let isNonPump: Bool?
- private var numberFormatter: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 2
- return formatter
- }
- private var tempTargetFormater: NumberFormatter {
- let formatter = NumberFormatter()
- formatter.numberStyle = .decimal
- formatter.maximumFractionDigits = 1
- return formatter
- }
- init(
- units: GlucoseUnits,
- type: DataType,
- date: Date,
- amount: Decimal? = nil,
- secondAmount: Decimal? = nil,
- duration: Decimal? = nil,
- id: String? = nil,
- idPumpEvent: String? = nil,
- isFPU: Bool? = nil,
- fpuID: String? = nil,
- note: String? = nil,
- isSMB: Bool? = nil,
- isNonPump: Bool? = nil
- ) {
- self.units = units
- self.type = type
- self.date = date
- self.amount = amount
- self.secondAmount = secondAmount
- self.duration = duration
- self.id = id ?? UUID().uuidString
- self.idPumpEvent = idPumpEvent
- self.isFPU = isFPU
- self.fpuID = fpuID
- self.note = note
- self.isSMB = isSMB
- self.isNonPump = isNonPump
- }
- static func == (lhs: Treatment, rhs: Treatment) -> Bool {
- lhs.id == rhs.id
- }
- func hash(into hasher: inout Hasher) {
- hasher.combine(id)
- }
- var amountText: String {
- guard let amount = amount else {
- return ""
- }
- if amount == 0, duration == 0 {
- return "Cancel temp"
- }
- switch type {
- case .carbs:
- return numberFormatter
- .string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carbs")
- case .fpus:
- return numberFormatter
- .string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carb equilvalents")
- case .bolus:
- var bolusText = " "
- if isSMB ?? false {
- bolusText += NSLocalizedString("Automatic", comment: "Automatic delivered treatments")
- } else if isNonPump ?? false {
- bolusText += NSLocalizedString("Non-Pump", comment: "Non-pump Insulin")
- } else {
- bolusText += NSLocalizedString("Manual", comment: "Manual Bolus")
- }
- return numberFormatter
- .string(from: amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit") + bolusText
- case .tempBasal:
- return numberFormatter
- .string(from: amount as NSNumber)! + NSLocalizedString(" U/hr", comment: "Unit insulin per hour")
- case .tempTarget:
- var converted = amount
- if units == .mmolL {
- converted = converted.asMmolL
- }
- guard var secondAmount = secondAmount else {
- return numberFormatter.string(from: converted as NSNumber)! + " \(units.rawValue)"
- }
- if units == .mmolL {
- secondAmount = secondAmount.asMmolL
- }
- return tempTargetFormater.string(from: converted as NSNumber)! + " - " + tempTargetFormater
- .string(from: secondAmount as NSNumber)! + " \(units.rawValue)"
- case .resume,
- .suspend:
- return type.name
- }
- }
- var color: Color {
- switch type {
- case .carbs:
- return .loopYellow
- case .fpus:
- return .orange.opacity(0.5)
- case .bolus:
- return Color.insulin
- case .tempBasal:
- return Color.insulin.opacity(0.4)
- case .resume,
- .suspend,
- .tempTarget:
- return .loopGray
- }
- }
- var durationText: String? {
- guard let duration = duration, duration > 0 else {
- return nil
- }
- return numberFormatter.string(from: duration as NSNumber)! + " min"
- }
- }
- class Glucose: Identifiable, Hashable, Equatable {
- static func == (lhs: DataTable.Glucose, rhs: DataTable.Glucose) -> Bool {
- lhs.glucose == rhs.glucose
- }
- let glucose: BloodGlucose
- init(glucose: BloodGlucose) {
- self.glucose = glucose
- }
- var id: String { glucose.id }
- }
- }
- protocol DataTableProvider: Provider {
- func pumpHistory() -> [PumpHistoryEvent]
- func tempTargets() -> [TempTarget]
- func carbs() -> [CarbsEntry]
- func glucose() -> [BloodGlucose]
- func deleteCarbs(_ treatement: DataTable.Treatment)
- func deleteInsulin(_ treatement: DataTable.Treatment)
- func deleteGlucose(id: String)
- }
|