NotificationsConfigRootView.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import SwiftUI
  2. import Swinject
  3. import ActivityKit
  4. import Combine
  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("Background_1"),
  37. Color("Background_1"),
  38. Color("Background_2")
  39. // Color("Background_1")
  40. ]),
  41. startPoint: .top,
  42. endPoint: .bottom
  43. )
  44. :
  45. LinearGradient(
  46. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  47. startPoint: .top,
  48. endPoint: .bottom
  49. )
  50. }
  51. @ViewBuilder private func liveActivitySection() -> some View {
  52. if #available(iOS 16.2, *) {
  53. Section(
  54. header: Text("Live Activity"),
  55. footer: Text(
  56. liveActivityFooterText()
  57. ),
  58. content: {
  59. if !systemLiveActivitySetting {
  60. Button("Open Settings App") {
  61. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  62. }
  63. } else {
  64. Toggle("Show Live Activity", isOn: $state.useLiveActivity) }
  65. }
  66. )
  67. .onReceive(resolver.resolve(LiveActivityBridge.self)!.$systemEnabled, perform: {
  68. self.systemLiveActivitySetting = $0
  69. })
  70. }
  71. }
  72. private func liveActivityFooterText() -> String {
  73. var footer =
  74. "Live activity displays blood glucose live on the lock screen and on the dynamic island (if available)"
  75. if !systemLiveActivitySetting {
  76. footer =
  77. "Live activities are turned OFF in system settings. To enable live activities, go to Settings app -> iAPS -> Turn live Activities ON.\n\n" +
  78. footer
  79. }
  80. return footer
  81. }
  82. var body: some View {
  83. Form {
  84. Section(header: Text("Glucose")) {
  85. Toggle("Show glucose on the app badge", isOn: $state.glucoseBadge)
  86. Toggle("Always Notify Glucose", isOn: $state.glucoseNotificationsAlways)
  87. Toggle("Also play alert sound", isOn: $state.useAlarmSound)
  88. Toggle("Also add source info", isOn: $state.addSourceInfoToGlucoseNotifications)
  89. HStack {
  90. Text("Low")
  91. Spacer()
  92. DecimalTextField("0", value: $state.lowGlucose, formatter: glucoseFormatter)
  93. Text(state.units.rawValue).foregroundColor(.secondary)
  94. }
  95. HStack {
  96. Text("High")
  97. Spacer()
  98. DecimalTextField("0", value: $state.highGlucose, formatter: glucoseFormatter)
  99. Text(state.units.rawValue).foregroundColor(.secondary)
  100. }
  101. }
  102. Section(header: Text("Other")) {
  103. HStack {
  104. Text("Carbs Required Threshold")
  105. Spacer()
  106. DecimalTextField("0", value: $state.carbsRequiredThreshold, formatter: carbsFormatter)
  107. Text("g").foregroundColor(.secondary)
  108. }
  109. }
  110. liveActivitySection()
  111. }.scrollContentBackground(.hidden).background(color)
  112. .onAppear(perform: configureView)
  113. .navigationBarTitle("Notifications")
  114. .navigationBarTitleDisplayMode(.automatic)
  115. }
  116. }
  117. }