LiveActivitySettingsRootView.swift 9.4 KB

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