Post

Replies

Boosts

Views

Activity

How can i clear bindable object.
struct AddFabButton: View { var body: some View { NavigationLink(destination: UpsertView(note: Note())){ VStack{ Image(systemName: "plus") .resizable() .scaledToFit() .frame(width: 20) .padding(.vertical, 15) .padding(.horizontal,10) } } .buttonStyle(.borderedProminent) .buttonBorderShape(.circle) .padding(.vertical,30) .padding(.horizontal) } } #Preview { AddFabButton() } // UpsertView.swift import SwiftUI import SwiftData var descriptor: FetchDescriptor<NoteBackground> { var descriptor = FetchDescriptor<NoteBackground>(sortBy: [SortDescriptor(\.createdAt)]) descriptor.fetchLimit = 1 return descriptor } struct UpsertView: View { @Environment(\.modelContext) private var modelContext @Environment(\.dismiss) var dismiss @Query(descriptor) var backgrounds: [NoteBackground] @Bindable var note: Note @State private var showBackgroundList: Bool = false @State private var selectedBackground: NoteBackground = NoteBackground(id: "0", textColor: "text") var body: some View { NavigationStack{ VStack{ VStack{ TextField("", text: $note.title, prompt: Text("Title") .foregroundStyle(Color(hex:selectedBackground.textColor).opacity(0.5)) .bold()) .foregroundStyle(Color(hex: selectedBackground.textColor)) .font(.title) .bold() ContentEditor(text: $note.content, textColor:Color(hex:selectedBackground.textColor)) } .padding() .onTapGesture { showBackgroundList = false } } .backgroundPickerList(with: $selectedBackground, isVisible: showBackgroundList) .noteItemBackground(with: selectedBackground.id ,isFullScreen: true) .animation(.easeIn(duration: 0.2), value: showBackgroundList) .frame(maxHeight: .infinity,alignment: .top) .foregroundStyle(.white) .ignoresSafeArea(.keyboard) .toolbar{ ToolbarItem(placement:.topBarTrailing){ HStack(spacing:20){ ShareNoteButton(title: note.title, content: note.content) MenuButton(iconName: "paintbrush", onPress: toggleBackgroundList, size: 20) MenuButton(iconName: "trash", onPress: onDelete, size: 20) } .padding(.horizontal) } } .onAppear{ self.selectedBackground = note.background ?? backgrounds[0] } .onChange(of: selectedBackground){ _ , newBackground in note.background = newBackground } } } } extension UpsertView{ private func toggleBackgroundList(){ showBackgroundList.toggle() } private func onDelete(){ dismiss() } private func onShare(){ } } #Preview { MainActor.assumeIsolated{ let config = ModelConfiguration(isStoredInMemoryOnly: true) let container = try! ModelContainer(for: NoteBackground.self, configurations: config) let background = NoteBackground(id: "0", image: "bird",createdAt: Date.now.addingTimeInterval(2*100)) container.mainContext.insert(background) let background2 = NoteBackground(id: "1", color: "#223233", textColor: "#000", createdAt: Date.now.addingTimeInterval(2*100)) container.mainContext.insert(background2) return NavigationStack{ UpsertView(note: Note()) } .modelContainer(container) } } after creating and save i want to clear note object I passed toUpsertView
0
0
301
Nov ’23