Refactor project, add loading/editing/saving notes, add client/server basic logic
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// NoteEditingSessionClient.swift
|
||||
// Peered
|
||||
//
|
||||
// Created by Oskar Chybowski on 05/10/2025.
|
||||
//
|
||||
|
||||
|
||||
import MultipeerConnectivity
|
||||
import Foundation
|
||||
|
||||
@Observable
|
||||
final class NoteEditingSessionClient: NSObject {
|
||||
private let session: MCSession
|
||||
private let advertiser: MCNearbyServiceAdvertiser
|
||||
|
||||
init(id: String) {
|
||||
let id = MCPeerID(displayName: id)
|
||||
session = MCSession(
|
||||
peer: id,
|
||||
securityIdentity: nil,
|
||||
encryptionPreference: .required
|
||||
)
|
||||
advertiser = MCNearbyServiceAdvertiser(
|
||||
peer: id,
|
||||
discoveryInfo: [:],
|
||||
serviceType: "peered"
|
||||
)
|
||||
super.init()
|
||||
advertiser.delegate = self
|
||||
}
|
||||
|
||||
func startBrowsingForNotes() {
|
||||
advertiser.startAdvertisingPeer()
|
||||
}
|
||||
|
||||
func stopBrowsingForNotes() {
|
||||
advertiser.stopAdvertisingPeer()
|
||||
}
|
||||
}
|
||||
|
||||
extension NoteEditingSessionClient: MCNearbyServiceAdvertiserDelegate {
|
||||
func advertiser(
|
||||
_ advertiser: MCNearbyServiceAdvertiser,
|
||||
didReceiveInvitationFromPeer peerID: MCPeerID,
|
||||
withContext context: Data?,
|
||||
invitationHandler: @escaping (Bool, MCSession?) -> Void
|
||||
) {
|
||||
DispatchQueue.main.async {
|
||||
invitationHandler(true, self.session)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,17 @@ struct NotesStorage {
|
||||
let storageProvider: FileManager = FileManager.default
|
||||
|
||||
func loadNotes() -> [Note] {
|
||||
let files = try! storageProvider.contentsOfDirectory(atPath: URL.documentsDirectory.path)
|
||||
let files = try! storageProvider
|
||||
.contentsOfDirectory(atPath: URL.documentsDirectory.path)
|
||||
var notes = [Note]()
|
||||
|
||||
for file in files.compactMap(URL.init) {
|
||||
for file in files.compactMap({
|
||||
URL(
|
||||
filePath: $0,
|
||||
directoryHint: .notDirectory,
|
||||
relativeTo: URL.documentsDirectory
|
||||
)
|
||||
}) {
|
||||
let name = file.lastPathComponent
|
||||
let note = Note(
|
||||
name: name,
|
||||
|
||||
Reference in New Issue
Block a user