DataTableDataFlow.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 bolus
  23. case tempBasal
  24. case tempTarget
  25. case suspend
  26. case resume
  27. var name: String {
  28. var name: String = ""
  29. switch self {
  30. case .carbs:
  31. name = "Carbs"
  32. case .bolus:
  33. name = "Bolus"
  34. case .tempBasal:
  35. name = "Temp Basal"
  36. case .tempTarget:
  37. name = "Temp Target"
  38. case .suspend:
  39. name = "Suspend"
  40. case .resume:
  41. name = "Resume"
  42. }
  43. return NSLocalizedString(name, comment: "Treatment type")
  44. }
  45. }
  46. class Treatment: Identifiable, Hashable, Equatable {
  47. var id: UUID
  48. let units: GlucoseUnits
  49. let type: DataType
  50. let date: Date
  51. let amount: Decimal?
  52. let secondAmount: Decimal?
  53. let duration: Decimal?
  54. private var numberFormater: NumberFormatter {
  55. let formatter = NumberFormatter()
  56. formatter.numberStyle = .decimal
  57. formatter.maximumFractionDigits = 2
  58. return formatter
  59. }
  60. init(
  61. units: GlucoseUnits,
  62. type: DataType,
  63. date: Date,
  64. amount: Decimal? = nil,
  65. secondAmount: Decimal? = nil,
  66. duration: Decimal? = nil,
  67. id: UUID? = nil
  68. ) {
  69. self.units = units
  70. self.type = type
  71. self.date = date
  72. self.amount = amount
  73. self.secondAmount = secondAmount
  74. self.duration = duration
  75. self.id = id ?? UUID()
  76. }
  77. static func == (lhs: Treatment, rhs: Treatment) -> Bool {
  78. lhs.id == rhs.id
  79. }
  80. func hash(into hasher: inout Hasher) {
  81. hasher.combine(id)
  82. }
  83. var amountText: String {
  84. guard let amount = amount else {
  85. return ""
  86. }
  87. if amount == 0, duration == 0 {
  88. return "Cancel temp"
  89. }
  90. switch type {
  91. case .carbs:
  92. return numberFormater.string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carbs")
  93. case .bolus:
  94. return numberFormater.string(from: amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  95. case .tempBasal:
  96. return numberFormater
  97. .string(from: amount as NSNumber)! + NSLocalizedString(" U/hr", comment: "Unit insulin per hour")
  98. case .tempTarget:
  99. var converted = amount
  100. if units == .mmolL {
  101. converted = converted.asMmolL
  102. }
  103. guard var secondAmount = secondAmount else {
  104. return numberFormater.string(from: converted as NSNumber)! + " \(units.rawValue)"
  105. }
  106. if units == .mmolL {
  107. secondAmount = secondAmount.asMmolL
  108. }
  109. return numberFormater.string(from: converted as NSNumber)! + " - " + numberFormater
  110. .string(from: secondAmount as NSNumber)! + " \(units.rawValue)"
  111. case .resume,
  112. .suspend:
  113. return type.name
  114. }
  115. }
  116. var color: Color {
  117. switch type {
  118. case .carbs:
  119. return .loopYellow
  120. case .bolus:
  121. return .insulin
  122. case .tempBasal:
  123. return Color.insulin.opacity(0.5)
  124. case .resume,
  125. .suspend,
  126. .tempTarget:
  127. return .loopGray
  128. }
  129. }
  130. var durationText: String? {
  131. guard let duration = duration, duration > 0 else {
  132. return nil
  133. }
  134. return numberFormater.string(from: duration as NSNumber)! + " min"
  135. }
  136. }
  137. class Glucose: Identifiable, Hashable, Equatable {
  138. static func == (lhs: DataTable.Glucose, rhs: DataTable.Glucose) -> Bool {
  139. lhs.glucose == rhs.glucose
  140. }
  141. let glucose: BloodGlucose
  142. init(glucose: BloodGlucose) {
  143. self.glucose = glucose
  144. }
  145. var id: String { glucose.id }
  146. }
  147. }
  148. protocol DataTableProvider: Provider {
  149. func pumpHistory() -> [PumpHistoryEvent]
  150. func tempTargets() -> [TempTarget]
  151. func carbs() -> [CarbsEntry]
  152. func glucose() -> [BloodGlucose]
  153. func deleteCarbs(_ treatement: DataTable.Treatment)
  154. func deleteInsulin(_ treatement: DataTable.Treatment)
  155. func deleteGlucose(id: String)
  156. }