DataTableDataFlow.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. } else if isSMB ?? false {
  128. bolusText += " " + NSLocalizedString("SMB", comment: "SMB")
  129. }
  130. return numberFormatter
  131. .string(from: amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit") + bolusText
  132. case .tempBasal:
  133. return numberFormatter
  134. .string(from: amount as NSNumber)! + NSLocalizedString(" U/hr", comment: "Unit insulin per hour")
  135. case .tempTarget:
  136. var converted = amount
  137. if units == .mmolL {
  138. converted = converted.asMmolL
  139. }
  140. guard var secondAmount = secondAmount else {
  141. return numberFormatter.string(from: converted as NSNumber)! + " \(units.rawValue)"
  142. }
  143. if units == .mmolL {
  144. secondAmount = secondAmount.asMmolL
  145. }
  146. return tempTargetFormater.string(from: converted as NSNumber)! + " - " + tempTargetFormater
  147. .string(from: secondAmount as NSNumber)! + " \(units.rawValue)"
  148. case .resume,
  149. .suspend:
  150. return type.name
  151. }
  152. }
  153. var color: Color {
  154. switch type {
  155. case .carbs:
  156. return .loopYellow
  157. case .fpus:
  158. return .red
  159. case .bolus:
  160. return .insulin
  161. case .tempBasal:
  162. return Color.insulin.opacity(0.5)
  163. case .resume,
  164. .suspend,
  165. .tempTarget:
  166. return .loopGray
  167. }
  168. }
  169. var durationText: String? {
  170. guard let duration = duration, duration > 0 else {
  171. return nil
  172. }
  173. return numberFormatter.string(from: duration as NSNumber)! + " min"
  174. }
  175. }
  176. class Glucose: Identifiable, Hashable, Equatable {
  177. static func == (lhs: DataTable.Glucose, rhs: DataTable.Glucose) -> Bool {
  178. lhs.glucose == rhs.glucose
  179. }
  180. let glucose: BloodGlucose
  181. init(glucose: BloodGlucose) {
  182. self.glucose = glucose
  183. }
  184. var id: String { glucose.id }
  185. }
  186. }
  187. protocol DataTableProvider: Provider {
  188. func pumpHistory() -> [PumpHistoryEvent]
  189. func tempTargets() -> [TempTarget]
  190. func carbs() -> [CarbsEntry]
  191. func glucose() -> [BloodGlucose]
  192. func deleteCarbs(_ treatement: DataTable.Treatment)
  193. func deleteInsulin(_ treatement: DataTable.Treatment)
  194. func deleteGlucose(id: String)
  195. }