Преглед изворни кода

fix: properly clear view when deleting the active Project

Sam Jaffe пре 2 недеља
родитељ
комит
2c226b926c
1 измењених фајлова са 17 додато и 10 уклоњено
  1. 17 10
      Todos/View/ContentView.swift

+ 17 - 10
Todos/View/ContentView.swift

@@ -14,17 +14,13 @@ struct ContentView: View {
   let inPreview = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
   
   @Query private var items: [Project]
-  
-  @State private var showingPopup = false
-  @State private var currentHint: URLHint = URLHint()
-  
+  @State private var selection: Project?
+
   var body: some View {
     NavigationSplitView {
-      List {
-        ForEach(items) { item in
-          NavigationLink {
-            ProjectPanelView(item: item)
-          } label: {
+      List(selection: $selection) {
+        ForEach(items, id: \.self) { item in
+          NavigationLink(value: item) {
             ProjectSidebarView(name: Bindable(item).name)
           } .contextMenu {
             Button(action: { deleteItem(item: item) }) {
@@ -43,7 +39,12 @@ struct ContentView: View {
         }
       }
     } detail: {
-      Text("Select an item")
+      if let selection = selection {
+        ProjectPanelView(item: selection)
+      } else {
+        let header = items.isEmpty ? "Create a New Project" : "Select a project from the sidebar"
+        ContentUnavailableView(header, systemImage: "doc.text.image.fill")
+      }
     }
     .onAppear(perform: autosave)
   }
@@ -100,12 +101,18 @@ struct ContentView: View {
   }
   
   private func deleteItem(item: Project) {
+    if let selection = selection, selection == item {
+      self.selection = nil
+    }
     withAnimation {
       modelContext.delete(item)
     }
   }
 
   private func deleteItems(offsets: IndexSet) {
+    if let selection = selection, offsets.contains(where: { items[$0] == selection }) {
+      self.selection = nil
+    }
     withAnimation {
       for index in offsets {
         modelContext.delete(items[index])