DataTableDataFlow.swift 5.7 KB

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