AddTempTargetRootView.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 ?
  57. ": \(computeTarget().asMmolL.formatted(.number.grouping(.never).rounded(rule: .towardZero).precision(.fractionLength(1)))) mmol/L" :
  58. ": \(computeTarget().formatted(.number.grouping(.never).rounded(rule: .towardZero).precision(.fractionLength(0)))) mg/dl"
  59. )
  60. ).foregroundColor(.secondary).italic()
  61. }
  62. }
  63. Section {
  64. HStack {
  65. Text("Duration")
  66. Spacer()
  67. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: true)
  68. Text("minutes").foregroundColor(.secondary)
  69. }
  70. DatePicker("Date", selection: $state.date)
  71. Button { isPromtPresented = true }
  72. label: { Text("Save as preset") }
  73. }
  74. Section {
  75. Button { state.enact() }
  76. label: { Text("Enact") }
  77. Button { state.cancel() }
  78. label: { Text("Cancel Temp Target") }
  79. }
  80. }
  81. .popover(isPresented: $isPromtPresented) {
  82. Form {
  83. Section(header: Text("Enter preset name")) {
  84. TextField("Name", text: $state.newPresetName)
  85. Button {
  86. state.save()
  87. isPromtPresented = false
  88. }
  89. label: { Text("Save") }
  90. Button { isPromtPresented = false }
  91. label: { Text("Cancel") }
  92. }
  93. }
  94. }
  95. .onAppear(perform: configureView)
  96. .navigationTitle("Enact Temp Target")
  97. .navigationBarTitleDisplayMode(.automatic)
  98. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  99. }
  100. func computeTarget() -> Decimal {
  101. let ratio = min(Decimal(state.percentage / 100), state.maxValue)
  102. let diff = Double(state.halfBasal - 100)
  103. let multiplier = state.percentage - (diff * (state.percentage / 100))
  104. var target = Decimal(diff + multiplier) / ratio
  105. if (state.halfBasal + (state.halfBasal + target - 100)) <= 0 {
  106. target = (state.halfBasal - 100 + (state.halfBasal - 100) * state.maxValue) / state.maxValue
  107. }
  108. return target
  109. }
  110. private func presetView(for preset: TempTarget) -> some View {
  111. var low = preset.targetBottom
  112. var high = preset.targetTop
  113. if state.units == .mmolL {
  114. low = low?.asMmolL
  115. high = high?.asMmolL
  116. }
  117. return HStack {
  118. VStack {
  119. HStack {
  120. Text(preset.displayName)
  121. Spacer()
  122. }
  123. HStack(spacing: 2) {
  124. Text(
  125. "\(formatter.string(from: (low ?? 0) as NSNumber)!) - \(formatter.string(from: (high ?? 0) as NSNumber)!)"
  126. )
  127. .foregroundColor(.secondary)
  128. .font(.caption)
  129. Text(state.units.rawValue)
  130. .foregroundColor(.secondary)
  131. .font(.caption)
  132. Text("for")
  133. .foregroundColor(.secondary)
  134. .font(.caption)
  135. Text("\(formatter.string(from: preset.duration as NSNumber)!)")
  136. .foregroundColor(.secondary)
  137. .font(.caption)
  138. Text("min")
  139. .foregroundColor(.secondary)
  140. .font(.caption)
  141. Spacer()
  142. }.padding(.top, 2)
  143. }
  144. .contentShape(Rectangle())
  145. .onTapGesture {
  146. state.enactPreset(id: preset.id)
  147. }
  148. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  149. .contentShape(Rectangle())
  150. .padding(.vertical)
  151. .onTapGesture {
  152. removeAlert = Alert(
  153. title: Text("Are you sure?"),
  154. message: Text("Delete preset \"\(preset.displayName)\""),
  155. primaryButton: .destructive(Text("Delete"), action: { state.removePreset(id: preset.id) }),
  156. secondaryButton: .cancel()
  157. )
  158. isRemoveAlertPresented = true
  159. }
  160. .alert(isPresented: $isRemoveAlertPresented) {
  161. removeAlert!
  162. }
  163. }
  164. }
  165. }
  166. }