Implementation: fix checking the username so it won't generate undefined behavior during ui rendering
This commit is contained in:
@@ -12,22 +12,24 @@ extension EnvironmentValues {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
@AppStorage("peered_username") private var username: String = "fallback_user"
|
@AppStorage("peered_username") private var username: String = "fallback_user"
|
||||||
@State private var notes = [Note]()
|
@State private var notes = [Note]()
|
||||||
@State private var notesClient: NoteEditingSessionClient?
|
@State private var notesClient: NoteEditingSessionClient?
|
||||||
@State private var ownPeer: OwnPeer?
|
@State private var ownPeer: OwnPeer?
|
||||||
|
|
||||||
var body: some View {
|
var isUsernameValid: Bool {
|
||||||
|
username != "fallback_user" && !username.isEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
List {
|
List {
|
||||||
Section("Your notes") {
|
Section("Your notes") {
|
||||||
ForEach(notes) { note in
|
ForEach(notes) { note in
|
||||||
NavigationLink(note.name) {
|
NavigationLink(note.name) {
|
||||||
let peer = ownPeer ?? .init(peer: .init(displayName: username))
|
if let ownPeer {
|
||||||
if ownPeer == nil {
|
NoteEditorScreen(note: note, peer: ownPeer)
|
||||||
ownPeer = peer
|
|
||||||
}
|
}
|
||||||
return NoteEditorScreen(note: note, peer: peer)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,25 +47,36 @@ struct ContentView: View {
|
|||||||
.environment(\.ownPeer, ownPeer ?? .fallback)
|
.environment(\.ownPeer, ownPeer ?? .fallback)
|
||||||
.navigationTitle("Peered")
|
.navigationTitle("Peered")
|
||||||
.toolbar {
|
.toolbar {
|
||||||
Button("Create note") {
|
Button("Create note") {
|
||||||
NotesStorage().createNote(name: "New Note")
|
NotesStorage().createNote(name: "New Note")
|
||||||
notes = NotesStorage().loadNotes()
|
notes = NotesStorage().loadNotes()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
notes = NotesStorage().loadNotes()
|
notes = NotesStorage().loadNotes()
|
||||||
if notesClient == nil {
|
if isUsernameValid {
|
||||||
notesClient = .init(peer: .init(displayName: username))
|
setupSession()
|
||||||
}
|
}
|
||||||
notesClient?.startBrowsingForNotes()
|
|
||||||
}
|
}
|
||||||
.sheet(isPresented: .constant(username == "fallback_user" || username.isEmpty)) {
|
.onChange(of: username) { _, newUsername in
|
||||||
SetUserNameBottomSheetView(username: $username)
|
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 {
|
#Preview {
|
||||||
ContentView()
|
ContentView()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user