Bugfixes, simplifications

This commit is contained in:
2026-05-16 13:58:38 +02:00
parent db92487782
commit 10e08435a7
6 changed files with 150 additions and 162 deletions
@@ -12,17 +12,18 @@ struct NoteEditorScreen: View {
let note: Note
@State private var noteAdvertiser: NoteEditingSessionServer
@State private var noteContent: String = ""
@State private var remoteNoteContent: String? = nil
@State private var timer = Timer.publish(every: 5, on: .current, in: .common)
.autoconnect()
@State private var showManageMembers = false
init(note: Note, peer: OwnPeer) {
self.note = note
self._noteAdvertiser = .init(initialValue: .init(peer: peer))
}
var body: some View {
TextEditor(text: $noteContent)
NoteTextEditor(text: $noteContent, remoteText: remoteNoteContent)
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Manage members") {
@@ -32,18 +33,29 @@ struct NoteEditorScreen: View {
}
.sheet(isPresented: $showManageMembers) {
NavigationStack {
ManageMembersView(
ManageMembersScreen(
noteAdvertiser: noteAdvertiser,
noteTitle: note.name,
noteContent: $noteContent
)
}
}
.onReceive(noteAdvertiser.noteChangesEmitter) { updatedNote in
self.noteContent = updatedNote
.onReceive(noteAdvertiser.noteChangesEmitter) { message in
guard message.senderID != noteAdvertiser.ownPeer.peer.displayName else { return }
remoteNoteContent = message.content
}
.task(id: noteContent) {
if noteContent == remoteNoteContent { return }
let connectedPeers = noteAdvertiser.visiblePeers
.filter { $0.state == .joined }
.map(\.mcPeer)
guard !connectedPeers.isEmpty else { return }
try? await Task.sleep(nanoseconds: 500_000_000)
guard !Task.isCancelled else { return }
noteAdvertiser.send(note: noteContent, to: connectedPeers)
}
.onAppear {
noteContent = try! String(contentsOf: note.path, encoding: .utf8)
noteContent = (try? String(contentsOf: note.path, encoding: .utf8)) ?? ""
noteAdvertiser.startServer()
}
.onDisappear {
@@ -54,8 +66,8 @@ struct NoteEditorScreen: View {
saveNote()
}
}
func saveNote() {
try! noteContent.write(to: note.path, atomically: true, encoding: .utf8)
try? noteContent.write(to: note.path, atomically: true, encoding: .utf8)
}
}