Explorar o código

refactor: change to using Tag wrapper type due to issue w/ handling of Array<String>

Sam Jaffe hai 3 semanas
pai
achega
c03f6c4346
Modificáronse 2 ficheiros con 13 adicións e 8 borrados
  1. 7 2
      Todos/Model/Task.swift
  2. 6 6
      Todos/View/TagBarView.swift

+ 7 - 2
Todos/Model/Task.swift

@@ -43,10 +43,14 @@ final class SubTask {
   }
 }
 
+struct Tag : Codable, Identifiable {
+  var id: String
+}
+
 @Model
 final class Task {
   var name: String
-  var tags: [String] = []
+  var tags: [Tag] = []
   var subtasks: [SubTask] = []
   var notes: String = ""
   var status: Status = Status.Default
@@ -58,7 +62,8 @@ final class Task {
   func yaml(_ indent: Int = 0) -> String {
     let h1 = String(repeating: "  ", count: indent)
     let h2 = String(repeating: "  ", count: indent + 1)
-    var rval = "  [\(status.rawValue)] \(name) (\(tags.joined(separator: " ")))\n"
+    var rval = h1 + "[\(status.rawValue)] \(name) "
+    rval += "(\(tags.map(\.id).joined(separator: " ")))\n"
     if !notes.isEmpty {
       rval += h2 + "# " + notes.replacingOccurrences(of: "\n", with: "\n" + h2 + "# ")
     }

+ 6 - 6
Todos/View/TagBarView.swift

@@ -8,20 +8,20 @@
 import SwiftUI
 
 struct TagBarView: View {
-  @Binding var tags: [String]
+  @Binding var tags: [Tag]
   @State private var active: String = ""
 
     var body: some View {
       HStack {
-        ForEach($tags, id: \.self) { tag in
-          TextField("", text: tag)
+        ForEach($tags) { tag in
+          TextField("", text: tag.id)
             .onSubmit {
-              tags.removeAll(where: { $0.isEmpty })
+              tags.removeAll(where: { $0.id.isEmpty })
             }
         }
         TextField("Tag", text: $active)
           .onSubmit {
-            tags.append(active)
+            tags.append(Tag(id: active))
             active = ""
           }
      }
@@ -29,6 +29,6 @@ struct TagBarView: View {
 }
 
 #Preview {
-  @Previewable @State var tags = Array<String>()
+  @Previewable @State var tags = Array<Tag>()
   TagBarView(tags: $tags)
 }