Add macOS support (#23)
* Add macOS support * Update README * Update to v0.2.0 * Document breaking changes * Delete widget tests
This commit is contained in:
@@ -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 "[]"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// NearbyFilesStore.swift
|
||||
// nearby_service
|
||||
//
|
||||
// Created by Kseniia Nikitina on 3/2/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import MultipeerConnectivity
|
||||
|
||||
class NearbyFilesStore {
|
||||
static let instance = NearbyFilesStore()
|
||||
|
||||
private var paths : [String] = []
|
||||
private var senderName: String? = nil
|
||||
private var maxCount: Int = 0
|
||||
private var count: Int = 0
|
||||
private var id: String? = nil
|
||||
|
||||
func startReceiving(command: NearbyStartCommand) {
|
||||
self.paths.removeAll()
|
||||
self.id = command.id
|
||||
self.senderName = command.senderName
|
||||
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 clear() {
|
||||
self.id = nil
|
||||
self.paths.removeAll()
|
||||
self.senderName = nil
|
||||
self.maxCount = 0
|
||||
self.count = 0
|
||||
}
|
||||
|
||||
func toDartFormat(peerID: MCPeerID) -> String? {
|
||||
if (senderName != nil && id != nil) {
|
||||
let object = [
|
||||
"id": id!,
|
||||
"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
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// NearbyRequestsStore.swift
|
||||
// nearby_service
|
||||
//
|
||||
// Created by Kseniia Nikitina on 5/2/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class NearbyRequestsStore {
|
||||
static let instance = NearbyRequestsStore()
|
||||
|
||||
private var requests : [NearbyMessageFilesRequest] = []
|
||||
|
||||
|
||||
func add(request: NearbyMessageFilesRequest) {
|
||||
requests.append(request)
|
||||
}
|
||||
|
||||
func find(for id: String) -> NearbyMessageFilesRequest? {
|
||||
return requests.first { request in
|
||||
return request.id == id
|
||||
}
|
||||
}
|
||||
|
||||
func remove(for id: String) {
|
||||
self.requests = requests.filter{$0.id != id}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user