SettingsRootView.swift 20 KB

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