-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ContentCoordinator.swift
161 lines (143 loc) · 5.36 KB
/
ContentCoordinator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import Combine
import SwiftUI
@MainActor
final class ContentCoordinator {
private let store: GroupStore
private let mapper: ContentModelMapper
private let applicationStore: ApplicationStore
let contentSelectionManager: SelectionManager<ContentViewModel>
let groupSelectionManager: SelectionManager<GroupViewModel>
let publisher: ContentPublisher = ContentPublisher()
init(_ store: GroupStore,
applicationStore: ApplicationStore,
contentSelectionManager: SelectionManager<ContentViewModel>,
groupSelectionManager: SelectionManager<GroupViewModel>) {
self.applicationStore = applicationStore
self.store = store
self.groupSelectionManager = groupSelectionManager
self.mapper = ContentModelMapper()
self.contentSelectionManager = contentSelectionManager
// Set initial selection
if let initialGroupSelection = groupSelectionManager.lastSelection,
let initialWorkflowSelection = contentSelectionManager.lastSelection {
render([initialGroupSelection], selectionOverrides: [initialWorkflowSelection])
}
}
func handle(_ action: SidebarView.Action) {
switch action {
case .refresh, .openScene, .addConfiguration, .updateConfiguration,
.moveGroups, .removeGroups, .deleteConfiguraiton:
// NOOP
break
case .moveWorkflows, .copyWorkflows:
render(groupSelectionManager.selections)
case .selectConfiguration:
render(groupSelectionManager.selections, calculateSelections: true)
case .selectGroups(let ids):
let shouldRemoveLastSelection = !publisher.data.isEmpty
handle(.refresh(ids))
if shouldRemoveLastSelection {
if let firstId = publisher.data.first?.id {
contentSelectionManager.setLastSelection(firstId)
} else {
contentSelectionManager.removeLastSelection()
}
} else if let lastSelection = contentSelectionManager.lastSelection {
// Check for invalid selections, reset the last selection to the first one.
// Otherwise, the focus updates won't work properly because it is looking for an
// identifier that does not exist in the current group.
if !publisher.data.contains(where: { $0.id == lastSelection }) {
if let firstId = publisher.data.first?.id {
contentSelectionManager.setLastSelection(firstId)
}
}
}
}
}
func handle(_ context: EditWorkflowGroupWindow.Context) {
switch context {
case .add(let workflowGroup):
render([workflowGroup.id])
case .edit(let workflowGroup):
render([workflowGroup.id])
}
}
func handle(_ action: ContentView.Action) {
// TODO: We should get rid of this guard.
guard let id = groupSelectionManager.selections.first,
var group = store.group(withId: id) else { return }
ContentViewActionReducer.reduce(
action,
groupStore: store,
selectionManager: contentSelectionManager,
group: &group)
switch action {
case .addWorkflow(let id):
store.updateGroups([group])
withAnimation {
render([group.id], selectionOverrides: [id])
}
case .selectWorkflow:
break
case .refresh(let ids):
render(ids, calculateSelections: true)
default:
store.updateGroups([group])
render([group.id], calculateSelections: true)
}
}
func handle(_ action: DetailView.Action) {
switch action {
case .singleDetailView:
render(groupSelectionManager.selections, calculateSelections: false)
}
}
// MARK: Private methods
private func render(_ groupIds: Set<GroupViewModel.ID>,
calculateSelections: Bool = false,
selectionOverrides: Set<Workflow.ID>? = nil) {
Benchmark.start("ContentCoordinator.render")
defer { Benchmark.finish("ContentCoordinator.render") }
var viewModels = [ContentViewModel]()
var newSelections = Set<ContentViewModel.ID>()
var selectedWorkflowIds = contentSelectionManager.selections
var firstViewModel: ContentViewModel?
for offset in store.groups.indices {
let group = store.groups[offset]
if groupIds.contains(group.id) {
for wOffset in group.workflows.indices {
let workflow = group.workflows[wOffset]
let viewModel = mapper.map(workflow)
if wOffset == 0 {
firstViewModel = viewModel
}
viewModels.append(viewModel)
if calculateSelections &&
!selectedWorkflowIds.isEmpty &&
selectedWorkflowIds.contains(viewModel.id) {
selectedWorkflowIds.remove(viewModel.id)
newSelections.insert(viewModel.id)
}
}
}
}
if calculateSelections {
if publisher.data.isEmpty {
if newSelections.isEmpty, let first = viewModels.first {
newSelections = [first.id]
}
} else if !contentSelectionManager.selections.intersection(viewModels.map(\.id)).isEmpty {
newSelections = contentSelectionManager.selections
} else if newSelections.isEmpty, let first = firstViewModel {
newSelections = [first.id]
}
contentSelectionManager.publish(newSelections)
} else if let selectionOverrides {
contentSelectionManager.publish(selectionOverrides)
if let first = selectionOverrides.first {
contentSelectionManager.setLastSelection(first)
}
}
publisher.publish(viewModels)
}
}