Refactor project, add loading/editing/saving notes, add client/server basic logic
This commit is contained in:
@@ -9,8 +9,32 @@ import SwiftUI
|
||||
|
||||
struct NoteEditorScreen: View {
|
||||
let note: Note
|
||||
@State private var noteAdvertiser: NoteEditingSessionServer
|
||||
@State private var noteContent: String = ""
|
||||
@State private var timer = Timer.publish(every: 5, on: .current, in: .common)
|
||||
.autoconnect()
|
||||
|
||||
init(note: Note, username: String) {
|
||||
self.note = note
|
||||
self._noteAdvertiser = .init(wrappedValue: .init(username: username))
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
TextEditor(text: .constant("eee"))
|
||||
TextEditor(text: $noteContent)
|
||||
.onAppear {
|
||||
noteContent = try! String(contentsOf: note.path, encoding: .utf8)
|
||||
noteAdvertiser.startServer()
|
||||
}
|
||||
.onDisappear {
|
||||
noteAdvertiser.stopServer()
|
||||
saveNote()
|
||||
}
|
||||
.onReceive(timer) { _ in
|
||||
saveNote()
|
||||
}
|
||||
}
|
||||
|
||||
func saveNote() {
|
||||
try! noteContent.write(to: note.path, atomically: true, encoding: .utf8)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user