Warning, /network/kdeconnect-ios/KDE Connect/KDE Connect/Swift Backend/Backend.swift is written in an unsupported language. File is not indexed.

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Lucas Wang <lucas.wang@tuta.io>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 // Original header below:
0008 //
0009 //  Backend.swift
0010 //  KDE Connect Test
0011 //
0012 //  Created by Lucas Wang on 2021-08-04.
0013 //
0014 
0015 import Foundation
0016 import CoreMotion
0017 
0018 // A place to house miscellaneous functions and variables for global usage by the rest of the app
0019 
0020 // Certificate Service provider, to be used for all certificate and Keychain operations
0021 let certificateService: CertificateService = CertificateService()
0022 
0023 // ViewModel object for devices-related functionalities
0024 // TODO: Should this be kept global or local to DevicesView()? Reference might break if this is
0025 // TODO: if global, make singelton
0026 // global but making it local to DevicesView() would likely make it harder to access values in it
0027 // We'll finish developing everything else and see if anything other than DevicesView() needs it.
0028 // I think we probably do since the Unpair function is now in the details view instead of the
0029 // DevicesView()
0030 let connectedDevicesViewModel: ConnectedDevicesViewModel = ConnectedDevicesViewModel()
0031 
0032 // Global ObservableObject to be Observed by needed structs for app-wide information
0033 @available(*, deprecated, renamed: "SelfDeviceData.shared")
0034 let selfDeviceData: SelfDeviceData = .shared
0035 
0036 // Background Service provider, bridged from Obj-C codebase
0037 let backgroundService: BackgroundService = {
0038 #if DEBUG
0039     let setupScreenshotDevices = ProcessInfo.processInfo.arguments.contains("setupScreenshotDevices")
0040 #else
0041     let setupScreenshotDevices = false
0042 #endif
0043     return BackgroundService(
0044         // disconnect background service from connected devices view model if taking screenshots
0045         // so the UI testing instances will only access to fake devices
0046         connectedDeviceViewModel: setupScreenshotDevices ? nil : connectedDevicesViewModel,
0047         certificateService: certificateService
0048     )
0049 }()
0050 
0051 // Device motion manager
0052 let motionManager: CMMotionManager = CMMotionManager()
0053 
0054 // Given the deviceId, saves/overwrites the device object from _device into _settings by encoding it and then into UserDefaults
0055 func saveDeviceToUserDefaults(deviceId: String) {
0056     let deviceData: Data?
0057     do {
0058         deviceData = try NSKeyedArchiver.archivedData(withRootObject: backgroundService._devices[deviceId]!, requiringSecureCoding: true)
0059     } catch {
0060         Logger(category: "Device").fault("\(error.localizedDescription, privacy: .public)")
0061         return
0062     }
0063     backgroundService.settings[backgroundService.devices[deviceId]!._id] = deviceData
0064     UserDefaults.standard.setValue(backgroundService.settings, forKey: "savedDevices")
0065 }