DataTableDataFlow.swift 6.1 KB

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