-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ContentCoordinator.swift
191 lines (171 loc) · 6.72 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import Bonzai
import Combine
import SwiftUI
@MainActor
final class ContentCoordinator {
private let applicationStore: ApplicationStore
private let contentSelectionManager: SelectionManager<ContentViewModel>
private let groupSelectionManager: SelectionManager<GroupViewModel>
private let mapper: ContentModelMapper
private let store: GroupStore
let groupPublisher: GroupPublisher = GroupPublisher(
.init(id: UUID().uuidString, name: "", icon: nil,
color: "", symbol: "", userModes: [], count: 0)
)
let contentPublisher: 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, .deleteConfiguration, .userMode:
// NOOP
break
case .moveWorkflows, .copyWorkflows:
render(groupSelectionManager.selections)
case .selectConfiguration:
render(groupSelectionManager.selections, calculateSelections: true)
case .selectGroups(let ids):
if let id = ids.first,
let firstGroup = store.group(withId: id) {
let group = SidebarMapper.map(firstGroup, applicationStore: applicationStore)
groupPublisher.publish(group)
}
let shouldRemoveLastSelection = !contentPublisher.data.isEmpty
handle(.refresh(ids))
if shouldRemoveLastSelection {
if let firstId = contentPublisher.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 !contentPublisher.data.contains(where: { $0.id == lastSelection }) {
if let firstId = contentPublisher.data.first?.id {
contentSelectionManager.setLastSelection(firstId)
}
}
}
}
}
func handle(_ context: EditWorkflowGroupWindow.Context) {
switch context {
case .add(let workflowGroup):
render([workflowGroup.id])
case .edit(let workflowGroup):
let workflowGroup = SidebarMapper.map(workflowGroup, applicationStore: applicationStore)
groupPublisher.publish(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])
}
NotificationCenter.default.post(.newWorkflow)
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(let action):
switch action {
case .applicationTrigger:
render(groupSelectionManager.selections, calculateSelections: false)
case .commandView(_, let action):
switch action {
case .changeDelay, .toggleNotify, .run: break
case .toggleEnabled, .updateName, .modify, .remove:
render(groupSelectionManager.selections, calculateSelections: false)
}
case .dropUrls, .duplicate, .moveCommand, .removeCommands,
.removeTrigger, .setIsEnabled, .updateKeyboardShortcuts,
.updateName, .updateExecution, .updateSnippet:
render(groupSelectionManager.selections, calculateSelections: false)
case .togglePassthrough, .runWorkflow, .trigger, .updateHoldDuration:
break
}
}
}
// MARK: Private methods
private func render(_ groupIds: Set<GroupViewModel.ID>,
calculateSelections: Bool = false,
selectionOverrides: Set<Workflow.ID>? = nil) {
Benchmark.shared.start("ContentCoordinator.render")
defer { Benchmark.shared.stop("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, groupId: group.id)
if wOffset == 0 {
firstViewModel = viewModel
}
viewModels.append(viewModel)
if calculateSelections &&
!selectedWorkflowIds.isEmpty &&
selectedWorkflowIds.contains(viewModel.id) {
selectedWorkflowIds.remove(viewModel.id)
newSelections.insert(viewModel.id)
}
}
}
}
contentPublisher.publish(viewModels)
if calculateSelections {
if contentPublisher.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)
}
}
}
}