SettingsStateModel.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 closedLoop = false
  12. @Published var debugOptions = false
  13. @Published var animatedBackground = 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. subscribeSetting(\.debugOptions, on: $debugOptions) { debugOptions = $0 }
  22. subscribeSetting(\.closedLoop, on: $closedLoop) { closedLoop = $0 }
  23. broadcaster.register(SettingsObserver.self, observer: self)
  24. buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "Unknown"
  25. versionNumber = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown"
  26. <<<<<<< HEAD
  27. // Read branch information from the branch.txt instead of infoDictionary
  28. if let branchFileURL = Bundle.main.url(forResource: "branch", withExtension: "txt"),
  29. let branchFileContent = try? String(contentsOf: branchFileURL)
  30. {
  31. let lines = branchFileContent.components(separatedBy: .newlines)
  32. for line in lines {
  33. let components = line.components(separatedBy: "=")
  34. if components.count == 2 {
  35. let key = components[0].trimmingCharacters(in: .whitespaces)
  36. let value = components[1].trimmingCharacters(in: .whitespaces)
  37. if key == "BRANCH" {
  38. branch = value
  39. break
  40. }
  41. }
  42. }
  43. } else {
  44. branch = "Unknown"
  45. }
  46. =======
  47. branch = BuildDetails.default.branchAndSha
  48. >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133
  49. copyrightNotice = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String ?? ""
  50. subscribeSetting(\.animatedBackground, on: $animatedBackground) { animatedBackground = $0 }
  51. serviceUIType = pluginManager.getServiceTypeByIdentifier("TidepoolService")
  52. }
  53. func logItems() -> [URL] {
  54. var items: [URL] = []
  55. if fileManager.fileExists(atPath: SimpleLogReporter.logFile) {
  56. items.append(URL(fileURLWithPath: SimpleLogReporter.logFile))
  57. }
  58. if fileManager.fileExists(atPath: SimpleLogReporter.logFilePrev) {
  59. items.append(URL(fileURLWithPath: SimpleLogReporter.logFilePrev))
  60. }
  61. return items
  62. }
  63. func uploadProfileAndSettings(_ force: Bool) {
  64. NSLog("SettingsState Upload Profile and Settings")
  65. nightscoutManager.uploadProfileAndSettings(force)
  66. }
  67. func hideSettingsModal() {
  68. hideModal()
  69. }
  70. // 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
  71. // Leaving it in here, as it may be a handy functionality for further testing or developers.
  72. // See https://github.com/nightscout/Trio/pull/277 for more information
  73. //
  74. // func resetLoopDocuments() {
  75. // guard let localDocuments = try? FileManager.default.url(
  76. // for: .documentDirectory,
  77. // in: .userDomainMask,
  78. // appropriateFor: nil,
  79. // create: true
  80. // ) else {
  81. // preconditionFailure("Could not get a documents directory URL.")
  82. // }
  83. // let storageURL = localDocuments.appendingPathComponent("PumpManagerState" + ".plist")
  84. // try? FileManager.default.removeItem(at: storageURL)
  85. // }
  86. }
  87. }
  88. extension Settings.StateModel: SettingsObserver {
  89. func settingsDidChange(_ settings: FreeAPSSettings) {
  90. closedLoop = settings.closedLoop
  91. debugOptions = settings.debugOptions
  92. }
  93. }
  94. extension Settings.StateModel: ServiceOnboardingDelegate {
  95. func serviceOnboarding(didCreateService service: Service) {
  96. debug(.nightscout, "Service with identifier \(service.pluginIdentifier) created")
  97. provider.tidepoolManager.addTidepoolService(service: service)
  98. }
  99. func serviceOnboarding(didOnboardService service: Service) {
  100. precondition(service.isOnboarded)
  101. debug(.nightscout, "Service with identifier \(service.pluginIdentifier) onboarded")
  102. }
  103. }
  104. extension Settings.StateModel: CompletionDelegate {
  105. func completionNotifyingDidComplete(_: CompletionNotifying) {
  106. setupTidepool = false
  107. provider.tidepoolManager.forceUploadData(device: fetchCgmManager.cgmManager?.cgmManagerStatus.device)
  108. }
  109. }