DataTableDataFlow.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import Foundation
  2. import SwiftUI
  3. enum DataTable {
  4. enum Config {}
  5. enum DataType: String, Equatable {
  6. case carbs
  7. case bolus
  8. case tempBasal
  9. case tempTarget
  10. case suspend
  11. case resume
  12. var name: String {
  13. switch self {
  14. case .carbs:
  15. return "Carbs"
  16. case .bolus:
  17. return "Bolus"
  18. case .tempBasal:
  19. return "Temp Basal"
  20. case .tempTarget:
  21. return "Temp Target"
  22. case .suspend:
  23. return "Suspend"
  24. case .resume:
  25. return "Resume"
  26. }
  27. }
  28. }
  29. class Item: Identifiable, Hashable, Equatable {
  30. let id = UUID()
  31. let units: GlucoseUnits
  32. let type: DataType
  33. let date: Date
  34. let amount: Decimal?
  35. let secondAmount: Decimal?
  36. let duration: Decimal?
  37. private var numberFormater: NumberFormatter {
  38. let formatter = NumberFormatter()
  39. formatter.numberStyle = .decimal
  40. formatter.maximumFractionDigits = 2
  41. return formatter
  42. }
  43. init(
  44. units: GlucoseUnits,
  45. type: DataType,
  46. date: Date,
  47. amount: Decimal? = nil,
  48. secondAmount: Decimal? = nil,
  49. duration: Decimal? = nil
  50. ) {
  51. self.units = units
  52. self.type = type
  53. self.date = date
  54. self.amount = amount
  55. self.secondAmount = secondAmount
  56. self.duration = duration
  57. }
  58. static func == (lhs: Item, rhs: Item) -> Bool {
  59. lhs.id == rhs.id
  60. }
  61. func hash(into hasher: inout Hasher) {
  62. hasher.combine(id)
  63. }
  64. var amountText: String {
  65. guard let amount = amount else {
  66. return ""
  67. }
  68. switch type {
  69. case .carbs:
  70. return numberFormater.string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carbs")
  71. case .bolus:
  72. return numberFormater.string(from: amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  73. case .tempBasal:
  74. return numberFormater
  75. .string(from: amount as NSNumber)! + NSLocalizedString(" U/hr", comment: "Unit insulin per hour")
  76. case .tempTarget:
  77. var converted = amount
  78. if units == .mmolL {
  79. converted = converted.asMmolL
  80. }
  81. guard var secondAmount = secondAmount else {
  82. return numberFormater.string(from: converted as NSNumber)! + " \(units.rawValue)"
  83. }
  84. if units == .mmolL {
  85. secondAmount = secondAmount.asMmolL
  86. }
  87. return numberFormater.string(from: converted as NSNumber)! + " - " + numberFormater
  88. .string(from: secondAmount as NSNumber)! + " \(units.rawValue)"
  89. case .resume,
  90. .suspend:
  91. return type.name
  92. }
  93. }
  94. var color: Color {
  95. switch type {
  96. case .carbs:
  97. return .loopYellow
  98. case .bolus:
  99. return .insulin
  100. case .tempBasal:
  101. return Color.insulin.opacity(0.5)
  102. case .resume,
  103. .suspend,
  104. .tempTarget:
  105. return .loopGray
  106. }
  107. }
  108. var durationText: String? {
  109. guard let duration = duration else {
  110. return nil
  111. }
  112. return numberFormater.string(from: duration as NSNumber)! + " min"
  113. }
  114. }
  115. }
  116. protocol DataTableProvider: Provider {
  117. func pumpHistory() -> [PumpHistoryEvent]
  118. func tempTargets() -> [TempTarget]
  119. func carbs() -> [CarbsEntry]
  120. func deleteCarbs(at date: Date)
  121. }