DataTableDataFlow.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 fpus
  23. case bolus
  24. case tempBasal
  25. case tempTarget
  26. case suspend
  27. case resume
  28. var name: String {
  29. var name: String = ""
  30. switch self {
  31. case .carbs:
  32. name = "Carbs"
  33. case .fpus:
  34. name = "Protein / Fat"
  35. case .bolus:
  36. name = "Bolus"
  37. case .tempBasal:
  38. name = "Temp Basal"
  39. case .tempTarget:
  40. name = "Temp Target"
  41. case .suspend:
  42. name = "Suspend"
  43. case .resume:
  44. name = "Resume"
  45. }
  46. return NSLocalizedString(name, comment: "Treatment type")
  47. }
  48. }
  49. class Treatment: Identifiable, Hashable, Equatable {
  50. let id: String
  51. let idPumpEvent: String?
  52. let units: GlucoseUnits
  53. let type: DataType
  54. let date: Date
  55. let amount: Decimal?
  56. let secondAmount: Decimal?
  57. let duration: Decimal?
  58. let isFPU: Bool?
  59. let fpuID: String?
  60. let note: String?
  61. let isNonPump: Bool?
  62. private var numberFormatter: NumberFormatter {
  63. let formatter = NumberFormatter()
  64. formatter.numberStyle = .decimal
  65. formatter.maximumFractionDigits = 2
  66. return formatter
  67. }
  68. private var tempTargetFormater: NumberFormatter {
  69. let formatter = NumberFormatter()
  70. formatter.numberStyle = .decimal
  71. formatter.maximumFractionDigits = 1
  72. return formatter
  73. }
  74. init(
  75. units: GlucoseUnits,
  76. type: DataType,
  77. date: Date,
  78. amount: Decimal? = nil,
  79. secondAmount: Decimal? = nil,
  80. duration: Decimal? = nil,
  81. id: String? = nil,
  82. idPumpEvent: String? = nil,
  83. isFPU: Bool? = false,
  84. fpuID: String? = nil,
  85. note: String? = nil,
  86. isNonPump: Bool? = nil
  87. ) {
  88. self.units = units
  89. self.type = type
  90. self.date = date
  91. self.amount = amount
  92. self.secondAmount = secondAmount
  93. self.duration = duration
  94. self.id = id ?? UUID().uuidString
  95. self.idPumpEvent = idPumpEvent
  96. self.isFPU = isFPU
  97. self.fpuID = fpuID
  98. self.note = note
  99. self.isNonPump = isNonPump
  100. }
  101. static func == (lhs: Treatment, rhs: Treatment) -> Bool {
  102. lhs.id == rhs.id
  103. }
  104. func hash(into hasher: inout Hasher) {
  105. hasher.combine(id)
  106. }
  107. var amountText: String {
  108. guard let amount = amount else {
  109. return ""
  110. }
  111. if amount == 0, duration == 0 {
  112. return "Cancel temp"
  113. }
  114. switch type {
  115. case .carbs:
  116. return numberFormatter.string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carbs")
  117. case .fpus:
  118. return numberFormatter
  119. .string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carb equilvalents")
  120. case .bolus:
  121. var bolusText = ""
  122. if isNonPump ?? false {
  123. bolusText += " " + NSLocalizedString("Non-Pump", comment: "Non-pump Insulin")
  124. }
  125. return numberFormatter
  126. .string(from: amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit") + bolusText
  127. case .tempBasal:
  128. return numberFormatter
  129. .string(from: amount as NSNumber)! + NSLocalizedString(" U/hr", comment: "Unit insulin per hour")
  130. case .tempTarget:
  131. var converted = amount
  132. if units == .mmolL {
  133. converted = converted.asMmolL
  134. }
  135. guard var secondAmount = secondAmount else {
  136. return numberFormatter.string(from: converted as NSNumber)! + " \(units.rawValue)"
  137. }
  138. if units == .mmolL {
  139. secondAmount = secondAmount.asMmolL
  140. }
  141. return tempTargetFormater.string(from: converted as NSNumber)! + " - " + tempTargetFormater
  142. .string(from: secondAmount as NSNumber)! + " \(units.rawValue)"
  143. case .resume,
  144. .suspend:
  145. return type.name
  146. }
  147. }
  148. var color: Color {
  149. switch type {
  150. case .carbs:
  151. return .loopYellow
  152. case .fpus:
  153. return .red
  154. case .bolus:
  155. return .insulin
  156. case .tempBasal:
  157. return Color.insulin.opacity(0.5)
  158. case .resume,
  159. .suspend,
  160. .tempTarget:
  161. return .loopGray
  162. }
  163. }
  164. var durationText: String? {
  165. guard let duration = duration, duration > 0 else {
  166. return nil
  167. }
  168. return numberFormatter.string(from: duration as NSNumber)! + " min"
  169. }
  170. }
  171. class Glucose: Identifiable, Hashable, Equatable {
  172. static func == (lhs: DataTable.Glucose, rhs: DataTable.Glucose) -> Bool {
  173. lhs.glucose == rhs.glucose
  174. }
  175. let glucose: BloodGlucose
  176. init(glucose: BloodGlucose) {
  177. self.glucose = glucose
  178. }
  179. var id: String { glucose.id }
  180. }
  181. }
  182. protocol DataTableProvider: Provider {
  183. func pumpHistory() -> [PumpHistoryEvent]
  184. func tempTargets() -> [TempTarget]
  185. func carbs() -> [CarbsEntry]
  186. func glucose() -> [BloodGlucose]
  187. func deleteCarbs(_ treatement: DataTable.Treatment)
  188. func deleteInsulin(_ treatement: DataTable.Treatment)
  189. func deleteGlucose(id: String)
  190. }