SettingsRootView.swift 17 KB

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