NotificationsView.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // FeatureSettingsView.swift
  3. // Trio
  4. //
  5. // Created by Deniz Cengiz on 26.07.24.
  6. //
  7. import Foundation
  8. import LoopKitUI
  9. import SwiftUI
  10. import Swinject
  11. struct NotificationsView: BaseView {
  12. let resolver: Resolver
  13. @ObservedObject var state: Settings.StateModel
  14. @State var notificationsDisabled = false
  15. @State var showAlert = false
  16. @State private var showSnoozeSheet = false
  17. @State private var shouldDisplayHint: Bool = false
  18. @State var hintDetent = PresentationDetent.large
  19. @State var selectedVerboseHint: AnyView? = AnyView(
  20. VStack(alignment: .leading, spacing: 10) {
  21. Text("Notifications give you important Trio information without requiring you to open the app.")
  22. Text(
  23. "Keep these turned ON in your phone’s settings to ensure you receive Trio Notifications, Critical Alerts, and Time Sensitive Notifications."
  24. )
  25. }
  26. )
  27. @State var hintLabel: String? = String(localized: "Manage iOS Preferences")
  28. @Environment(\.colorScheme) var colorScheme
  29. @Environment(AppState.self) var appState
  30. var body: some View {
  31. List {
  32. Section(
  33. header: Text("Mute Trio Alarms"),
  34. content: {
  35. VStack {
  36. Button {
  37. showSnoozeSheet = true
  38. } label: {
  39. HStack {
  40. Image(systemName: "zzz").foregroundStyle(.tint)
  41. Text("Snooze All Alarms").bold()
  42. }
  43. .font(.title3)
  44. }
  45. .frame(maxWidth: .infinity, alignment: .center)
  46. .buttonStyle(.bordered)
  47. }.padding(.vertical)
  48. }
  49. ).listRowBackground(Color.chart)
  50. Section(
  51. header: Text("Manage iOS Preferences"),
  52. content: {
  53. notificationsEnabledStatus
  54. manageNotifications
  55. }
  56. ).listRowBackground(Color.chart)
  57. Section(
  58. header: Text("Notification Center"),
  59. content: {
  60. Text("Glucose Alarms")
  61. .navigationLink(to: .glucoseAlerts, from: self)
  62. Text("Device Alarms")
  63. .navigationLink(to: .deviceAlarms, from: self)
  64. Text("Day & Night Windows")
  65. .navigationLink(to: .alarmWindows, from: self)
  66. }
  67. ).listRowBackground(Color.chart)
  68. Section(
  69. header: Text("Other Notifications"),
  70. content: {
  71. if #available(iOS 16.2, *) {
  72. Text("Live Activity").navigationLink(to: .liveActivitySettings, from: self)
  73. }
  74. Text("Calendar Events").navigationLink(to: .calendarEventSettings, from: self)
  75. }
  76. ).listRowBackground(Color.chart)
  77. }
  78. .listSectionSpacing(sectionSpacing)
  79. .onReceive(
  80. resolver.resolve(AlertPermissionsChecker.self)!.$notificationsDisabled,
  81. perform: {
  82. if notificationsDisabled != $0 {
  83. notificationsDisabled = $0
  84. if notificationsDisabled {
  85. showAlert = true
  86. }
  87. }
  88. }
  89. )
  90. .alert(
  91. isPresented: self.$showAlert,
  92. content: { self.notificationReminder() }
  93. )
  94. .sheet(isPresented: $shouldDisplayHint) {
  95. SettingInputHintView(
  96. hintDetent: $hintDetent,
  97. shouldDisplayHint: $shouldDisplayHint,
  98. hintLabel: hintLabel ?? "",
  99. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  100. sheetTitle: String(localized: "Help", comment: "Help sheet title")
  101. )
  102. }
  103. .sheet(isPresented: $showSnoozeSheet) {
  104. SnoozeAlertsSheetView(resolver: resolver, isPresented: $showSnoozeSheet)
  105. }
  106. .scrollContentBackground(.hidden)
  107. .background(appState.trioBackgroundColor(for: colorScheme))
  108. .navigationTitle("Notifications")
  109. .navigationBarTitleDisplayMode(.automatic)
  110. }
  111. }
  112. extension NotificationsView {
  113. func notificationReminder() -> Alert {
  114. Alert(
  115. title: Text("\u{2757} Notifications are Required"),
  116. message: Text(
  117. "Please authorize notifications by tapping 'Open iOS Settings' > 'Notifications' and enable 'Allow Notifications' for 'Notification Center' and 'Banners' Alerts."
  118. ),
  119. dismissButton: .default(Text("Ok"))
  120. )
  121. }
  122. @ViewBuilder private func onOff(_ val: Bool) -> some View {
  123. if val {
  124. Text(String(localized: "On", comment: "Notification Setting Status is On"))
  125. } else {
  126. HStack {
  127. Image(systemName: "exclamationmark.triangle.fill").foregroundColor(.critical)
  128. Text(String(localized: "Off", comment: "Notification Setting Status is Off"))
  129. }
  130. }
  131. }
  132. private var manageNotifications: some View {
  133. Button(action: { UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!) }) {
  134. HStack {
  135. Text(String(localized: "Open iOS Settings", comment: "Manage Permissions in Settings button text"))
  136. Spacer()
  137. Image(systemName: "chevron.right").foregroundColor(.gray).font(.footnote)
  138. }
  139. }
  140. .accentColor(.primary)
  141. }
  142. private var notificationsEnabledStatus: some View {
  143. HStack {
  144. Text(String(localized: "Notifications", comment: "Notifications Status text"))
  145. Spacer()
  146. onOff(!notificationsDisabled)
  147. }
  148. }
  149. }