format code
This commit is contained in:
@@ -8,8 +8,8 @@
|
||||
import Foundation
|
||||
|
||||
struct Note: Identifiable {
|
||||
var id: URL { path }
|
||||
var id: URL { path }
|
||||
|
||||
let name: String
|
||||
let path: URL
|
||||
let name: String
|
||||
let path: URL
|
||||
}
|
||||
|
||||
@@ -1,131 +1,140 @@
|
||||
import MultipeerConnectivity
|
||||
import Foundation
|
||||
import Combine
|
||||
import Foundation
|
||||
import MultipeerConnectivity
|
||||
|
||||
struct NoteInvitation: Identifiable {
|
||||
struct NoteContent: Codable {
|
||||
let title: String
|
||||
let noteSnapshot: String
|
||||
}
|
||||
|
||||
var id: MCPeerID { invitatorID }
|
||||
var noteName: String { note.title }
|
||||
let invitatorID: MCPeerID
|
||||
let note: NoteContent
|
||||
private var invitationHandler: ((Bool) -> Void)?
|
||||
|
||||
init(
|
||||
invitatorID: MCPeerID,
|
||||
note: NoteContent,
|
||||
invitationHandler: ((Bool) -> Void)? = nil
|
||||
) {
|
||||
self.invitatorID = invitatorID
|
||||
self.note = note
|
||||
self.invitationHandler = invitationHandler
|
||||
}
|
||||
|
||||
mutating func accept() {
|
||||
invitationHandler?(true)
|
||||
invitationHandler = nil
|
||||
}
|
||||
|
||||
mutating func decline() {
|
||||
invitationHandler?(false)
|
||||
invitationHandler = nil
|
||||
}
|
||||
struct NoteContent: Codable {
|
||||
let title: String
|
||||
let noteSnapshot: String
|
||||
}
|
||||
|
||||
var id: MCPeerID { invitatorID }
|
||||
var noteName: String { note.title }
|
||||
let invitatorID: MCPeerID
|
||||
let note: NoteContent
|
||||
private var invitationHandler: ((Bool) -> Void)?
|
||||
|
||||
init(
|
||||
invitatorID: MCPeerID,
|
||||
note: NoteContent,
|
||||
invitationHandler: ((Bool) -> Void)? = nil
|
||||
) {
|
||||
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(set) var ownPeer: MCPeerID
|
||||
|
||||
var invitations: [NoteInvitation] = []
|
||||
let noteChangesEmitter = PassthroughSubject<NoteMessage, Never>()
|
||||
private let session: MCSession
|
||||
private let advertiser: MCNearbyServiceAdvertiser
|
||||
private(set) var ownPeer: MCPeerID
|
||||
|
||||
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
|
||||
session.delegate = self
|
||||
}
|
||||
|
||||
func startBrowsingForNotes() {
|
||||
advertiser.startAdvertisingPeer()
|
||||
}
|
||||
|
||||
func stopBrowsingForNotes() {
|
||||
advertiser.stopAdvertisingPeer()
|
||||
session.disconnect()
|
||||
}
|
||||
|
||||
func send(note: String, to peer: MCPeerID) {
|
||||
let message = NoteMessage(senderID: ownPeer.displayName, content: note)
|
||||
guard let data = try? JSONEncoder().encode(message) else { return }
|
||||
try? session.send(data, toPeers: [peer], with: .reliable)
|
||||
}
|
||||
var invitations: [NoteInvitation] = []
|
||||
let noteChangesEmitter = PassthroughSubject<NoteMessage, Never>()
|
||||
|
||||
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
|
||||
session.delegate = self
|
||||
}
|
||||
|
||||
func startBrowsingForNotes() {
|
||||
advertiser.startAdvertisingPeer()
|
||||
}
|
||||
|
||||
func stopBrowsingForNotes() {
|
||||
advertiser.stopAdvertisingPeer()
|
||||
session.disconnect()
|
||||
}
|
||||
|
||||
func send(note: String, to peer: MCPeerID) {
|
||||
let message = NoteMessage(senderID: ownPeer.displayName, content: note)
|
||||
guard let data = try? JSONEncoder().encode(message) else { return }
|
||||
try? session.send(data, 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 { return }
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.invitations.append(
|
||||
.init(
|
||||
invitatorID: peerID,
|
||||
note: noteContent,
|
||||
invitationHandler: { [weak self, invitationHandler] accepted in
|
||||
guard let self else { return }
|
||||
invitationHandler(accepted, self.session)
|
||||
DispatchQueue.main.async {
|
||||
self.invitations.removeAll { $0.id == peerID }
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
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 { return }
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.invitations.append(
|
||||
.init(
|
||||
invitatorID: peerID,
|
||||
note: noteContent,
|
||||
invitationHandler: { [weak self, invitationHandler] accepted in
|
||||
guard let self else { return }
|
||||
invitationHandler(accepted, self.session)
|
||||
DispatchQueue.main.async {
|
||||
self.invitations.removeAll { $0.id == peerID }
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension NoteEditingSessionClient: MCSessionDelegate {
|
||||
func session(
|
||||
_ session: MCSession,
|
||||
peer peerID: MCPeerID,
|
||||
didChange state: MCSessionState
|
||||
) {}
|
||||
|
||||
func session(
|
||||
_ session: MCSession,
|
||||
didReceive data: Data,
|
||||
fromPeer peerID: MCPeerID
|
||||
) {
|
||||
guard let message = try? JSONDecoder().decode(NoteMessage.self, from: data) else { return }
|
||||
DispatchQueue.main.async {
|
||||
self.noteChangesEmitter.send(message)
|
||||
}
|
||||
}
|
||||
|
||||
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {}
|
||||
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {}
|
||||
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: (any Error)?) {}
|
||||
func session(
|
||||
_ session: MCSession,
|
||||
peer peerID: MCPeerID,
|
||||
didChange state: MCSessionState
|
||||
) {}
|
||||
|
||||
func session(
|
||||
_ session: MCSession,
|
||||
didReceive data: Data,
|
||||
fromPeer peerID: MCPeerID
|
||||
) {
|
||||
guard let message = try? JSONDecoder().decode(NoteMessage.self, from: data) else { return }
|
||||
DispatchQueue.main.async {
|
||||
self.noteChangesEmitter.send(message)
|
||||
}
|
||||
}
|
||||
|
||||
func session(
|
||||
_ session: MCSession, didReceive stream: InputStream, withName streamName: String,
|
||||
fromPeer peerID: MCPeerID
|
||||
) {}
|
||||
func session(
|
||||
_ session: MCSession, didStartReceivingResourceWithName resourceName: String,
|
||||
fromPeer peerID: MCPeerID, with progress: Progress
|
||||
) {}
|
||||
func session(
|
||||
_ session: MCSession, didFinishReceivingResourceWithName resourceName: String,
|
||||
fromPeer peerID: MCPeerID, at localURL: URL?, withError error: (any Error)?
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -1,14 +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)
|
||||
}
|
||||
}
|
||||
@Binding var invitation: NoteInvitation
|
||||
let joinTapped: () -> Void
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Text(invitation.noteName)
|
||||
Spacer()
|
||||
Button("Join", action: joinTapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
struct NoteMessage: Codable {
|
||||
let senderID: String
|
||||
let content: String
|
||||
let senderID: String
|
||||
let content: String
|
||||
}
|
||||
|
||||
@@ -8,72 +8,76 @@
|
||||
import Foundation
|
||||
|
||||
protocol StorageProvider {
|
||||
func contentsOfDirectory(atPath path: String) throws -> [String]
|
||||
|
||||
@discardableResult
|
||||
func createFile(atPath path: String, contents data: Data?, attributes attr: [FileAttributeKey: Any]?) -> Bool
|
||||
func contentsOfDirectory(atPath path: String) throws -> [String]
|
||||
|
||||
@discardableResult
|
||||
func createFile(
|
||||
atPath path: String, contents data: Data?, attributes attr: [FileAttributeKey: Any]?
|
||||
) -> Bool
|
||||
}
|
||||
|
||||
extension FileManager: StorageProvider {}
|
||||
|
||||
struct NotesStorage {
|
||||
let storageProvider: StorageProvider
|
||||
let rootDirectory: URL
|
||||
let storageProvider: StorageProvider
|
||||
let rootDirectory: URL
|
||||
|
||||
init(
|
||||
storageProvider: StorageProvider = FileManager.default,
|
||||
rootDirectory: URL = .documentsDirectory
|
||||
) {
|
||||
self.storageProvider = storageProvider
|
||||
self.rootDirectory = rootDirectory
|
||||
}
|
||||
init(
|
||||
storageProvider: StorageProvider = FileManager.default,
|
||||
rootDirectory: URL = .documentsDirectory
|
||||
) {
|
||||
self.storageProvider = storageProvider
|
||||
self.rootDirectory = rootDirectory
|
||||
}
|
||||
|
||||
func loadNotes() -> [Note] {
|
||||
let files = try! storageProvider
|
||||
.contentsOfDirectory(atPath: rootDirectory.path)
|
||||
var notes = [Note]()
|
||||
func loadNotes() -> [Note] {
|
||||
let files =
|
||||
try! storageProvider
|
||||
.contentsOfDirectory(atPath: rootDirectory.path)
|
||||
var notes = [Note]()
|
||||
|
||||
for file in files.compactMap({
|
||||
URL(
|
||||
filePath: $0,
|
||||
directoryHint: .notDirectory,
|
||||
relativeTo: rootDirectory
|
||||
)
|
||||
}) {
|
||||
let name = file.lastPathComponent
|
||||
let note = Note(
|
||||
name: name,
|
||||
path: file
|
||||
)
|
||||
notes.append(note)
|
||||
}
|
||||
for file in files.compactMap({
|
||||
URL(
|
||||
filePath: $0,
|
||||
directoryHint: .notDirectory,
|
||||
relativeTo: rootDirectory
|
||||
)
|
||||
}) {
|
||||
let name = file.lastPathComponent
|
||||
let note = Note(
|
||||
name: name,
|
||||
path: file
|
||||
)
|
||||
notes.append(note)
|
||||
}
|
||||
|
||||
return notes
|
||||
}
|
||||
return notes
|
||||
}
|
||||
|
||||
func createNote(name: String) {
|
||||
let currentNotes = loadNotes()
|
||||
var index: Int? = nil
|
||||
var proposedName: String {
|
||||
index.map { name + " \($0)" } ?? name
|
||||
}
|
||||
func createNote(name: String) {
|
||||
let currentNotes = loadNotes()
|
||||
var index: Int? = nil
|
||||
var proposedName: String {
|
||||
index.map { name + " \($0)" } ?? name
|
||||
}
|
||||
|
||||
while currentNotes.contains(where: { $0.name == proposedName }) {
|
||||
if let _index = index {
|
||||
index = _index + 1
|
||||
} else {
|
||||
index = 1
|
||||
}
|
||||
}
|
||||
while currentNotes.contains(where: { $0.name == proposedName }) {
|
||||
if let _index = index {
|
||||
index = _index + 1
|
||||
} else {
|
||||
index = 1
|
||||
}
|
||||
}
|
||||
|
||||
let pathToWrite = rootDirectory
|
||||
.appendingPathComponent(proposedName)
|
||||
.appendingPathExtension(for: .text)
|
||||
let pathToWrite =
|
||||
rootDirectory
|
||||
.appendingPathComponent(proposedName)
|
||||
.appendingPathExtension(for: .text)
|
||||
|
||||
storageProvider.createFile(
|
||||
atPath: pathToWrite.path,
|
||||
contents: Data(),
|
||||
attributes: nil
|
||||
)
|
||||
}
|
||||
storageProvider.createFile(
|
||||
atPath: pathToWrite.path,
|
||||
contents: Data(),
|
||||
attributes: nil
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user