SettingsStateModel.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // Read branch information from the branch.txt instead of infoDictionary
  21. if let branchFileURL = Bundle.main.url(forResource: "branch", withExtension: "txt"),
  22. let branchFileContent = try? String(contentsOf: branchFileURL)
  23. {
  24. let lines = branchFileContent.components(separatedBy: .newlines)
  25. for line in lines {
  26. let components = line.components(separatedBy: "=")
  27. if components.count == 2 {
  28. let key = components[0].trimmingCharacters(in: .whitespaces)
  29. let value = components[1].trimmingCharacters(in: .whitespaces)
  30. if key == "BRANCH" {
  31. branch = value
  32. break
  33. }
  34. }
  35. }
  36. } else {
  37. branch = "Unknown"
  38. }
  39. copyrightNotice = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String ?? ""
  40. subscribeSetting(\.animatedBackground, on: $animatedBackground) { animatedBackground = $0 }
  41. }
  42. func logItems() -> [URL] {
  43. var items: [URL] = []
  44. if fileManager.fileExists(atPath: SimpleLogReporter.logFile) {
  45. items.append(URL(fileURLWithPath: SimpleLogReporter.logFile))
  46. }
  47. if fileManager.fileExists(atPath: SimpleLogReporter.logFilePrev) {
  48. items.append(URL(fileURLWithPath: SimpleLogReporter.logFilePrev))
  49. }
  50. return items
  51. }
  52. func uploadProfile() {
  53. NSLog("SettingsState Upload Profile")
  54. nightscoutManager.uploadProfile()
  55. }
  56. func hideSettingsModal() {
  57. nightscoutManager.uploadProfile()
  58. hideModal()
  59. }
  60. }
  61. }
  62. extension Settings.StateModel: SettingsObserver {
  63. func settingsDidChange(_ settings: FreeAPSSettings) {
  64. closedLoop = settings.closedLoop
  65. debugOptions = settings.debugOptions
  66. }
  67. }