LiveActivitySettingsRootView.swift 9.6 KB

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