PreferencesEditorRootView.swift 5.8 KB

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