| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- //
- // Task.swift
- // Todos
- //
- // Created by Sam Jaffe on 2/28/26.
- //
- import Foundation
- import SwiftData
- import SwiftUI
- @Model
- final class Task: Codable {
- var sortOrder: Int = 0
- var name: String = ""
- var project: Project?
- var category: String = ""
- @Relationship(deleteRule: .cascade, inverse: \Tag.task)
- var tags: [Tag] = []
- @Relationship(deleteRule: .cascade, inverse: \SubTask.task)
- var subtasks: [SubTask] = []
- var notes: String = ""
- var status: Status = Status.todo
- init(parent: Project? = nil) {
- self.project = parent
- self.category = parent?.category ?? ""
- self.sortOrder = parent?.tasks.count ?? 0
- }
- func yaml(_ indent: Int = 0) -> String {
- let hdr = String(repeating: " ", count: indent)
- let subhdr = hdr + " # "
- var rval = hdr + "[\(status.rawValue)] \(name) "
- rval += "(\(tags.map(\.id).joined(separator: " ")))\n"
- if !notes.isEmpty {
- rval += subhdr + notes.replacingOccurrences(of: "\n", with: "\n" + subhdr) + "\n"
- }
- rval += subtasks.map({ $0.yaml(indent + 1) }).joined()
- return rval
- }
- enum CodingKeys: CodingKey { case name, category, tags, subtasks, notes, status }
- required init(from decoder: any Decoder) throws {
- let container = try decoder.container(keyedBy: CodingKeys.self)
- name = try container.decode(String.self, forKey: .name)
- category = try container.decode(String.self, forKey: .category)
- tags = try container.decode([Tag].self, forKey: .tags)
- subtasks = try container.decode([SubTask].self, forKey: .subtasks)
- notes = try container.decode(String.self, forKey: .notes)
- status = try container.decode(Status.self, forKey: .status)
- tags.forEach({ $0.task = self })
- for (index, item) in subtasks.enumerated() {
- item.task = self
- item.sortOrder = index
- }
- }
- func encode(to encoder: any Encoder) throws {
- var container = encoder.container(keyedBy: CodingKeys.self)
- try container.encode(name, forKey: .name)
- try container.encode(category, forKey: .category)
- try container.encode(tags, forKey: .tags)
- try container.encode(subtasks, forKey: .subtasks)
- try container.encode(notes, forKey: .notes)
- try container.encode(status, forKey: .status)
- }
- }
|