PreferencesEditorRootView.swift 5.1 KB

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