DataTableDataFlow.swift 6.0 KB

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