Warning, /network/kdeconnect-ios/KDE Connect/KDE Connect/Swift Backend/ConnectedDevicesViewModel.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 //  ConnectedDevicesViewModel.swift
0010 //  KDE Connect Test
0011 //
0012 //  Created by Lucas Wang on 2021-08-09.
0013 //
0014 
0015 import SwiftUI
0016 import AVFoundation
0017 import CryptoKit
0018 import Combine
0019 
0020 extension Notification.Name {
0021     static let didReceivePairRequestNotification = Notification.Name("didReceivePairRequestNotification")
0022     static let pairRequestTimedOutNotification = Notification.Name("pairRequestTimedOutNotification")
0023     static let pairRequestSucceedNotification = Notification.Name("pairRequestSucceedNotification")
0024     static let pairRequestRejectedNotification = Notification.Name("pairRequestRejectedNotification")
0025 }
0026 
0027 @objc class ConnectedDevicesViewModel: NSObject, ObservableObject {
0028     @Published
0029     var connectedDevices: [String: String] = [:]
0030     @Published
0031     var visibleDevices: [String: String] = [:]
0032     @Published
0033     var savedDevices: [String: String] = [:]
0034     private let logger = Logger()
0035     
0036     @objc func onPairRequest(_ deviceId: String!) {
0037         NotificationCenter.default.post(name: .didReceivePairRequestNotification, object: nil,
0038                                         userInfo: ["deviceID": deviceId!])
0039     }
0040     
0041     @objc func onPairTimeout(_ deviceId: String!) {
0042         NotificationCenter.default.post(name: .pairRequestTimedOutNotification, object: nil,
0043                                         userInfo: ["deviceID": deviceId!])
0044     }
0045     
0046     @objc func onPairSuccess(_ deviceId: String!) {
0047         guard let cert = certificateService.tempRemoteCerts[deviceId] else {
0048             SystemSound.audioError.play()
0049             logger.fault("Pairing succeeded without certificate for remote device \(deviceId!, privacy: .private(mask: .hash))")
0050             return
0051         }
0052         
0053         let status = certificateService.saveRemoteDeviceCertToKeychain(cert: cert, deviceId: deviceId)
0054         logger.info("Remote certificate saved into local Keychain with status \(status)")
0055         backgroundService._devices[deviceId]!._SHA256HashFormatted = certificateService.SHA256HashDividedAndFormatted(hashDescription: SHA256.hash(data: SecCertificateCopyData(certificateService.tempRemoteCerts[deviceId]!) as Data).description)
0056         
0057         onDevicesListUpdated()
0058         NotificationCenter.default.post(name: .pairRequestSucceedNotification, object: nil,
0059                                         userInfo: ["deviceID": deviceId!])
0060     }
0061     
0062     @objc func onPairRejected(_ deviceId: String!) {
0063         NotificationCenter.default.post(name: .pairRequestRejectedNotification, object: nil,
0064                                         userInfo: ["deviceID": deviceId!])
0065     }
0066     
0067     @objc func onDevicesListUpdated(
0068         devicesListsMap: [String: [String: String]] = backgroundService.getDevicesLists()
0069     ) {
0070         DispatchQueue.main.async { [weak self] in
0071             withAnimation {
0072                 guard let self = self else { return }
0073                 self.connectedDevices = devicesListsMap["connected"]!
0074                 self.visibleDevices = devicesListsMap["visible"]!
0075                 self.savedDevices = devicesListsMap["remembered"]!
0076             }
0077         }
0078     }
0079 
0080     @objc static func isDeviceCurrentlyPairedAndConnected(_ deviceId: String) -> Bool {
0081         if let device = backgroundService._devices[deviceId] {
0082             return device.isPaired() && device.isReachable()
0083         }
0084         return false
0085     }
0086     
0087     @objc static func getDirectIPList() -> [String] {
0088         return SelfDeviceData.shared.directIPs
0089     }
0090 }