DataTableDataFlow.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import Foundation
  2. import SwiftUI
  3. enum DataTable {
  4. enum Config {}
  5. enum Mode: String, Hashable, Identifiable, CaseIterable {
  6. case treatments
  7. case glucose
  8. var id: String { rawValue }
  9. var name: String {
  10. var name: String = ""
  11. switch self {
  12. case .treatments:
  13. name = "Treatments"
  14. case .glucose:
  15. name = "Glucose"
  16. }
  17. return NSLocalizedString(name, comment: "History Mode")
  18. }
  19. }
  20. enum DataType: String, Equatable {
  21. case carbs
  22. case bolus
  23. case tempBasal
  24. case tempTarget
  25. case suspend
  26. case resume
  27. var name: String {
  28. var name: String = ""
  29. switch self {
  30. case .carbs:
  31. name = "Carbs"
  32. case .bolus:
  33. name = "Bolus"
  34. case .tempBasal:
  35. name = "Temp Basal"
  36. case .tempTarget:
  37. name = "Temp Target"
  38. case .suspend:
  39. name = "Suspend"
  40. case .resume:
  41. name = "Resume"
  42. }
  43. return NSLocalizedString(name, comment: "Treatment type")
  44. }
  45. }
  46. class Treatment: Identifiable, Hashable, Equatable {
  47. let id: String
  48. let idPumpEvent: String?
  49. let units: GlucoseUnits
  50. let type: DataType
  51. let date: Date
  52. let amount: Decimal?
  53. let secondAmount: Decimal?
  54. let duration: Decimal?
  55. private var numberFormater: NumberFormatter {
  56. let formatter = NumberFormatter()
  57. formatter.numberStyle = .decimal
  58. formatter.maximumFractionDigits = 2
  59. return formatter
  60. }
  61. init(
  62. units: GlucoseUnits,
  63. type: DataType,
  64. date: Date,
  65. amount: Decimal? = nil,
  66. secondAmount: Decimal? = nil,
  67. duration: Decimal? = nil,
  68. id: String? = nil,
  69. idPumpEvent: String? = nil
  70. ) {
  71. self.units = units
  72. self.type = type
  73. self.date = date
  74. self.amount = amount
  75. self.secondAmount = secondAmount
  76. self.duration = duration
  77. self.id = id ?? UUID().uuidString
  78. self.idPumpEvent = idPumpEvent
  79. }
  80. static func == (lhs: Treatment, rhs: Treatment) -> Bool {
  81. lhs.id == rhs.id
  82. }
  83. func hash(into hasher: inout Hasher) {
  84. hasher.combine(id)
  85. }
  86. var amountText: String {
  87. guard let amount = amount else {
  88. return ""
  89. }
  90. if amount == 0, duration == 0 {
  91. return "Cancel temp"
  92. }
  93. switch type {
  94. case .carbs:
  95. return numberFormater.string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carbs")
  96. case .bolus:
  97. return numberFormater.string(from: amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  98. case .tempBasal:
  99. return numberFormater
  100. .string(from: amount as NSNumber)! + NSLocalizedString(" U/hr", comment: "Unit insulin per hour")
  101. case .tempTarget:
  102. var converted = amount
  103. if units == .mmolL {
  104. converted = converted.asMmolL
  105. }
  106. guard var secondAmount = secondAmount else {
  107. return numberFormater.string(from: converted as NSNumber)! + " \(units.rawValue)"
  108. }
  109. if units == .mmolL {
  110. secondAmount = secondAmount.asMmolL
  111. }
  112. return numberFormater.string(from: converted as NSNumber)! + " - " + numberFormater
  113. .string(from: secondAmount as NSNumber)! + " \(units.rawValue)"
  114. case .resume,
  115. .suspend:
  116. return type.name
  117. }
  118. }
  119. var color: Color {
  120. switch type {
  121. case .carbs:
  122. return .loopYellow
  123. case .bolus:
  124. return .insulin
  125. case .tempBasal:
  126. return Color.insulin.opacity(0.5)
  127. case .resume,
  128. .suspend,
  129. .tempTarget:
  130. return .loopGray
  131. }
  132. }
  133. var durationText: String? {
  134. guard let duration = duration, duration > 0 else {
  135. return nil
  136. }
  137. return numberFormater.string(from: duration as NSNumber)! + " min"
  138. }
  139. }
  140. class Glucose: Identifiable, Hashable, Equatable {
  141. static func == (lhs: DataTable.Glucose, rhs: DataTable.Glucose) -> Bool {
  142. lhs.glucose == rhs.glucose
  143. }
  144. let glucose: BloodGlucose
  145. init(glucose: BloodGlucose) {
  146. self.glucose = glucose
  147. }
  148. var id: String { glucose.id }
  149. }
  150. }
  151. protocol DataTableProvider: Provider {
  152. func pumpHistory() -> [PumpHistoryEvent]
  153. func tempTargets() -> [TempTarget]
  154. func carbs() -> [CarbsEntry]
  155. func glucose() -> [BloodGlucose]
  156. func deleteCarbs(_ treatement: DataTable.Treatment)
  157. func deleteInsulin(_ treatement: DataTable.Treatment)
  158. func deleteGlucose(id: String)
  159. }