Task.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 {
  32. var name: String
  33. var notes: String = ""
  34. var status: Status = Status.Todo
  35. init(name: String) {
  36. self.name = name
  37. }
  38. func yaml(_ indent: Int = 0) -> String {
  39. let h1 = String(repeating: " ", count: indent)
  40. let h2 = String(repeating: " ", count: indent + 1)
  41. var rval = h1 + "[\(status.rawValue)] \(name)\n"
  42. if !notes.isEmpty {
  43. rval += h2 + "# " + notes.replacingOccurrences(of: "\n", with: "\n" + h2 + "# ")
  44. }
  45. return rval
  46. }
  47. }
  48. struct Tag : Codable, Identifiable {
  49. var id: String
  50. }
  51. @Model
  52. final class Task {
  53. var name: String
  54. var tags: [Tag] = []
  55. var subtasks: [SubTask] = []
  56. var notes: String = ""
  57. var status: Status = Status.Todo
  58. init(name: String) {
  59. self.name = name
  60. }
  61. func yaml(_ indent: Int = 0) -> String {
  62. let h1 = String(repeating: " ", count: indent)
  63. let h2 = String(repeating: " ", count: indent + 1)
  64. var rval = h1 + "[\(status.rawValue)] \(name) "
  65. rval += "(\(tags.map(\.id).joined(separator: " ")))\n"
  66. if !notes.isEmpty {
  67. rval += h2 + "# " + notes.replacingOccurrences(of: "\n", with: "\n" + h2 + "# ")
  68. }
  69. rval += subtasks.map({ $0.yaml(indent + 1) }).joined()
  70. return rval
  71. }
  72. }