DataTableDataFlow.swift 3.4 KB

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