SettingsRootView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. Text("Statistics").navigationLink(to: .statistics, from: self)
  75. }
  76. }
  77. ).listRowBackground(Color.chart)
  78. SettingInputSection(
  79. decimalValue: $decimalPlaceholder,
  80. booleanValue: $state.closedLoop,
  81. shouldDisplayHint: $shouldDisplayHint,
  82. selectedVerboseHint: Binding(
  83. get: { selectedVerboseHint },
  84. set: {
  85. selectedVerboseHint = $0
  86. hintLabel = "Closed Loop"
  87. }
  88. ),
  89. units: state.units,
  90. type: .boolean,
  91. label: "Closed Loop",
  92. miniHint: "Enables automated insulin delivery. Requires active CGM sensor session and connected pump.",
  93. 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.",
  94. headerText: "Automated Insulin Delivery"
  95. )
  96. Section(
  97. header: Text("Trio Configuration"),
  98. content: {
  99. ForEach(SettingItems.trioConfig) { item in
  100. Text(item.title).navigationLink(to: item.view, from: self)
  101. }
  102. }
  103. )
  104. .listRowBackground(Color.chart)
  105. Section(
  106. header: Text("Support & Community"),
  107. content: {
  108. Button {
  109. showShareSheet.toggle()
  110. } label: {
  111. HStack {
  112. Text("Share Logs")
  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://github.com/nightscout/Trio/issues/new/choose") {
  123. UIApplication.shared.open(url)
  124. }
  125. } label: {
  126. HStack {
  127. Text("Submit Ticket on GitHub")
  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://discord.gg/FnwFEFUwXE") {
  138. UIApplication.shared.open(url)
  139. }
  140. } label: {
  141. HStack {
  142. Text("Trio Discord")
  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://m.facebook.com/groups/1351938092206709/") {
  153. UIApplication.shared.open(url)
  154. }
  155. } label: {
  156. HStack {
  157. Text("Trio Facebook")
  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. Button {
  167. if let url = URL(string: "https://diy-trio.org/") {
  168. UIApplication.shared.open(url)
  169. }
  170. } label: {
  171. HStack {
  172. Text("Trio Website")
  173. .foregroundColor(.primary)
  174. Spacer()
  175. Image(systemName: "chevron.right")
  176. .foregroundColor(.secondary)
  177. .font(.footnote)
  178. }
  179. }
  180. .frame(maxWidth: .infinity, alignment: .leading)
  181. }
  182. ).listRowBackground(Color.chart)
  183. } else {
  184. Section(
  185. header: Text("Search Results"),
  186. content: {
  187. ForEach(filteredItems) { filteredItem in
  188. VStack(alignment: .leading) {
  189. Text(filteredItem.matchedContent).bold()
  190. if let path = filteredItem.settingItem.path {
  191. Text(path.map(\.stringValue).joined(separator: " > "))
  192. .font(.caption)
  193. .foregroundColor(.secondary)
  194. }
  195. }.navigationLink(to: filteredItem.settingItem.view, from: self)
  196. }
  197. }
  198. ).listRowBackground(Color.chart)
  199. }
  200. // Section {
  201. // Text("Targets")
  202. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.bgTargets), from: self)
  203. // Text("Sensitivities")
  204. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.insulinSensitivities), from: self)
  205. // }.listRowBackground(Color.chart)
  206. // TODO: remove this more or less entirely; add build-time flag to enable Middleware; add settings export feature
  207. // Section {
  208. // Toggle("Developer Options", isOn: $state.debugOptions)
  209. // if state.debugOptions {
  210. // Group {
  211. // HStack {
  212. // Text("NS Upload Profile and Settings")
  213. // Button("Upload") { state.uploadProfileAndSettings(true) }
  214. // .frame(maxWidth: .infinity, alignment: .trailing)
  215. // .buttonStyle(.borderedProminent)
  216. // }
  217. // // 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
  218. // // Leaving it in here, as it may be a handy functionality for further testing or developers.
  219. // // See https://github.com/nightscout/Trio/pull/277 for more information
  220. // //
  221. // // HStack {
  222. // // Text("Delete Stored Pump State Binary Files")
  223. // // Button("Delete") { state.resetLoopDocuments() }
  224. // // .frame(maxWidth: .infinity, alignment: .trailing)
  225. // // .buttonStyle(.borderedProminent)
  226. // // }
  227. // }
  228. // Group {
  229. // Text("Preferences")
  230. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.preferences), from: self)
  231. // Text("Pump Settings")
  232. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.settings), from: self)
  233. // Text("Autosense")
  234. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.autosense), from: self)
  235. // // Text("Pump History")
  236. // // .navigationLink(to: .configEditor(file: OpenAPS.Monitor.pumpHistory), from: self)
  237. // Text("Basal profile")
  238. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.basalProfile), from: self)
  239. // Text("Targets ranges")
  240. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.bgTargets), from: self)
  241. // Text("Temp targets")
  242. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.tempTargets), from: self)
  243. // }
  244. //
  245. // Group {
  246. // Text("Pump profile")
  247. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.pumpProfile), from: self)
  248. // Text("Profile")
  249. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.profile), from: self)
  250. // // Text("Carbs")
  251. // // .navigationLink(to: .configEditor(file: OpenAPS.Monitor.carbHistory), from: self)
  252. // // Text("Announcements")
  253. // // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.announcements), from: self)
  254. // // Text("Enacted announcements")
  255. // // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.announcementsEnacted), from: self)
  256. // Text("Autotune")
  257. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.autotune), from: self)
  258. // }
  259. //
  260. // Group {
  261. // Text("Target presets")
  262. // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.tempTargetsPresets), from: self)
  263. // Text("Calibrations")
  264. // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.calibrations), from: self)
  265. // Text("Middleware")
  266. // .navigationLink(to: .configEditor(file: OpenAPS.Middleware.determineBasal), from: self)
  267. // // Text("Statistics")
  268. // // .navigationLink(to: .configEditor(file: OpenAPS.Monitor.statistics), from: self)
  269. // Text("Edit settings json")
  270. // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.settings), from: self)
  271. // }
  272. // }
  273. // }.listRowBackground(Color.chart)
  274. }.scrollContentBackground(.hidden).background(color)
  275. .sheet(isPresented: $shouldDisplayHint) {
  276. SettingInputHintView(
  277. hintDetent: $hintDetent,
  278. shouldDisplayHint: $shouldDisplayHint,
  279. hintLabel: hintLabel ?? "",
  280. hintText: selectedVerboseHint ?? "",
  281. sheetTitle: "Help"
  282. )
  283. }
  284. .sheet(isPresented: $showShareSheet) {
  285. ShareSheet(activityItems: state.logItems())
  286. }
  287. .onAppear(perform: configureView)
  288. .navigationTitle("Settings")
  289. .navigationBarTitleDisplayMode(.automatic)
  290. .toolbar {
  291. ToolbarItem(placement: .topBarTrailing) {
  292. Button(
  293. action: {
  294. if let url = URL(string: "https://triodocs.org/") {
  295. UIApplication.shared.open(url)
  296. }
  297. },
  298. label: {
  299. HStack {
  300. Text("Trio Docs")
  301. Image(systemName: "questionmark.circle")
  302. }
  303. }
  304. )
  305. }
  306. }
  307. .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
  308. .screenNavigation(self)
  309. }
  310. }
  311. }