Task.swift 2.0 KB

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