SettingsStateModel.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // Read branch information from the branch.txt instead of infoDictionary
  27. if let branchFileURL = Bundle.main.url(forResource: "branch", withExtension: "txt"),
  28. let branchFileContent = try? String(contentsOf: branchFileURL)
  29. {
  30. let lines = branchFileContent.components(separatedBy: .newlines)
  31. for line in lines {
  32. let components = line.components(separatedBy: "=")
  33. if components.count == 2 {
  34. let key = components[0].trimmingCharacters(in: .whitespaces)
  35. let value = components[1].trimmingCharacters(in: .whitespaces)
  36. if key == "BRANCH" {
  37. branch = value
  38. break
  39. }
  40. }
  41. }
  42. } else {
  43. branch = "Unknown"
  44. }
  45. copyrightNotice = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String ?? ""
  46. subscribeSetting(\.animatedBackground, on: $animatedBackground) { animatedBackground = $0 }
  47. serviceUIType = pluginManager.getServiceTypeByIdentifier("TidepoolService")
  48. }
  49. func logItems() -> [URL] {
  50. var items: [URL] = []
  51. if fileManager.fileExists(atPath: SimpleLogReporter.logFile) {
  52. items.append(URL(fileURLWithPath: SimpleLogReporter.logFile))
  53. }
  54. if fileManager.fileExists(atPath: SimpleLogReporter.logFilePrev) {
  55. items.append(URL(fileURLWithPath: SimpleLogReporter.logFilePrev))
  56. }
  57. return items
  58. }
  59. func uploadProfileAndSettings(_ force: Bool) {
  60. NSLog("SettingsState Upload Profile and Settings")
  61. nightscoutManager.uploadProfileAndSettings(force)
  62. }
  63. func hideSettingsModal() {
  64. hideModal()
  65. }
  66. }
  67. }
  68. extension Settings.StateModel: SettingsObserver {
  69. func settingsDidChange(_ settings: FreeAPSSettings) {
  70. closedLoop = settings.closedLoop
  71. debugOptions = settings.debugOptions
  72. }
  73. }
  74. extension Settings.StateModel: ServiceOnboardingDelegate {
  75. func serviceOnboarding(didCreateService service: Service) {
  76. debug(.nightscout, "Service with identifier \(service.pluginIdentifier) created")
  77. provider.tidePoolManager.addTidePoolService(service: service)
  78. }
  79. func serviceOnboarding(didOnboardService service: Service) {
  80. precondition(service.isOnboarded)
  81. debug(.nightscout, "Service with identifier \(service.pluginIdentifier) onboarded")
  82. }
  83. }
  84. extension Settings.StateModel: CompletionDelegate {
  85. func completionNotifyingDidComplete(_: CompletionNotifying) {
  86. setupTidePool = false
  87. provider.tidePoolManager.forceUploadData(device: fetchCgmManager.cgmManager?.cgmManagerStatus.device)
  88. }
  89. }