LiveActivitySettingsRootView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import ActivityKit
  2. import SwiftUI
  3. import Swinject
  4. extension LiveActivitySettings {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @StateObject var state = StateModel()
  8. @State private var shouldDisplayHint: Bool = false
  9. @State var hintDetent = PresentationDetent.large
  10. @State var selectedVerboseHint: String?
  11. @State var hintLabel: String?
  12. @State private var decimalPlaceholder: Decimal = 0.0
  13. @State private var booleanPlaceholder: Bool = false
  14. @State private var systemLiveActivitySetting: Bool = {
  15. if #available(iOS 16.2, *) {
  16. ActivityAuthorizationInfo().areActivitiesEnabled
  17. } else {
  18. false
  19. }
  20. }()
  21. @Environment(\.colorScheme) var colorScheme
  22. @Environment(AppState.self) var appState
  23. var body: some View {
  24. List {
  25. if !systemLiveActivitySetting {
  26. Section(
  27. header: Text("Display Live Data From Trio"),
  28. content: {
  29. Text("Live Activities must be enabled under iOS Settings to allow Trio to display live data.")
  30. }
  31. ).listRowBackground(Color.chart)
  32. Section {
  33. Button {
  34. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  35. }
  36. label: { Label("Open iOS Settings", systemImage: "gear.circle").font(.title3).padding() }
  37. .frame(maxWidth: .infinity, alignment: .center)
  38. .buttonStyle(.bordered)
  39. }
  40. .listRowBackground(Color.clear)
  41. } else {
  42. SettingInputSection(
  43. decimalValue: $decimalPlaceholder,
  44. booleanValue: $state.useLiveActivity,
  45. shouldDisplayHint: $shouldDisplayHint,
  46. selectedVerboseHint: Binding(
  47. get: { selectedVerboseHint },
  48. set: {
  49. selectedVerboseHint = $0
  50. hintLabel = "Enable Live Activity"
  51. }
  52. ),
  53. units: state.units,
  54. type: .boolean,
  55. label: "Enable Live Activity",
  56. miniHint: "Live Activities display Trio's glucose readings, and other current data on the iPhone Lock Screen and in the Dynamic Island",
  57. verboseHint: "With Live Activities, you can let Trio display most current data, e.g. glucose reading from CGM, insulin on board, carbohydrates on board, or even a glucose trend chart, on the iPhone Lock Screen and in the Dynamic Island. It allows you to refer to live information at a glance and perform quick actions in your diabetes management.",
  58. headerText: "Display Live Data From Trio"
  59. )
  60. if state.useLiveActivity {
  61. Section {
  62. VStack {
  63. Picker(
  64. selection: $state.lockScreenView,
  65. label: Text("Lock Screen Widget Style")
  66. ) {
  67. ForEach(LockScreenView.allCases) { selection in
  68. Text(selection.displayName).tag(selection)
  69. }
  70. }.padding(.top)
  71. HStack(alignment: .top) {
  72. Text(
  73. "Trio Live Activities can be simplistic or detailed in their information display. See hint for more details."
  74. )
  75. .font(.footnote)
  76. .foregroundColor(.secondary)
  77. .lineLimit(nil)
  78. Spacer()
  79. Button(
  80. action: {
  81. hintLabel = "Lock Screen Widget Style"
  82. selectedVerboseHint =
  83. "Trio's simple lock screen widget only display current glucose reading, trend arrow, delta and the timestamp of the current reading.\n\nThe detailed Lock Screen widget offers users a glucose chart, glucose trend arrow, glucose delta, current insulin and carbohydrates on board, and an icon as an indicator for running overrides."
  84. shouldDisplayHint.toggle()
  85. },
  86. label: {
  87. HStack {
  88. Image(systemName: "questionmark.circle")
  89. }
  90. }
  91. ).buttonStyle(BorderlessButtonStyle())
  92. }.padding(.top)
  93. }.padding(.bottom)
  94. if state.lockScreenView == .detailed {
  95. HStack {
  96. NavigationLink(
  97. "Widget Configuration",
  98. destination: LiveActivityWidgetConfiguration(
  99. resolver: resolver,
  100. state: state
  101. )
  102. ).foregroundStyle(Color.accentColor)
  103. }
  104. }
  105. }.listRowBackground(Color.chart)
  106. }
  107. }
  108. }
  109. .onReceive(resolver.resolve(LiveActivityBridge.self)!.$systemEnabled, perform: {
  110. self.systemLiveActivitySetting = $0
  111. })
  112. .sheet(isPresented: $shouldDisplayHint) {
  113. SettingInputHintView(
  114. hintDetent: $hintDetent,
  115. shouldDisplayHint: $shouldDisplayHint,
  116. hintLabel: hintLabel ?? "",
  117. hintText: selectedVerboseHint ?? "",
  118. sheetTitle: "Help"
  119. )
  120. }
  121. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  122. .onAppear(perform: configureView)
  123. .navigationTitle("Live Activity")
  124. .navigationBarTitleDisplayMode(.automatic)
  125. }
  126. }
  127. }