CalendarEventSettingsRootView.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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: """
  48. When enabled, Trio creates customizable calendar events in an iCloud calendar'
  49. Default: OFF
  50. """,
  51. verboseHint: VStack {
  52. Text("Default: OFF").bold()
  53. Text("""
  54. When enabled, Trio will create a calendar event with every successful loop cycle. The previous calendar event will be deleted.
  55. You can customize this with the calendar of your choosing, emojis, and IOB/COB.
  56. """)
  57. },
  58. headerText: "Diabetes Data as Calendar Event"
  59. )
  60. if state.calendarIDs.isNotEmpty, state.useCalendar {
  61. Section {
  62. VStack {
  63. Picker("Choose Calendar", selection: $state.currentCalendarID) {
  64. ForEach(state.calendarIDs, id: \.self) {
  65. Text($0).tag($0)
  66. }
  67. }
  68. }
  69. }.listRowBackground(Color.chart)
  70. SettingInputSection(
  71. decimalValue: $decimalPlaceholder,
  72. booleanValue: $state.displayCalendarEmojis,
  73. shouldDisplayHint: $shouldDisplayHint,
  74. selectedVerboseHint: Binding(
  75. get: { selectedVerboseHint },
  76. set: {
  77. selectedVerboseHint = $0.map { AnyView($0) }
  78. hintLabel = "Display Emojis as Labels"
  79. }
  80. ),
  81. units: state.units,
  82. type: .boolean,
  83. label: "Display Emojis as Labels",
  84. miniHint: """
  85. Enable to use emojis instead of "IOB" or "COB" and to indicate in-range and out-of-range glucose readings
  86. Default: OFF
  87. """,
  88. verboseHint: VStack {
  89. Text("Default: OFF").bold()
  90. Text("""
  91. When enabled, the calendar event created will indicate whether glucose readings are in-range or out-of-range using the following color emojis:
  92. 🟢: In-Range
  93. 🟠: Above-Range
  94. 🔴: Below-Range
  95. If "Display IOB and COB" is also enabled, "IOB" and "COB" will be replaced with the following emojis:
  96. 💉: IOB
  97. 🥨: COB
  98. """)
  99. }
  100. )
  101. SettingInputSection(
  102. decimalValue: $decimalPlaceholder,
  103. booleanValue: $state.displayCalendarIOBandCOB,
  104. shouldDisplayHint: $shouldDisplayHint,
  105. selectedVerboseHint: Binding(
  106. get: { selectedVerboseHint },
  107. set: {
  108. selectedVerboseHint = $0.map { AnyView($0) }
  109. hintLabel = "Display IOB and COB"
  110. }
  111. ),
  112. units: state.units,
  113. type: .boolean,
  114. label: "Display IOB and COB",
  115. miniHint: """
  116. Include IOB and COB in the calendar event created by Trio
  117. Default: OFF
  118. """,
  119. verboseHint: VStack {
  120. Text("Default: OFF").bold()
  121. Text("""
  122. When enabled, Trio will include the current IOB and COB values in the calendar event created.
  123. """)
  124. }
  125. )
  126. } else if state.useCalendar {
  127. if #available(iOS 17.0, *) {
  128. Text(
  129. "If you are not seeing calendars to choose here, please go to Settings -> Trio -> Calendars and change permissions to \"Full Access\""
  130. ).font(.footnote)
  131. Button("Open Settings") {
  132. // Get the settings URL and open it
  133. if let url = URL(string: UIApplication.openSettingsURLString) {
  134. UIApplication.shared.open(url)
  135. }
  136. }
  137. }
  138. }
  139. }
  140. .sheet(isPresented: $shouldDisplayHint) {
  141. SettingInputHintView(
  142. hintDetent: $hintDetent,
  143. shouldDisplayHint: $shouldDisplayHint,
  144. hintLabel: hintLabel ?? "",
  145. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  146. sheetTitle: "Help"
  147. )
  148. }
  149. .scrollContentBackground(.hidden).background(color)
  150. .onAppear(perform: configureView)
  151. .navigationTitle("Calendar Events")
  152. .navigationBarTitleDisplayMode(.automatic)
  153. }
  154. }
  155. }