DataTableDataFlow.swift 6.2 KB

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