DataTableDataFlow.swift 6.5 KB

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