DataTableDataFlow.swift 3.7 KB

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