SettingsMenuView.swift 11 KB

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