DataTableDataFlow.swift 6.1 KB

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