MealSettingsRootView.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. import SwiftUI
  2. import Swinject
  3. extension MealSettings {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. @State private var shouldDisplayHint: Bool = false
  8. @State var hintDetent = PresentationDetent.large
  9. @State var selectedVerboseHint: AnyView?
  10. @State var hintLabel: String?
  11. @State private var decimalPlaceholder: Decimal = 0.0
  12. @State private var booleanPlaceholder: Bool = false
  13. @State private var displayPickerMaxCarbs: Bool = false
  14. @State private var displayPickerMaxFat: Bool = false
  15. @State private var displayPickerMaxProtein: Bool = false
  16. @Environment(\.colorScheme) var colorScheme
  17. @Environment(AppState.self) var appState
  18. private var conversionFormatter: NumberFormatter {
  19. let formatter = NumberFormatter()
  20. formatter.numberStyle = .decimal
  21. formatter.maximumFractionDigits = 1
  22. return formatter
  23. }
  24. private var intFormater: NumberFormatter {
  25. let formatter = NumberFormatter()
  26. formatter.allowsFloats = false
  27. return formatter
  28. }
  29. private var formatter: NumberFormatter {
  30. let formatter = NumberFormatter()
  31. formatter.numberStyle = .decimal
  32. return formatter
  33. }
  34. var body: some View {
  35. List {
  36. Section(
  37. header: Text("Limits per Entry"),
  38. content: {
  39. VStack {
  40. VStack {
  41. HStack {
  42. Text("Max Carbs")
  43. Spacer()
  44. Group {
  45. Text(state.maxCarbs.description)
  46. .foregroundColor(!displayPickerMaxCarbs ? .primary : .accentColor)
  47. Text(" g").foregroundColor(.secondary)
  48. }
  49. }
  50. .onTapGesture {
  51. displayPickerMaxCarbs.toggle()
  52. }
  53. }.padding(.top)
  54. if displayPickerMaxCarbs {
  55. let setting = PickerSettingsProvider.shared.settings.maxCarbs
  56. Picker(selection: $state.maxCarbs, label: Text("")) {
  57. ForEach(
  58. PickerSettingsProvider.shared.generatePickerValues(from: setting, units: state.units),
  59. id: \.self
  60. ) { value in
  61. Text("\(value.description)").tag(value)
  62. }
  63. }
  64. .pickerStyle(WheelPickerStyle())
  65. .frame(maxWidth: .infinity)
  66. }
  67. if state.useFPUconversion {
  68. VStack {
  69. HStack {
  70. Text("Max Fat")
  71. Spacer()
  72. Group {
  73. Text(state.maxFat.description)
  74. .foregroundColor(!displayPickerMaxFat ? .primary : .accentColor)
  75. Text(" g").foregroundColor(.secondary)
  76. }
  77. }
  78. .onTapGesture {
  79. displayPickerMaxFat.toggle()
  80. }
  81. }
  82. .padding(.top)
  83. if displayPickerMaxFat {
  84. let setting = PickerSettingsProvider.shared.settings.maxFat
  85. Picker(selection: $state.maxFat, label: Text("")) {
  86. ForEach(
  87. PickerSettingsProvider.shared.generatePickerValues(from: setting, units: state.units),
  88. id: \.self
  89. ) { value in
  90. Text("\(value.description)").tag(value)
  91. }
  92. }
  93. .pickerStyle(WheelPickerStyle())
  94. .frame(maxWidth: .infinity)
  95. }
  96. VStack {
  97. HStack {
  98. Text("Max Protein")
  99. Spacer()
  100. Group {
  101. Text(state.maxProtein.description)
  102. .foregroundColor(!displayPickerMaxProtein ? .primary : .accentColor)
  103. Text(" g").foregroundColor(.secondary)
  104. }
  105. }
  106. .onTapGesture {
  107. displayPickerMaxProtein.toggle()
  108. }
  109. }
  110. .padding(.top)
  111. if displayPickerMaxProtein {
  112. let setting = PickerSettingsProvider.shared.settings.maxProtein
  113. Picker(selection: $state.maxProtein, label: Text("")) {
  114. ForEach(
  115. PickerSettingsProvider.shared.generatePickerValues(from: setting, units: state.units),
  116. id: \.self
  117. ) { value in
  118. Text("\(value.description)").tag(value)
  119. }
  120. }
  121. .pickerStyle(WheelPickerStyle())
  122. .frame(maxWidth: .infinity)
  123. }
  124. }
  125. HStack(alignment: .center) {
  126. Text(
  127. "Set limits for each type of macro per meal entry."
  128. )
  129. .lineLimit(nil)
  130. .font(.footnote)
  131. .foregroundColor(.secondary)
  132. Spacer()
  133. Button(
  134. action: {
  135. hintLabel = String(localized: "Limits per Entry")
  136. selectedVerboseHint =
  137. AnyView(
  138. VStack(alignment: .leading, spacing: 5) {
  139. Text("Max Carbs:").bold()
  140. Text("Enter the largest carb value allowed per meal entry")
  141. Text("Max Fat:").bold()
  142. Text("Enter the largest fat value allowed per meal entry")
  143. Text("Max Protein:").bold()
  144. Text("Enter the largest protein value allowed per meal entry")
  145. }
  146. )
  147. shouldDisplayHint.toggle()
  148. },
  149. label: {
  150. HStack {
  151. Image(systemName: "questionmark.circle")
  152. }
  153. }
  154. ).buttonStyle(BorderlessButtonStyle())
  155. }.padding(.top)
  156. }.padding(.bottom)
  157. }
  158. ).listRowBackground(Color.chart)
  159. SettingInputSection(
  160. decimalValue: $state.maxMealAbsorptionTime,
  161. booleanValue: $booleanPlaceholder,
  162. shouldDisplayHint: $shouldDisplayHint,
  163. selectedVerboseHint: Binding(
  164. get: { selectedVerboseHint },
  165. set: {
  166. selectedVerboseHint = $0.map { AnyView($0) }
  167. hintLabel = String(localized: "Maximum Meal Absorption Time")
  168. }
  169. ),
  170. units: state.units,
  171. type: .decimal("maxMealAbsorptionTime"),
  172. label: String(localized: "Max Meal Absorption Time"),
  173. miniHint: String(
  174. localized: "The maximum duration for tracking carb entries in estimating Carbs on Board (COB)"
  175. ),
  176. verboseHint:
  177. VStack(alignment: .leading, spacing: 10) {
  178. Text("Default: 6 hours").bold()
  179. Text(
  180. "Carb entries will be fully decayed by the number of hours specified as Max Meal Absorption Time. Meals that are high in fat and/or protein can have long lasting effects on glucose levels. To allow such late meal effects to be considered by the carb decay model, a longer Max Meal Absorption Time than the default 6 hours can be set."
  181. )
  182. Text(
  183. "If carb entries decay too slowly, it is possible to set a lower than default setting. But this should typically be adressed by tuning ISF and CR settings instead, which in combination determines the rate of carb decay."
  184. )
  185. Text(
  186. "Min 4 hours, max 10 hours."
  187. )
  188. }
  189. )
  190. SettingInputSection(
  191. decimalValue: $decimalPlaceholder,
  192. booleanValue: $state.useFPUconversion,
  193. shouldDisplayHint: $shouldDisplayHint,
  194. selectedVerboseHint: Binding(
  195. get: { selectedVerboseHint },
  196. set: {
  197. selectedVerboseHint = $0.map { AnyView($0) }
  198. hintLabel = String(localized: "Enable Fat and Protein Entries")
  199. }
  200. ),
  201. units: state.units,
  202. type: .boolean,
  203. label: String(localized: "Enable Fat and Protein Entries"),
  204. miniHint: String(localized: "Add fat and protein macros to meal entries."),
  205. verboseHint: VStack(alignment: .leading, spacing: 10) {
  206. Text("Default: OFF").bold()
  207. VStack(spacing: 10) {
  208. Text(
  209. "Enabling this setting allows you to log fat and protein, which are then converted into future carb equivalents using the Warsaw Method."
  210. )
  211. VStack(alignment: .leading, spacing: 5) {
  212. Text("Warsaw Method:").bold()
  213. Text(
  214. "The Warsaw Method helps account for the delayed glucose spikes caused by fat and protein in meals. It uses Fat-Protein Units (FPU) to calculate the carb effect from fat and protein. The system spreads insulin delivery over several hours to mimic natural insulin release, helping to manage post-meal glucose spikes."
  215. )
  216. }
  217. VStack(alignment: .center, spacing: 5) {
  218. Text("Fat Conversion").bold()
  219. Text("𝑭 = fat(g) × 90%")
  220. }
  221. VStack(alignment: .center, spacing: 5) {
  222. Text("Protein Conversion").bold()
  223. Text("𝑷 = protein(g) × 40%")
  224. }
  225. VStack(alignment: .center, spacing: 5) {
  226. Text("FPU Conversion").bold()
  227. Text("𝑭 + 𝑷 = g CHO")
  228. }
  229. VStack(alignment: .leading, spacing: 5) {
  230. Text(
  231. "You can personalize the conversion calculation by adjusting the following settings that will appear when this option is enabled:"
  232. )
  233. Text("• Fat and Protein Delay")
  234. Text("• Spread Interval")
  235. Text("• Fat and Protein Percentage")
  236. }
  237. }
  238. },
  239. headerText: String(localized: "Fat and Protein")
  240. )
  241. if state.useFPUconversion {
  242. SettingInputSection(
  243. decimalValue: $state.delay,
  244. booleanValue: $booleanPlaceholder,
  245. shouldDisplayHint: $shouldDisplayHint,
  246. selectedVerboseHint: Binding(
  247. get: { selectedVerboseHint },
  248. set: {
  249. selectedVerboseHint = $0.map { AnyView($0) }
  250. hintLabel = String(localized: "Fat and Protein Delay")
  251. }
  252. ),
  253. units: state.units,
  254. type: .decimal("delay"),
  255. label: String(localized: "Fat and Protein Delay"),
  256. miniHint: String(localized: "Delay between fat & protein entry and first FPU entry."),
  257. verboseHint:
  258. VStack(alignment: .leading, spacing: 10) {
  259. Text("Default: 60 min").bold()
  260. Text(
  261. "The Fat and Protein Delay setting defines the time between when you log fat and protein and when the system starts delivering insulin for the Fat-Protein Unit Carb Equivalents (FPUs)."
  262. )
  263. Text(
  264. "This delay accounts for the slower absorption of fat and protein, as calculated by the Warsaw Method, ensuring insulin delivery is properly timed to manage glucose spikes caused by high-fat, high-protein meals."
  265. )
  266. }
  267. )
  268. SettingInputSection(
  269. decimalValue: $state.minuteInterval,
  270. booleanValue: $booleanPlaceholder,
  271. shouldDisplayHint: $shouldDisplayHint,
  272. selectedVerboseHint: Binding(
  273. get: { selectedVerboseHint },
  274. set: {
  275. selectedVerboseHint = $0.map { AnyView($0) }
  276. hintLabel = String(localized: "Spread Interval")
  277. }
  278. ),
  279. units: state.units,
  280. type: .decimal("minuteInterval"),
  281. label: String(localized: "Spread Interval"),
  282. miniHint: String(localized: "Time interval between FPUs."),
  283. verboseHint:
  284. VStack(alignment: .leading, spacing: 10) {
  285. Text("Default: 30 minutes").bold()
  286. Text(
  287. "This determines how many minutes will be between individual Fat-Protein Unit Carb Equivalent (FPU) entries from a single Fat and/or Protein bolus calculator entry."
  288. )
  289. Text(
  290. "Entries are capped at 33 grams each, with up to three entries, for a max total of 99 grams."
  291. )
  292. }
  293. )
  294. SettingInputSection(
  295. decimalValue: $state.individualAdjustmentFactor,
  296. booleanValue: $booleanPlaceholder,
  297. shouldDisplayHint: $shouldDisplayHint,
  298. selectedVerboseHint: Binding(
  299. get: { selectedVerboseHint },
  300. set: {
  301. selectedVerboseHint = $0.map { AnyView($0) }
  302. hintLabel = String(localized: "Fat and Protein Percentage")
  303. }
  304. ),
  305. units: state.units,
  306. type: .decimal("individualAdjustmentFactor"),
  307. label: String(localized: "Fat and Protein Percentage"),
  308. miniHint: String(localized: "Adjust the Warsaw Method FPU Conversion rate."),
  309. verboseHint: VStack(alignment: .leading, spacing: 10) {
  310. Text("Default: 50%").bold()
  311. VStack(spacing: 10) {
  312. Text("This setting changes how much effect the fat and protein entry has on FPUs.")
  313. VStack(alignment: .center, spacing: 5) {
  314. Text("50% is half effect:").bold()
  315. Text("(Fat × 45%) + (Protein × 20%)")
  316. Text("100% is full effect:").bold()
  317. Text("(Fat × 90%) + (Protein × 40%)")
  318. Text("110% makes fat-to-carbs ratio essentially equal:").bold()
  319. Text("(Fat × 99%) + (Protein x 44%)")
  320. }
  321. .multilineTextAlignment(.center)
  322. .fixedSize(horizontal: false, vertical: true)
  323. Text(
  324. "Tip: You may find that your normal carb ratio needs to increase to a larger number when you begin adding fat and protein entries. For this reason, it is best to start with a factor of about 50%."
  325. )
  326. }
  327. }
  328. )
  329. }
  330. }
  331. .listSectionSpacing(sectionSpacing)
  332. .sheet(isPresented: $shouldDisplayHint) {
  333. SettingInputHintView(
  334. hintDetent: $hintDetent,
  335. shouldDisplayHint: $shouldDisplayHint,
  336. hintLabel: hintLabel ?? "",
  337. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  338. sheetTitle: String(localized: "Help", comment: "Help sheet title")
  339. )
  340. }
  341. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  342. .onAppear(perform: configureView)
  343. .navigationBarTitle("Meal Settings")
  344. .navigationBarTitleDisplayMode(.automatic)
  345. }
  346. }
  347. }