AddTempTargetRootView.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import SwiftUI
  2. import Swinject
  3. extension AddTempTarget {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. @State private var isPromtPresented = false
  8. @State private var isRemoveAlertPresented = false
  9. @State private var removeAlert: Alert?
  10. @State private var isEditing = false
  11. private var formatter: NumberFormatter {
  12. let formatter = NumberFormatter()
  13. formatter.numberStyle = .decimal
  14. formatter.maximumFractionDigits = 1
  15. return formatter
  16. }
  17. var body: some View {
  18. Form {
  19. if !state.presets.isEmpty {
  20. Section(header: Text("Presets")) {
  21. ForEach(state.presets) { preset in
  22. presetView(for: preset)
  23. }
  24. }
  25. }
  26. Section(
  27. header: Text("Basal Insulin and Sensitivity ratio"),
  28. footer: Text(
  29. NSLocalizedString(
  30. "A lower 'Half Basal Target' setting will reduce the basal and raise the ISF earlier, at a lower target glucose.",
  31. comment: ""
  32. ) +
  33. NSLocalizedString(" Your setting: ", comment: "") + "\(state.halfBasal) " +
  34. NSLocalizedString("mg/dl. Autosens.max limits the max endpoint", comment: "") +
  35. " (\(state.maxValue * 100) %)"
  36. )
  37. ) {
  38. VStack {
  39. Slider(
  40. value: $state.percentage,
  41. in: 15 ...
  42. Double(state.maxValue * 100),
  43. step: 1,
  44. onEditingChanged: { editing in
  45. isEditing = editing
  46. }
  47. )
  48. Text("\(state.percentage.formatted(.number)) %")
  49. .foregroundColor(isEditing ? .orange : .blue)
  50. .font(.largeTitle)
  51. Divider()
  52. Text(
  53. NSLocalizedString("Target", comment: "") +
  54. (
  55. state
  56. .units == .mmolL ? ": \(computeTarget().asMmolL.formatted(.number)) mmol/L" :
  57. ": \(computeTarget().formatted(.number)) mg/dl"
  58. )
  59. ).foregroundColor(.secondary).italic()
  60. }
  61. }
  62. Section {
  63. HStack {
  64. Text("Duration")
  65. Spacer()
  66. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: true)
  67. Text("minutes").foregroundColor(.secondary)
  68. }
  69. DatePicker("Date", selection: $state.date)
  70. Button { isPromtPresented = true }
  71. label: { Text("Save as preset") }
  72. }
  73. Section {
  74. Button { state.enact() }
  75. label: { Text("Enact") }
  76. Button { state.cancel() }
  77. label: { Text("Cancel Temp Target") }
  78. }
  79. }
  80. .popover(isPresented: $isPromtPresented) {
  81. Form {
  82. Section(header: Text("Enter preset name")) {
  83. TextField("Name", text: $state.newPresetName)
  84. Button {
  85. state.save()
  86. isPromtPresented = false
  87. }
  88. label: { Text("Save") }
  89. Button { isPromtPresented = false }
  90. label: { Text("Cancel") }
  91. }
  92. }
  93. }
  94. .onAppear(perform: configureView)
  95. .navigationTitle("Enact Temp Target")
  96. .navigationBarTitleDisplayMode(.automatic)
  97. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  98. }
  99. func computeTarget() -> Decimal {
  100. let ratio = min(Decimal(state.percentage / 100), state.maxValue)
  101. let diff = Double(state.halfBasal - 100)
  102. let multiplier = state.percentage - (diff * (state.percentage / 100))
  103. var target = Decimal(diff + multiplier) / ratio
  104. if (state.halfBasal + (state.halfBasal + target - 100)) <= 0 {
  105. target = (state.halfBasal - 100 + (state.halfBasal - 100) * state.maxValue) / state.maxValue
  106. }
  107. return target
  108. }
  109. private func presetView(for preset: TempTarget) -> some View {
  110. var low = preset.targetBottom
  111. var high = preset.targetTop
  112. if state.units == .mmolL {
  113. low = low?.asMmolL
  114. high = high?.asMmolL
  115. }
  116. return HStack {
  117. VStack {
  118. HStack {
  119. Text(preset.displayName)
  120. Spacer()
  121. }
  122. HStack(spacing: 2) {
  123. Text(
  124. "\(formatter.string(from: (low ?? 0) as NSNumber)!) - \(formatter.string(from: (high ?? 0) as NSNumber)!)"
  125. )
  126. .foregroundColor(.secondary)
  127. .font(.caption)
  128. Text(state.units.rawValue)
  129. .foregroundColor(.secondary)
  130. .font(.caption)
  131. Text("for")
  132. .foregroundColor(.secondary)
  133. .font(.caption)
  134. Text("\(formatter.string(from: preset.duration as NSNumber)!)")
  135. .foregroundColor(.secondary)
  136. .font(.caption)
  137. Text("min")
  138. .foregroundColor(.secondary)
  139. .font(.caption)
  140. Spacer()
  141. }.padding(.top, 2)
  142. }
  143. .contentShape(Rectangle())
  144. .onTapGesture {
  145. state.enactPreset(id: preset.id)
  146. }
  147. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  148. .contentShape(Rectangle())
  149. .padding(.vertical)
  150. .onTapGesture {
  151. removeAlert = Alert(
  152. title: Text("Are you sure?"),
  153. message: Text("Delete preset \"\(preset.displayName)\""),
  154. primaryButton: .destructive(Text("Delete"), action: { state.removePreset(id: preset.id) }),
  155. secondaryButton: .cancel()
  156. )
  157. isRemoveAlertPresented = true
  158. }
  159. .alert(isPresented: $isRemoveAlertPresented) {
  160. removeAlert!
  161. }
  162. }
  163. }
  164. }
  165. }