NotificationsView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // FeatureSettingsView.swift
  3. // FreeAPS
  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 shouldDisplayHint: Bool = false
  17. @State var hintDetent = PresentationDetent.large
  18. @State var selectedVerboseHint: String? =
  19. "Notifications give you important Trio information without requiring you to open the app.\n\nKeep these turned ON in your phone’s settings to ensure you receive Trio Notifications, Critical Alerts, and Time Sensitive Notifications."
  20. @State var hintLabel: String? = "Manage iOS Preferences"
  21. @Environment(\.colorScheme) var colorScheme
  22. @Environment(AppState.self) var appState
  23. var body: some View {
  24. Form {
  25. Section(
  26. header: Text("Manage iOS Preferences"),
  27. content: {
  28. manageNotifications
  29. }
  30. ).listRowBackground(Color.chart)
  31. Section {
  32. VStack {
  33. notificationsEnabledStatus
  34. HStack(alignment: .top) {
  35. Text(
  36. "Notifications give you important Trio information without requiring you to open the app."
  37. )
  38. .font(.footnote)
  39. .foregroundColor(.secondary)
  40. .lineLimit(nil)
  41. Spacer()
  42. Button(
  43. action: {
  44. hintLabel = "Manage iOS Preferences"
  45. selectedVerboseHint =
  46. "Notifications give you important Trio information without requiring you to open the app.\n\nKeep these turned ON in your phone’s settings to ensure you receive Trio Notifications, Critical Alerts, and Time Sensitive Notifications."
  47. shouldDisplayHint.toggle()
  48. },
  49. label: {
  50. HStack {
  51. Image(systemName: "questionmark.circle")
  52. }
  53. }
  54. ).buttonStyle(BorderlessButtonStyle())
  55. }.padding(.top)
  56. }.padding(.bottom)
  57. }.listRowBackground(Color.chart)
  58. Section(
  59. header: Text("Notification Center"),
  60. content: {
  61. Text("Trio Notifications")
  62. .navigationLink(to: .glucoseNotificationSettings, from: self)
  63. if #available(iOS 16.2, *) {
  64. Text("Live Activity").navigationLink(to: .liveActivitySettings, from: self)
  65. }
  66. Text("Calendar Events").navigationLink(to: .calendarEventSettings, from: self)
  67. }
  68. ).listRowBackground(Color.chart)
  69. }
  70. .onReceive(
  71. resolver.resolve(AlertPermissionsChecker.self)!.$notificationsDisabled,
  72. perform: {
  73. if notificationsDisabled != $0 {
  74. notificationsDisabled = $0
  75. if notificationsDisabled {
  76. showAlert = true
  77. }
  78. }
  79. }
  80. )
  81. .alert(
  82. isPresented: self.$showAlert,
  83. content: { self.notificationReminder() }
  84. )
  85. .sheet(isPresented: $shouldDisplayHint) {
  86. SettingInputHintView(
  87. hintDetent: $hintDetent,
  88. shouldDisplayHint: $shouldDisplayHint,
  89. hintLabel: hintLabel ?? "",
  90. hintText: selectedVerboseHint ?? "",
  91. sheetTitle: "Help"
  92. )
  93. }
  94. .scrollContentBackground(.hidden)
  95. .background(appState.trioBackgroundColor(for: colorScheme))
  96. .navigationTitle("Notifications")
  97. .navigationBarTitleDisplayMode(.automatic)
  98. }
  99. }
  100. extension NotificationsView {
  101. func notificationReminder() -> Alert {
  102. Alert(
  103. title: Text("\u{2757} Notifications are Required"),
  104. message: Text(
  105. "Please authorize notifications by tapping 'Open iOS Settings' > 'Notifications' and enable 'Allow Notifications' for 'Notification Center' and 'Banners' Alerts."
  106. ),
  107. dismissButton: .default(Text("Ok"))
  108. )
  109. }
  110. @ViewBuilder private func onOff(_ val: Bool) -> some View {
  111. if val {
  112. Text(NSLocalizedString("On", comment: "Notification Setting Status is On"))
  113. } else {
  114. HStack {
  115. Image(systemName: "exclamationmark.triangle.fill").foregroundColor(.critical)
  116. Text(NSLocalizedString("Off", comment: "Notification Setting Status is Off"))
  117. }
  118. }
  119. }
  120. private var manageNotifications: some View {
  121. Button(action: { UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!) }) {
  122. HStack {
  123. Text(NSLocalizedString("Open iOS Settings", comment: "Manage Permissions in Settings button text"))
  124. Spacer()
  125. Image(systemName: "chevron.right").foregroundColor(.gray).font(.footnote)
  126. }
  127. }
  128. .accentColor(.primary)
  129. }
  130. private var notificationsEnabledStatus: some View {
  131. HStack {
  132. Text(NSLocalizedString("Notifications", comment: "Notifications Status text"))
  133. Spacer()
  134. onOff(!notificationsDisabled)
  135. }
  136. }
  137. }