PreferencesEditorRootView.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import SwiftUI
  2. import Swinject
  3. struct InfoText: Identifiable {
  4. var id: String { description }
  5. let description: String
  6. let oref0Variable: String
  7. }
  8. extension PreferencesEditor {
  9. struct RootView: BaseView {
  10. let resolver: Resolver
  11. @StateObject var state = StateModel()
  12. @Environment(\.colorScheme) var colorScheme
  13. var color: LinearGradient {
  14. colorScheme == .dark ? LinearGradient(
  15. gradient: Gradient(colors: [
  16. Color.bgDarkBlue,
  17. Color.bgDarkerDarkBlue
  18. ]),
  19. startPoint: .top,
  20. endPoint: .bottom
  21. )
  22. :
  23. LinearGradient(
  24. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  25. startPoint: .top,
  26. endPoint: .bottom
  27. )
  28. }
  29. private var formatter: NumberFormatter {
  30. let formatter = NumberFormatter()
  31. formatter.numberStyle = .decimal
  32. return formatter
  33. }
  34. @State private var infoButtonPressed: InfoText?
  35. var body: some View {
  36. Form {
  37. Section(header: Text("Trio").textCase(nil)) {
  38. Picker("Glucose units", selection: $state.unitsIndex) {
  39. Text("mg/dL").tag(0)
  40. Text("mmol/L").tag(1)
  41. }
  42. <<<<<<< HEAD
  43. =======
  44. HStack {
  45. Text("Recommended Bolus Percentage")
  46. TextFieldWithToolBar(text: $state.insulinReqPercentage, placeholder: "", numberFormatter: formatter)
  47. }
  48. Toggle("Skip Bolus screen after carbs", isOn: $state.skipBolusScreenAfterCarbs)
  49. >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133
  50. }
  51. ForEach(state.sections.indexed(), id: \.1.id) { sectionIndex, section in
  52. Section(header: Text(section.displayName)) {
  53. ForEach(section.fields.indexed(), id: \.1.id) { fieldIndex, field in
  54. HStack {
  55. switch field.type {
  56. case .boolean:
  57. ZStack {
  58. Button("", action: {
  59. infoButtonPressed = InfoText(
  60. description: field.infoText,
  61. oref0Variable: field.displayName
  62. )
  63. })
  64. Toggle(isOn: self.$state.sections[sectionIndex].fields[fieldIndex].boolValue) {
  65. Text(field.displayName)
  66. }
  67. }
  68. case .decimal:
  69. ZStack {
  70. Button("", action: {
  71. infoButtonPressed = InfoText(
  72. description: field.infoText,
  73. oref0Variable: field.displayName
  74. )
  75. })
  76. Text(field.displayName)
  77. }
  78. TextFieldWithToolBar(
  79. text: self.$state.sections[sectionIndex].fields[fieldIndex].decimalValue,
  80. placeholder: "0",
  81. numberFormatter: formatter
  82. )
  83. case .insulinCurve:
  84. Picker(
  85. selection: $state.sections[sectionIndex].fields[fieldIndex].insulinCurveValue,
  86. label: Text(field.displayName)
  87. ) {
  88. ForEach(InsulinCurve.allCases) { v in
  89. Text(v.rawValue).tag(v)
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. .scrollContentBackground(.hidden).background(color)
  99. .onAppear(perform: configureView)
  100. .navigationTitle("Preferences")
  101. .navigationBarTitleDisplayMode(.automatic)
  102. .navigationBarItems(
  103. trailing:
  104. Button {
  105. let lang = Locale.current.languageCode ?? "en"
  106. if lang == "en" {
  107. UIApplication.shared.open(
  108. URL(
  109. string: "https://openaps.readthedocs.io/en/latest/docs/While%20You%20Wait%20For%20Gear/preferences-and-safety-settings.html"
  110. )!,
  111. options: [:],
  112. completionHandler: nil
  113. )
  114. } else {
  115. UIApplication.shared.open(
  116. URL(
  117. string: "https://openaps-readthedocs-io.translate.goog/en/latest/docs/While%20You%20Wait%20For%20Gear/preferences-and-safety-settings.html?_x_tr_sl=en&_x_tr_tl=\(lang)&_x_tr_hl=\(lang)"
  118. )!,
  119. options: [:],
  120. completionHandler: nil
  121. )
  122. }
  123. }
  124. label: { Image(systemName: "questionmark.circle") }
  125. )
  126. .alert(item: $infoButtonPressed) { infoButton in
  127. Alert(
  128. title: Text("\(infoButton.oref0Variable)"),
  129. message: Text("\(infoButton.description)"),
  130. dismissButton: .default(Text("OK"))
  131. )
  132. }
  133. }
  134. }
  135. }