83 lines
1.8 KiB
Swift
83 lines
1.8 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 isUsernameValid: Bool {
|
|
username != "fallback_user" && !username.isEmpty
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.environment(\.ownPeer, ownPeer ?? .fallback)
|
|
.navigationTitle("Peered")
|
|
.toolbar {
|
|
Button("Create note") {
|
|
NotesStorage().createNote(name: "New Note")
|
|
notes = NotesStorage().loadNotes()
|
|
}
|
|
}
|
|
}
|
|
.onAppear {
|
|
notes = NotesStorage().loadNotes()
|
|
if isUsernameValid {
|
|
setupSession()
|
|
}
|
|
}
|
|
.onChange(of: username) { _, newUsername in
|
|
guard isUsernameValid else { return }
|
|
setupSession()
|
|
}
|
|
.sheet(isPresented: .constant(!isUsernameValid)) {
|
|
SetUserNameBottomSheetView(username: $username)
|
|
}
|
|
}
|
|
|
|
private func setupSession() {
|
|
notesClient?.stopBrowsingForNotes()
|
|
let peer = OwnPeer(peer: .init(displayName: username))
|
|
ownPeer = peer
|
|
notesClient = .init(peer: peer.peer)
|
|
notesClient?.startBrowsingForNotes()
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
ContentView()
|
|
}
|