SettingsRootView.swift 19 KB

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