Explorar o código

refactor: properly set sortOrder when deserializing

Sam Jaffe hai 2 semanas
pai
achega
b9254c06b2
Modificáronse 3 ficheiros con 10 adicións e 5 borrados
  1. 6 3
      Todos/Model/Project.swift
  2. 4 1
      Todos/Model/Task.swift
  3. 0 1
      Todos/View/ContentView.swift

+ 6 - 3
Todos/Model/Project.swift

@@ -54,16 +54,19 @@ final class Project: Codable, Ordered, Aggregate {
 
   required init(from decoder: any Decoder) throws {
     let container = try decoder.container(keyedBy: CodingKeys.self)
-    category = try container.decode(String.self, forKey: .category)
     name = try container.decode(String.self, forKey: .name)
+    category = try container.decode(String.self, forKey: .category)
     tasks = try container.decode([Task].self, forKey: .tasks)
-    tasks.forEach({ $0.project = self })
+    for (index, item) in tasks.enumerated() {
+      item.project = self
+      item.sortOrder = index
+    }
   }
 
   func encode(to encoder: any Encoder) throws {
     var container = encoder.container(keyedBy: CodingKeys.self)
+    try container.encode(name, forKey: .name)
     try container.encode(category, forKey: .category)
     try container.encode(tasks, forKey: .tasks)
-    try container.encode(name, forKey: .name)
   }
 }

+ 4 - 1
Todos/Model/Task.swift

@@ -70,7 +70,10 @@ final class Task: Codable, Ordered, Aggregate {
     notes = try container.decode(String.self, forKey: .notes)
     status = try container.decode(Status.self, forKey: .status)
     tags.forEach({ $0.task = self })
-    subtasks.forEach({ $0.task = self })
+    for (index, item) in subtasks.enumerated() {
+      item.task = self
+      item.sortOrder = index
+    }
   }
 
   func encode(to encoder: any Encoder) throws {

+ 0 - 1
Todos/View/ContentView.swift

@@ -67,7 +67,6 @@ struct ContentView: View {
   }
 
   private func autosave() {
-    items.forEach({ $0.uuid = UUID() })
     if inPreview {
       // This isn't great, but we shouldn't be running this in a preview
       // environment in the first place, so w/e.