NotificationsConfigRootView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. )
  65. .onReceive(resolver.resolve(LiveActivityBridge.self)!.$systemEnabled, perform: {
  66. self.systemLiveActivitySetting = $0
  67. })
  68. }
  69. }
  70. private func liveActivityFooterText() -> String {
  71. var footer =
  72. "Live activity displays blood glucose live on the lock screen and on the dynamic island (if available)"
  73. if !systemLiveActivitySetting {
  74. footer =
  75. "Live activities are turned OFF in system settings. To enable live activities, go to Settings app -> iAPS -> Turn live Activities ON.\n\n" +
  76. footer
  77. }
  78. return footer
  79. }
  80. var body: some View {
  81. Form {
  82. Section(header: Text("Glucose")) {
  83. Toggle("Show glucose on the app badge", isOn: $state.glucoseBadge)
  84. Toggle("Always Notify Glucose", isOn: $state.glucoseNotificationsAlways)
  85. Toggle("Also play alert sound", isOn: $state.useAlarmSound)
  86. Toggle("Also add source info", isOn: $state.addSourceInfoToGlucoseNotifications)
  87. HStack {
  88. Text("Low")
  89. Spacer()
  90. DecimalTextField("0", value: $state.lowGlucose, formatter: glucoseFormatter)
  91. Text(state.units.rawValue).foregroundColor(.secondary)
  92. }
  93. HStack {
  94. Text("High")
  95. Spacer()
  96. DecimalTextField("0", value: $state.highGlucose, formatter: glucoseFormatter)
  97. Text(state.units.rawValue).foregroundColor(.secondary)
  98. }
  99. }
  100. Section(header: Text("Other")) {
  101. HStack {
  102. Text("Carbs Required Threshold")
  103. Spacer()
  104. DecimalTextField("0", value: $state.carbsRequiredThreshold, formatter: carbsFormatter)
  105. Text("g").foregroundColor(.secondary)
  106. }
  107. }
  108. liveActivitySection()
  109. }.scrollContentBackground(.hidden).background(color)
  110. .onAppear(perform: configureView)
  111. .navigationBarTitle("Notifications")
  112. .navigationBarTitleDisplayMode(.automatic)
  113. }
  114. }
  115. }