DataTableDataFlow.swift 6.2 KB

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