PreferencesEditorRootView.swift 5.4 KB

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