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