PreferencesEditorRootView.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Section(header: Text("OpenAPS")) {
  33. Picker(selection: $state.insulinCurveField.value, label: Text(state.insulinCurveField.displayName)) {
  34. ForEach(InsulinCurve.allCases) { v in
  35. Text(v.rawValue).tag(v)
  36. }
  37. }
  38. ForEach(state.boolFields.indexed(), id: \.1.id) { index, field in
  39. HStack {
  40. Button("", action: {
  41. infoButtonPressed = InfoText(description: field.infoText, oref0Variable: field.displayName)
  42. })
  43. Toggle(field.displayName, isOn: self.$state.boolFields[index].value)
  44. }
  45. }
  46. ForEach(state.decimalFields.indexed(), id: \.1.id) { index, field in
  47. HStack {
  48. Button("", action: {
  49. infoButtonPressed = InfoText(description: field.infoText, oref0Variable: field.displayName)
  50. })
  51. Text(field.displayName)
  52. DecimalTextField("0", value: self.$state.decimalFields[index].value, formatter: formatter)
  53. }
  54. }
  55. }
  56. Section {
  57. Text("Edit settings json")
  58. .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.settings), from: self)
  59. }
  60. }
  61. .onAppear(perform: configureView)
  62. .navigationTitle("Preferences")
  63. .navigationBarTitleDisplayMode(.automatic)
  64. .alert(item: $infoButtonPressed) { infoButton in
  65. Alert(
  66. title: Text("\(infoButton.oref0Variable)"),
  67. message: Text("\(infoButton.description)"),
  68. dismissButton: .default(Text("OK"))
  69. )
  70. }
  71. }
  72. }
  73. }