DataTableDataFlow.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. if amount == 0, duration == 0 {
  69. return "Cancel temp"
  70. }
  71. switch type {
  72. case .carbs:
  73. return numberFormater.string(from: amount as NSNumber)! + NSLocalizedString(" g", comment: "gram of carbs")
  74. case .bolus:
  75. return numberFormater.string(from: amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
  76. case .tempBasal:
  77. return numberFormater
  78. .string(from: amount as NSNumber)! + NSLocalizedString(" U/hr", comment: "Unit insulin per hour")
  79. case .tempTarget:
  80. var converted = amount
  81. if units == .mmolL {
  82. converted = converted.asMmolL
  83. }
  84. guard var secondAmount = secondAmount else {
  85. return numberFormater.string(from: converted as NSNumber)! + " \(units.rawValue)"
  86. }
  87. if units == .mmolL {
  88. secondAmount = secondAmount.asMmolL
  89. }
  90. return numberFormater.string(from: converted as NSNumber)! + " - " + numberFormater
  91. .string(from: secondAmount as NSNumber)! + " \(units.rawValue)"
  92. case .resume,
  93. .suspend:
  94. return type.name
  95. }
  96. }
  97. var color: Color {
  98. switch type {
  99. case .carbs:
  100. return .loopYellow
  101. case .bolus:
  102. return .insulin
  103. case .tempBasal:
  104. return Color.insulin.opacity(0.5)
  105. case .resume,
  106. .suspend,
  107. .tempTarget:
  108. return .loopGray
  109. }
  110. }
  111. var durationText: String? {
  112. guard let duration = duration, duration > 0 else {
  113. return nil
  114. }
  115. return numberFormater.string(from: duration as NSNumber)! + " min"
  116. }
  117. }
  118. }
  119. protocol DataTableProvider: Provider {
  120. func pumpHistory() -> [PumpHistoryEvent]
  121. func tempTargets() -> [TempTarget]
  122. func carbs() -> [CarbsEntry]
  123. func deleteCarbs(at date: Date)
  124. }