LiveActivitySettingsRootView.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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: AnyView?
  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.map { AnyView($0) }
  65. hintLabel = "Enable Live Activity"
  66. }
  67. ),
  68. units: state.units,
  69. type: .boolean,
  70. label: "Enable Live Activity",
  71. miniHint: """
  72. Live Activities display Trio's glucose readings and other current data on the iPhone Lock Screen and in the Dynamic Island
  73. Default: OFF
  74. """,
  75. verboseHint: VStack(spacing: 10) {
  76. Text("Default: OFF").bold()
  77. Text(
  78. "With Live Activities, Trio displays your choice of the following current data on your iPhone's Lock Screen and in the Dynamic Island:"
  79. )
  80. VStack(alignment: .leading) {
  81. Text("• Current Glucose Reading")
  82. Text("• IOB: Insulin On Board")
  83. Text("• COB: Carbohydrates On Board")
  84. Text("• Last Updated: Time of Last Loop Cycle")
  85. Text("• Glucose Trend Chart")
  86. }
  87. Text(
  88. "It allows you to refer to live information at a glance and perform quick actions in your diabetes management."
  89. )
  90. },
  91. headerText: "Display Live Data From Trio"
  92. )
  93. if state.useLiveActivity {
  94. Section {
  95. VStack {
  96. Picker(
  97. selection: $state.lockScreenView,
  98. label: Text("Lock Screen Widget Style")
  99. ) {
  100. ForEach(LockScreenView.allCases) { selection in
  101. Text(selection.displayName).tag(selection)
  102. }
  103. }.padding(.top)
  104. HStack(alignment: .top) {
  105. Text(
  106. "Trio Live Activities can be simplistic or detailed in their information display. See hint for more details."
  107. )
  108. .font(.footnote)
  109. .foregroundColor(.secondary)
  110. .lineLimit(nil)
  111. Spacer()
  112. Button(
  113. action: {
  114. hintLabel = "Lock Screen Widget Style"
  115. selectedVerboseHint =
  116. AnyView(
  117. VStack(spacing: 10) {
  118. Text(
  119. "Trio's Simple Lock Screen Widget only display current glucose reading, trend arrow, delta and the timestamp of the current reading."
  120. )
  121. Text(
  122. "The Detailed Lock Screen Widget offers users a glucose chart as well as the ability to customize the information provided in the Detailed Widget using the following options:"
  123. )
  124. VStack(alignment: .leading) {
  125. Text("• Current Glucose Reading")
  126. Text("• IOB: Insulin On Board")
  127. Text("• COB: Carbohydrates On Board")
  128. Text("• Last Updated: Time of Last Loop Cycle")
  129. }
  130. }
  131. )
  132. shouldDisplayHint.toggle()
  133. },
  134. label: {
  135. HStack {
  136. Image(systemName: "questionmark.circle")
  137. }
  138. }
  139. ).buttonStyle(BorderlessButtonStyle())
  140. }.padding(.top)
  141. }.padding(.bottom)
  142. if state.lockScreenView == .detailed {
  143. HStack {
  144. NavigationLink(
  145. "Widget Configuration",
  146. destination: LiveActivityWidgetConfiguration(
  147. resolver: resolver,
  148. state: state
  149. )
  150. ).foregroundStyle(Color.accentColor)
  151. }
  152. }
  153. }.listRowBackground(Color.chart)
  154. }
  155. }
  156. }
  157. .onReceive(resolver.resolve(LiveActivityBridge.self)!.$systemEnabled, perform: {
  158. self.systemLiveActivitySetting = $0
  159. })
  160. .sheet(isPresented: $shouldDisplayHint) {
  161. SettingInputHintView(
  162. hintDetent: $hintDetent,
  163. shouldDisplayHint: $shouldDisplayHint,
  164. hintLabel: hintLabel ?? "",
  165. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  166. sheetTitle: "Help"
  167. )
  168. }
  169. .scrollContentBackground(.hidden).background(color)
  170. .onAppear(perform: configureView)
  171. .navigationTitle("Live Activity")
  172. .navigationBarTitleDisplayMode(.automatic)
  173. }
  174. }
  175. }