Task.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // Task.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 2/28/26.
  6. //
  7. import Foundation
  8. import SwiftData
  9. enum Status : String, CaseIterable, Identifiable, Codable {
  10. case Default = " "
  11. case Complete = "V"
  12. case InProgress = "C"
  13. case Hiatus = "H"
  14. case Waiting = "R"
  15. var id: Self { self }
  16. var description: String { self.rawValue }
  17. var isStrong: Bool {
  18. self == .Complete || self == .Hiatus || self == .Waiting
  19. }
  20. }
  21. @Model
  22. final class SubTask {
  23. var name: String
  24. var notes: String = ""
  25. var status: Status = Status.Default
  26. init(name: String) {
  27. self.name = name
  28. }
  29. func yaml(_ indent: Int = 0) -> String {
  30. let h1 = String(repeating: " ", count: indent)
  31. let h2 = String(repeating: " ", count: indent + 1)
  32. var rval = h1 + "[\(status.rawValue)] \(name)\n"
  33. if !notes.isEmpty {
  34. rval += h2 + "# " + notes.replacingOccurrences(of: "\n", with: "\n" + h2 + "# ")
  35. }
  36. return rval
  37. }
  38. }
  39. @Model
  40. final class Task {
  41. var name: String
  42. var tags: [String] = []
  43. var subtasks: [SubTask] = []
  44. var notes: String = ""
  45. var status: Status = Status.Default
  46. init(name: String) {
  47. self.name = name
  48. }
  49. func yaml(_ indent: Int = 0) -> String {
  50. let h1 = String(repeating: " ", count: indent)
  51. let h2 = String(repeating: " ", count: indent + 1)
  52. var rval = " [\(status.rawValue)] \(name) (\(tags.joined(separator: " ")))\n"
  53. if !notes.isEmpty {
  54. rval += h2 + "# " + notes.replacingOccurrences(of: "\n", with: "\n" + h2 + "# ")
  55. }
  56. rval += subtasks.map({ $0.yaml(indent + 1) }).joined()
  57. return rval
  58. }
  59. }