| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import LoopKit
- import LoopKitUI
- import SwiftUI
- extension Settings {
- final class StateModel: BaseStateModel<Provider> {
- @Injected() private var broadcaster: Broadcaster!
- @Injected() private var fileManager: FileManager!
- @Injected() private var nightscoutManager: NightscoutManager!
- @Injected() var pluginManager: PluginManager!
- @Injected() var fetchCgmManager: FetchGlucoseManager!
- @Published var closedLoop = false
- @Published var debugOptions = false
- @Published var animatedBackground = false
- @Published var serviceUIType: ServiceUI.Type?
- @Published var setupTidePool = false
- private(set) var buildNumber = ""
- private(set) var versionNumber = ""
- private(set) var branch = ""
- private(set) var copyrightNotice = ""
- override func subscribe() {
- subscribeSetting(\.debugOptions, on: $debugOptions) { debugOptions = $0 }
- subscribeSetting(\.closedLoop, on: $closedLoop) { closedLoop = $0 }
- broadcaster.register(SettingsObserver.self, observer: self)
- buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "Unknown"
- versionNumber = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown"
- // Read branch information from the branch.txt instead of infoDictionary
- if let branchFileURL = Bundle.main.url(forResource: "branch", withExtension: "txt"),
- let branchFileContent = try? String(contentsOf: branchFileURL)
- {
- let lines = branchFileContent.components(separatedBy: .newlines)
- for line in lines {
- let components = line.components(separatedBy: "=")
- if components.count == 2 {
- let key = components[0].trimmingCharacters(in: .whitespaces)
- let value = components[1].trimmingCharacters(in: .whitespaces)
- if key == "BRANCH" {
- branch = value
- break
- }
- }
- }
- } else {
- branch = "Unknown"
- }
- copyrightNotice = Bundle.main.infoDictionary?["NSHumanReadableCopyright"] as? String ?? ""
- subscribeSetting(\.animatedBackground, on: $animatedBackground) { animatedBackground = $0 }
- serviceUIType = pluginManager.getServiceTypeByIdentifier("TidepoolService")
- }
- func logItems() -> [URL] {
- var items: [URL] = []
- if fileManager.fileExists(atPath: SimpleLogReporter.logFile) {
- items.append(URL(fileURLWithPath: SimpleLogReporter.logFile))
- }
- if fileManager.fileExists(atPath: SimpleLogReporter.logFilePrev) {
- items.append(URL(fileURLWithPath: SimpleLogReporter.logFilePrev))
- }
- return items
- }
- func uploadProfileAndSettings(_ force: Bool) {
- NSLog("SettingsState Upload Profile and Settings")
- nightscoutManager.uploadProfileAndSettings(force)
- }
- func hideSettingsModal() {
- hideModal()
- }
- }
- }
- extension Settings.StateModel: SettingsObserver {
- func settingsDidChange(_ settings: FreeAPSSettings) {
- closedLoop = settings.closedLoop
- debugOptions = settings.debugOptions
- }
- }
- extension Settings.StateModel: ServiceOnboardingDelegate {
- func serviceOnboarding(didCreateService service: Service) {
- debug(.nightscout, "Service with identifier \(service.pluginIdentifier) created")
- provider.tidePoolManager.addTidePoolService(service: service)
- }
- func serviceOnboarding(didOnboardService service: Service) {
- precondition(service.isOnboarded)
- debug(.nightscout, "Service with identifier \(service.pluginIdentifier) onboarded")
- }
- }
- extension Settings.StateModel: CompletionDelegate {
- func completionNotifyingDidComplete(_: CompletionNotifying) {
- setupTidePool = false
- provider.tidePoolManager.forceUploadData(device: fetchCgmManager.cgmManager?.cgmManagerStatus.device)
- }
- }
|