OverrideProfilesRootView.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import CoreData
  2. import SwiftUI
  3. import Swinject
  4. extension OverrideProfilesConfig {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @StateObject var state = StateModel()
  8. @State private var isEditing = false
  9. @State private var showAlert = false
  10. @State private var showingDetail = false
  11. @State private var isPresented = true
  12. @Environment(\.dismiss) var dismiss
  13. private var formatter: NumberFormatter {
  14. let formatter = NumberFormatter()
  15. formatter.numberStyle = .decimal
  16. formatter.maximumFractionDigits = 0
  17. return formatter
  18. }
  19. var body: some View {
  20. Form {
  21. Section(
  22. header: Text("Override your Basal, ISF and CR profiles"),
  23. footer: Text("" + (!state.isEnabled ? "Currently no Override active" : ""))
  24. ) {
  25. Toggle(isOn: $state.isEnabled) {
  26. Text("Override Profiles")
  27. }._onBindingChange($state.isEnabled, perform: { _ in
  28. if !state.isEnabled {
  29. state.duration = 0
  30. state.percentage = 100
  31. state._indefinite = false
  32. state.saveSettings()
  33. }
  34. })
  35. }
  36. if state.isEnabled {
  37. Section(
  38. header: Text("Total Insulin Adjustment"),
  39. footer: Text(
  40. "Your profile basal insulin will be adjusted with the override percentage and your profile ISF and CR will be inversly adjusted with the percentage.\n\nIf you toggle off the override every profile setting will return to normal."
  41. )
  42. ) {
  43. VStack {
  44. Slider(
  45. value: $state.percentage,
  46. in: 10 ... 200,
  47. step: 1,
  48. onEditingChanged: { editing in
  49. isEditing = editing
  50. }
  51. ).accentColor(state.percentage >= 130 ? .red : .blue)
  52. Text("\(state.percentage.formatted(.number)) %")
  53. .foregroundColor(
  54. state
  55. .percentage >= 130 ? .red :
  56. (isEditing ? .orange : .blue)
  57. )
  58. .font(.largeTitle)
  59. Spacer()
  60. Toggle(isOn: $state._indefinite) {
  61. Text("Enable indefinitely")
  62. }
  63. }
  64. if !state._indefinite {
  65. HStack {
  66. Text("Duration")
  67. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: false)
  68. Text("minutes").foregroundColor(.secondary)
  69. }
  70. }
  71. Button("Save") {
  72. showAlert.toggle()
  73. }
  74. .disabled(
  75. state.isEnabled == false || state
  76. .percentage == 100 || (!state._indefinite && state.duration == 0)
  77. )
  78. .accentColor(.orange)
  79. .buttonStyle(BorderlessButtonStyle())
  80. .font(.callout)
  81. .frame(maxWidth: .infinity, alignment: .center)
  82. .controlSize(.mini)
  83. .alert(
  84. "Selected Override:\n\n\(state.percentage.formatted(.number)) %, " +
  85. (state.duration > 0 ? "\(state.duration) min" : " infinite duration.") + "\n\n" +
  86. "Saving this override will change your basal insulin, ISF and CR during the entire selected duration. Tapping save will start your new overide or edit your current active override.",
  87. isPresented: $showAlert,
  88. actions: {
  89. Button("Cancel", role: .cancel) {}
  90. Button("Start Override", role: .destructive) {
  91. if state.percentage == 100 {
  92. state.isEnabled = false
  93. } else { state.isEnabled = true }
  94. if state._indefinite {
  95. state.duration = 0
  96. } else if state.duration == 0 {
  97. state.isEnabled = false
  98. }
  99. state.saveSettings()
  100. dismiss()
  101. }
  102. }
  103. )
  104. }
  105. }
  106. }.onAppear { state.savedSettings() }
  107. }
  108. }
  109. }