DataTableDataFlow.swift 5.0 KB

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