| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- //
- // Aggregate.swift
- // Todos
- //
- // Created by Sam Jaffe on 3/5/26.
- //
- import Foundation
- import SwiftUI
- protocol Aggregate {
- associatedtype Element
- func move(fromOffsets: IndexSet, toOffset: Int)
- func remove(_ item: Element)
- func reindex()
- }
- extension Project: Aggregate {
- typealias Element = Task
- func move(fromOffsets: IndexSet, toOffset: Int) {
- tasks.move(fromOffsets: fromOffsets, toOffset: toOffset)
- reindex()
- }
- func remove(_ item: Element) {
- tasks.removeAll(where: { $0.id == item.id })
- reindex()
- }
- func reindex() {
- for (index, item) in tasks.enumerated() {
- item.sortOrder = index
- }
- }
- }
- extension Task: Aggregate {
- typealias Element = SubTask
- func move(fromOffsets: IndexSet, toOffset: Int) {
- subtasks.move(fromOffsets: fromOffsets, toOffset: toOffset)
- reindex()
- }
- func remove(_ item: Element) {
- subtasks.removeAll(where: { $0.id == item.id })
- reindex()
- }
- func reindex() {
- for (index, item) in subtasks.enumerated() {
- item.sortOrder = index
- }
- }
- }
|