2 Commits 6e228bd5b7 ... d70e45db28

Tác giả SHA1 Thông báo Ngày
  Sam Jaffe d70e45db28 refactor: generate new UUID whenever calling init() 2 tuần trước cách đây
  Sam Jaffe b9254c06b2 refactor: properly set sortOrder when deserializing 2 tuần trước cách đây
3 tập tin đã thay đổi với 12 bổ sung5 xóa
  1. 8 3
      Todos/Model/Project.swift
  2. 4 1
      Todos/Model/Task.swift
  3. 0 1
      Todos/View/ContentView.swift

+ 8 - 3
Todos/Model/Project.swift

@@ -21,6 +21,7 @@ final class Project: Codable, Ordered, Aggregate {
   var tasks: [Task] = []
 
   init(sortOrder: Int = 0) {
+    self.uuid = UUID()
     self.sortOrder = sortOrder
   }
 
@@ -54,16 +55,20 @@ 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)
+    uuid = UUID()
     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.