DynamicRootView.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import SwiftUI
  2. import Swinject
  3. extension Dynamic {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. private var conversionFormatter: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.numberStyle = .decimal
  10. formatter.maximumFractionDigits = 1
  11. return formatter
  12. }
  13. @Environment(\.colorScheme) var colorScheme
  14. var color: LinearGradient {
  15. colorScheme == .dark ? LinearGradient(
  16. gradient: Gradient(colors: [
  17. Color.bgDarkBlue,
  18. Color.bgDarkerDarkBlue
  19. ]),
  20. startPoint: .top,
  21. endPoint: .bottom
  22. )
  23. :
  24. LinearGradient(
  25. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  26. startPoint: .top,
  27. endPoint: .bottom
  28. )
  29. }
  30. private var formatter: NumberFormatter {
  31. let formatter = NumberFormatter()
  32. formatter.numberStyle = .decimal
  33. return formatter
  34. }
  35. private var glucoseFormatter: NumberFormatter {
  36. let formatter = NumberFormatter()
  37. formatter.numberStyle = .decimal
  38. if state.unit == .mmolL {
  39. formatter.maximumFractionDigits = 1
  40. } else { formatter.maximumFractionDigits = 0 }
  41. formatter.roundingMode = .halfUp
  42. return formatter
  43. }
  44. var body: some View {
  45. Form {
  46. Section {
  47. HStack {
  48. Toggle("Activate Dynamic Sensitivity (ISF)", isOn: $state.useNewFormula)
  49. }
  50. if state.useNewFormula {
  51. HStack {
  52. Toggle("Activate Dynamic Carb Ratio (CR)", isOn: $state.enableDynamicCR)
  53. }
  54. }
  55. } header: { Text("Enable") }
  56. if state.useNewFormula {
  57. Section {
  58. HStack {
  59. Toggle("Use Sigmoid Formula", isOn: $state.sigmoid)
  60. }
  61. } header: { Text("Formula") }
  62. Section {
  63. HStack {
  64. Text("Adjustment Factor")
  65. Spacer()
  66. DecimalTextField("0", value: $state.adjustmentFactor, formatter: formatter)
  67. }
  68. HStack {
  69. Text("Weighted Average of TDD. Weight of past 24 hours:")
  70. Spacer()
  71. DecimalTextField("0", value: $state.weightPercentage, formatter: formatter)
  72. }
  73. HStack {
  74. Toggle("Adjust basal", isOn: $state.tddAdjBasal)
  75. }
  76. } header: { Text("Settings") }
  77. Section {
  78. HStack {
  79. Text("Threshold Setting")
  80. Spacer()
  81. DecimalTextField("0", value: $state.threshold_setting, formatter: glucoseFormatter)
  82. Text(state.unit.rawValue)
  83. }
  84. } header: { Text("Safety") }
  85. }
  86. }
  87. .scrollContentBackground(.hidden).background(color)
  88. .onAppear(perform: configureView)
  89. .navigationBarTitle("Dynamic ISF")
  90. .navigationBarTitleDisplayMode(.automatic)
  91. .onDisappear {
  92. state.saveIfChanged()
  93. }
  94. }
  95. }
  96. }