NotificationsConfigRootView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. @ViewBuilder private func liveActivitySection() -> some View {
  33. if #available(iOS 16.2, *) {
  34. Section(
  35. header: Text("Live Activity"),
  36. footer: Text(
  37. liveActivityFooterText()
  38. ),
  39. content: {
  40. if !systemLiveActivitySetting {
  41. Button("Open Settings App") {
  42. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  43. }
  44. } else {
  45. Toggle("Show Live Activity", isOn: $state.useLiveActivity)
  46. }
  47. Picker(
  48. selection: $state.lockScreenView,
  49. label: Text("Lock screen widget")
  50. ) {
  51. ForEach(LockScreenView.allCases) { selection in
  52. Text(selection.displayName).tag(selection)
  53. }
  54. }
  55. }
  56. )
  57. .onReceive(resolver.resolve(LiveActivityBridge.self)!.$systemEnabled, perform: {
  58. self.systemLiveActivitySetting = $0
  59. })
  60. }
  61. }
  62. private func liveActivityFooterText() -> String {
  63. var footer =
  64. "Live activity displays blood glucose live on the lock screen and on the dynamic island (if available)"
  65. if !systemLiveActivitySetting {
  66. footer =
  67. "Live activities are turned OFF in system settings. To enable live activities, go to Settings app -> Trio -> Turn live Activities ON.\n\n" +
  68. footer
  69. }
  70. return footer
  71. }
  72. var body: some View {
  73. Form {
  74. Section(header: Text("Glucose")) {
  75. Toggle("Show glucose on the app badge", isOn: $state.glucoseBadge)
  76. Toggle("Always Notify Glucose", isOn: $state.glucoseNotificationsAlways)
  77. Toggle("Also play alert sound", isOn: $state.useAlarmSound)
  78. Toggle("Also add source info", isOn: $state.addSourceInfoToGlucoseNotifications)
  79. HStack {
  80. Text("Low")
  81. Spacer()
  82. TextFieldWithToolBar(text: $state.lowGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
  83. Text(state.units.rawValue).foregroundColor(.secondary)
  84. }
  85. HStack {
  86. Text("High")
  87. Spacer()
  88. TextFieldWithToolBar(text: $state.highGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
  89. Text(state.units.rawValue).foregroundColor(.secondary)
  90. }
  91. }
  92. Section(header: Text("Other")) {
  93. HStack {
  94. Text("Carbs Required Threshold")
  95. Spacer()
  96. TextFieldWithToolBar(text: $state.carbsRequiredThreshold, placeholder: "0", numberFormatter: carbsFormatter)
  97. Text("g").foregroundColor(.secondary)
  98. }
  99. }
  100. liveActivitySection()
  101. }.scrollContentBackground(.hidden)
  102. .onAppear(perform: configureView)
  103. .navigationBarTitle("Notifications")
  104. .navigationBarTitleDisplayMode(.automatic)
  105. }
  106. }
  107. }