PreferencesEditorRootView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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("FreeAPS X")) {
  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 Insulin Fraction")
  28. DecimalTextField("", value: $state.insulinReqFraction, formatter: formatter)
  29. }
  30. Toggle("Skip Bolus screen after carbs", isOn: $state.skipBolusScreenAfterCarbs)
  31. }
  32. ForEach(state.sections.indexed(), id: \.1.id) { sectionIndex, section in
  33. Section(header: Text(section.displayName)) {
  34. ForEach(section.fields.indexed(), id: \.1.id) { fieldIndex, field in
  35. HStack {
  36. switch field.type {
  37. case .boolean:
  38. ZStack {
  39. Button("", action: {
  40. infoButtonPressed = InfoText(
  41. description: field.infoText,
  42. oref0Variable: field.displayName
  43. )
  44. })
  45. Toggle(isOn: self.$state.sections[sectionIndex].fields[fieldIndex].boolValue) {
  46. Text(field.displayName)
  47. }
  48. }
  49. case .decimal:
  50. ZStack {
  51. Button("", action: {
  52. infoButtonPressed = InfoText(
  53. description: field.infoText,
  54. oref0Variable: field.displayName
  55. )
  56. })
  57. Text(field.displayName)
  58. }
  59. DecimalTextField(
  60. "0",
  61. value: self.$state.sections[sectionIndex].fields[fieldIndex].decimalValue,
  62. formatter: formatter
  63. )
  64. case .insulinCurve:
  65. Picker(
  66. selection: $state.sections[sectionIndex].fields[fieldIndex].insulinCurveValue,
  67. label: Text(field.displayName)
  68. ) {
  69. ForEach(InsulinCurve.allCases) { v in
  70. Text(v.rawValue).tag(v)
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. Section {
  79. Text("Edit settings json")
  80. .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.settings), from: self)
  81. }
  82. }
  83. .onAppear(perform: configureView)
  84. .navigationTitle("Preferences")
  85. .navigationBarTitleDisplayMode(.automatic)
  86. .alert(item: $infoButtonPressed) { infoButton in
  87. Alert(
  88. title: Text("\(infoButton.oref0Variable)"),
  89. message: Text("\(infoButton.description)"),
  90. dismissButton: .default(Text("OK"))
  91. )
  92. }
  93. }
  94. }
  95. }