NotificationsConfigRootView.swift 5.2 KB

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