feat(*): add sender to files pack result

This commit is contained in:
ksenia312
2024-02-04 22:25:57 +01:00
parent a1ac4488ca
commit d6840a4313
25 changed files with 199 additions and 152 deletions
+7 -8
View File
@@ -6,30 +6,29 @@
//
import Foundation
import MultipeerConnectivity
class NearbyStartCommand {
init( id: String, filesCount: Int) {
self.id = id
init(senderName: String, filesCount: Int) {
self.senderName = senderName
self.filesCount = filesCount
}
static func fromUserInfo(userInfo: NearbyUserInfo)-> NearbyStartCommand? {
if let id = userInfo.dictionary["id"] as? String ,
if let name = userInfo.dictionary["name"] as? String,
let filesCount = userInfo.dictionary["filesCount"] as? Int
{
return NearbyStartCommand(
id: id, filesCount: filesCount
)
return NearbyStartCommand(senderName: name, filesCount: filesCount)
}
return nil
}
func toDictionary() -> [String: Any] {
return ["id": id, "filesCount": filesCount]
return ["name": senderName, "filesCount": filesCount]
}
let id: String
let senderName: String
let filesCount: Int
}
+4 -2
View File
@@ -47,15 +47,17 @@ extension NearbySession: MCSessionDelegate {
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {
guard let localURL = localURL else { return }
let destinationURL = localURL.deletingLastPathComponent().appendingPathComponent("\(resourceName)")
var destinationURL = localURL.deletingLastPathComponent().appendingPathComponent("\(resourceName)")
if FileManager.default.fileExists(atPath: destinationURL.path) {
destinationURL = localURL.deletingLastPathComponent().appendingPathComponent("New_\(resourceName)")
}
do {
try FileManager.default.moveItem(at: localURL, to: destinationURL)
} catch {
Logger.error(message: "Error moving file: \(error)")
}
NotificationCenter.default.post(
name: ON_RESOURCE_RECEIVED,
object: nil,
+7 -2
View File
@@ -124,8 +124,13 @@ class NearbyManager: NSObject {
do {
let device = NearbyDevicesStore.instance.find(for: receiverId)
if let requireDevice = device {
try requireDevice.session?.session?.send(
try JSONSerialization.data(withJSONObject: NearbyStartCommand( id: id, filesCount: paths.count).toDictionary()),
let command = NearbyStartCommand(
senderName: requireDevice.name,
filesCount: paths.count
).toDictionary()
try requireDevice.session?.session?.send(
try JSONSerialization.data(withJSONObject: command),
toPeers: [requireDevice.peerID],
with: MCSessionSendDataMode.reliable
)
+5 -1
View File
@@ -33,13 +33,17 @@ extension NearbyServicePlugin {
}
@objc func onResourceReceived(notification: Notification) {
DispatchQueue.main.async {
if let userInfo = NearbyUserInfo.fromDictionary(userInfo: notification.userInfo) {
if let url = userInfo.dictionary["url"] as? URL {
NearbyFilesStore.instance.add(url: url)
if (NearbyFilesStore.instance.checkIsFull()) {
self.channel.invokeMethod(DART_COMMAND_RESOURCES_RECEIVED, arguments: NearbyFilesStore.instance.toDartFormat())
self.channel.invokeMethod(DART_COMMAND_RESOURCES_RECEIVED, arguments: NearbyFilesStore.instance.toDartFormat(peerID: userInfo.peerID))
NearbyFilesStore.instance.clear()
}
}
}
+25 -11
View File
@@ -6,18 +6,19 @@
//
import Foundation
import MultipeerConnectivity
class NearbyFilesStore {
static let instance = NearbyFilesStore()
private var paths : [String] = []
private var id: String? = nil
private var senderName: String? = nil
private var maxCount: Int = 0
private var count: Int = 0
func startReceiving(command: NearbyStartCommand) {
self.paths.removeAll()
self.id = command.id
self.senderName = command.senderName
self.maxCount = command.filesCount
self.count = 0
}
@@ -31,16 +32,29 @@ class NearbyFilesStore {
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
func clear() {
self.paths.removeAll()
self.senderName = nil
self.maxCount = 0
self.count = 0
}
func toDartFormat(peerID: MCPeerID) -> String? {
if (senderName != nil) {
let object = [
"files": paths.map { ["path": $0]},
"sender": ["id": peerID.displayName, "displayName": senderName!]
] as [String : Any]
do {
let jsonData = try JSONSerialization.data(withJSONObject: object)
if let jsonString = String(data: jsonData, encoding: .utf8) {
return jsonString
}
} catch {
return nil
}
} catch {
return "[]"
return nil
}
return "[]"
return nil
}
}