SubTask.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // SubTask.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 3/5/26.
  6. //
  7. import Foundation
  8. import SwiftData
  9. @Model
  10. final class SubTask: Codable {
  11. var sortOrder: Int = 0
  12. var name: String = ""
  13. var task: Task?
  14. var notes: String = ""
  15. var status: Status = Status.todo
  16. init(parent: Task? = nil) {
  17. self.task = parent
  18. self.sortOrder = parent?.subtasks.count ?? 0
  19. }
  20. func yaml(_ indent: Int = 0) -> String {
  21. let hdr = String(repeating: " ", count: indent)
  22. let subhdr = hdr + " # "
  23. var rval = hdr + "- [\(status.rawValue)] \(name)\n"
  24. if !notes.isEmpty {
  25. rval += subhdr + notes.replacingOccurrences(of: "\n", with: "\n" + subhdr) + "\n"
  26. }
  27. return rval
  28. }
  29. enum CodingKeys: CodingKey { case name, notes, status }
  30. required init(from decoder: any Decoder) throws {
  31. let container = try decoder.container(keyedBy: CodingKeys.self)
  32. name = try container.decode(String.self, forKey: .name)
  33. notes = try container.decode(String.self, forKey: .notes)
  34. status = try container.decode(Status.self, forKey: .status)
  35. }
  36. func encode(to encoder: any Encoder) throws {
  37. var container = encoder.container(keyedBy: CodingKeys.self)
  38. try container.encode(name, forKey: .name)
  39. try container.encode(notes, forKey: .notes)
  40. try container.encode(status, forKey: .status)
  41. }
  42. }