SettingsStateModel.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import LoopKit
  2. import LoopKitUI
  3. import SwiftUI
  4. extension Settings {
  5. final class StateModel: BaseStateModel<Provider> {
  6. @Injected() private var broadcaster: Broadcaster!
  7. @Injected() private var fileManager: FileManager!
  8. @Injected() private var nightscoutManager: NightscoutManager!
  9. @Injected() var pluginManager: PluginManager!
  10. @Injected() var fetchCgmManager: FetchGlucoseManager!
  11. @Published var units: GlucoseUnits = .mgdL
  12. @Published var closedLoop = false
  13. @Published var debugOptions = false
  14. @Published var serviceUIType: ServiceUI.Type?
  15. @Published var setupTidepool = false
  16. private(set) var buildNumber = ""
  17. private(set) var versionNumber = ""
  18. private(set) var branch = ""
  19. private(set) var copyrightNotice = ""
  20. override func subscribe() {
  21. units = settingsManager.settings.units
  22. subscribeSetting(\.debugOptions, on: $debugOptions) { debugOptions = $0 }
  23. subscribeSetting(\.closedLoop, on: $closedLoop) { closedLoop = $0 }
  24. broadcaster.register(SettingsObserver.self, observer: self)
  25. buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "Unknown"
  26. versionNumber = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown"
  27. branch = BuildDetails.default.branchAndSha
  28. copyrightNotice = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String ?? ""
  29. serviceUIType = pluginManager.getServiceTypeByIdentifier("TidepoolService")
  30. }
  31. func logItems() -> [URL] {
  32. var items: [URL] = []
  33. if fileManager.fileExists(atPath: SimpleLogReporter.logFile) {
  34. items.append(URL(fileURLWithPath: SimpleLogReporter.logFile))
  35. }
  36. if fileManager.fileExists(atPath: SimpleLogReporter.logFilePrev) {
  37. items.append(URL(fileURLWithPath: SimpleLogReporter.logFilePrev))
  38. }
  39. return items
  40. }
  41. func uploadProfileAndSettings(_ force: Bool) async {
  42. NSLog("SettingsState Upload Profile and Settings")
  43. await nightscoutManager.uploadProfileAndSettings(force)
  44. }
  45. func hideSettingsModal() {
  46. hideModal()
  47. }
  48. // 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
  49. // Leaving it in here, as it may be a handy functionality for further testing or developers.
  50. // See https://github.com/nightscout/Trio/pull/277 for more information
  51. //
  52. // func resetLoopDocuments() {
  53. // guard let localDocuments = try? FileManager.default.url(
  54. // for: .documentDirectory,
  55. // in: .userDomainMask,
  56. // appropriateFor: nil,
  57. // create: true
  58. // ) else {
  59. // preconditionFailure("Could not get a documents directory URL.")
  60. // }
  61. // let storageURL = localDocuments.appendingPathComponent("PumpManagerState" + ".plist")
  62. // try? FileManager.default.removeItem(at: storageURL)
  63. // }
  64. }
  65. }
  66. extension Settings.StateModel: SettingsObserver {
  67. func settingsDidChange(_ settings: FreeAPSSettings) {
  68. closedLoop = settings.closedLoop
  69. debugOptions = settings.debugOptions
  70. }
  71. }
  72. extension Settings.StateModel: ServiceOnboardingDelegate {
  73. func serviceOnboarding(didCreateService service: Service) {
  74. debug(.nightscout, "Service with identifier \(service.pluginIdentifier) created")
  75. provider.tidepoolManager.addTidepoolService(service: service)
  76. }
  77. func serviceOnboarding(didOnboardService service: Service) {
  78. precondition(service.isOnboarded)
  79. debug(.nightscout, "Service with identifier \(service.pluginIdentifier) onboarded")
  80. }
  81. }
  82. extension Settings.StateModel: CompletionDelegate {
  83. func completionNotifyingDidComplete(_: CompletionNotifying) {
  84. setupTidepool = false
  85. provider.tidepoolManager.forceUploadData(device: fetchCgmManager.cgmManager?.cgmManagerStatus.device)
  86. }
  87. }