Open History to see the last commit
1import Foundation
2
3enum ThreadArrangementSection: String, CaseIterable, Hashable {
4 case pinned, active, snoozed, settled
5
6 var title: String { rawValue.capitalized }
7
8 var orderSection: FeatureThreadOrderSection? {
9 switch self {
10 case .pinned: .pinned
11 case .active: .active
12 case .snoozed, .settled: nil
13 }
14 }
15}
16
17struct ThreadArrangementDestination: Equatable {
18 let section: ThreadArrangementSection
19 var targetID: String? = nil
20 var after = false
21}
22
23struct ThreadArrangementRow {
24 let section: ThreadArrangementSection
25 var thread: FeatureThread?
26 var id: String { thread.map { "thread:\($0.id)" } ?? "section:\(section.rawValue)" }
27}
28
29/// Resolves drops against all environments, without the Home project filter.
30/// A destination identifies a row rather than an index so a stale drag cannot
31/// silently move next to a different thread.
32enum ThreadArrangementPlanner {
33 enum LifecycleAction: Equatable {
34 case pin, unpin, unsettle, unsnooze
35 }
36
37 struct Plan {
38 let section: FeatureThreadOrderSection?
39 let orderedIDs: [String]
40 let assignments: [FeatureThreadOrderAssignment]
41 }
42
43 static func destination(
44 rows: [ThreadArrangementRow],
45 insertionIndex: Int,
46 isBeforeHeader: Bool
47 ) -> ThreadArrangementDestination? {
48 if insertionIndex == rows.count, let last = rows.last {
49 return .init(section: last.section, targetID: last.thread?.id, after: true)
50 }
51 guard rows.indices.contains(insertionIndex) else { return nil }
52 let row = rows[insertionIndex]
53 // UIKit uses the next header's index for the preceding section's end.
54 // A pointer on the header itself still targets that section, including
55 // empty sections and the collapsed Settled shelf.
56 if row.thread == nil, isBeforeHeader, insertionIndex > 0 {
57 let previous = rows[insertionIndex - 1]
58 return .init(section: previous.section, targetID: previous.thread?.id, after: true)
59 }
60 return .init(section: row.section, targetID: row.thread?.id)
61 }
62
63 static func lifecycle(
64 _ thread: FeatureThread,
65 section: FeatureThreadOrderSection,
66 now: Date
67 ) -> [LifecycleAction] {
68 if section == .pinned { return [.pin] }
69 var actions: [LifecycleAction] = []
70 if thread.pinnedAt != nil { actions.append(.unpin) }
71 if thread.isEffectivelySettled() { actions.append(.unsettle) }
72 if thread.isEffectivelySnoozed(at: now) { actions.append(.unsnooze) }
73 return actions
74 }
75
76 static func section(of thread: FeatureThread, now: Date) -> ThreadArrangementSection {
77 if thread.isEffectivelySnoozed(at: now) { return .snoozed }
78 if thread.supportsSettlement == true, thread.isEffectivelySettled() { return .settled }
79 return thread.pinnedAt == nil ? .active : .pinned
80 }
81
82 static func canEnter(
83 _ thread: FeatureThread,
84 section: FeatureThreadOrderSection,
85 now: Date
86 ) -> Bool {
87 guard !thread.isArchived, ThreadOrderPlanner.isWritable(thread, section: section) else {
88 return false
89 }
90 if thread.isEffectivelySettled(), thread.supportsSettlement != true { return false }
91 let source = self.section(of: thread, now: now)
92 if source.orderSection == section { return true }
93 if (section == .pinned || thread.pinnedAt != nil), thread.supportsPinning != true {
94 return false
95 }
96 if thread.isEffectivelySnoozed(at: now), thread.supportsSnooze != true { return false }
97 return true
98 }
99
100 static func plan(
101 id: String,
102 destination: ThreadArrangementDestination,
103 threads: [FeatureThread],
104 connectedEnvironmentIDs: Set<String>,
105 now: Date
106 ) -> Plan? {
107 guard let moved = threads.first(where: { $0.id == id }),
108 !moved.isArchived,
109 connectedEnvironmentIDs.contains(moved.environmentID ?? "") else { return nil }
110 if destination.section == .settled {
111 guard moved.supportsSettlement == true, !moved.isEffectivelySettled(),
112 moved.canSettleNow(at: now) else { return nil }
113 return Plan(section: nil, orderedIDs: [], assignments: [])
114 }
115 guard let section = destination.section.orderSection,
116 canEnter(moved, section: section, now: now) else { return nil }
117 let current = DailyUXSidebarIndex.orderedSection(threads, section: section, now: now)
118 var ordered = current.filter { $0.id != id }
119 let index: Int
120 if let target = destination.targetID {
121 guard let targetIndex = ordered.firstIndex(where: { $0.id == target }) else { return nil }
122 index = targetIndex + (destination.after ? 1 : 0)
123 } else {
124 index = destination.after ? ordered.count : 0
125 }
126 ordered.insert(moved, at: index)
127 guard ordered.map(\.id) != current.map(\.id),
128 let assignments = ThreadOrderPlanner.planDrop(
129 ordered: ordered,
130 all: threads,
131 section: section,
132 connectedEnvironmentIDs: connectedEnvironmentIDs,
133 movedID: id
134 ) else { return nil }
135 return Plan(section: section, orderedIDs: ordered.map(\.id), assignments: assignments)
136 }
137}