DataTableDataFlow.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import CoreData
  2. import Foundation
  3. import SwiftUI
  4. enum DataTable {
  5. enum Config {}
  6. enum Mode: String, Hashable, Identifiable, CaseIterable {
  7. case treatments
  8. case meals
  9. case glucose
  10. var id: String { rawValue }
  11. var name: String {
  12. var name: String = ""
  13. switch self {
  14. case .treatments:
  15. name = "Treatments"
  16. case .meals:
  17. name = "Meals"
  18. case .glucose:
  19. name = "Glucose"
  20. }
  21. return NSLocalizedString(name, comment: "History Mode")
  22. }
  23. }
  24. enum DataType: String, Equatable {
  25. case carbs
  26. case fpus
  27. case bolus
  28. case tempBasal
  29. case tempTarget
  30. case suspend
  31. case resume
  32. var name: String {
  33. var name: String = ""
  34. switch self {
  35. case .carbs:
  36. name = "Carbs"
  37. case .fpus:
  38. name = "Protein / Fat"
  39. case .bolus:
  40. name = "Bolus"
  41. case .tempBasal:
  42. name = "Temp Basal"
  43. case .tempTarget:
  44. name = "Temp Target"
  45. case .suspend:
  46. name = "Suspend"
  47. case .resume:
  48. name = "Resume"
  49. }
  50. return NSLocalizedString(name, comment: "Treatment type")
  51. }
  52. }
  53. class Treatment: Identifiable, Hashable, Equatable {
  54. let id: String
  55. let idPumpEvent: String?
  56. let units: GlucoseUnits
  57. let type: DataType
  58. let date: Date
  59. let amount: Decimal?
  60. let secondAmount: Decimal?
  61. let duration: Decimal?
  62. let isFPU: Bool?
  63. let fpuID: String?
  64. let note: String?
  65. let isSMB: Bool?
  66. let isExternal: Bool?
  67. private var numberFormatter: NumberFormatter {
  68. let formatter = NumberFormatter()
  69. formatter.numberStyle = .decimal
  70. formatter.maximumFractionDigits = 2
  71. return formatter
  72. }
  73. private var tempTargetFormater: NumberFormatter {
  74. let formatter = NumberFormatter()
  75. formatter.numberStyle = .decimal
  76. formatter.maximumFractionDigits = 1
  77. return formatter
  78. }
  79. init(
  80. units: GlucoseUnits,
  81. type: DataType,
  82. date: Date,
  83. amount: Decimal? = nil,
  84. secondAmount: Decimal? = nil,
  85. duration: Decimal? = nil,
  86. id: String? = nil,
  87. idPumpEvent: String? = nil,
  88. isFPU: Bool? = nil,
  89. fpuID: String? = nil,
  90. note: String? = nil,
  91. isSMB: Bool? = nil,
  92. isExternal: Bool? = nil
  93. ) {
  94. self.units = units
  95. self.type = type
  96. self.date = date
  97. self.amount = amount
  98. self.secondAmount = secondAmount
  99. self.duration = duration
  100. self.id = id ?? UUID().uuidString
  101. self.idPumpEvent = idPumpEvent
  102. self.isFPU = isFPU
  103. self.fpuID = fpuID
  104. self.note = note
  105. self.isSMB = isSMB
  106. self.isExternal = isExternal
  107. }
  108. static func == (lhs: Treatment, rhs: Treatment) -> Bool {
  109. lhs.id == rhs.id
  110. }
  111. func hash(into hasher: inout Hasher) {
  112. hasher.combine(id)
  113. }
  114. var amountText: String {
  115. guard let amount = amount else {
  116. return ""
  117. }
  118. if amount == 0, duration == 0 {
  119. return "Cancel temp"
  120. }
  121. switch type {
  122. case .carbs:
  123. return numberFormatter
  124. .string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carbs")
  125. case .fpus:
  126. return numberFormatter
  127. .string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carb equilvalents")
  128. case .bolus:
  129. var bolusText = " "
  130. if isSMB ?? false {}
  131. else if isExternal ?? false {
  132. bolusText += NSLocalizedString("External", comment: "External Insulin")
  133. } else {
  134. bolusText += NSLocalizedString("Manual", comment: "Manual Bolus")
  135. }
  136. return numberFormatter
  137. .string(from: amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit") + bolusText
  138. case .tempBasal:
  139. return numberFormatter
  140. .string(from: amount as NSNumber)! + NSLocalizedString(" U/hr", comment: "Unit insulin per hour")
  141. case .tempTarget:
  142. var converted = amount
  143. if units == .mmolL {
  144. converted = converted.asMmolL
  145. }
  146. guard var secondAmount = secondAmount else {
  147. return numberFormatter.string(from: converted as NSNumber)! + " \(units.rawValue)"
  148. }
  149. if units == .mmolL {
  150. secondAmount = secondAmount.asMmolL
  151. }
  152. return tempTargetFormater.string(from: converted as NSNumber)! + " - " + tempTargetFormater
  153. .string(from: secondAmount as NSNumber)! + " \(units.rawValue)"
  154. case .resume,
  155. .suspend:
  156. return type.name
  157. }
  158. }
  159. var color: Color {
  160. switch type {
  161. case .carbs:
  162. return .loopYellow
  163. case .fpus:
  164. return .orange.opacity(0.5)
  165. case .bolus:
  166. return Color.insulin
  167. case .tempBasal:
  168. return Color.insulin.opacity(0.4)
  169. case .resume,
  170. .suspend,
  171. .tempTarget:
  172. return .loopGray
  173. }
  174. }
  175. var durationText: String? {
  176. guard let duration = duration, duration > 0 else {
  177. return nil
  178. }
  179. return numberFormatter.string(from: duration as NSNumber)! + " min"
  180. }
  181. }
  182. class Glucose: Identifiable, Hashable, Equatable {
  183. static func == (lhs: DataTable.Glucose, rhs: DataTable.Glucose) -> Bool {
  184. lhs.glucose == rhs.glucose
  185. }
  186. let glucose: BloodGlucose
  187. init(glucose: BloodGlucose) {
  188. self.glucose = glucose
  189. }
  190. var id: String { glucose.id }
  191. }
  192. }
  193. protocol DataTableProvider: Provider {
  194. func deleteCarbsFromNightscout(withID id: String) async
  195. func deleteInsulin(with treatmentObjectID: NSManagedObjectID) async
  196. }