PreferencesEditorRootView.swift 5.6 KB

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