@applicaster/zapp-react-native-utils 14.0.0-alpha.9551591420 → 14.0.0-alpha.9725730345

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.
@@ -6,6 +6,7 @@ exports[`focusManager should be defined 1`] = `
6
6
  "disableFocus": [Function],
7
7
  "enableFocus": [Function],
8
8
  "findPreferredFocusChild": [Function],
9
+ "focusTopNavigation": [Function],
9
10
  "focusableTree": Tree {
10
11
  "loadingCounter": 0,
11
12
  "root": {
@@ -24,6 +25,9 @@ exports[`focusManager should be defined 1`] = `
24
25
  "invokeHandler": [Function],
25
26
  "isCurrentFocusOnTheTopScreen": [Function],
26
27
  "isFocusDisabled": [Function],
28
+ "isFocusOn": [Function],
29
+ "isFocusOnContent": [Function],
30
+ "isFocusOnMenu": [Function],
27
31
  "isGroupItemFocused": [Function],
28
32
  "longPress": [Function],
29
33
  "moveFocus": [Function],
@@ -63,6 +67,7 @@ exports[`focusManagerIOS should be defined 1`] = `
63
67
  "getGroupRootById": [Function],
64
68
  "getPreferredFocusChild": [Function],
65
69
  "invokeHandler": [Function],
70
+ "isFocusOn": [Function],
66
71
  "isGroupItemFocused": [Function],
67
72
  "moveFocus": [Function],
68
73
  "on": [Function],
@@ -33,7 +33,7 @@ describe("focusManager", () => {
33
33
 
34
34
  expect(success).toBe(true);
35
35
  expect(mockSetFocus).toBeCalledTimes(1);
36
- expect(mockSetFocus).toBeCalledWith(null);
36
+ expect(mockSetFocus).toBeCalledWith(null, undefined);
37
37
  });
38
38
 
39
39
  describe("register", () => {});
@@ -1,6 +1,7 @@
1
1
  import { NativeModules } from "react-native";
2
2
  import * as R from "ramda";
3
3
 
4
+ import { isCurrentFocusOn } from "../focusManagerAux/utils";
4
5
  import { Tree } from "./treeDataStructure/Tree";
5
6
  import { findFocusableNode } from "./treeDataStructure/Utils";
6
7
  import { subscriber } from "../../functionUtils";
@@ -391,6 +392,14 @@ export const focusManager = (function () {
391
392
  return node;
392
393
  }
393
394
 
395
+ function isFocusOn(id): boolean {
396
+ const currentFocusNode = focusableTree.findInTree(
397
+ getCurrentFocus()?.props?.id
398
+ );
399
+
400
+ return id && isCurrentFocusOn(id, currentFocusNode);
401
+ }
402
+
394
403
  return {
395
404
  on,
396
405
  invokeHandler,
@@ -412,5 +421,6 @@ export const focusManager = (function () {
412
421
  getGroupRootById,
413
422
  isGroupItemFocused,
414
423
  getPreferredFocusChild,
424
+ isFocusOn,
415
425
  };
416
426
  })();
@@ -14,6 +14,15 @@ import { subscriber } from "../../functionUtils";
14
14
  import { coreLogger } from "../../logger";
15
15
  import { ACTION } from "./utils/enums";
16
16
 
17
+ import {
18
+ findSelectedTabId,
19
+ findSelectedMenuId,
20
+ isTabsScreenContentFocused,
21
+ isCurrentFocusOnContent,
22
+ isCurrentFocusOnMenu,
23
+ isCurrentFocusOn,
24
+ } from "../focusManagerAux/utils";
25
+
17
26
  const logger = coreLogger.addSubsystem("focusManager");
18
27
 
19
28
  const isFocusEnabled = (focusableItem): boolean => {
@@ -100,7 +109,7 @@ export const focusManager = (function () {
100
109
  * @private
101
110
  * @param {Object} direction of the navigation which led to this action
102
111
  */
103
- function focus(direction) {
112
+ function focus(direction, context?: FocusManager.FocusContext) {
104
113
  const currentFocusable = getCurrentFocus();
105
114
 
106
115
  if (
@@ -108,7 +117,7 @@ export const focusManager = (function () {
108
117
  !currentFocusable.isGroup &&
109
118
  currentFocusable.isMounted()
110
119
  ) {
111
- currentFocusable.setFocus(direction);
120
+ currentFocusable.setFocus(direction, context);
112
121
  }
113
122
  }
114
123
 
@@ -205,7 +214,7 @@ export const focusManager = (function () {
205
214
  * @param {Array<string>} ids - An array of node IDs to update.
206
215
  * @param {boolean} setFocus - A flag indicating whether to set focus (true) or blur (false) on the nodes.
207
216
  */
208
- const updateNodeFocus = (ids, action) => {
217
+ const updateNodeFocus = (ids, action, context: FocusManager.FocusContext) => {
209
218
  if (!ids || ids.length === 0) {
210
219
  return; // Nothing to do
211
220
  }
@@ -222,11 +231,13 @@ export const focusManager = (function () {
222
231
 
223
232
  // Function to apply the action (focus or blur)
224
233
  const applyAction = (node) => {
234
+ const direction = undefined;
235
+
225
236
  if (node && node.component) {
226
237
  if (action === "focus") {
227
- node.component.setFocus();
238
+ node.component.setFocus(direction, context);
228
239
  } else if (action === "blur") {
229
- node.component.setBlur();
240
+ node.component.setBlur(direction, context);
230
241
  }
231
242
  }
232
243
  };
@@ -253,7 +264,11 @@ export const focusManager = (function () {
253
264
  * @param {Object} direction of the navigation, which led to this focus change
254
265
  * to another group or not. defaults to false
255
266
  */
256
- function setFocus(id: string, direction?: FocusManager.Web.Direction) {
267
+ function setFocus(
268
+ id: string,
269
+ direction?: FocusManager.Web.Direction,
270
+ context?: FocusManager.FocusContext
271
+ ) {
257
272
  if (focusDisabled) return false;
258
273
 
259
274
  // due to optimisiation it's recommanded to set currentFocusNode before setFocus
@@ -266,21 +281,65 @@ export const focusManager = (function () {
266
281
  );
267
282
 
268
283
  // Set focus on current node parents and blur on previous node parents
269
- updateNodeFocus(currentNodeParentsIDs, ACTION.FOCUS);
270
- updateNodeFocus(previousNodeParentsIDs, ACTION.BLUR);
284
+ updateNodeFocus(currentNodeParentsIDs, ACTION.FOCUS, context);
285
+ updateNodeFocus(previousNodeParentsIDs, ACTION.BLUR, context);
271
286
 
272
287
  currentFocusNode = focusableTree.findInTree(id);
273
288
  }
274
289
 
275
290
  setLastFocusOnParentNode(currentFocusNode);
276
291
 
277
- focus(direction);
292
+ focus(direction, context);
293
+ }
294
+
295
+ function isFocusOnContent() {
296
+ return isCurrentFocusOnContent(currentFocusNode);
297
+ }
298
+
299
+ function isFocusOnMenu() {
300
+ return isCurrentFocusOnMenu(currentFocusNode);
301
+ }
302
+
303
+ // Move focus to appropriate top navigation tab with context
304
+ function focusTopNavigation(isTabsScreen: boolean, item: ZappEntry) {
305
+ const landFocusTo = (id) => {
306
+ if (id) {
307
+ // set focus on selected menu item
308
+ const direction = undefined;
309
+
310
+ const context: FocusManager.FocusContext = {
311
+ source: "back",
312
+ preserveScroll: true,
313
+ };
314
+
315
+ logger.log({ message: "landFocusTo", data: { id } });
316
+
317
+ blur(direction);
318
+ setFocus(id, direction, context);
319
+ }
320
+ };
321
+
322
+ if (isTabsScreen && isTabsScreenContentFocused(currentFocusNode)) {
323
+ const selectedTabId = findSelectedTabId(item);
324
+
325
+ // Set focus with back button context to tabs-menu
326
+ landFocusTo(selectedTabId);
327
+
328
+ return;
329
+ }
330
+
331
+ const selectedMenuItemId = findSelectedMenuId(focusableTree);
332
+ // Set focus with back button context to top-menu
333
+ landFocusTo(selectedMenuItemId);
278
334
  }
279
335
 
280
336
  /**
281
337
  * sets the initial focus when the screen loads, or when focus is lost
282
338
  */
283
- function setInitialFocus(lastAddedParentNode?: any) {
339
+ function setInitialFocus(
340
+ lastAddedParentNode?: any,
341
+ context?: FocusManager.FocusContext
342
+ ) {
284
343
  const preferredFocus = findPriorityItem(
285
344
  lastAddedParentNode?.children || focusableTree.root.children
286
345
  );
@@ -326,7 +385,7 @@ export const focusManager = (function () {
326
385
  },
327
386
  });
328
387
 
329
- focusableItem && setFocus(focusCandidate.id, null);
388
+ focusableItem && setFocus(focusCandidate.id, null, context);
330
389
 
331
390
  return { success: true };
332
391
  }
@@ -546,6 +605,14 @@ export const focusManager = (function () {
546
605
  return preferredFocus[0];
547
606
  }
548
607
 
608
+ function isFocusOn(id): boolean {
609
+ return (
610
+ id &&
611
+ isCurrentFocusOnTheTopScreen() &&
612
+ isCurrentFocusOn(id, currentFocusNode)
613
+ );
614
+ }
615
+
549
616
  /**
550
617
  * this is the list of the functions available externally
551
618
  * when importing the focus manager
@@ -576,5 +643,9 @@ export const focusManager = (function () {
576
643
  recoverFocus,
577
644
  isCurrentFocusOnTheTopScreen,
578
645
  findPreferredFocusChild,
646
+ focusTopNavigation,
647
+ isFocusOnContent,
648
+ isFocusOnMenu,
649
+ isFocusOn,
579
650
  };
580
651
  })();
@@ -1,14 +1,31 @@
1
- import { isNotNil } from "@applicaster/zapp-react-native-utils/reactUtils/helpers";
2
- import { find, last, pathOr, startsWith } from "ramda";
1
+ import {
2
+ isNil,
3
+ last,
4
+ startsWith,
5
+ find,
6
+ pathOr,
7
+ } from "@applicaster/zapp-react-native-utils/utils";
8
+
3
9
  import {
4
10
  QUICK_BRICK_CONTENT,
5
11
  QUICK_BRICK_NAVBAR,
6
12
  } from "@applicaster/quick-brick-core/const";
7
13
 
14
+ import {
15
+ getFocusableId,
16
+ SCREEN_PICKER_CONTAINER,
17
+ } from "@applicaster/zapp-react-native-utils/screenPickerUtils";
18
+
8
19
  // run check each 300 ms
9
20
  // run this check too often could lead to performance penalty on low-end devices
10
21
  const HOW_OFTEN_TO_CHECK_CONDITION = 300; // ms
11
22
 
23
+ const isTopMenu = (node) => startsWith(QUICK_BRICK_NAVBAR, node?.id);
24
+ const isContent = (node) => startsWith(QUICK_BRICK_CONTENT, node?.id);
25
+ const isRoot = (node) => node?.id === "root";
26
+
27
+ const isScrenPicker = (node) => startsWith(SCREEN_PICKER_CONTAINER, node?.id);
28
+
12
29
  type Props = {
13
30
  maxTimeout: number;
14
31
  conditionFn: () => boolean;
@@ -49,7 +66,7 @@ export const waitForActiveScreen = (currentRoute: string, focusableTree) => {
49
66
 
50
67
  const route = find((route) => route.id === currentRoute, routes);
51
68
 
52
- return isNotNil(route);
69
+ return !isNil(route);
53
70
  };
54
71
 
55
72
  return waitUntil({
@@ -99,3 +116,89 @@ export const waitForContent = (focusableTree) => {
99
116
  conditionFn: contentHasAnyChildren,
100
117
  });
101
118
  };
119
+
120
+ export const findSelectedTabId = (item: ZappEntry): string => {
121
+ const selectedTabId = getFocusableId(item.id);
122
+
123
+ return selectedTabId;
124
+ };
125
+
126
+ export const findSelectedMenuId = (focusableTree) => {
127
+ // Set focus with back button context
128
+ const navbar = getNavbarNode(focusableTree);
129
+
130
+ const selectedMenuItemId = find(
131
+ (child) => child.component.props.selected,
132
+ navbar.children
133
+ )?.id;
134
+
135
+ return selectedMenuItemId;
136
+ };
137
+
138
+ export const isTabsScreenContentFocused = (node) => {
139
+ if (isRoot(node)) {
140
+ return false;
141
+ }
142
+
143
+ if (isTopMenu(node)) {
144
+ return false;
145
+ }
146
+
147
+ if (isContent(node)) {
148
+ return false;
149
+ }
150
+
151
+ if (isScrenPicker(node)) {
152
+ return true;
153
+ }
154
+
155
+ return isTabsScreenContentFocused(node.parent);
156
+ };
157
+
158
+ export const isCurrentFocusOnMenu = (node) => {
159
+ if (isRoot(node)) {
160
+ return false;
161
+ }
162
+
163
+ if (isTopMenu(node)) {
164
+ return true;
165
+ }
166
+
167
+ if (isContent(node)) {
168
+ return false;
169
+ }
170
+
171
+ return isCurrentFocusOnMenu(node.parent);
172
+ };
173
+
174
+ export const isCurrentFocusOnContent = (node) => {
175
+ if (isRoot(node)) {
176
+ return false;
177
+ }
178
+
179
+ if (isTopMenu(node)) {
180
+ return false;
181
+ }
182
+
183
+ if (isContent(node)) {
184
+ return true;
185
+ }
186
+
187
+ return isCurrentFocusOnContent(node.parent);
188
+ };
189
+
190
+ export const isCurrentFocusOn = (id, node) => {
191
+ if (!node) {
192
+ return false;
193
+ }
194
+
195
+ if (isRoot(node)) {
196
+ return false;
197
+ }
198
+
199
+ if (node?.id === id) {
200
+ return true;
201
+ }
202
+
203
+ return isCurrentFocusOn(id, node.parent);
204
+ };
@@ -1,6 +1,5 @@
1
1
  import { getAllSpecificStyles } from "../manifestKeyParser";
2
2
 
3
- // Mock the dependencies
4
3
  jest.mock("@applicaster/zapp-react-native-utils/reactUtils", () => ({
5
4
  platformSelect: jest.fn((platforms) => platforms.samsung_tv), // Default to samsung for tests
6
5
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "14.0.0-alpha.9551591420",
3
+ "version": "14.0.0-alpha.9725730345",
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": "14.0.0-alpha.9551591420",
30
+ "@applicaster/applicaster-types": "14.0.0-alpha.9725730345",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",
@@ -12,7 +12,6 @@ describe("Debug utils", () => {
12
12
  // Clear the timers object
13
13
  Object.keys(timers).forEach((key) => delete timers[key]);
14
14
 
15
- // Mock performance.now()
16
15
  // eslint-disable-next-line no-undef
17
16
  performanceNowMock = jest.spyOn(performance, "now");
18
17
  performanceNowMock.mockReturnValue(0); // Initial value
@@ -1,5 +1,5 @@
1
1
  export { useRivers } from "./useRivers";
2
2
 
3
- export { useHomeRiver } from "./useHomeRiver";
3
+ export { useHomeRiver, getHomeRiver } from "./useHomeRiver";
4
4
 
5
5
  export { ZStoreProvider, useZStore } from "./ZStoreProvider";
@@ -1,8 +1,10 @@
1
- import * as R from "ramda";
2
1
  import { useRivers } from "./useRivers";
3
2
 
3
+ export const getHomeRiver = (rivers: Record<string, ZappRiver>) =>
4
+ Object.values(rivers).find((river: ZappRiver) => river.home);
5
+
4
6
  export const useHomeRiver = () => {
5
7
  const rivers = useRivers();
6
8
 
7
- return R.compose(R.find(R.propEq("home", true)), R.values)(rivers);
9
+ return getHomeRiver(rivers);
8
10
  };
@@ -0,0 +1,7 @@
1
+ export const getFocusableId = (id) => `PickerItem.${id}`;
2
+
3
+ export const getPickerSelectorId = (id) => `PickerSelector.${id}`;
4
+
5
+ export const SCREEN_PICKER_CONTAINER = "ScreenPickerContainer";
6
+
7
+ export const getScreenPickerId = (id) => `${SCREEN_PICKER_CONTAINER}.${id}`;
@@ -0,0 +1,36 @@
1
+ import { find } from "../find";
2
+
3
+ test("example 1", () => {
4
+ const predicate = <T>(_: T, index: number): boolean => index === 0;
5
+ const xs = ["1", "2", "2", "3", "4"];
6
+
7
+ expect(find(predicate, xs)).toBe("1");
8
+ });
9
+
10
+ test("example 2", () => {
11
+ const predicate = <T>(_: T, index: number): boolean => index === 0;
12
+ const xs: string[] = [];
13
+
14
+ expect(find(predicate, xs)).toBe(undefined);
15
+ });
16
+
17
+ test("example 3", () => {
18
+ const predicate = () => false;
19
+ const xs = ["1", "2", "2", "3"];
20
+
21
+ expect(find(predicate, xs)).toBe(undefined);
22
+ });
23
+
24
+ test("example 4", () => {
25
+ const predicate = <T>(_: T, index: number): boolean => index === 1;
26
+ const xs = ["1", "2", "2", "3"];
27
+
28
+ expect(find(predicate, xs)).toBe("2");
29
+ });
30
+
31
+ test("example 5", () => {
32
+ const predicate = <T>(_: T, index: number): boolean => index === 2;
33
+ const xs = ["1", "2.1", "2", "3", "2", "4"];
34
+
35
+ expect(find(predicate, xs)).toBe("2");
36
+ });
@@ -0,0 +1,37 @@
1
+ import { pathOr } from "../pathOr";
2
+
3
+ test("example 1", () => {
4
+ const defaultValue = "defaultValue";
5
+ const path = ["a", "b", "c"];
6
+ const xs = { a: { b: { c: 1 } } };
7
+
8
+ const output = 1;
9
+
10
+ expect(pathOr(defaultValue, path, xs)).toEqual(output);
11
+ });
12
+
13
+ test("example 2", () => {
14
+ const defaultValue = "defaultValue";
15
+ const path = ["a", "b"];
16
+ const xs = { a: { b: { c: 1 } } };
17
+
18
+ const output = { c: 1 };
19
+
20
+ expect(pathOr(defaultValue, path, xs)).toEqual(output);
21
+ });
22
+
23
+ test("example 3", () => {
24
+ const defaultValue = "defaultValue";
25
+ const path = ["a", "b", "x"];
26
+ const xs = { a: { b: { c: 1 } } };
27
+
28
+ expect(pathOr(defaultValue, path, xs)).toBe(defaultValue);
29
+ });
30
+
31
+ test("example 4", () => {
32
+ const defaultValue = "defaultValue";
33
+ const path = ["a", "b", "c"];
34
+ const xs = undefined;
35
+
36
+ expect(pathOr(defaultValue, path, xs)).toBe(defaultValue);
37
+ });
@@ -0,0 +1,30 @@
1
+ import { startsWith } from "../startsWith";
2
+
3
+ describe("startsWith", () => {
4
+ it("returns false when str is null", () => {
5
+ expect(startsWith("a", null)).toBe(false);
6
+ });
7
+
8
+ it("returns false when str is undefined", () => {
9
+ expect(startsWith("a", undefined)).toBe(false);
10
+ });
11
+
12
+ it("returns true when string starts with target", () => {
13
+ expect(startsWith("he", "hello")).toBe(true);
14
+ expect(startsWith("", "hello")).toBe(true); // empty target always matches
15
+ });
16
+
17
+ it("returns false when string does not start with target", () => {
18
+ expect(startsWith("yo", "hello")).toBe(false);
19
+ });
20
+
21
+ it("works with single character target", () => {
22
+ expect(startsWith("h", "hello")).toBe(true);
23
+ expect(startsWith("x", "hello")).toBe(false);
24
+ });
25
+
26
+ it("is case-sensitive", () => {
27
+ expect(startsWith("He", "hello")).toBe(false);
28
+ expect(startsWith("he", "hello")).toBe(true);
29
+ });
30
+ });
package/utils/find.ts ADDED
@@ -0,0 +1,3 @@
1
+ export const find = (predicate, xs) => {
2
+ return (xs || []).find((x, index) => predicate(x, index));
3
+ };
package/utils/index.ts CHANGED
@@ -2,6 +2,12 @@ export { chunk } from "./chunk";
2
2
 
3
3
  export { times } from "./times";
4
4
 
5
+ export { startsWith } from "./startsWith";
6
+
7
+ export { find } from "./find";
8
+
9
+ export { pathOr } from "./pathOr";
10
+
5
11
  export {
6
12
  cloneDeep as clone,
7
13
  flatten,
@@ -18,4 +24,5 @@ export {
18
24
  map,
19
25
  trim,
20
26
  toString,
27
+ last,
21
28
  } from "lodash";
@@ -0,0 +1,5 @@
1
+ import { get } from "lodash";
2
+
3
+ export const pathOr = (defaultValue, path, record) => {
4
+ return get(record, path, defaultValue);
5
+ };
@@ -0,0 +1,9 @@
1
+ import { isNil } from "lodash";
2
+
3
+ export const startsWith = (target, str) => {
4
+ if (isNil(str)) {
5
+ return false;
6
+ }
7
+
8
+ return str.startsWith(target);
9
+ };