78 lines
1.9 KiB
Swift
78 lines
1.9 KiB
Swift
//
|
|
// AllNotesScreen.swift
|
|
// Peered
|
|
//
|
|
// Created by Oskar Chybowski on 11/05/2025.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct AllNotesScreen: View {
|
|
@AppStorage("peered_username") private var username: String?
|
|
@State private var notes = [Note]()
|
|
@State private var notesClient: NoteEditingSessionClient?
|
|
@State private var ownPeer: OwnPeer?
|
|
|
|
var isUsernameValid: Bool {
|
|
!(username.map(\.isEmpty) ?? true)
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
Section("Your notes") {
|
|
ForEach(notes) { note in
|
|
NavigationLink(note.name) {
|
|
if let ownPeer {
|
|
NoteEditorScreen(note: note, peer: ownPeer)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if let notesClient {
|
|
Section("External notes") {
|
|
ForEach(notesClient.invitations) { invitation in
|
|
NavigationLink(invitation.noteName) {
|
|
SharedNoteEditor(invitation: invitation, noteClient: notesClient)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Peered")
|
|
.toolbar {
|
|
Button("Create note") {
|
|
NotesStorage().createNote(name: "New Note")
|
|
notes = NotesStorage().loadNotes()
|
|
}
|
|
}
|
|
}
|
|
.onAppear {
|
|
notes = NotesStorage().loadNotes()
|
|
if let username, isUsernameValid {
|
|
setupSession(username: username)
|
|
}
|
|
}
|
|
.onChange(of: username) { _, newUsername in
|
|
guard let newUsername, isUsernameValid else { return }
|
|
setupSession(username: newUsername)
|
|
}
|
|
.sheet(isPresented: .constant(!isUsernameValid)) {
|
|
SetUserNameBottomSheetView(username: $username)
|
|
}
|
|
}
|
|
|
|
private func setupSession(username: String) {
|
|
notesClient?.stopBrowsingForNotes()
|
|
let peer = OwnPeer(peer: .init(displayName: username))
|
|
ownPeer = peer
|
|
notesClient = .init(peer: peer.peer)
|
|
notesClient?.startBrowsingForNotes()
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
AllNotesScreen()
|
|
}
|