SettingsRootView.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. 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(.white)
  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(.white)
  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(.white)
  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(.white)
  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(.white)
  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. // TODO: remove this more or less entirely; add build-time flag to enable Middleware; add settings export feature
  200. // Section {
  201. // Toggle("Developer Options", isOn: $state.debugOptions)
  202. // if state.debugOptions {
  203. // Group {
  204. // HStack {
  205. // Text("NS Upload Profile and Settings")
  206. // Button("Upload") { state.uploadProfileAndSettings(true) }
  207. // .frame(maxWidth: .infinity, alignment: .trailing)
  208. // .buttonStyle(.borderedProminent)
  209. // }
  210. // // 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
  211. // // Leaving it in here, as it may be a handy functionality for further testing or developers.
  212. // // See https://github.com/nightscout/Trio/pull/277 for more information
  213. // //
  214. // // HStack {
  215. // // Text("Delete Stored Pump State Binary Files")
  216. // // Button("Delete") { state.resetLoopDocuments() }
  217. // // .frame(maxWidth: .infinity, alignment: .trailing)
  218. // // .buttonStyle(.borderedProminent)
  219. // // }
  220. // }
  221. // Group {
  222. // Text("Preferences")
  223. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.preferences), from: self)
  224. // Text("Pump Settings")
  225. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.settings), from: self)
  226. // Text("Autosense")
  227. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.autosense), from: self)
  228. // // Text("Pump History")
  229. // // .navigationLink(to: .configEditor(file: OpenAPS.Monitor.pumpHistory), from: self)
  230. // Text("Basal profile")
  231. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.basalProfile), from: self)
  232. // Text("Targets ranges")
  233. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.bgTargets), from: self)
  234. // Text("Temp targets")
  235. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.tempTargets), from: self)
  236. // }
  237. //
  238. // Group {
  239. // Text("Pump profile")
  240. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.pumpProfile), from: self)
  241. // Text("Profile")
  242. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.profile), from: self)
  243. // // Text("Carbs")
  244. // // .navigationLink(to: .configEditor(file: OpenAPS.Monitor.carbHistory), from: self)
  245. // // Text("Announcements")
  246. // // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.announcements), from: self)
  247. // // Text("Enacted announcements")
  248. // // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.announcementsEnacted), from: self)
  249. // Text("Autotune")
  250. // .navigationLink(to: .configEditor(file: OpenAPS.Settings.autotune), from: self)
  251. // }
  252. //
  253. // Group {
  254. // Text("Target presets")
  255. // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.tempTargetsPresets), from: self)
  256. // Text("Calibrations")
  257. // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.calibrations), from: self)
  258. // Text("Middleware")
  259. // .navigationLink(to: .configEditor(file: OpenAPS.Middleware.determineBasal), from: self)
  260. // // Text("Statistics")
  261. // // .navigationLink(to: .configEditor(file: OpenAPS.Monitor.statistics), from: self)
  262. // Text("Edit settings json")
  263. // .navigationLink(to: .configEditor(file: OpenAPS.FreeAPS.settings), from: self)
  264. // }
  265. // }
  266. // }.listRowBackground(Color.chart)
  267. }.scrollContentBackground(.hidden).background(color)
  268. .sheet(isPresented: $shouldDisplayHint) {
  269. SettingInputHintView(
  270. hintDetent: $hintDetent,
  271. shouldDisplayHint: $shouldDisplayHint,
  272. hintLabel: hintLabel ?? "",
  273. hintText: selectedVerboseHint ?? "",
  274. sheetTitle: "Help"
  275. )
  276. }
  277. .sheet(isPresented: $showShareSheet) {
  278. ShareSheet(activityItems: state.logItems())
  279. }
  280. .onAppear(perform: configureView)
  281. .navigationTitle("Settings")
  282. .navigationBarTitleDisplayMode(.automatic)
  283. .toolbar {
  284. ToolbarItem(placement: .topBarTrailing) {
  285. Button(
  286. action: {
  287. if let url = URL(string: "https://triodocs.org/") {
  288. UIApplication.shared.open(url)
  289. }
  290. },
  291. label: {
  292. HStack {
  293. Text("Trio Docs")
  294. Image(systemName: "questionmark.circle")
  295. }
  296. }
  297. )
  298. }
  299. }
  300. .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
  301. .onDisappear(perform: { state.uploadProfileAndSettings(false) })
  302. .screenNavigation(self)
  303. }
  304. }
  305. }