AddProfileForm.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import Foundation
  2. import SwiftUI
  3. struct AddProfileForm: View {
  4. @Environment(\.presentationMode) var presentationMode
  5. @StateObject var state: OverrideProfilesConfig.StateModel
  6. @State private var isEditing = false
  7. @State private var overrideTarget = false
  8. @Environment(\.colorScheme) var colorScheme
  9. @State private var showAlert = false
  10. @State private var alertString = ""
  11. @Environment(\.dismiss) var dismiss
  12. var color: LinearGradient {
  13. colorScheme == .dark ? LinearGradient(
  14. gradient: Gradient(colors: [
  15. Color.bgDarkBlue,
  16. Color.bgDarkerDarkBlue
  17. ]),
  18. startPoint: .top,
  19. endPoint: .bottom
  20. )
  21. :
  22. LinearGradient(
  23. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  24. startPoint: .top,
  25. endPoint: .bottom
  26. )
  27. }
  28. private var formatter: NumberFormatter {
  29. let formatter = NumberFormatter()
  30. formatter.numberStyle = .decimal
  31. formatter.maximumFractionDigits = 0
  32. return formatter
  33. }
  34. private var glucoseFormatter: NumberFormatter {
  35. let formatter = NumberFormatter()
  36. formatter.numberStyle = .decimal
  37. formatter.maximumFractionDigits = 0
  38. if state.units == .mmolL {
  39. formatter.maximumFractionDigits = 1
  40. }
  41. formatter.roundingMode = .halfUp
  42. return formatter
  43. }
  44. var body: some View {
  45. NavigationView {
  46. Form {
  47. addProfile()
  48. }.scrollContentBackground(.hidden).background(color)
  49. .navigationTitle("Add Profile")
  50. .navigationBarItems(trailing: Button("Cancel") {
  51. presentationMode.wrappedValue.dismiss()
  52. })
  53. }
  54. }
  55. @ViewBuilder private func addProfile() -> some View {
  56. Section {
  57. VStack {
  58. TextField("Name", text: $state.profileName)
  59. }
  60. } header: {
  61. Text("Name")
  62. }.listRowBackground(Color.chart)
  63. Section {
  64. VStack {
  65. Spacer()
  66. Text("\(state.percentageProfiles.formatted(.number)) %")
  67. .foregroundColor(
  68. state
  69. .percentageProfiles >= 130 ? .red :
  70. (isEditing ? .orange : Color.tabBar)
  71. )
  72. .font(.largeTitle)
  73. Slider(
  74. value: $state.percentageProfiles,
  75. in: 10 ... 200,
  76. step: 1,
  77. onEditingChanged: { editing in
  78. isEditing = editing
  79. }
  80. )
  81. Spacer()
  82. Toggle(isOn: $state._indefinite) {
  83. Text("Enable indefinitely")
  84. }
  85. }
  86. if !state._indefinite {
  87. HStack {
  88. Text("Duration")
  89. TextFieldWithToolBar(text: $state.durationProfile, placeholder: "0", numberFormatter: formatter)
  90. Text("minutes").foregroundColor(.secondary)
  91. }
  92. }
  93. HStack {
  94. Toggle(isOn: $state.override_target) {
  95. Text("Override Profile Target")
  96. }
  97. }
  98. if state.override_target {
  99. HStack {
  100. Text("Target Glucose")
  101. TextFieldWithToolBar(text: $state.target, placeholder: "0", numberFormatter: glucoseFormatter)
  102. Text(state.units.rawValue).foregroundColor(.secondary)
  103. }
  104. }
  105. HStack {
  106. Toggle(isOn: $state.advancedSettings) {
  107. Text("More options")
  108. }
  109. }
  110. if state.advancedSettings {
  111. HStack {
  112. Toggle(isOn: $state.smbIsOff) {
  113. Text("Disable SMBs")
  114. }
  115. }
  116. HStack {
  117. Toggle(isOn: $state.smbIsAlwaysOff) {
  118. Text("Schedule when SMBs are Off")
  119. }.disabled(!state.smbIsOff)
  120. }
  121. if state.smbIsAlwaysOff {
  122. HStack {
  123. Text("First Hour SMBs are Off (24 hours)")
  124. TextFieldWithToolBar(text: $state.start, placeholder: "0", numberFormatter: formatter)
  125. Text("hour").foregroundColor(.secondary)
  126. }
  127. HStack {
  128. Text("Last Hour SMBs are Off (24 hours)")
  129. TextFieldWithToolBar(text: $state.end, placeholder: "0", numberFormatter: formatter)
  130. Text("hour").foregroundColor(.secondary)
  131. }
  132. }
  133. HStack {
  134. Toggle(isOn: $state.isfAndCr) {
  135. Text("Change ISF and CR")
  136. }
  137. }
  138. if !state.isfAndCr {
  139. HStack {
  140. Toggle(isOn: $state.isf) {
  141. Text("Change ISF")
  142. }
  143. }
  144. HStack {
  145. Toggle(isOn: $state.cr) {
  146. Text("Change CR")
  147. }
  148. }
  149. }
  150. HStack {
  151. Text("SMB Minutes")
  152. TextFieldWithToolBar(text: $state.smbMinutes, placeholder: "0", numberFormatter: formatter)
  153. Text("minutes").foregroundColor(.secondary)
  154. }
  155. HStack {
  156. Text("UAM SMB Minutes")
  157. TextFieldWithToolBar(text: $state.uamMinutes, placeholder: "0", numberFormatter: formatter)
  158. Text("minutes").foregroundColor(.secondary)
  159. }
  160. }
  161. startAndSaveProfiles
  162. }
  163. header: { Text("Add custom Override") }
  164. footer: {
  165. Text(
  166. "Your profile basal insulin will be adjusted with the override percentage and your profile ISF and CR will be inversly adjusted with the percentage."
  167. )
  168. }.listRowBackground(Color.chart)
  169. }
  170. private var startAndSaveProfiles: some View {
  171. HStack {
  172. Button("Start new Profile") {
  173. showAlert.toggle()
  174. alertString = "\(state.percentageProfiles.formatted(.number)) %, " +
  175. (
  176. state.durationProfile > 0 || !state
  177. ._indefinite ?
  178. (
  179. state
  180. .durationProfile
  181. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(0))) +
  182. " min."
  183. ) :
  184. NSLocalizedString(" infinite duration.", comment: "")
  185. ) +
  186. (
  187. (state.target == 0 || !state.override_target) ? "" :
  188. (" Target: " + state.target.formatted() + " " + state.units.rawValue + ".")
  189. )
  190. +
  191. (
  192. state
  193. .smbIsOff ?
  194. NSLocalizedString(
  195. " SMBs are disabled either by schedule or during the entire duration.",
  196. comment: ""
  197. ) : ""
  198. )
  199. +
  200. "\n\n"
  201. +
  202. NSLocalizedString(
  203. "Starting this override will change your Profiles and/or your Target Glucose used for looping during the entire selected duration. Tapping ”Start Profile” will start your new profile or edit your current active profile.",
  204. comment: ""
  205. )
  206. }
  207. .disabled(unChanged())
  208. .buttonStyle(BorderlessButtonStyle())
  209. .font(.callout)
  210. .controlSize(.mini)
  211. .alert(
  212. "Start Profile",
  213. isPresented: $showAlert,
  214. actions: {
  215. Button("Cancel", role: .cancel) { state.isEnabled = false }
  216. Button("Start Profile", role: .destructive) {
  217. Task {
  218. if state._indefinite { state.durationProfile = 0 }
  219. state.isEnabled.toggle()
  220. await state.saveAsProfile()
  221. await state.resetStateVariables()
  222. dismiss()
  223. }
  224. }
  225. },
  226. message: {
  227. Text(alertString)
  228. }
  229. )
  230. Button {
  231. Task {
  232. await state.savePreset()
  233. dismiss()
  234. }
  235. }
  236. label: { Text("Save as Profile") }
  237. .tint(.orange)
  238. .frame(maxWidth: .infinity, alignment: .trailing)
  239. .buttonStyle(BorderlessButtonStyle())
  240. .controlSize(.mini)
  241. .disabled(unChanged())
  242. }
  243. }
  244. private func unChanged() -> Bool {
  245. let isChanged = (
  246. state.percentageProfiles == 100 && !state.override_target && !state.smbIsOff && !state
  247. .advancedSettings
  248. ) ||
  249. (!state._indefinite && state.durationProfile == 0) || (state.override_target && state.target == 0) ||
  250. (
  251. state.percentageProfiles == 100 && !state.override_target && !state.smbIsOff && state.isf && state.cr && state
  252. .smbMinutes == state.defaultSmbMinutes && state.uamMinutes == state.defaultUamMinutes
  253. )
  254. return isChanged
  255. }
  256. }