TargetsEditorStateModel.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import SwiftUI
  2. extension TargetsEditor {
  3. final class StateModel: BaseStateModel<Provider> {
  4. @Injected() private var nightscout: NightscoutManager!
  5. @Published var items: [Item] = []
  6. @Published var initialItems: [Item] = []
  7. @Published var shouldDisplaySaving: Bool = false
  8. let timeValues = stride(from: 0.0, to: 1.days.timeInterval, by: 30.minutes.timeInterval).map { $0 }
  9. var rateValues: [Decimal] {
  10. var values = stride(from: 72.0, to: 180.01, by: 1.0).map { Decimal($0) }
  11. if units == .mmolL {
  12. values = values.filter { Int(truncating: $0 as NSNumber) % 2 == 0 }
  13. }
  14. return values
  15. }
  16. var canAdd: Bool {
  17. guard let lastItem = items.last else { return true }
  18. return lastItem.timeIndex < timeValues.count - 1
  19. }
  20. var hasChanges: Bool {
  21. initialItems != items
  22. }
  23. private(set) var units: GlucoseUnits = .mgdL
  24. override func subscribe() {
  25. units = settingsManager.settings.units
  26. let profile = provider.profile
  27. items = profile.targets.map { value in
  28. let timeIndex = timeValues.firstIndex(of: Double(value.offset * 60)) ?? 0
  29. let lowIndex = rateValues.firstIndex(of: value.low) ?? 0
  30. let highIndex = rateValues.firstIndex(of: value.high) ?? 0
  31. return Item(lowIndex: lowIndex, highIndex: highIndex, timeIndex: timeIndex)
  32. }
  33. initialItems = items.map { Item(lowIndex: $0.lowIndex, highIndex: $0.highIndex, timeIndex: $0.timeIndex) }
  34. }
  35. func add() {
  36. var time = 0
  37. var low = 0
  38. var high = 0
  39. if let last = items.last {
  40. time = last.timeIndex + 1
  41. low = last.lowIndex
  42. high = low
  43. }
  44. let newItem = Item(lowIndex: low, highIndex: high, timeIndex: time)
  45. items.append(newItem)
  46. }
  47. func save() {
  48. guard hasChanges else { return }
  49. shouldDisplaySaving.toggle()
  50. let targets = items.map { item -> BGTargetEntry in
  51. let formatter = DateFormatter()
  52. formatter.timeZone = TimeZone(secondsFromGMT: 0)
  53. formatter.dateFormat = "HH:mm:ss"
  54. let date = Date(timeIntervalSince1970: self.timeValues[item.timeIndex])
  55. let minutes = Int(date.timeIntervalSince1970 / 60)
  56. let low = self.rateValues[item.lowIndex]
  57. let high = low
  58. return BGTargetEntry(low: low, high: high, start: formatter.string(from: date), offset: minutes)
  59. }
  60. let profile = BGTargets(units: .mgdL, userPreferredUnits: .mgdL, targets: targets)
  61. provider.saveProfile(profile)
  62. initialItems = items.map { Item(lowIndex: $0.lowIndex, highIndex: $0.highIndex, timeIndex: $0.timeIndex) }
  63. Task.detached(priority: .low) {
  64. debug(.nightscout, "Attempting to upload targets to Nightscout")
  65. await self.nightscout.uploadProfiles()
  66. }
  67. }
  68. func validate() {
  69. DispatchQueue.main.async {
  70. let uniq = Array(Set(self.items))
  71. let sorted = uniq.sorted { $0.timeIndex < $1.timeIndex }
  72. .map { item -> Item in
  73. Item(lowIndex: item.lowIndex, highIndex: item.highIndex, timeIndex: item.timeIndex)
  74. }
  75. sorted.first?.timeIndex = 0
  76. self.items = sorted
  77. if self.items.isEmpty {
  78. self.units = self.settingsManager.settings.units
  79. }
  80. }
  81. }
  82. }
  83. }