DataTableDataFlow.swift 5.6 KB

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