PreferencesEditorRootView.swift 5.7 KB

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