@applicaster/zapp-react-native-utils 17.0.0-alpha.3352797522 → 17.0.0-alpha.9322493301
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/actionsExecutor/actions/navigateToScreen.ts +13 -2
- package/appUtils/HooksManager/index.ts +10 -2
- package/appUtils/focusManager/__tests__/focusLogs.test.ts +137 -0
- package/appUtils/focusManager/focusLogsFlag.ts +48 -0
- package/appUtils/focusManager/index.ts +117 -10
- package/appUtils/platform/platformUtils.ts +4 -20
- package/appUtils/playerManager/player.ts +2 -0
- package/debugging/DebugFeatues.tsx +2 -25
- package/debugging/DebugFeatures.ts +495 -0
- package/debugging/__tests__/DebugFeatures.test.ts +313 -0
- package/debugging/index.ts +3 -0
- package/errorUtils/index.ts +2 -0
- package/focusManager/FocusManager.ts +8 -4
- package/focusManager/__tests__/FocusManager.test.ts +3 -3
- package/focusManager/focusToolbox.ts +208 -0
- package/focusManager/index.ts +7 -0
- package/package.json +2 -2
- package/reactHooks/cell-click/helpers.ts +3 -1
- package/reactHooks/navigation/usePresentScreen.ts +17 -4
- package/reactHooks/resolvers/__tests__/useCellResolver.test.tsx +22 -0
- package/reactHooks/resolvers/useCellResolver.ts +4 -1
- package/reactHooks/resolvers/useComponentResolver.ts +10 -0
- package/reactUtils/__tests__/deviceTargetGuards.test.js +73 -0
- package/reactUtils/index.ts +28 -0
- package/uiActionsRegistrator/resolveActionIdentifiers.ts +5 -1
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { localStorage as NativeLocalStorageMock } from "../../../zapp-react-native-bridge/ZappStorage/__mocks__/android";
|
|
2
|
+
|
|
3
|
+
const STATE_KEY = "__applicasterDebugFeatures";
|
|
4
|
+
|
|
5
|
+
type LoadedModule = {
|
|
6
|
+
DebugFeaturesModule: typeof import("../DebugFeatures");
|
|
7
|
+
LegacyModule: typeof import("../DebugFeatues");
|
|
8
|
+
ReactNative: typeof import("react-native");
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const loadModules = (
|
|
12
|
+
nativeDebugBridge?: Record<string, jest.Mock>
|
|
13
|
+
): LoadedModule => {
|
|
14
|
+
delete (globalThis as any)[STATE_KEY];
|
|
15
|
+
NativeLocalStorageMock.__clear();
|
|
16
|
+
|
|
17
|
+
let DebugFeaturesModule: typeof import("../DebugFeatures");
|
|
18
|
+
let LegacyModule: typeof import("../DebugFeatues");
|
|
19
|
+
let ReactNative: typeof import("react-native");
|
|
20
|
+
|
|
21
|
+
jest.isolateModules(() => {
|
|
22
|
+
ReactNative = require("react-native");
|
|
23
|
+
ReactNative.NativeModules.LocalStorage = NativeLocalStorageMock;
|
|
24
|
+
|
|
25
|
+
if (nativeDebugBridge) {
|
|
26
|
+
ReactNative.NativeModules.DebugFeaturesBridge = nativeDebugBridge;
|
|
27
|
+
} else {
|
|
28
|
+
delete ReactNative.NativeModules.DebugFeaturesBridge;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
DebugFeaturesModule = require("../DebugFeatures");
|
|
32
|
+
LegacyModule = require("../DebugFeatues");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
DebugFeaturesModule,
|
|
37
|
+
LegacyModule,
|
|
38
|
+
ReactNative,
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
describe("DebugFeatures", () => {
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
delete (globalThis as any)[STATE_KEY];
|
|
45
|
+
const ReactNative = require("react-native");
|
|
46
|
+
delete ReactNative.NativeModules.DebugFeaturesBridge;
|
|
47
|
+
jest.restoreAllMocks();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("when native DebugFeaturesBridge is available", () => {
|
|
51
|
+
it("delegates registration, reading, writing, and receives native change events", async () => {
|
|
52
|
+
const nativeBridge = {
|
|
53
|
+
registerFeatureFlag: jest.fn().mockResolvedValue(true),
|
|
54
|
+
getFeatures: jest.fn().mockResolvedValue([
|
|
55
|
+
{
|
|
56
|
+
id: "plugin.flag",
|
|
57
|
+
group: "Plugin",
|
|
58
|
+
description: "Test flag",
|
|
59
|
+
initialState: true,
|
|
60
|
+
enabled: true,
|
|
61
|
+
value: "true",
|
|
62
|
+
},
|
|
63
|
+
]),
|
|
64
|
+
isFeatureFlagEnabled: jest.fn().mockResolvedValue(true),
|
|
65
|
+
toggleFeatureFlag: jest.fn().mockResolvedValue(false),
|
|
66
|
+
getStringValue: jest.fn().mockResolvedValue("123"),
|
|
67
|
+
setStringValue: jest.fn().mockResolvedValue("abc"),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const { DebugFeaturesModule, ReactNative } = loadModules(nativeBridge);
|
|
71
|
+
const { default: DebugFeatures, subscribe } = DebugFeaturesModule;
|
|
72
|
+
const instance = DebugFeatures.getInstance();
|
|
73
|
+
|
|
74
|
+
await expect(
|
|
75
|
+
instance.registerFeatureFlag({
|
|
76
|
+
id: "plugin.flag",
|
|
77
|
+
group: "Plugin",
|
|
78
|
+
description: "Test flag",
|
|
79
|
+
initialState: true,
|
|
80
|
+
})
|
|
81
|
+
).resolves.toBe(true);
|
|
82
|
+
|
|
83
|
+
expect(nativeBridge.registerFeatureFlag).toHaveBeenCalledWith({
|
|
84
|
+
id: "plugin.flag",
|
|
85
|
+
group: "Plugin",
|
|
86
|
+
description: "Test flag",
|
|
87
|
+
initialState: true,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
await expect(instance.getFeatures()).resolves.toEqual([
|
|
91
|
+
{
|
|
92
|
+
id: "plugin.flag",
|
|
93
|
+
group: "Plugin",
|
|
94
|
+
description: "Test flag",
|
|
95
|
+
initialState: true,
|
|
96
|
+
enabled: true,
|
|
97
|
+
value: "true",
|
|
98
|
+
},
|
|
99
|
+
]);
|
|
100
|
+
|
|
101
|
+
await expect(instance.isFeatureFlagEnabled("plugin.flag")).resolves.toBe(
|
|
102
|
+
true
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
await expect(
|
|
106
|
+
instance.toggleFeatureFlag("plugin.flag", false)
|
|
107
|
+
).resolves.toBe(false);
|
|
108
|
+
|
|
109
|
+
await expect(instance.getStringValue("plugin.str")).resolves.toBe("123");
|
|
110
|
+
|
|
111
|
+
await expect(instance.setStringValue("plugin.str", "abc")).resolves.toBe(
|
|
112
|
+
"abc"
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const globalEvents = [];
|
|
116
|
+
const keyEvents = [];
|
|
117
|
+
const unsubGlobal = subscribe((event) => globalEvents.push(event));
|
|
118
|
+
|
|
119
|
+
const unsubKey = subscribe(
|
|
120
|
+
(event) => keyEvents.push(event),
|
|
121
|
+
"plugin.flag"
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
ReactNative.DeviceEventEmitter.emit("DebugFeatures.changed", {
|
|
125
|
+
id: "plugin.flag",
|
|
126
|
+
value: "true",
|
|
127
|
+
enabled: true,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
ReactNative.DeviceEventEmitter.emit("DebugFeatures.changed", {
|
|
131
|
+
id: "other.flag",
|
|
132
|
+
value: "hello",
|
|
133
|
+
enabled: false,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
expect(globalEvents).toEqual([
|
|
137
|
+
{ id: "plugin.flag", value: "true", enabled: true },
|
|
138
|
+
{ id: "other.flag", value: "hello", enabled: false },
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
expect(keyEvents).toEqual([
|
|
142
|
+
{ id: "plugin.flag", value: "true", enabled: true },
|
|
143
|
+
]);
|
|
144
|
+
|
|
145
|
+
unsubGlobal();
|
|
146
|
+
unsubKey();
|
|
147
|
+
|
|
148
|
+
ReactNative.DeviceEventEmitter.emit("DebugFeatures.changed", {
|
|
149
|
+
id: "plugin.flag",
|
|
150
|
+
value: "false",
|
|
151
|
+
enabled: false,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
expect(globalEvents).toHaveLength(2);
|
|
155
|
+
expect(keyEvents).toHaveLength(1);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("when native DebugFeaturesBridge is missing", () => {
|
|
160
|
+
it("falls back to LocalStorage and in-JS registry and notifies subscribers", async () => {
|
|
161
|
+
const { DebugFeaturesModule, LegacyModule } = loadModules();
|
|
162
|
+
|
|
163
|
+
const {
|
|
164
|
+
default: DebugFeatures,
|
|
165
|
+
registerFeatureFlag,
|
|
166
|
+
getFeatures,
|
|
167
|
+
isFeatureFlagEnabled,
|
|
168
|
+
toggleFeatureFlag,
|
|
169
|
+
getStringValue,
|
|
170
|
+
setStringValue,
|
|
171
|
+
subscribe,
|
|
172
|
+
} = DebugFeaturesModule;
|
|
173
|
+
|
|
174
|
+
const globalListener = jest.fn();
|
|
175
|
+
const keyListener = jest.fn();
|
|
176
|
+
const unsubGlobal = subscribe(globalListener);
|
|
177
|
+
const unsubKey = subscribe(keyListener, "flag.initial_true");
|
|
178
|
+
|
|
179
|
+
await expect(
|
|
180
|
+
registerFeatureFlag({
|
|
181
|
+
id: "flag.initial_true",
|
|
182
|
+
group: "Debug",
|
|
183
|
+
description: "Starts enabled",
|
|
184
|
+
initialState: true,
|
|
185
|
+
})
|
|
186
|
+
).resolves.toBe(true);
|
|
187
|
+
|
|
188
|
+
await expect(
|
|
189
|
+
registerFeatureFlag({
|
|
190
|
+
id: "flag.default_off",
|
|
191
|
+
group: "Debug",
|
|
192
|
+
description: "Starts disabled",
|
|
193
|
+
})
|
|
194
|
+
).resolves.toBe(false);
|
|
195
|
+
|
|
196
|
+
expect(globalListener).toHaveBeenCalledWith({
|
|
197
|
+
id: "flag.initial_true",
|
|
198
|
+
value: "true",
|
|
199
|
+
enabled: true,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
expect(keyListener).toHaveBeenCalledWith({
|
|
203
|
+
id: "flag.initial_true",
|
|
204
|
+
value: "true",
|
|
205
|
+
enabled: true,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// Re-registering initialState: true does not overwrite an explicit false
|
|
209
|
+
await expect(toggleFeatureFlag("flag.initial_true", false)).resolves.toBe(
|
|
210
|
+
false
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
await expect(
|
|
214
|
+
registerFeatureFlag({
|
|
215
|
+
id: "flag.initial_true",
|
|
216
|
+
group: "Debug",
|
|
217
|
+
description: "Starts enabled",
|
|
218
|
+
initialState: true,
|
|
219
|
+
})
|
|
220
|
+
).resolves.toBe(false);
|
|
221
|
+
|
|
222
|
+
await expect(isFeatureFlagEnabled("flag.initial_true")).resolves.toBe(
|
|
223
|
+
false
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
await expect(
|
|
227
|
+
LegacyModule.default
|
|
228
|
+
.getInstance()
|
|
229
|
+
.isFeatureFlagEnabled("flag.initial_true")
|
|
230
|
+
).resolves.toBe(false);
|
|
231
|
+
|
|
232
|
+
await expect(
|
|
233
|
+
DebugFeatures.getInstance().isFeatureFlagEnabled("flag.default_off")
|
|
234
|
+
).resolves.toBe(false);
|
|
235
|
+
|
|
236
|
+
await expect(setStringValue("flag.str", "123")).resolves.toBe("123");
|
|
237
|
+
await expect(getStringValue("flag.str")).resolves.toBe("123");
|
|
238
|
+
await expect(setStringValue("flag.str", "")).resolves.toBeNull();
|
|
239
|
+
await expect(getStringValue("flag.str")).resolves.toBeNull();
|
|
240
|
+
|
|
241
|
+
await expect(getFeatures()).resolves.toEqual([
|
|
242
|
+
{
|
|
243
|
+
id: "flag.default_off",
|
|
244
|
+
group: "Debug",
|
|
245
|
+
description: "Starts disabled",
|
|
246
|
+
initialState: false,
|
|
247
|
+
enabled: false,
|
|
248
|
+
value: null,
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
id: "flag.initial_true",
|
|
252
|
+
group: "Debug",
|
|
253
|
+
description: "Starts enabled",
|
|
254
|
+
initialState: true,
|
|
255
|
+
enabled: false,
|
|
256
|
+
value: "false",
|
|
257
|
+
},
|
|
258
|
+
]);
|
|
259
|
+
|
|
260
|
+
expect(globalListener).toHaveBeenLastCalledWith({
|
|
261
|
+
id: "flag.str",
|
|
262
|
+
value: null,
|
|
263
|
+
enabled: false,
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
unsubGlobal();
|
|
267
|
+
unsubKey();
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("logs an error via X-Ray logger when native module is present but a method is missing", async () => {
|
|
271
|
+
const logErrorMock = jest.fn();
|
|
272
|
+
|
|
273
|
+
jest.doMock("../../logger", () => ({
|
|
274
|
+
...jest.requireActual("../../logger"),
|
|
275
|
+
createLogger: () => ({
|
|
276
|
+
log_verbose: jest.fn(),
|
|
277
|
+
log_debug: jest.fn(),
|
|
278
|
+
log_info: jest.fn(),
|
|
279
|
+
log_warning: jest.fn(),
|
|
280
|
+
log_error: logErrorMock,
|
|
281
|
+
}),
|
|
282
|
+
}));
|
|
283
|
+
|
|
284
|
+
const partialBridge = {
|
|
285
|
+
registerFeatureFlag: jest.fn().mockResolvedValue(true),
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const { DebugFeaturesModule } = loadModules(partialBridge);
|
|
289
|
+
|
|
290
|
+
const { registerFeatureFlag, isFeatureFlagEnabled, getStringValue } =
|
|
291
|
+
DebugFeaturesModule;
|
|
292
|
+
|
|
293
|
+
try {
|
|
294
|
+
await expect(
|
|
295
|
+
registerFeatureFlag({ id: "flag.partial", initialState: true })
|
|
296
|
+
).resolves.toBe(true);
|
|
297
|
+
|
|
298
|
+
await expect(isFeatureFlagEnabled("flag.partial")).resolves.toBe(false);
|
|
299
|
+
await expect(getStringValue("flag.partial")).resolves.toBeNull();
|
|
300
|
+
|
|
301
|
+
expect(logErrorMock).toHaveBeenCalledWith(
|
|
302
|
+
"DebugFeaturesBridge.isFeatureFlagEnabled is missing on native module"
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
expect(logErrorMock).toHaveBeenCalledWith(
|
|
306
|
+
"DebugFeaturesBridge.getStringValue is missing on native module"
|
|
307
|
+
);
|
|
308
|
+
} finally {
|
|
309
|
+
jest.dontMock("../../logger");
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
});
|
package/errorUtils/index.ts
CHANGED
|
@@ -14,6 +14,8 @@ export const getParsedResponse = (err): XRayEventData => {
|
|
|
14
14
|
|
|
15
15
|
const xRayData: XRayEventData = {
|
|
16
16
|
data: {
|
|
17
|
+
// What X-Ray diagnostics read is top level in `data`: the error's context carries it.
|
|
18
|
+
diagnostic: context?.diagnostic,
|
|
17
19
|
context,
|
|
18
20
|
name,
|
|
19
21
|
params,
|
|
@@ -451,13 +451,17 @@ class FocusManager {
|
|
|
451
451
|
}
|
|
452
452
|
|
|
453
453
|
private hasFocus = (node) => {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
454
|
+
const propsId = node?.component?.current?.props?.id;
|
|
455
|
+
|
|
456
|
+
// A focused cell keeps its buttons as children. Count that cell, not only leaves.
|
|
457
|
+
if (
|
|
458
|
+
this.isFocused(propsId) ||
|
|
459
|
+
(node?.id !== propsId && this.isFocused(node?.id))
|
|
460
|
+
) {
|
|
457
461
|
return true;
|
|
458
462
|
}
|
|
459
463
|
|
|
460
|
-
return false;
|
|
464
|
+
return node.children?.some((child) => this.hasFocus(child)) ?? false;
|
|
461
465
|
};
|
|
462
466
|
|
|
463
467
|
isAnyDescendantFocused(id: string) {
|
|
@@ -123,11 +123,11 @@ describe("FocusManager", () => {
|
|
|
123
123
|
/* check state of focus */
|
|
124
124
|
expect(focusManager.isAnyDescendantFocused("group")).toBe(false);
|
|
125
125
|
|
|
126
|
-
/* set focus on 2nd child */
|
|
126
|
+
/* set focus on 2nd child, which itself has children */
|
|
127
127
|
focusManager.setFocus(child2.current.props.id);
|
|
128
128
|
|
|
129
|
-
/*
|
|
130
|
-
expect(focusManager.isAnyDescendantFocused("group")).toBe(
|
|
129
|
+
/* the focused node is the child, not one of its leaves */
|
|
130
|
+
expect(focusManager.isAnyDescendantFocused("group")).toBe(true);
|
|
131
131
|
});
|
|
132
132
|
|
|
133
133
|
it("focusManager isFocusableChildOf should be defined", () => {
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { registerXrayToolbox, XrayToolSpec } from "../logger/xrayToolbox";
|
|
2
|
+
|
|
3
|
+
import { focusManager } from "./FocusManager";
|
|
4
|
+
import { TreeNode } from "./TreeNode";
|
|
5
|
+
import { Tree } from "./Tree";
|
|
6
|
+
|
|
7
|
+
import type { Touchable } from "@applicaster/zapp-react-native-ui-components/Components/Focusable/Touchable";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* X-Ray `focus.*` toolbox for Android TV: lets an agent read and move focus by focusable id,
|
|
11
|
+
* without a remote and without depending on screen size or layout. It is built on the
|
|
12
|
+
* class-based FocusManager that Android uses (unlike tvOS, which uses the IIFE focus manager
|
|
13
|
+
* with its own native focus module).
|
|
14
|
+
*
|
|
15
|
+
* The manager's `focusedId` follows the native Android focus engine (the app's own code moves
|
|
16
|
+
* focus through the manager, and native focus events report back into it), and `setFocus`
|
|
17
|
+
* moves focus the same way the app's own code does. Registered by the android focus manager
|
|
18
|
+
* module; skipped when the X-Ray plugin has no React toolbox API (release builds).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
type FocusManagerApi = {
|
|
22
|
+
focusedId: string | null;
|
|
23
|
+
isFocused: (id: string | number) => boolean;
|
|
24
|
+
setFocus: (newFocus: string, options?: unknown, context?: unknown) => void;
|
|
25
|
+
focused: Touchable | null | undefined;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** `Tree` keeps its roots in a private field; this is the only access point to it. */
|
|
29
|
+
const internalTree = (manager: FocusManagerApi): Tree | undefined => {
|
|
30
|
+
const holder = manager as FocusManagerApi & { tree?: Tree };
|
|
31
|
+
|
|
32
|
+
return holder.tree;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** The tree's roots, through the private field. */
|
|
36
|
+
const treeRoots = (tree: Tree): TreeNode[] =>
|
|
37
|
+
(tree as unknown as { tree: TreeNode[] }).tree;
|
|
38
|
+
|
|
39
|
+
const nodeProps = (node: TreeNode | null): { testID?: unknown } =>
|
|
40
|
+
(node?.component?.current?.props ?? {}) as { testID?: unknown };
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The focused item. On Android the manager's `focusedId` is the focused focusable itself and
|
|
44
|
+
* follows the native engine, so no tree walk is needed (unlike tvOS, where it only tracks
|
|
45
|
+
* focus across groups).
|
|
46
|
+
*/
|
|
47
|
+
const describeFocused = (manager: FocusManagerApi) => {
|
|
48
|
+
const itemId = manager.focusedId;
|
|
49
|
+
|
|
50
|
+
if (!itemId) return null;
|
|
51
|
+
|
|
52
|
+
const tree = internalTree(manager);
|
|
53
|
+
const node = tree?.find(itemId) ?? null;
|
|
54
|
+
const groups: string[] = [];
|
|
55
|
+
|
|
56
|
+
let parent = node?.parentId ? tree?.findParent(node.parentId) : null;
|
|
57
|
+
|
|
58
|
+
while (parent?.id && groups.length < 32) {
|
|
59
|
+
groups.push(parent.id);
|
|
60
|
+
parent = parent.parentId ? tree?.findParent(parent.parentId) : null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const testID = nodeProps(node).testID;
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
itemId,
|
|
67
|
+
groupId: node?.parentId ?? null,
|
|
68
|
+
testID: typeof testID === "string" ? testID : null,
|
|
69
|
+
groups,
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
type TreeView = {
|
|
74
|
+
id: string;
|
|
75
|
+
group?: true;
|
|
76
|
+
focused?: true;
|
|
77
|
+
testID?: string;
|
|
78
|
+
children?: TreeView[];
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** A node as the agent sees it: keys only when they say something, to keep large screens short. */
|
|
82
|
+
const toTreeView = (manager: FocusManagerApi, node: TreeNode): TreeView => {
|
|
83
|
+
const testID = nodeProps(node).testID;
|
|
84
|
+
|
|
85
|
+
const children = (node.children ?? []).map((child) =>
|
|
86
|
+
toTreeView(manager, child)
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
id: node.id,
|
|
91
|
+
...(node.isFocusableCell ? {} : { group: true as const }),
|
|
92
|
+
...(manager.isFocused(node.id) ? { focused: true as const } : {}),
|
|
93
|
+
...(typeof testID === "string" ? { testID } : {}),
|
|
94
|
+
...(children.length ? { children } : {}),
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const tools = (manager: FocusManagerApi): XrayToolSpec[] => [
|
|
99
|
+
{
|
|
100
|
+
name: "current",
|
|
101
|
+
description:
|
|
102
|
+
"The focused item: itemId, groupId, testID and the ids of its enclosing groups. null when nothing is.",
|
|
103
|
+
handler: () => describeFocused(manager),
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
name: "tree",
|
|
107
|
+
description:
|
|
108
|
+
"The focus manager's tree of registered Focusables: id, group: true for groups, focused: true on " +
|
|
109
|
+
"the focused item, testID, children. Only rendered Focusables are registered. A screen can hold " +
|
|
110
|
+
"hundreds; pass groupId for one group's subtree. Item ids are what set takes.",
|
|
111
|
+
parameters: [
|
|
112
|
+
{
|
|
113
|
+
name: "groupId",
|
|
114
|
+
type: "string",
|
|
115
|
+
description:
|
|
116
|
+
"Return only this group's subtree. Default: the whole tree.",
|
|
117
|
+
required: false,
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
handler: ({ groupId }) => {
|
|
121
|
+
const tree = internalTree(manager);
|
|
122
|
+
|
|
123
|
+
if (!tree) throw new Error("Focus manager tree is not available");
|
|
124
|
+
|
|
125
|
+
if (groupId === undefined || groupId === null || groupId === "") {
|
|
126
|
+
return treeRoots(tree).map((root) => toTreeView(manager, root));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const node = tree.find(String(groupId));
|
|
130
|
+
|
|
131
|
+
if (!node) throw new Error(`No Focusable group "${groupId}"`);
|
|
132
|
+
|
|
133
|
+
return toTreeView(manager, node);
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
name: "set",
|
|
138
|
+
description:
|
|
139
|
+
"Focus an item through the focus manager (setFocus), as the app's own code does. " +
|
|
140
|
+
"focused: false when it did not land within 3 s (unknown ids, or the item is not rendered, e.g. " +
|
|
141
|
+
"scrolled out of a virtualised list). It replaces a focus the app is still setting, so call it " +
|
|
142
|
+
"once the screen is idle (ui.waitFor idle).",
|
|
143
|
+
parameters: [
|
|
144
|
+
{
|
|
145
|
+
name: "itemId",
|
|
146
|
+
type: "string",
|
|
147
|
+
description: "Focusable item id (from tree).",
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
handler: ({ itemId }) =>
|
|
151
|
+
new Promise((resolve) => {
|
|
152
|
+
let done = false;
|
|
153
|
+
|
|
154
|
+
const finish = (result: { focused: boolean; timedOut?: boolean }) => {
|
|
155
|
+
if (done) return;
|
|
156
|
+
|
|
157
|
+
done = true;
|
|
158
|
+
resolve({ ...result, current: describeFocused(manager) });
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const timer = setTimeout(
|
|
162
|
+
() => finish({ focused: false, timedOut: true }),
|
|
163
|
+
3000
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
const poll = () => {
|
|
167
|
+
if (done) return;
|
|
168
|
+
|
|
169
|
+
if (manager.isFocused(String(itemId))) {
|
|
170
|
+
clearTimeout(timer);
|
|
171
|
+
finish({ focused: true });
|
|
172
|
+
} else {
|
|
173
|
+
setTimeout(poll, 50);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
manager.setFocus(String(itemId));
|
|
178
|
+
setTimeout(poll, 50);
|
|
179
|
+
}),
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: "select",
|
|
183
|
+
description:
|
|
184
|
+
"Press select on the focused item, as the remote's click does.",
|
|
185
|
+
handler: () => {
|
|
186
|
+
const ref = manager.focused;
|
|
187
|
+
|
|
188
|
+
if (!ref?.onPress) {
|
|
189
|
+
throw new Error("Nothing selectable is focused");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// The app's focus callbacks pass the Touchable instance (ref.props.id), so onPress gets the instance too.
|
|
193
|
+
ref.onPress(ref);
|
|
194
|
+
|
|
195
|
+
return { selected: describeFocused(manager) };
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
export const registerAndroidFocusToolbox = (
|
|
201
|
+
manager: FocusManagerApi = focusManager
|
|
202
|
+
) =>
|
|
203
|
+
registerXrayToolbox({
|
|
204
|
+
name: "focus",
|
|
205
|
+
description:
|
|
206
|
+
"Android TV focus: current item, Focusable tree, focus by id, select.",
|
|
207
|
+
tools: tools(manager),
|
|
208
|
+
});
|
package/focusManager/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { toBooleanWithDefaultTrue } from "../booleanUtils";
|
|
|
7
7
|
import { isNilOrEmpty } from "../reactUtils/helpers";
|
|
8
8
|
import { isAndroidTVPlatform } from "../reactUtils";
|
|
9
9
|
import { noop } from "../functionUtils";
|
|
10
|
+
import { registerAndroidFocusToolbox } from "./focusToolbox";
|
|
10
11
|
|
|
11
12
|
export function useFocusManager() {
|
|
12
13
|
return focusManager;
|
|
@@ -132,3 +133,9 @@ export const useIsFocusable = (props: Props): void => {
|
|
|
132
133
|
hasDataError,
|
|
133
134
|
]);
|
|
134
135
|
};
|
|
136
|
+
|
|
137
|
+
// The Android focus manager registers the X-Ray focus toolbox itself (mirrors the tvOS focus
|
|
138
|
+
// manager); it is a no-op in builds without the X-Ray React toolbox bridge.
|
|
139
|
+
if (isAndroidTVPlatform()) {
|
|
140
|
+
registerAndroidFocusToolbox(focusManager);
|
|
141
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "17.0.0-alpha.
|
|
3
|
+
"version": "17.0.0-alpha.9322493301",
|
|
4
4
|
"description": "Applicaster Zapp React Native utilities package",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@applicaster/applicaster-types": "17.0.0-alpha.
|
|
30
|
+
"@applicaster/applicaster-types": "17.0.0-alpha.9322493301",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
|
@@ -5,7 +5,9 @@ export const warnEmptyContentType = (item) =>
|
|
|
5
5
|
message:
|
|
6
6
|
"The feed or feed item selected is missing content type. For more info see link to docs below.",
|
|
7
7
|
data: {
|
|
8
|
-
|
|
8
|
+
// For X-Ray diagnostics: keep when the message is reworded.
|
|
9
|
+
diagnostic: { code: "entry-type-missing" },
|
|
10
|
+
entry: item,
|
|
9
11
|
docs: "https://docs.applicaster.com/using-zapp/app-building-walk-through#add-type-mapping",
|
|
10
12
|
},
|
|
11
13
|
});
|
|
@@ -44,8 +44,11 @@ interface Dependencies {
|
|
|
44
44
|
contentTypes: ZappContentTypes;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
const failure = (
|
|
48
|
-
|
|
47
|
+
const failure = (
|
|
48
|
+
error: string,
|
|
49
|
+
data?: Record<string, unknown>
|
|
50
|
+
): Promise<PresentScreenResult> => {
|
|
51
|
+
log_error(`presentScreen: ${error}`, data);
|
|
49
52
|
|
|
50
53
|
return Promise.resolve({ success: false, cancelled: false, error });
|
|
51
54
|
};
|
|
@@ -77,13 +80,23 @@ export const presentScreen = ({
|
|
|
77
80
|
const screenId = contentTypes?.[typeMapping]?.screen_id;
|
|
78
81
|
|
|
79
82
|
if (!screenId) {
|
|
80
|
-
return failure(`can not resolve screen type mapping: ${typeMapping}
|
|
83
|
+
return failure(`can not resolve screen type mapping: ${typeMapping}`, {
|
|
84
|
+
// For X-Ray diagnostics: keep when the message is reworded.
|
|
85
|
+
diagnostic: {
|
|
86
|
+
code: "action-type-mapping",
|
|
87
|
+
case: contentTypes?.[typeMapping] ? "no_screen" : "missing",
|
|
88
|
+
},
|
|
89
|
+
type_mapping: typeMapping,
|
|
90
|
+
});
|
|
81
91
|
}
|
|
82
92
|
|
|
83
93
|
const river = rivers?.[screenId];
|
|
84
94
|
|
|
85
95
|
if (!river) {
|
|
86
|
-
return failure(`can not resolve river for screen id: ${screenId}
|
|
96
|
+
return failure(`can not resolve river for screen id: ${screenId}`, {
|
|
97
|
+
diagnostic: { code: "action-type-mapping", case: "screen_deleted" },
|
|
98
|
+
type_mapping: typeMapping,
|
|
99
|
+
});
|
|
87
100
|
}
|
|
88
101
|
|
|
89
102
|
return new Promise<PresentScreenResult>((resolve) => {
|
|
@@ -66,4 +66,26 @@ describe("cellResolver", () => {
|
|
|
66
66
|
|
|
67
67
|
expect(testCellModule).toBeCalled();
|
|
68
68
|
});
|
|
69
|
+
|
|
70
|
+
it("does not share a renderer between components with the same styles", () => {
|
|
71
|
+
testCellModule.mockClear();
|
|
72
|
+
|
|
73
|
+
renderHook(
|
|
74
|
+
() => useCellResolver({ component: { ...component, id: "a" } }),
|
|
75
|
+
{
|
|
76
|
+
wrapper,
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
renderHook(
|
|
81
|
+
() => useCellResolver({ component: { ...component, id: "b" } }),
|
|
82
|
+
{
|
|
83
|
+
wrapper,
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
expect(testCellModule).toHaveBeenCalledTimes(2);
|
|
88
|
+
|
|
89
|
+
expect(testCellModule.mock.calls[1][0].component.id).toBe("b");
|
|
90
|
+
});
|
|
69
91
|
});
|