CalendarEventSettingsRootView.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import SwiftUI
  2. import Swinject
  3. extension CalendarEventSettings {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. @State private var shouldDisplayHint: Bool = false
  8. @State var hintDetent = PresentationDetent.large
  9. @State var selectedVerboseHint: AnyView?
  10. @State var hintLabel: String?
  11. @State private var decimalPlaceholder: Decimal = 0.0
  12. @State private var booleanPlaceholder: Bool = false
  13. @Environment(\.colorScheme) var colorScheme
  14. @EnvironmentObject var appIcons: Icons
  15. private var color: LinearGradient {
  16. colorScheme == .dark ? LinearGradient(
  17. gradient: Gradient(colors: [
  18. Color.bgDarkBlue,
  19. Color.bgDarkerDarkBlue
  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. var body: some View {
  32. List {
  33. SettingInputSection(
  34. decimalValue: $decimalPlaceholder,
  35. booleanValue: $state.useCalendar,
  36. shouldDisplayHint: $shouldDisplayHint,
  37. selectedVerboseHint: Binding(
  38. get: { selectedVerboseHint },
  39. set: {
  40. selectedVerboseHint = $0.map { AnyView($0) }
  41. hintLabel = "Create Events in Calendar"
  42. }
  43. ),
  44. units: state.units,
  45. type: .boolean,
  46. label: "Create Events in Calendar",
  47. miniHint: "Uses calendar events to display current data.",
  48. verboseHint:
  49. VStack(alignment: .leading, spacing: 10) {
  50. Text("Default: OFF").bold()
  51. Text(
  52. "When enabled, Trio will create a customizable calendar event to keep you notified of your current glucose reading with every successful loop cycle."
  53. )
  54. Text(
  55. "This is useful if you use CarPlay or a variety of other external services that limit the view of most apps, but allows the calendar app"
  56. )
  57. Text(
  58. "Once enabled, the available customizations will appear. You can customize with the calendar of your choosing, use of emoji labels, and the inclusion of IOB & COB data."
  59. )
  60. Text("Note: Once a new calendar event is created, the previous event will be deleted.")
  61. },
  62. headerText: "Diabetes Data as Calendar Event"
  63. )
  64. if state.calendarIDs.isNotEmpty, state.useCalendar {
  65. Section {
  66. VStack {
  67. Picker("Choose Calendar", selection: $state.currentCalendarID) {
  68. ForEach(state.calendarIDs, id: \.self) {
  69. Text($0).tag($0)
  70. }
  71. }
  72. }
  73. }.listRowBackground(Color.chart)
  74. SettingInputSection(
  75. decimalValue: $decimalPlaceholder,
  76. booleanValue: $state.displayCalendarEmojis,
  77. shouldDisplayHint: $shouldDisplayHint,
  78. selectedVerboseHint: Binding(
  79. get: { selectedVerboseHint },
  80. set: {
  81. selectedVerboseHint = $0.map { AnyView($0) }
  82. hintLabel = "Display Emojis as Labels"
  83. }
  84. ),
  85. units: state.units,
  86. type: .boolean,
  87. label: "Display Emojis as Labels",
  88. miniHint: "Emojis used instead of text for data labels.",
  89. verboseHint: VStack(alignment: .leading, spacing: 10) {
  90. Text("Default: OFF").bold()
  91. VStack(alignment: .leading, spacing: 5) {
  92. Text(
  93. "When enabled, the calendar event created will indicate whether glucose readings are in-range or out-of-range using the following color emojis:"
  94. )
  95. Text("🟢: In-Range")
  96. Text("🟠: Above-Range")
  97. Text("🔴: Below-Range")
  98. }
  99. VStack(alignment: .leading, spacing: 5) {
  100. Text(
  101. "If \"Display IOB and COB\" is also enabled, \"IOB\" and \"COB\" will be replaced with the following emojis:"
  102. )
  103. Text("💉: IOB")
  104. Text("🥨: COB")
  105. }
  106. }
  107. )
  108. SettingInputSection(
  109. decimalValue: $decimalPlaceholder,
  110. booleanValue: $state.displayCalendarIOBandCOB,
  111. shouldDisplayHint: $shouldDisplayHint,
  112. selectedVerboseHint: Binding(
  113. get: { selectedVerboseHint },
  114. set: {
  115. selectedVerboseHint = $0.map { AnyView($0) }
  116. hintLabel = "Display IOB and COB"
  117. }
  118. ),
  119. units: state.units,
  120. type: .boolean,
  121. label: "Display IOB and COB",
  122. miniHint: "Include IOB & COB in the calendar event data.",
  123. verboseHint: VStack(alignment: .leading, spacing: 10) {
  124. Text("Default: OFF").bold()
  125. Text(
  126. "When enabled, Trio will include the current IOB and COB values, along with the current glucose reading, in each calendar event created."
  127. )
  128. }
  129. )
  130. } else if state.useCalendar {
  131. if #available(iOS 17.0, *) {
  132. Text(
  133. "If you are not seeing calendars to choose here, please go to Settings -> Trio -> Calendars and change permissions to \"Full Access\""
  134. ).font(.footnote)
  135. Button("Open Settings") {
  136. // Get the settings URL and open it
  137. if let url = URL(string: UIApplication.openSettingsURLString) {
  138. UIApplication.shared.open(url)
  139. }
  140. }
  141. }
  142. }
  143. }
  144. .listSectionSpacing(sectionSpacing)
  145. .sheet(isPresented: $shouldDisplayHint) {
  146. SettingInputHintView(
  147. hintDetent: $hintDetent,
  148. shouldDisplayHint: $shouldDisplayHint,
  149. hintLabel: hintLabel ?? "",
  150. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  151. sheetTitle: "Help"
  152. )
  153. }
  154. .scrollContentBackground(.hidden).background(color)
  155. .onAppear(perform: configureView)
  156. .navigationTitle("Calendar Events")
  157. .navigationBarTitleDisplayMode(.automatic)
  158. }
  159. }
  160. }