ContactTrickStateModel.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import ConnectIQ
  2. import SwiftUI
  3. enum ContactTrickValue: String, JSON, CaseIterable, Identifiable, Codable {
  4. var id: String { rawValue }
  5. case none
  6. case glucose
  7. case eventualBG
  8. case delta
  9. case trend
  10. case lastLoopDate
  11. case cob
  12. case iob
  13. case ring
  14. var displayName: String {
  15. switch self {
  16. case .none:
  17. return NSLocalizedString("NoneContactValue", comment: "")
  18. case .glucose:
  19. return NSLocalizedString("GlucoseContactValue", comment: "")
  20. case .eventualBG:
  21. return NSLocalizedString("EventualBGContactValue", comment: "")
  22. case .delta:
  23. return NSLocalizedString("DeltaContactValue", comment: "")
  24. case .trend:
  25. return NSLocalizedString("TrendContactValue", comment: "")
  26. case .lastLoopDate:
  27. return NSLocalizedString("LastLoopTimeContactValue", comment: "")
  28. case .cob:
  29. return NSLocalizedString("COBContactValue", comment: "")
  30. case .iob:
  31. return NSLocalizedString("IOBContactValue", comment: "")
  32. case .ring:
  33. return NSLocalizedString("LoopStatusContactValue", comment: "")
  34. }
  35. }
  36. }
  37. enum ContactTrickLayout: String, JSON, CaseIterable, Identifiable, Codable {
  38. var id: String { rawValue }
  39. case single
  40. case split
  41. var displayName: String {
  42. switch self {
  43. case .single:
  44. return NSLocalizedString("Single", comment: "")
  45. case .split:
  46. return NSLocalizedString("Split", comment: "")
  47. }
  48. }
  49. }
  50. enum ContactTrickLargeRing: String, JSON, CaseIterable, Identifiable, Codable {
  51. var id: String { rawValue }
  52. case none
  53. case loop
  54. case iob
  55. case cob
  56. case iobcob
  57. var displayName: String {
  58. switch self {
  59. case .none:
  60. return NSLocalizedString("DontShowRing", comment: "")
  61. case .loop:
  62. return NSLocalizedString("LoopStatusRing", comment: "")
  63. case .iob:
  64. return NSLocalizedString("IOBRing", comment: "")
  65. case .cob:
  66. return NSLocalizedString("COBRing", comment: "")
  67. case .iobcob:
  68. return NSLocalizedString("IOB+COBRing", comment: "")
  69. }
  70. }
  71. }
  72. extension ContactTrick {
  73. final class StateModel: BaseStateModel<Provider> {
  74. @Published private(set) var syncInProgress = false
  75. @Published private(set) var items: [Item] = []
  76. @Published private(set) var changed: Bool = false
  77. var units: GlucoseUnits = .mmolL
  78. override func subscribe() {
  79. units = settingsManager.settings.units
  80. items = provider.contacts.enumerated().map { index, contact in
  81. Item(
  82. index: index,
  83. entry: contact
  84. )
  85. }
  86. changed = false
  87. }
  88. func add() {
  89. let newItem = Item(
  90. index: items.count,
  91. entry: ContactTrickEntry()
  92. )
  93. items.append(newItem)
  94. changed = true
  95. }
  96. func update(_ atIndex: Int, _ value: ContactTrickEntry) {
  97. items[atIndex].entry = value
  98. changed = true
  99. }
  100. func remove(atOffsets: IndexSet) {
  101. items.remove(atOffsets: atOffsets)
  102. changed = true
  103. }
  104. func save() {
  105. syncInProgress = true
  106. let contacts = items.map { item -> ContactTrickEntry in
  107. item.entry
  108. }
  109. provider.saveContacts(contacts)
  110. .receive(on: DispatchQueue.main)
  111. .sink { _ in
  112. self.syncInProgress = false
  113. self.changed = false
  114. } receiveValue: { contacts in
  115. contacts.enumerated().forEach { index, item in
  116. self.items[index].entry = item
  117. }
  118. }
  119. .store(in: &lifetime)
  120. }
  121. }
  122. }