NotificationsConfigRootView.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import ActivityKit
  2. import Combine
  3. import SwiftUI
  4. import Swinject
  5. extension NotificationsConfig {
  6. struct RootView: BaseView {
  7. let resolver: Resolver
  8. @StateObject var state = StateModel()
  9. @State private var systemLiveActivitySetting: Bool = {
  10. if #available(iOS 16.1, *) {
  11. ActivityAuthorizationInfo().areActivitiesEnabled
  12. } else {
  13. false
  14. }
  15. }()
  16. private var glucoseFormatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 0
  20. if state.units == .mmolL {
  21. formatter.maximumFractionDigits = 1
  22. }
  23. formatter.roundingMode = .halfUp
  24. return formatter
  25. }
  26. private var carbsFormatter: NumberFormatter {
  27. let formatter = NumberFormatter()
  28. formatter.numberStyle = .decimal
  29. formatter.maximumFractionDigits = 0
  30. return formatter
  31. }
  32. @Environment(\.colorScheme) var colorScheme
  33. var color: LinearGradient {
  34. colorScheme == .dark ? LinearGradient(
  35. gradient: Gradient(colors: [
  36. Color.bgDarkBlue,
  37. Color.bgDarkerDarkBlue
  38. ]),
  39. startPoint: .top,
  40. endPoint: .bottom
  41. )
  42. :
  43. LinearGradient(
  44. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  45. startPoint: .top,
  46. endPoint: .bottom
  47. )
  48. }
  49. @ViewBuilder private func liveActivitySection() -> some View {
  50. if #available(iOS 16.2, *) {
  51. Section(
  52. header: Text("Live Activity"),
  53. footer: Text(
  54. liveActivityFooterText()
  55. ),
  56. content: {
  57. if !systemLiveActivitySetting {
  58. Button("Open Settings App") {
  59. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  60. }
  61. } else {
  62. Toggle("Show Live Activity", isOn: $state.useLiveActivity)
  63. }
  64. Picker(
  65. selection: $state.lockScreenView,
  66. label: Text("Lock screen widget")
  67. ) {
  68. ForEach(LockScreenView.allCases) { selection in
  69. Text(selection.displayName).tag(selection)
  70. }
  71. }
  72. }
  73. )
  74. .onReceive(resolver.resolve(LiveActivityBridge.self)!.$systemEnabled, perform: {
  75. self.systemLiveActivitySetting = $0
  76. })
  77. }
  78. }
  79. private func liveActivityFooterText() -> String {
  80. var footer =
  81. "Live activity displays blood glucose live on the lock screen and on the dynamic island (if available)"
  82. if !systemLiveActivitySetting {
  83. footer =
  84. "Live activities are turned OFF in system settings. To enable live activities, go to Settings app -> Trio -> Turn live Activities ON.\n\n" +
  85. footer
  86. }
  87. return footer
  88. }
  89. var body: some View {
  90. Form {
  91. Section(header: Text("Glucose")) {
  92. Toggle("Show glucose on the app badge", isOn: $state.glucoseBadge)
  93. Toggle("Always Notify Glucose", isOn: $state.glucoseNotificationsAlways)
  94. Toggle("Also play alert sound", isOn: $state.useAlarmSound)
  95. Toggle("Also add source info", isOn: $state.addSourceInfoToGlucoseNotifications)
  96. HStack {
  97. Text("Low")
  98. Spacer()
  99. TextFieldWithToolBar(text: $state.lowGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
  100. Text(state.units.rawValue).foregroundColor(.secondary)
  101. }
  102. HStack {
  103. Text("High")
  104. Spacer()
  105. TextFieldWithToolBar(text: $state.highGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
  106. Text(state.units.rawValue).foregroundColor(.secondary)
  107. }
  108. }
  109. Section(header: Text("Other")) {
  110. HStack {
  111. Text("Carbs Required Threshold")
  112. Spacer()
  113. TextFieldWithToolBar(
  114. text: $state.carbsRequiredThreshold,
  115. placeholder: "0",
  116. numberFormatter: carbsFormatter
  117. )
  118. Text("g").foregroundColor(.secondary)
  119. }
  120. }
  121. liveActivitySection()
  122. }.scrollContentBackground(.hidden).background(color)
  123. .onAppear(perform: configureView)
  124. .navigationBarTitle("Notifications")
  125. .navigationBarTitleDisplayMode(.automatic)
  126. }
  127. }
  128. }