move Peered to implementation directory
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Note.swift
|
||||
// Peered
|
||||
//
|
||||
// Created by Oskar Chybowski on 25/09/2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct Note: Identifiable {
|
||||
var id: URL { path }
|
||||
|
||||
let name: String
|
||||
let path: URL
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import MultipeerConnectivity
|
||||
import Foundation
|
||||
|
||||
struct NoteInvitation: Identifiable {
|
||||
struct NoteContent: Codable {
|
||||
let title: String
|
||||
let noteSnapshot: String
|
||||
}
|
||||
|
||||
var id: MCPeerID { invitatorID }
|
||||
let noteName: String
|
||||
let invitatorID: MCPeerID
|
||||
let note: NoteContent
|
||||
private var invitationHandler: ((Bool) -> Void)?
|
||||
|
||||
init(
|
||||
noteName: String,
|
||||
invitatorID: MCPeerID,
|
||||
note: NoteContent,
|
||||
invitationHandler: ((Bool) -> Void)? = nil
|
||||
) {
|
||||
self.noteName = noteName
|
||||
self.invitatorID = invitatorID
|
||||
self.note = note
|
||||
self.invitationHandler = invitationHandler
|
||||
}
|
||||
|
||||
mutating func accept() {
|
||||
invitationHandler?(true)
|
||||
invitationHandler = nil
|
||||
}
|
||||
|
||||
mutating func decline() {
|
||||
invitationHandler?(false)
|
||||
invitationHandler = nil
|
||||
}
|
||||
}
|
||||
|
||||
@Observable
|
||||
final class NoteEditingSessionClient: NSObject {
|
||||
private let session: MCSession
|
||||
private let advertiser: MCNearbyServiceAdvertiser
|
||||
private let ownPeer: MCPeerID
|
||||
|
||||
var invitations: [NoteInvitation] = [] {
|
||||
didSet {
|
||||
print(invitations)
|
||||
}
|
||||
}
|
||||
|
||||
init(peer: MCPeerID) {
|
||||
ownPeer = peer
|
||||
session = MCSession(
|
||||
peer: peer,
|
||||
securityIdentity: nil,
|
||||
encryptionPreference: .required
|
||||
)
|
||||
advertiser = MCNearbyServiceAdvertiser(
|
||||
peer: peer,
|
||||
discoveryInfo: [:],
|
||||
serviceType: "peered"
|
||||
)
|
||||
super.init()
|
||||
advertiser.delegate = self
|
||||
}
|
||||
|
||||
func startBrowsingForNotes() {
|
||||
advertiser.startAdvertisingPeer()
|
||||
}
|
||||
|
||||
func stopBrowsingForNotes() {
|
||||
advertiser.stopAdvertisingPeer()
|
||||
}
|
||||
|
||||
func send(note: String, to peer: MCPeerID) {
|
||||
try! session.send(
|
||||
note.data(using: .utf8)!,
|
||||
toPeers: [peer],
|
||||
with: .reliable
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension NoteEditingSessionClient: MCNearbyServiceAdvertiserDelegate {
|
||||
func advertiser(
|
||||
_ advertiser: MCNearbyServiceAdvertiser,
|
||||
didReceiveInvitationFromPeer peerID: MCPeerID,
|
||||
withContext context: Data?,
|
||||
invitationHandler: @escaping (Bool, MCSession?) -> Void
|
||||
) {
|
||||
guard
|
||||
let context,
|
||||
let noteContent = try? JSONDecoder().decode(NoteInvitation.NoteContent.self, from: context)
|
||||
else { fatalError() }
|
||||
|
||||
invitations.append(
|
||||
.init(
|
||||
noteName: noteContent.title,
|
||||
invitatorID: peerID,
|
||||
note: noteContent,
|
||||
invitationHandler: { [weak self, invitationHandler] accepted in
|
||||
guard let self else { return }
|
||||
invitationHandler(accepted, self.session)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
guard let idx = self.invitations.firstIndex(where: { $0.id == peerID }) else { return }
|
||||
self.invitations.remove(at: idx)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import SwiftUI
|
||||
|
||||
struct NoteInvitationView: View {
|
||||
@Binding var invitation: NoteInvitation
|
||||
let joinTapped: () -> Void
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Text(invitation.noteName)
|
||||
Spacer()
|
||||
Button("Join", action: joinTapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// NotesStorage.swift
|
||||
// Peered
|
||||
//
|
||||
// Created by Oskar Chybowski on 25/09/2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct NotesStorage {
|
||||
let storageProvider: FileManager = FileManager.default
|
||||
|
||||
func loadNotes() -> [Note] {
|
||||
let files = try! storageProvider
|
||||
.contentsOfDirectory(atPath: URL.documentsDirectory.path)
|
||||
var notes = [Note]()
|
||||
|
||||
for file in files.compactMap({
|
||||
URL(
|
||||
filePath: $0,
|
||||
directoryHint: .notDirectory,
|
||||
relativeTo: URL.documentsDirectory
|
||||
)
|
||||
}) {
|
||||
let name = file.lastPathComponent
|
||||
let note = Note(
|
||||
name: name,
|
||||
path: file
|
||||
)
|
||||
|
||||
notes.append(note)
|
||||
}
|
||||
|
||||
return notes
|
||||
}
|
||||
|
||||
func createNote(name: String) {
|
||||
let currentNotes = loadNotes()
|
||||
var proposedName = name
|
||||
var index: Int? = nil
|
||||
|
||||
while currentNotes.contains(where: { $0.name == proposedName }) {
|
||||
if let _index = index {
|
||||
index = _index + 1
|
||||
} else {
|
||||
index = 1
|
||||
}
|
||||
}
|
||||
|
||||
proposedName = if let index {
|
||||
"\(proposedName) \(index)"
|
||||
} else {
|
||||
proposedName
|
||||
}
|
||||
let pathToWrite = URL.documentsDirectory.appendingPathComponent(proposedName).appendingPathExtension(for: .text)
|
||||
try! String().write(to: pathToWrite, atomically: true, encoding: .utf8)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user