feat(ios): files sending

This commit is contained in:
ksenia312
2024-02-04 16:23:48 +01:00
parent 32a9f2a923
commit 66a79eb4ba
29 changed files with 682 additions and 236 deletions
@@ -0,0 +1,59 @@
//
// NearbyDevicesStore.swift
// nearby_service
//
// Created by Kseniia Nikitina on 16/1/24.
//
import Foundation
import MultipeerConnectivity
class NearbyDevicesStore : NSObject {
static let instance = NearbyDevicesStore()
private var devices : [NearbyDevice] = []
func getDevices() -> [NearbyDevice] {
return devices
}
func find(for deviceId: String) -> NearbyDevice? {
return devices.first { device in
return device.peerID.displayName == deviceId
}
}
func add(for peerID: MCPeerID, discoveryInfo: [String: String]? = nil) -> NearbyDevice? {
devices = devices.filter{$0.peerID.displayName != peerID.displayName}
let device = NearbyDevice.fromDictionary(for: discoveryInfo, with: peerID)
self.devices.append(device)
return device
}
func remove(for peerID: MCPeerID) {
self.devices = devices.filter{$0.peerID.displayName != peerID.displayName}
}
func clear() {
self.devices = []
}
func toDartFormat() -> String {
let devicesObject = devices.map { device in
return device.toDictionary()
}
do {
let jsonData = try JSONSerialization.data(withJSONObject: devicesObject)
if let jsonString = String(data: jsonData, encoding: .utf8) {
return jsonString
}
} catch {
return "[]"
}
return "[]"
}
}
+46
View File
@@ -0,0 +1,46 @@
//
// NearbyFilesStore.swift
// nearby_service
//
// Created by Kseniia Nikitina on 3/2/24.
//
import Foundation
class NearbyFilesStore {
static let instance = NearbyFilesStore()
private var paths : [String] = []
private var id: String? = nil
private var maxCount: Int = 0
private var count: Int = 0
func startReceiving(command: NearbyStartCommand) {
self.paths.removeAll()
self.id = command.id
self.maxCount = command.filesCount
self.count = 0
}
func add(url: URL) {
paths.append(url.path)
self.count = self.count + 1
}
func checkIsFull() -> Bool {
return maxCount <= count
}
func toDartFormat() -> String? {
let pathsObject = paths.map { ["path": $0]}
do {
let jsonData = try JSONSerialization.data(withJSONObject: pathsObject)
if let jsonString = String(data: jsonData, encoding: .utf8) {
return jsonString
}
} catch {
return "[]"
}
return "[]"
}
}