SettingsStateModel.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import SwiftUI
  2. extension Settings {
  3. final class StateModel: BaseStateModel<Provider> {
  4. @Injected() private var broadcaster: Broadcaster!
  5. @Injected() private var fileManager: FileManager!
  6. @Injected() private var nightscoutManager: NightscoutManager!
  7. @Published var closedLoop = false
  8. @Published var debugOptions = false
  9. @Published var animatedBackground = false
  10. private(set) var buildNumber = ""
  11. private(set) var versionNumber = ""
  12. private(set) var branch = ""
  13. private(set) var copyrightNotice = ""
  14. override func subscribe() {
  15. subscribeSetting(\.debugOptions, on: $debugOptions) { debugOptions = $0 }
  16. subscribeSetting(\.closedLoop, on: $closedLoop) { closedLoop = $0 }
  17. broadcaster.register(SettingsObserver.self, observer: self)
  18. buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "Unknown"
  19. versionNumber = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown"
  20. branch = Bundle.main.infoDictionary?["BuildBranch"] as? String ?? "Unknown"
  21. copyrightNotice = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String ?? ""
  22. subscribeSetting(\.animatedBackground, on: $animatedBackground) { animatedBackground = $0 }
  23. }
  24. func logItems() -> [URL] {
  25. var items: [URL] = []
  26. if fileManager.fileExists(atPath: SimpleLogReporter.logFile) {
  27. items.append(URL(fileURLWithPath: SimpleLogReporter.logFile))
  28. }
  29. if fileManager.fileExists(atPath: SimpleLogReporter.logFilePrev) {
  30. items.append(URL(fileURLWithPath: SimpleLogReporter.logFilePrev))
  31. }
  32. return items
  33. }
  34. func uploadProfile() {
  35. NSLog("SettingsState Upload Profile")
  36. nightscoutManager.uploadProfile()
  37. }
  38. func hideSettingsModal() {
  39. nightscoutManager.uploadProfile()
  40. hideModal()
  41. }
  42. }
  43. }
  44. extension Settings.StateModel: SettingsObserver {
  45. func settingsDidChange(_ settings: FreeAPSSettings) {
  46. closedLoop = settings.closedLoop
  47. debugOptions = settings.debugOptions
  48. }
  49. }