-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathChartManager.ts
More file actions
259 lines (223 loc) · 6 KB
/
ChartManager.ts
File metadata and controls
259 lines (223 loc) · 6 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import {
TapGesture,
PanGesture,
PinchGesture,
RotationGesture,
FlingGesture,
LongPressGesture,
ForceTouchGesture,
NativeGesture,
ManualGesture,
HoverGesture,
GestureStateChangeEvent,
State,
} from 'react-native-gesture-handler';
export const WAVE_DELAY_MS = 150;
const Colors = {
BLUE: 'var(--swm-blue-light-80)',
GREEN: 'var(--swm-green-light-80)',
YELLOW: 'var(--swm-yellow-light-80)',
RED: 'var(--swm-red-light-80)',
};
export type Item = {
id: number;
label?: string;
subtext?: string;
isVisible: boolean;
highlightColor: string;
};
const stateToName = new Map<number, string>([
[State.UNDETERMINED, 'UNDETERMINED'],
[State.FAILED, 'FAILED'],
[State.BEGAN, 'BEGAN'],
[State.CANCELLED, 'CANCELLED'],
[State.ACTIVE, 'ACTIVE'],
[State.END, 'END'],
]);
const labelColorMap = new Map<string, string>([
[stateToName.get(State.BEGAN), Colors.BLUE],
[stateToName.get(State.ACTIVE), Colors.GREEN],
[stateToName.get(State.END), Colors.BLUE],
[stateToName.get(State.FAILED), Colors.RED],
[stateToName.get(State.CANCELLED), Colors.RED],
[stateToName.get(State.UNDETERMINED), Colors.YELLOW],
]);
class ChartConnection {
id: number;
from: number;
to: number;
}
type GesturesUnion =
| TapGesture
| PanGesture
| PinchGesture
| RotationGesture
| FlingGesture
| LongPressGesture
| ForceTouchGesture
| NativeGesture
| ManualGesture
| HoverGesture;
type IdObject = {
began: number;
active: number;
end: number;
failed: number;
cancelled: number;
undetermined: number;
};
export class GestureHandle {
// within gesture, States can be used as unique IDs pointing to the item pool
_itemIds: IdObject;
get idObject() {
return this._itemIds;
}
set idObject(newObject: IdObject) {
this._itemIds = newObject;
}
}
export default class ChartManager {
private _items: Item[] = [];
private _connections: ChartConnection[] = [];
private _layout: number[][];
private _listeners: Map<number, Map<number, (isActive: boolean) => void>> =
new Map();
public static EMPTY_SPACE_ID = 0;
constructor() {
this.addItem(null, null, false);
}
get items(): Item[] {
return this._items;
}
get connections(): ChartConnection[] {
return this._connections;
}
get layout(): number[][] {
return this._layout;
}
set layout(layoutGrid: number[][]) {
this._layout = layoutGrid;
}
public addListener(
itemId: number,
listener: (isActive: boolean) => void
): number {
const listenerId = this._listeners.get(itemId)?.size - 1;
// another map is used inside of _listeners to seamlessly remove listening functions from _listeners
if (this._listeners.has(itemId)) {
this._listeners.get(itemId).set(listenerId, listener);
} else {
this._listeners.set(itemId, new Map([[0, listener]]));
}
return listenerId;
}
public removeListener(itemId: number, listenerId: number): void {
this._listeners.get(itemId).delete(listenerId);
}
public clearListeners(): void {
this._listeners.clear();
}
public addItem(
label: State | string = null,
subtext: string | null = null,
isVisible: boolean = true
): [(isActive: boolean) => void, number] {
const newId = this._items.length;
if (typeof label == 'number') {
label = stateToName.get(label);
}
const highlightColor = labelColorMap.get(label) ?? Colors.YELLOW;
const newItem = {
id: newId,
label: label,
subtext: subtext,
position: null,
isVisible: isVisible,
highlightColor: highlightColor,
};
this._items.push(newItem);
// this callback will be used by a .onX hook to broadcast this event to all listeners
return [
(isActive: boolean) => {
this._listeners.get(newId)?.forEach((listener) => listener(isActive));
},
newId,
];
}
public addConnection(fromId: number, toId: number) {
this._connections.push({
id: this._connections.length,
from: fromId,
to: toId,
});
}
public newGesture(
gesture: GesturesUnion
): [GestureHandle, GesturesUnion, () => void] {
const [beganCallback, beganId] = this.addItem(State.BEGAN);
const [activeCallback, activeId] = this.addItem(State.ACTIVE);
const [endCallback, endId] = this.addItem(State.END);
const [failedCallback, failedId] = this.addItem(State.FAILED);
const [cancelledCallback, cancelledId] = this.addItem(State.CANCELLED);
const [undeterminedCallback, undeterminedId] = this.addItem(
State.UNDETERMINED
);
const handle = new GestureHandle();
handle.idObject = {
began: beganId,
active: activeId,
end: endId,
failed: failedId,
cancelled: cancelledId,
undetermined: undeterminedId,
} as IdObject;
undeterminedCallback(true);
const resetAllStates = (event: GestureStateChangeEvent<unknown>) => {
undeterminedCallback(true);
if (event.state === State.FAILED) {
failedCallback(true);
}
if (event.state === State.CANCELLED) {
cancelledCallback(true);
}
setTimeout(() => {
beganCallback(false);
activeCallback(false);
}, WAVE_DELAY_MS);
setTimeout(() => {
endCallback(false);
failedCallback(false);
cancelledCallback(false);
}, 2 * WAVE_DELAY_MS);
};
gesture
.onBegin(() => {
beganCallback(true);
undeterminedCallback(false);
})
.onStart(() => {
beganCallback(false);
activeCallback(true);
})
.onEnd(() => {
endCallback(true);
})
.onFinalize((event: GestureStateChangeEvent<unknown>) => {
resetAllStates(event);
});
[
[undeterminedId, beganId],
[beganId, activeId],
[beganId, failedId],
[activeId, endId],
[activeId, cancelledId],
[beganId, cancelledId],
].forEach(([from, to]) => {
this.addConnection(from, to);
});
const resetCb = () => {
undeterminedCallback(true);
};
return [handle, gesture, resetCb];
}
}