DataTableDataFlow.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. <<<<<<< HEAD
  124. return numberFormatter
  125. .string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carbs")
  126. =======
  127. return numberFormatter.string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carbs")
  128. >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133
  129. case .fpus:
  130. return numberFormatter
  131. .string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carb equilvalents")
  132. case .bolus:
  133. <<<<<<< HEAD
  134. var bolusText = " "
  135. if isSMB ?? false {}
  136. else if isExternal ?? false {
  137. bolusText += NSLocalizedString("External", comment: "External Insulin")
  138. } else {
  139. bolusText += NSLocalizedString("Manual", comment: "Manual Bolus")
  140. =======
  141. var bolusText = ""
  142. if isExternal ?? false {
  143. bolusText += " " + NSLocalizedString("External", comment: "External Insulin")
  144. >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133
  145. }
  146. return numberFormatter
  147. .string(from: amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit") + bolusText
  148. case .tempBasal:
  149. return numberFormatter
  150. .string(from: amount as NSNumber)! + NSLocalizedString(" U/hr", comment: "Unit insulin per hour")
  151. case .tempTarget:
  152. var converted = amount
  153. if units == .mmolL {
  154. converted = converted.asMmolL
  155. }
  156. guard var secondAmount = secondAmount else {
  157. return numberFormatter.string(from: converted as NSNumber)! + " \(units.rawValue)"
  158. }
  159. if units == .mmolL {
  160. secondAmount = secondAmount.asMmolL
  161. }
  162. return tempTargetFormater.string(from: converted as NSNumber)! + " - " + tempTargetFormater
  163. .string(from: secondAmount as NSNumber)! + " \(units.rawValue)"
  164. case .resume,
  165. .suspend:
  166. return type.name
  167. }
  168. }
  169. var color: Color {
  170. switch type {
  171. case .carbs:
  172. return .loopYellow
  173. case .fpus:
  174. return .orange.opacity(0.5)
  175. case .bolus:
  176. return Color.insulin
  177. case .tempBasal:
  178. return Color.insulin.opacity(0.4)
  179. case .resume,
  180. .suspend,
  181. .tempTarget:
  182. return .loopGray
  183. }
  184. }
  185. var durationText: String? {
  186. guard let duration = duration, duration > 0 else {
  187. return nil
  188. }
  189. return numberFormatter.string(from: duration as NSNumber)! + " min"
  190. }
  191. }
  192. class Glucose: Identifiable, Hashable, Equatable {
  193. static func == (lhs: DataTable.Glucose, rhs: DataTable.Glucose) -> Bool {
  194. lhs.glucose == rhs.glucose
  195. }
  196. let glucose: BloodGlucose
  197. init(glucose: BloodGlucose) {
  198. self.glucose = glucose
  199. }
  200. var id: String { glucose.id }
  201. }
  202. }
  203. protocol DataTableProvider: Provider {
  204. func deleteCarbsFromNightscout(withID id: String) async
  205. func deleteInsulin(with treatmentObjectID: NSManagedObjectID) async
  206. }