SettingsMenuView.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // LoopFollow
  2. // SettingsMenuView.swift
  3. // Created by Jonas Björkert.
  4. import SwiftUI
  5. import UIKit
  6. struct SettingsMenuView: View {
  7. // MARK: - Observed Objects
  8. @ObservedObject private var nightscoutURL = Storage.shared.url
  9. @ObservedObject private var settingsPath = Observable.shared.settingsPath
  10. // MARK: – Local state
  11. @State private var latestVersion: String?
  12. @State private var versionTint: Color = .secondary
  13. // MARK: – Body
  14. var body: some View {
  15. NavigationStack(path: $settingsPath.value) {
  16. List {
  17. // ───────── Data settings ─────────
  18. dataSection
  19. // ───────── App settings ─────────
  20. Section("App Settings") {
  21. NavigationRow(title: "Background Refresh Settings",
  22. icon: "arrow.clockwise")
  23. {
  24. settingsPath.value.append(Sheet.backgroundRefresh)
  25. }
  26. NavigationRow(title: "General Settings",
  27. icon: "gearshape")
  28. {
  29. settingsPath.value.append(Sheet.general)
  30. }
  31. NavigationRow(title: "Graph Settings",
  32. icon: "chart.xyaxis.line")
  33. {
  34. settingsPath.value.append(Sheet.graph)
  35. }
  36. if !nightscoutURL.value.isEmpty {
  37. NavigationRow(title: "Information Display Settings",
  38. icon: "info.circle")
  39. {
  40. settingsPath.value.append(Sheet.infoDisplay)
  41. }
  42. NavigationRow(title: "Remote Settings",
  43. icon: "antenna.radiowaves.left.and.right")
  44. {
  45. settingsPath.value.append(Sheet.remote)
  46. }
  47. }
  48. }
  49. // ───────── Alarms ─────────
  50. Section {
  51. NavigationRow(title: "Alarms",
  52. icon: "bell")
  53. {
  54. settingsPath.value.append(Sheet.alarmsList)
  55. }
  56. NavigationRow(title: "Alarm Settings",
  57. icon: "bell.badge")
  58. {
  59. settingsPath.value.append(Sheet.alarmSettings)
  60. }
  61. }
  62. // ───────── Integrations ─────────
  63. Section("Integrations") {
  64. NavigationRow(title: "Calendar",
  65. icon: "calendar")
  66. {
  67. settingsPath.value.append(Sheet.calendar)
  68. }
  69. NavigationRow(title: "Contact",
  70. icon: "person.circle")
  71. {
  72. settingsPath.value.append(Sheet.contact)
  73. }
  74. }
  75. // ───────── Advanced / Logs ─────────
  76. Section("Advanced Settings") {
  77. NavigationRow(title: "Advanced Settings",
  78. icon: "exclamationmark.shield")
  79. {
  80. settingsPath.value.append(Sheet.advanced)
  81. }
  82. }
  83. Section("Logging") {
  84. NavigationRow(title: "View Log",
  85. icon: "doc.text.magnifyingglass")
  86. {
  87. settingsPath.value.append(Sheet.viewLog)
  88. }
  89. ActionRow(title: "Share Logs",
  90. icon: "square.and.arrow.up",
  91. action: shareLogs)
  92. }
  93. // ───────── Community ─────────
  94. Section("Community") {
  95. LinkRow(title: "LoopFollow Facebook Group",
  96. icon: "person.2.fill",
  97. url: URL(string: "https://www.facebook.com/groups/loopfollowlnl")!)
  98. }
  99. // ───────── Build info ─────────
  100. buildInfoSection
  101. }
  102. .navigationTitle("Settings")
  103. .navigationDestination(for: Sheet.self) { $0.destination }
  104. }
  105. .task { await refreshVersionInfo() }
  106. }
  107. // MARK: – Section builders
  108. @ViewBuilder
  109. private var dataSection: some View {
  110. Section("Data Settings") {
  111. Picker("Units",
  112. selection: Binding(
  113. get: { Storage.shared.units.value },
  114. set: { Storage.shared.units.value = $0 }
  115. )) {
  116. Text("mg/dL").tag("mg/dL")
  117. Text("mmol/L").tag("mmol/L")
  118. }
  119. .pickerStyle(.segmented)
  120. NavigationRow(title: "Nightscout Settings",
  121. icon: "network")
  122. {
  123. settingsPath.value.append(Sheet.nightscout)
  124. }
  125. NavigationRow(title: "Dexcom Settings",
  126. icon: "sensor.tag.radiowaves.forward")
  127. {
  128. settingsPath.value.append(Sheet.dexcom)
  129. }
  130. }
  131. }
  132. @ViewBuilder
  133. private var buildInfoSection: some View {
  134. let build = BuildDetails.default
  135. let ver = AppVersionManager().version()
  136. Section("Build Information") {
  137. keyValue("Version", ver, tint: versionTint)
  138. keyValue("Latest version", latestVersion ?? "Fetching…")
  139. if !(build.isMacApp() || build.isSimulatorBuild()) {
  140. keyValue(build.expirationHeaderString,
  141. dateTimeUtils.formattedDate(from: build.calculateExpirationDate()))
  142. }
  143. keyValue("Built",
  144. dateTimeUtils.formattedDate(from: build.buildDate()))
  145. keyValue("Branch", build.branchAndSha)
  146. }
  147. }
  148. // MARK: – Helpers
  149. private func keyValue(_ key: String,
  150. _ value: String,
  151. tint: Color = .secondary) -> some View
  152. {
  153. HStack {
  154. Text(key)
  155. Spacer()
  156. Text(value).foregroundColor(tint)
  157. }
  158. }
  159. private func refreshVersionInfo() async {
  160. let mgr = AppVersionManager()
  161. let (latest, newer, blacklisted) = await mgr.checkForNewVersionAsync()
  162. latestVersion = latest ?? "Unknown"
  163. let current = mgr.version()
  164. versionTint = blacklisted ? .red
  165. : newer ? .orange
  166. : latest == current ? .green
  167. : .secondary
  168. }
  169. private func shareLogs() {
  170. let files = LogManager.shared.logFilesForTodayAndYesterday()
  171. guard !files.isEmpty else {
  172. UIApplication.shared.topMost?.presentSimpleAlert(
  173. title: "No Logs Available",
  174. message: "There are no logs to share."
  175. )
  176. return
  177. }
  178. let avc = UIActivityViewController(activityItems: files,
  179. applicationActivities: nil)
  180. UIApplication.shared.topMost?.present(avc, animated: true)
  181. }
  182. }
  183. // MARK: – Sheet routing
  184. private enum Sheet: Hashable, Identifiable {
  185. case nightscout, dexcom
  186. case backgroundRefresh
  187. case general, graph
  188. case infoDisplay
  189. case alarmsList, alarmSettings
  190. case remote
  191. case calendar, contact
  192. case advanced
  193. case viewLog
  194. var id: Self { self }
  195. @ViewBuilder
  196. var destination: some View {
  197. switch self {
  198. case .nightscout: NightscoutSettingsView(viewModel: .init())
  199. case .dexcom: DexcomSettingsView(viewModel: .init())
  200. case .backgroundRefresh: BackgroundRefreshSettingsView(viewModel: .init())
  201. case .general: GeneralSettingsView()
  202. case .graph: GraphSettingsView()
  203. case .infoDisplay: InfoDisplaySettingsView(viewModel: .init())
  204. case .alarmsList: AlarmListView()
  205. case .alarmSettings: AlarmSettingsView()
  206. case .remote: RemoteSettingsView(viewModel: .init())
  207. case .calendar: CalendarSettingsView()
  208. case .contact: ContactSettingsView(viewModel: .init())
  209. case .advanced: AdvancedSettingsView(viewModel: .init())
  210. case .viewLog: LogView(viewModel: .init())
  211. }
  212. }
  213. }
  214. // MARK: – UIKit helpers (unchanged)
  215. import UIKit
  216. extension UIApplication {
  217. var topMost: UIViewController? {
  218. guard var top = keyWindow?.rootViewController else { return nil }
  219. while let presented = top.presentedViewController {
  220. top = presented
  221. }
  222. return top
  223. }
  224. }
  225. extension UIViewController {
  226. func presentSimpleAlert(title: String, message: String) {
  227. let a = UIAlertController(title: title,
  228. message: message,
  229. preferredStyle: .alert)
  230. a.addAction(UIAlertAction(title: "OK", style: .default))
  231. present(a, animated: true)
  232. }
  233. }