Aggregate.swift 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // Aggregate.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 3/5/26.
  6. //
  7. import Foundation
  8. import SwiftUI
  9. protocol Aggregate {
  10. associatedtype Element
  11. func move(fromOffsets: IndexSet, toOffset: Int)
  12. func remove(_ item: Element)
  13. func reindex()
  14. }
  15. extension Project : Aggregate {
  16. typealias Element = Task
  17. func move(fromOffsets: IndexSet, toOffset: Int) {
  18. tasks.move(fromOffsets: fromOffsets, toOffset: toOffset)
  19. reindex()
  20. }
  21. func remove(_ item: Element) {
  22. tasks.removeAll(where: { $0.id == item.id })
  23. reindex()
  24. }
  25. func reindex() {
  26. for (index, item) in tasks.enumerated() {
  27. item.sortOrder = index
  28. }
  29. }
  30. }
  31. extension Task : Aggregate {
  32. typealias Element = SubTask
  33. func move(fromOffsets: IndexSet, toOffset: Int) {
  34. subtasks.move(fromOffsets: fromOffsets, toOffset: toOffset)
  35. reindex()
  36. }
  37. func remove(_ item: Element) {
  38. subtasks.removeAll(where: { $0.id == item.id })
  39. reindex()
  40. }
  41. func reindex() {
  42. for (index, item) in subtasks.enumerated() {
  43. item.sortOrder = index
  44. }
  45. }
  46. }