70 lines
1.6 KiB
Swift
70 lines
1.6 KiB
Swift
//
|
|
// ContentView.swift
|
|
// Peered
|
|
//
|
|
// Created by Oskar Chybowski on 11/05/2025.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
extension EnvironmentValues {
|
|
@Entry var ownPeer: OwnPeer = .fallback
|
|
}
|
|
|
|
struct ContentView: View {
|
|
@AppStorage("peered_username") private var username: String = "fallback_user"
|
|
@State private var notes = [Note]()
|
|
@State private var notesClient: NoteEditingSessionClient?
|
|
@State private var ownPeer: OwnPeer?
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
Section("Your notes") {
|
|
ForEach(notes) { note in
|
|
NavigationLink(note.name) {
|
|
let peer = ownPeer ?? .init(peer: .init(displayName: username))
|
|
if ownPeer == nil {
|
|
ownPeer = peer
|
|
}
|
|
return NoteEditorScreen(note: note, peer: peer)
|
|
}
|
|
}
|
|
}
|
|
|
|
if let notesClient {
|
|
Section("External notes") {
|
|
ForEach(notesClient.invitations) { invitation in
|
|
NavigationLink(invitation.noteName) {
|
|
SharedNoteEditor(invitation: invitation, noteClient: notesClient)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.environment(\.ownPeer, ownPeer ?? .fallback)
|
|
.navigationTitle("Peered")
|
|
.toolbar {
|
|
Button("Create note") {
|
|
NotesStorage().createNote(name: "New Note")
|
|
notes = NotesStorage().loadNotes()
|
|
}
|
|
}
|
|
}
|
|
.onAppear {
|
|
notes = NotesStorage().loadNotes()
|
|
if notesClient == nil {
|
|
notesClient = .init(peer: .init(displayName: username))
|
|
}
|
|
notesClient?.startBrowsingForNotes()
|
|
}
|
|
.sheet(isPresented: .constant(username == "fallback_user" || username.isEmpty)) {
|
|
SetUserNameBottomSheetView(username: $username)
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
ContentView()
|
|
}
|