PreferencesEditorRootView.swift 5.0 KB

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