LiveActivitySettingsRootView.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. private var color: LinearGradient {
  23. colorScheme == .dark ? LinearGradient(
  24. gradient: Gradient(colors: [
  25. Color.bgDarkBlue,
  26. Color.bgDarkerDarkBlue
  27. ]),
  28. startPoint: .top,
  29. endPoint: .bottom
  30. )
  31. :
  32. LinearGradient(
  33. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  34. startPoint: .top,
  35. endPoint: .bottom
  36. )
  37. }
  38. var body: some View {
  39. List {
  40. if !systemLiveActivitySetting {
  41. Section(
  42. header: Text("Display Live Data From Trio"),
  43. content: {
  44. Text("Live Activities must be enabled under iOS Settings to allow Trio to display live data.")
  45. }
  46. ).listRowBackground(Color.chart)
  47. Section {
  48. Button {
  49. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
  50. }
  51. label: { Label("Open iOS Settings", systemImage: "gear.circle").font(.title3).padding() }
  52. .frame(maxWidth: .infinity, alignment: .center)
  53. .buttonStyle(.bordered)
  54. }
  55. .listRowBackground(Color.clear)
  56. } else {
  57. SettingInputSection(
  58. decimalValue: $decimalPlaceholder,
  59. booleanValue: $state.useLiveActivity,
  60. shouldDisplayHint: $shouldDisplayHint,
  61. selectedVerboseHint: Binding(
  62. get: { selectedVerboseHint },
  63. set: {
  64. selectedVerboseHint = $0
  65. hintLabel = "Show Live Activity"
  66. }
  67. ),
  68. type: .boolean,
  69. label: "Enable Live Activity",
  70. miniHint: "Live Activities display Trio's glucose readings, and other current data on the iPhone Lock Screen and in the Dynamic Island",
  71. 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.",
  72. headerText: "Display Live Data From Trio"
  73. )
  74. if state.useLiveActivity {
  75. Section {
  76. VStack {
  77. Picker(
  78. selection: $state.lockScreenView,
  79. label: Text("Lock Screen Widget Style")
  80. ) {
  81. ForEach(LockScreenView.allCases) { selection in
  82. Text(selection.displayName).tag(selection)
  83. }
  84. }.padding(.top)
  85. HStack(alignment: .top) {
  86. Text(
  87. "Trio Live Activities can be simplistic or detailed in their information display. See hint for more details."
  88. )
  89. .font(.footnote)
  90. .foregroundColor(.secondary)
  91. .lineLimit(nil)
  92. Spacer()
  93. Button(
  94. action: {
  95. hintLabel = "Lock Screen Widget Style"
  96. selectedVerboseHint =
  97. "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."
  98. shouldDisplayHint.toggle()
  99. },
  100. label: {
  101. HStack {
  102. Image(systemName: "questionmark.circle")
  103. }
  104. }
  105. ).buttonStyle(BorderlessButtonStyle())
  106. }.padding(.top)
  107. }.padding(.bottom)
  108. }.listRowBackground(Color.chart)
  109. }
  110. }
  111. }
  112. .onReceive(resolver.resolve(LiveActivityBridge.self)!.$systemEnabled, perform: {
  113. self.systemLiveActivitySetting = $0
  114. })
  115. .sheet(isPresented: $shouldDisplayHint) {
  116. SettingInputHintView(
  117. hintDetent: $hintDetent,
  118. shouldDisplayHint: $shouldDisplayHint,
  119. hintLabel: hintLabel ?? "",
  120. hintText: selectedVerboseHint ?? "",
  121. sheetTitle: "Help"
  122. )
  123. }
  124. .scrollContentBackground(.hidden).background(color)
  125. .onAppear(perform: configureView)
  126. .navigationTitle("Live Activity")
  127. .navigationBarTitleDisplayMode(.automatic)
  128. }
  129. }
  130. }