Task.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Todo = " "
  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. var label: String {
  21. switch self {
  22. case .Todo: return "square.and.pencil"
  23. case .Complete: return "checkmark"
  24. case .InProgress: return "ellipsis.circle"
  25. case .Hiatus: return "clock.badge.questionmark"
  26. case .Waiting: return "airplane.circle"
  27. }
  28. }
  29. }
  30. @Model
  31. final class SubTask: Codable {
  32. var name: String
  33. var task: Task?
  34. var notes: String = ""
  35. var status: Status = Status.Todo
  36. init(name: String, parent: Task? = nil) {
  37. self.name = name
  38. self.task = parent
  39. }
  40. func yaml(_ indent: Int = 0) -> String {
  41. let hdr = String(repeating: " ", count: indent)
  42. let subhdr = hdr + " # "
  43. var rval = hdr + "- [\(status.rawValue)] \(name)\n"
  44. if !notes.isEmpty {
  45. rval += subhdr + notes.replacingOccurrences(of: "\n", with: "\n" + subhdr) + "\n"
  46. }
  47. return rval
  48. }
  49. enum CodingKeys: CodingKey { case name, notes, status }
  50. required init(from decoder: any Decoder) throws {
  51. let container = try decoder.container(keyedBy: CodingKeys.self)
  52. name = try container.decode(String.self, forKey: .name)
  53. notes = try container.decode(String.self, forKey: .notes)
  54. status = try container.decode(Status.self, forKey: .status)
  55. }
  56. func encode(to encoder: any Encoder) throws {
  57. var container = encoder.container(keyedBy: CodingKeys.self)
  58. try container.encode(name, forKey: .name)
  59. try container.encode(notes, forKey: .notes)
  60. try container.encode(status, forKey: .status)
  61. }
  62. }
  63. @Model
  64. final class Tag: Codable {
  65. var id: String
  66. var task: Task?
  67. init(id: String, parent: Task? = nil) {
  68. self.id = id
  69. self.task = parent
  70. }
  71. func like(_ str: String) -> Bool {
  72. return id.caseInsensitiveCompare(str) == .orderedSame
  73. }
  74. required init(from decoder: any Decoder) throws {
  75. id = try decoder.singleValueContainer().decode(String.self)
  76. }
  77. func encode(to encoder: any Encoder) throws {
  78. var single = encoder.singleValueContainer()
  79. try single.encode(id)
  80. }
  81. }
  82. @Model
  83. final class Task: Codable {
  84. var name: String
  85. var project: Project?
  86. var category: String = ""
  87. @Relationship(deleteRule: .cascade, inverse: \Tag.task)
  88. var tags: [Tag] = []
  89. @Relationship(deleteRule: .cascade, inverse: \SubTask.task)
  90. var subtasks: [SubTask] = []
  91. var notes: String = ""
  92. var status: Status = Status.Todo
  93. init(name: String, parent: Project? = nil) {
  94. self.name = name
  95. self.project = parent
  96. self.category = parent?.category ?? ""
  97. }
  98. func yaml(_ indent: Int = 0) -> String {
  99. let hdr = String(repeating: " ", count: indent)
  100. let subhdr = hdr + " # "
  101. var rval = hdr + "[\(status.rawValue)] \(name) "
  102. rval += "(\(tags.map(\.id).joined(separator: " ")))\n"
  103. if !notes.isEmpty {
  104. rval += subhdr + notes.replacingOccurrences(of: "\n", with: "\n" + subhdr) + "\n"
  105. }
  106. rval += subtasks.map({ $0.yaml(indent + 1) }).joined()
  107. return rval
  108. }
  109. enum CodingKeys: CodingKey { case name, category, tags, subtasks, notes, status }
  110. required init(from decoder: any Decoder) throws {
  111. let container = try decoder.container(keyedBy: CodingKeys.self)
  112. name = try container.decode(String.self, forKey: .name)
  113. category = try container.decode(String.self, forKey: .category)
  114. tags = try container.decode([Tag].self, forKey: .tags)
  115. subtasks = try container.decode([SubTask].self, forKey: .subtasks)
  116. notes = try container.decode(String.self, forKey: .notes)
  117. status = try container.decode(Status.self, forKey: .status)
  118. }
  119. func encode(to encoder: any Encoder) throws {
  120. var container = encoder.container(keyedBy: CodingKeys.self)
  121. try container.encode(name, forKey: .name)
  122. try container.encode(category, forKey: .category)
  123. try container.encode(tags, forKey: .tags)
  124. try container.encode(subtasks, forKey: .subtasks)
  125. try container.encode(notes, forKey: .notes)
  126. try container.encode(status, forKey: .status)
  127. }
  128. }