PreferencesEditorRootView.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. private var formatter: NumberFormatter {
  13. let formatter = NumberFormatter()
  14. formatter.numberStyle = .decimal
  15. return formatter
  16. }
  17. @State private var infoButtonPressed: InfoText?
  18. var body: some View {
  19. Form {
  20. Section(header: Text("Trio").textCase(nil)) {
  21. Picker("Glucose units", selection: $state.unitsIndex) {
  22. Text("mg/dL").tag(0)
  23. Text("mmol/L").tag(1)
  24. }
  25. HStack {
  26. Text("Recommended Bolus Percentage")
  27. DecimalTextField("", value: $state.insulinReqPercentage, formatter: formatter)
  28. }
  29. Toggle("Skip Bolus screen after carbs", isOn: $state.skipBolusScreenAfterCarbs)
  30. }
  31. ForEach(state.sections.indexed(), id: \.1.id) { sectionIndex, section in
  32. Section(header: Text(section.displayName)) {
  33. ForEach(section.fields.indexed(), id: \.1.id) { fieldIndex, field in
  34. HStack {
  35. switch field.type {
  36. case .boolean:
  37. ZStack {
  38. Button("", action: {
  39. infoButtonPressed = InfoText(
  40. description: field.infoText,
  41. oref0Variable: field.displayName
  42. )
  43. })
  44. Toggle(isOn: self.$state.sections[sectionIndex].fields[fieldIndex].boolValue) {
  45. Text(field.displayName)
  46. }
  47. }
  48. case .decimal:
  49. ZStack {
  50. Button("", action: {
  51. infoButtonPressed = InfoText(
  52. description: field.infoText,
  53. oref0Variable: field.displayName
  54. )
  55. })
  56. Text(field.displayName)
  57. }
  58. DecimalTextField(
  59. "0",
  60. value: self.$state.sections[sectionIndex].fields[fieldIndex].decimalValue,
  61. formatter: formatter
  62. )
  63. case .insulinCurve:
  64. Picker(
  65. selection: $state.sections[sectionIndex].fields[fieldIndex].insulinCurveValue,
  66. label: Text(field.displayName)
  67. ) {
  68. ForEach(InsulinCurve.allCases) { v in
  69. Text(v.rawValue).tag(v)
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. .onAppear(perform: configureView)
  79. .navigationTitle("Preferences")
  80. .navigationBarTitleDisplayMode(.automatic)
  81. .navigationBarItems(
  82. trailing:
  83. Button {
  84. let lang = Locale.current.languageCode ?? "en"
  85. if lang == "en" {
  86. UIApplication.shared.open(
  87. URL(
  88. string: "https://openaps.readthedocs.io/en/latest/docs/While%20You%20Wait%20For%20Gear/preferences-and-safety-settings.html"
  89. )!,
  90. options: [:],
  91. completionHandler: nil
  92. )
  93. } else {
  94. UIApplication.shared.open(
  95. URL(
  96. 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)"
  97. )!,
  98. options: [:],
  99. completionHandler: nil
  100. )
  101. }
  102. }
  103. label: { Image(systemName: "questionmark.circle") }
  104. )
  105. .alert(item: $infoButtonPressed) { infoButton in
  106. Alert(
  107. title: Text("\(infoButton.oref0Variable)"),
  108. message: Text("\(infoButton.description)"),
  109. dismissButton: .default(Text("OK"))
  110. )
  111. }
  112. }
  113. }
  114. }