SettingsRootView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import HealthKit
  2. import LoopKit
  3. import LoopKitUI
  4. import SwiftUI
  5. import Swinject
  6. extension Settings {
  7. struct RootView: BaseView {
  8. let resolver: Resolver
  9. @StateObject var state = StateModel()
  10. @State private var showShareSheet = false
  11. @State private var searchText: String = ""
  12. @State private var shouldDisplayHint: Bool = false
  13. @State var hintDetent = PresentationDetent.large
  14. @State var selectedVerboseHint: String?
  15. @State var hintLabel: String?
  16. @State private var decimalPlaceholder: Decimal = 0.0
  17. @State private var booleanPlaceholder: Bool = false
  18. @Environment(\.colorScheme) var colorScheme
  19. @EnvironmentObject var appIcons: Icons
  20. private var color: LinearGradient {
  21. colorScheme == .dark ? LinearGradient(
  22. gradient: Gradient(colors: [
  23. Color.bgDarkBlue,
  24. Color.bgDarkerDarkBlue
  25. ]),
  26. startPoint: .top,
  27. endPoint: .bottom
  28. )
  29. :
  30. LinearGradient(
  31. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  32. startPoint: .top,
  33. endPoint: .bottom
  34. )
  35. }
  36. private var filteredItems: [FilteredSettingItem] {
  37. SettingItems.filteredItems(searchText: searchText)
  38. }
  39. var body: some View {
  40. Form {
  41. if searchText.isEmpty {
  42. let buildDetails = BuildDetails.default
  43. Section(
  44. header: Text("BRANCH: \(buildDetails.branchAndSha)").textCase(nil),
  45. content: {
  46. let versionNumber = Bundle.main.releaseVersionNumber ?? "Unknown"
  47. let buildNumber = Bundle.main.buildVersionNumber ?? "Unknown"
  48. Group {
  49. HStack {
  50. Image(uiImage: UIImage(named: appIcons.appIcon.rawValue) ?? UIImage())
  51. .resizable()
  52. .aspectRatio(contentMode: .fit)
  53. .frame(width: 50, height: 50)
  54. .padding(.trailing, 10)
  55. VStack(alignment: .leading) {
  56. Text("Trio v\(versionNumber) (\(buildNumber))")
  57. .font(.headline)
  58. if let expirationDate = buildDetails.calculateExpirationDate() {
  59. let formattedDate = DateFormatter.localizedString(
  60. from: expirationDate,
  61. dateStyle: .medium,
  62. timeStyle: .none
  63. )
  64. Text("\(buildDetails.expirationHeaderString): \(formattedDate)")
  65. .font(.footnote)
  66. .foregroundColor(.secondary)
  67. } else {
  68. Text("Simulator Build has no expiry")
  69. .font(.footnote)
  70. .foregroundColor(.secondary)
  71. }
  72. }
  73. }
  74. }
  75. }
  76. ).listRowBackground(Color.chart)
  77. SettingInputSection(
  78. decimalValue: $decimalPlaceholder,
  79. booleanValue: $state.closedLoop,
  80. shouldDisplayHint: $shouldDisplayHint,
  81. selectedVerboseHint: Binding(
  82. get: { selectedVerboseHint },
  83. set: {
  84. selectedVerboseHint = $0
  85. hintLabel = "Closed Loop"
  86. }
  87. ),
  88. units: state.units,
  89. type: .boolean,
  90. label: "Closed Loop",
  91. miniHint: "Enables automated insulin delivery. Requires active CGM sensor session and connected pump.",
  92. verboseHint: "Running Trio in closed loop mode requires an active CGM sensor session and a connected pump. This enables automated insulin delivery.\n\nBefore enabling, dial in your settings (basal / insulin sensitivity / carb ratio), and familiarize yourself with the app.",
  93. headerText: "Automated Insulin Delivery"
  94. )
  95. Section(
  96. header: Text("Trio Configuration"),
  97. content: {
  98. ForEach(SettingItems.trioConfig) { item in
  99. Text(item.title).navigationLink(to: item.view, from: self)
  100. }
  101. }
  102. )
  103. .listRowBackground(Color.chart)
  104. Section(
  105. header: Text("Support & Community"),
  106. content: {
  107. Button {
  108. showShareSheet.toggle()
  109. } label: {
  110. HStack {
  111. Text("Share Logs")
  112. .foregroundColor(.primary)
  113. Spacer()
  114. Image(systemName: "chevron.right")
  115. .foregroundColor(.secondary)
  116. .font(.footnote)
  117. }
  118. }
  119. .frame(maxWidth: .infinity, alignment: .leading)
  120. Button {
  121. if let url = URL(string: "https://github.com/nightscout/Trio/issues/new/choose") {
  122. UIApplication.shared.open(url)
  123. }
  124. } label: {
  125. HStack {
  126. Text("Submit Ticket on GitHub")
  127. .foregroundColor(.primary)
  128. Spacer()
  129. Image(systemName: "chevron.right")
  130. .foregroundColor(.secondary)
  131. .font(.footnote)
  132. }
  133. }
  134. .frame(maxWidth: .infinity, alignment: .leading)
  135. Button {
  136. if let url = URL(string: "https://discord.gg/FnwFEFUwXE") {
  137. UIApplication.shared.open(url)
  138. }
  139. } label: {
  140. HStack {
  141. Text("Trio Discord")
  142. .foregroundColor(.primary)
  143. Spacer()
  144. Image(systemName: "chevron.right")
  145. .foregroundColor(.secondary)
  146. .font(.footnote)
  147. }
  148. }
  149. .frame(maxWidth: .infinity, alignment: .leading)
  150. Button {
  151. if let url = URL(string: "https://m.facebook.com/groups/1351938092206709/") {
  152. UIApplication.shared.open(url)
  153. }
  154. } label: {
  155. HStack {
  156. Text("Trio Facebook")
  157. .foregroundColor(.primary)
  158. Spacer()
  159. Image(systemName: "chevron.right")
  160. .foregroundColor(.secondary)
  161. .font(.footnote)
  162. }
  163. }
  164. .frame(maxWidth: .infinity, alignment: .leading)
  165. Button {
  166. if let url = URL(string: "https://diy-trio.org/") {
  167. UIApplication.shared.open(url)
  168. }
  169. } label: {
  170. HStack {
  171. Text("Trio Website")
  172. .foregroundColor(.primary)
  173. Spacer()
  174. Image(systemName: "chevron.right")
  175. .foregroundColor(.secondary)
  176. .font(.footnote)
  177. }
  178. }
  179. .frame(maxWidth: .infinity, alignment: .leading)
  180. }
  181. ).listRowBackground(Color.chart)
  182. } else {
  183. Section(
  184. header: Text("Search Results"),
  185. content: {
  186. ForEach(filteredItems) { filteredItem in
  187. VStack(alignment: .leading) {
  188. Text(filteredItem.matchedContent).bold()
  189. if let path = filteredItem.settingItem.path {
  190. Text(path.map(\.stringValue).joined(separator: " > "))
  191. .font(.caption)
  192. .foregroundColor(.secondary)
  193. }
  194. }.navigationLink(to: filteredItem.settingItem.view, from: self)
  195. }
  196. }
  197. ).listRowBackground(Color.chart)
  198. }
  199. // Section {
  200. // Text("Targets")
  201. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.bgTargets), from: self)
  202. // Text("Sensitivities")
  203. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.insulinSensitivities), from: self)
  204. // Text("Profile")
  205. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.profile), from: self)
  206. // Text("Preferences")
  207. // .navigationLink(
  208. // to: .configEditor(file: OpenAPS.Settings.preferences),
  209. // from: self
  210. // )
  211. // }.listRowBackground(Color.chart)
  212. // TODO: remove this more or less entirely; add build-time flag to enable Middleware; add settings export feature
  213. // Section {
  214. // Toggle("Developer Options", isOn: $state.debugOptions)
  215. // if state.debugOptions {
  216. // Group {
  217. // HStack {
  218. // Text("NS Upload Profile and Settings")
  219. // Button("Upload") { state.uploadProfileAndSettings(true) }
  220. // .frame(maxWidth: .infinity, alignment: .trailing)
  221. // .buttonStyle(.borderedProminent)
  222. // }
  223. // // Commenting this out for now, as not needed and possibly dangerous for users to be able to nuke their pump pairing informations via the debug menu
  224. // // Leaving it in here, as it may be a handy functionality for further testing or developers.
  225. // // See https://github.com/nightscout/Trio/pull/277 for more information
  226. // //
  227. // // HStack {
  228. // // Text("Delete Stored Pump State Binary Files")
  229. // // Button("Delete") { state.resetLoopDocuments() }
  230. // // .frame(maxWidth: .infinity, alignment: .trailing)
  231. // // .buttonStyle(.borderedProminent)
  232. // // }
  233. // }
  234. // Group {
  235. // Text("Preferences")
  236. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.preferences), from: self)
  237. // Text("Pump Settings")
  238. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.settings), from: self)
  239. // Text("Autosense")
  240. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.autosense), from: self)
  241. // // Text("Pump History")
  242. // // .navigationLink(to: .configEditor(file: OpenAPS.Monitor.pumpHistory), from: self)
  243. // Text("Basal profile")
  244. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.basalProfile), from: self)
  245. // Text("Targets ranges")
  246. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.bgTargets), from: self)
  247. // Text("Temp targets")
  248. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.tempTargets), from: self)
  249. // }
  250. //
  251. // Group {
  252. // Text("Pump profile")
  253. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.pumpProfile), from: self)
  254. // Text("Profile")
  255. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.profile), from: self)
  256. // // Text("Carbs")
  257. // // .navigationLink(to: .configEditor(file: OpenAPS.Monitor.carbHistory), from: self)
  258. // // Text("Announcements")
  259. // // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.announcements), from: self)
  260. // // Text("Enacted announcements")
  261. // // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.announcementsEnacted), from: self)
  262. // Text("Autotune")
  263. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.autotune), from: self)
  264. // }
  265. //
  266. // Group {
  267. // Text("Target presets")
  268. // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.tempTargetsPresets), from: self)
  269. // Text("Calibrations")
  270. // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.calibrations), from: self)
  271. // Text("Middleware")
  272. // .navigationLink(to: .configEditor(file: OpenAPS.Middleware.determineBasal), from: self)
  273. // // Text("Statistics")
  274. // // .navigationLink(to: .configEditor(file: OpenAPS.Monitor.statistics), from: self)
  275. // Text("Edit settings json")
  276. // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.settings), from: self)
  277. // }
  278. // }
  279. // }.listRowBackground(Color.chart)
  280. }.scrollContentBackground(.hidden).background(color)
  281. .sheet(isPresented: $shouldDisplayHint) {
  282. SettingInputHintView(
  283. hintDetent: $hintDetent,
  284. shouldDisplayHint: $shouldDisplayHint,
  285. hintLabel: hintLabel ?? "",
  286. hintText: selectedVerboseHint ?? "",
  287. sheetTitle: "Help"
  288. )
  289. }
  290. .sheet(isPresented: $showShareSheet) {
  291. ShareSheet(activityItems: state.logItems())
  292. }
  293. .onAppear(perform: configureView)
  294. .navigationTitle("Settings")
  295. .navigationBarTitleDisplayMode(.automatic)
  296. .toolbar {
  297. ToolbarItem(placement: .topBarTrailing) {
  298. Button(
  299. action: {
  300. if let url = URL(string: "https://triodocs.org/") {
  301. UIApplication.shared.open(url)
  302. }
  303. },
  304. label: {
  305. HStack {
  306. Text("Trio Docs")
  307. Image(systemName: "questionmark.circle")
  308. }
  309. }
  310. )
  311. }
  312. }
  313. .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
  314. .screenNavigation(self)
  315. }
  316. }
  317. }