DataTableDataFlow.swift 6.5 KB

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