@applicaster/zapp-react-native-utils 12.0.0-alpha.5114122134 → 12.0.0-alpha.5208030571

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.
Files changed (65) hide show
  1. package/actionsExecutor/ActionExecutorContext.tsx +16 -8
  2. package/analyticsUtils/__tests__/analyticsManager.test.js +75 -73
  3. package/analyticsUtils/helpers/hooks.ts +1 -1
  4. package/appUtils/HooksManager/index.js +3 -1
  5. package/appUtils/focusManager/index.ts +124 -25
  6. package/appUtils/focusManager/treeDataStructure/Utils/index.js +97 -33
  7. package/appUtils/focusManager/utils/index.ts +65 -0
  8. package/appUtils/keyCodes/__tests__/__snapshots__/keyCodes.test.js.snap +1 -1
  9. package/appUtils/keyCodes/__tests__/keyCodes.test.js +80 -85
  10. package/appUtils/keyCodes/keys.web.js +1 -0
  11. package/appUtils/playerManager/OverlayObserver/OverlaysObserver.ts +53 -0
  12. package/appUtils/playerManager/conts.ts +5 -0
  13. package/appUtils/playerManager/player.ts +47 -4
  14. package/appUtils/playerManager/playerFactory.ts +15 -1
  15. package/appUtils/playerManager/playerNative.ts +35 -0
  16. package/appUtils/playerManager/usePlayerState.tsx +13 -1
  17. package/appUtils/playerManager/usePlayerTitleSummaryOverlay.tsx +50 -0
  18. package/cellUtils/__tests__/cellUtils.test.ts +59 -0
  19. package/cellUtils/__tests__/fixtures.ts +13 -0
  20. package/cellUtils/index.ts +46 -2
  21. package/focusManager/index.ts +1 -1
  22. package/functionUtils/__tests__/__snapshots__/functionUtils.test.js.snap +2 -2
  23. package/functionUtils/__tests__/functionUtils.test.js +244 -248
  24. package/localizationUtils/__tests__/localizationUtils.test.ts +17 -13
  25. package/localizationUtils/index.ts +6 -3
  26. package/logger/index.ts +1 -1
  27. package/manifestUtils/defaultManifestConfigurations/generalContent.js +6 -0
  28. package/manifestUtils/defaultManifestConfigurations/player.js +10 -1
  29. package/manifestUtils/index.js +5 -1
  30. package/manifestUtils/secondaryImage/index.js +18 -15
  31. package/modalState/index.ts +57 -0
  32. package/navigationUtils/index.ts +56 -7
  33. package/package.json +3 -2
  34. package/playerUtils/configurationGenerator.ts +11 -1
  35. package/pluginUtils/index.ts +11 -7
  36. package/reactHooks/autoscrolling/__tests__/useTrackCurrentAutoScrollingElement.test.ts +2 -0
  37. package/reactHooks/autoscrolling/__tests__/{useTrackedView.test.ts → useTrackedView.test.tsx} +18 -1
  38. package/reactHooks/cell-click/__tests__/index.test.js +6 -2
  39. package/reactHooks/cell-click/index.ts +2 -2
  40. package/reactHooks/flatList/useLoadNextPageIfNeeded.ts +16 -9
  41. package/reactHooks/index.ts +2 -0
  42. package/reactHooks/layout/__tests__/{index.test.js → index.test.tsx} +66 -59
  43. package/reactHooks/layout/getDeviceInfo/index.ts +4 -2
  44. package/reactHooks/layout/types/index.ts +2 -0
  45. package/reactHooks/layout/useDimensions/helpers/__tests__/getInitialDimensions.native.test.js +11 -16
  46. package/reactHooks/layout/useDimensions/helpers/__tests__/getInitialDimensions.test.js +12 -14
  47. package/reactHooks/layout/useDimensions/useDimensions.ts +13 -7
  48. package/reactHooks/navigation/__mocks__/index.ts +15 -0
  49. package/reactHooks/navigation/__mocks__/useNavigation.ts +7 -0
  50. package/reactHooks/navigation/__tests__/index.test.tsx +234 -78
  51. package/reactHooks/navigation/index.ts +262 -81
  52. package/reactHooks/resolvers/__tests__/useCellResolver.test.tsx +3 -1
  53. package/reactHooks/screen/__mocks__/useScreenContext.ts +3 -1
  54. package/reactHooks/screen/index.ts +2 -0
  55. package/reactHooks/screen/useScreenContext.ts +5 -4
  56. package/reactHooks/screen/useScreenData.ts +1 -1
  57. package/reactHooks/screen/useTargetScreenData.ts +7 -0
  58. package/reactHooks/state/useRivers.ts +3 -1
  59. package/reactHooks/useListenEventBusEvent.ts +1 -4
  60. package/reactHooks/videoModal/hooks/useVideoModalScreenData.tsx +10 -9
  61. package/reactUtils/helpers.tsx +1 -1
  62. package/riverComponetsMeasurementProvider/index.tsx +2 -2
  63. package/stringUtils/index.ts +5 -0
  64. package/testUtils/getByTestId.ts +14 -0
  65. package/testUtils/index.tsx +38 -23
@@ -11,11 +11,9 @@ import * as QuickBrickManager from "@applicaster/zapp-react-native-bridge/QuickB
11
11
  import { showConfirmationDialog } from "../alertUtils";
12
12
  import { createCloudEvent, sendCloudEvent } from "../cloudEventsUtils";
13
13
  import { createLogger } from "../logger";
14
- import {
15
- StorageMultiSelectProvider
16
- } from "@applicaster/zapp-react-native-bridge/ZappStorage/StorageMultiSelectProvider";
14
+ import { StorageMultiSelectProvider } from "@applicaster/zapp-react-native-bridge/ZappStorage/StorageMultiSelectProvider";
17
15
 
18
- const { log_error, log_debug, log_info } = createLogger({
16
+ const { log_error, log_info } = createLogger({
19
17
  subsystem: "ActionExecutorContext",
20
18
  category: "General",
21
19
  });
@@ -110,11 +108,17 @@ const prepareDefaultActions = (actionExecutor) => {
110
108
  const tag = entry?.extensions?.tag;
111
109
 
112
110
  if (keyNamespace && tag) {
113
- const multiSelectProvider = StorageMultiSelectProvider.getProvider(keyNamespace);
111
+ const multiSelectProvider =
112
+ StorageMultiSelectProvider.getProvider(keyNamespace);
114
113
 
115
114
  const selectedItems = await multiSelectProvider.getSelectedAsync();
116
115
  const isTagInSelectedItems = selectedItems.includes(tag);
117
- log_info(`handleAction: localStorageToggleFlag event will ${isTagInSelectedItems ? "remove" : "add"} tag: ${tag} for keyNamespace: ${keyNamespace}, current selectedItems: ${selectedItems}`);
116
+
117
+ log_info(
118
+ `handleAction: localStorageToggleFlag event will ${
119
+ isTagInSelectedItems ? "remove" : "add"
120
+ } tag: ${tag} for keyNamespace: ${keyNamespace}, current selectedItems: ${selectedItems}`
121
+ );
118
122
 
119
123
  if (selectedItems.includes(tag)) {
120
124
  await multiSelectProvider.removeItem(tag);
@@ -122,13 +126,17 @@ const prepareDefaultActions = (actionExecutor) => {
122
126
  await multiSelectProvider.addItem(tag);
123
127
  }
124
128
  } else {
125
- log_error("handleAction: localStorageToggleFlag event missing keyNamespace or tag", { keyNamespace, tag });
129
+ log_error(
130
+ "handleAction: localStorageToggleFlag event missing keyNamespace or tag",
131
+ { keyNamespace, tag }
132
+ );
126
133
 
127
134
  return ActionResult.Error;
128
135
  }
129
136
 
130
137
  return ActionResult.Success;
131
- });
138
+ }
139
+ );
132
140
  };
133
141
 
134
142
  export const ActionExecutorContext =
@@ -1,18 +1,18 @@
1
+ import * as ReactNative from "react-native";
2
+
1
3
  const mock_postEvent = jest.fn();
2
4
  const mock_setPlugins = jest.fn();
3
5
  const mock_postTimedEvent = jest.fn();
4
6
  const mock_endTimedEvent = jest.fn();
5
7
 
6
- jest.mock("react-native", () => ({
7
- NativeModules: {
8
- AnalyticsBridge: {
9
- postEvent: mock_postEvent,
10
- setPlugins: mock_setPlugins,
11
- postTimedEvent: mock_postTimedEvent,
12
- endTimedEvent: mock_endTimedEvent,
13
- },
14
- },
15
- }));
8
+ const AnalyticsBridge = {
9
+ postEvent: mock_postEvent,
10
+ setPlugins: mock_setPlugins,
11
+ postTimedEvent: mock_postTimedEvent,
12
+ endTimedEvent: mock_endTimedEvent,
13
+ };
14
+
15
+ ReactNative.NativeModules.AnalyticsBridge = AnalyticsBridge;
16
16
 
17
17
  const eventName = "event";
18
18
  const payload = {};
@@ -26,90 +26,92 @@ function clearAllMocks() {
26
26
  mock_endTimedEvent.mockClear();
27
27
  }
28
28
 
29
- describe("isValidAnalyticsData", () => {
30
- beforeEach(clearAllMocks);
29
+ describe("AnalyticsManager", () => {
30
+ describe("isValidAnalyticsData", () => {
31
+ beforeEach(clearAllMocks);
31
32
 
32
- it("returns false if eventName is not a string", () => {
33
- expect(analyticsManager.isValidAnalyticsData(42)).toBe(false);
34
- expect(analyticsManager.isValidAnalyticsData({})).toBe(false);
35
- expect(analyticsManager.isValidAnalyticsData([])).toBe(false);
36
- expect(analyticsManager.isValidAnalyticsData(() => ({}))).toBe(false);
37
- });
33
+ it("returns false if eventName is not a string", () => {
34
+ expect(analyticsManager.isValidAnalyticsData(42)).toBe(false);
35
+ expect(analyticsManager.isValidAnalyticsData({})).toBe(false);
36
+ expect(analyticsManager.isValidAnalyticsData([])).toBe(false);
37
+ expect(analyticsManager.isValidAnalyticsData(() => ({}))).toBe(false);
38
+ });
38
39
 
39
- it("returns false if payload is not an object", () => {
40
- expect(analyticsManager.isValidAnalyticsData(eventName, "string")).toBe(
41
- false
42
- );
40
+ it("returns false if payload is not an object", () => {
41
+ expect(analyticsManager.isValidAnalyticsData(eventName, "string")).toBe(
42
+ false
43
+ );
43
44
 
44
- expect(analyticsManager.isValidAnalyticsData(eventName, 42)).toBe(false);
45
+ expect(analyticsManager.isValidAnalyticsData(eventName, 42)).toBe(false);
45
46
 
46
- expect(analyticsManager.isValidAnalyticsData(eventName, () => ({}))).toBe(
47
- false
48
- );
49
- });
47
+ expect(analyticsManager.isValidAnalyticsData(eventName, () => ({}))).toBe(
48
+ false
49
+ );
50
+ });
50
51
 
51
- it("returns true if eventName is a string and payload is null or an object", () => {
52
- expect(analyticsManager.isValidAnalyticsData(eventName)).toBe(true);
52
+ it("returns true if eventName is a string and payload is null or an object", () => {
53
+ expect(analyticsManager.isValidAnalyticsData(eventName)).toBe(true);
53
54
 
54
- expect(analyticsManager.isValidAnalyticsData(eventName, payload)).toBe(
55
- true
56
- );
55
+ expect(analyticsManager.isValidAnalyticsData(eventName, payload)).toBe(
56
+ true
57
+ );
58
+ });
57
59
  });
58
- });
59
60
 
60
- describe("postAnalyticEvent", () => {
61
- beforeEach(clearAllMocks);
61
+ describe("postAnalyticEvent", () => {
62
+ beforeEach(clearAllMocks);
62
63
 
63
- it("calls the postEvent method of the analytic bridge", () => {
64
- analyticsManager.postAnalyticEvent(eventName, payload);
65
- expect(mock_postEvent).toHaveBeenCalledWith(eventName, payload);
66
- });
64
+ it("calls the postEvent method of the analytic bridge", () => {
65
+ analyticsManager.postAnalyticEvent(eventName, payload);
66
+ expect(mock_postEvent).toHaveBeenCalledWith(eventName, payload);
67
+ });
67
68
 
68
- it("skips the event if it is not valid", () => {
69
- analyticsManager.postAnalyticEvent(42);
70
- expect(mock_postTimedEvent).not.toHaveBeenCalled();
69
+ it("skips the event if it is not valid", () => {
70
+ analyticsManager.postAnalyticEvent(42);
71
+ expect(mock_postTimedEvent).not.toHaveBeenCalled();
72
+ });
71
73
  });
72
- });
73
74
 
74
- describe("providePlugins", () => {
75
- beforeEach(clearAllMocks);
75
+ describe("providePlugins", () => {
76
+ beforeEach(clearAllMocks);
76
77
 
77
- it("calls the setPlugins method of the analytic bridge", () => {
78
- const plugins = [];
79
- const plugin_configurations = {};
80
- const callback = jest.fn();
78
+ it("calls the setPlugins method of the analytic bridge", () => {
79
+ const plugins = [];
80
+ const plugin_configurations = {};
81
+ const callback = jest.fn();
81
82
 
82
- analyticsManager.providePlugins(plugins, plugin_configurations, callback);
83
+ analyticsManager.providePlugins(plugins, plugin_configurations, callback);
83
84
 
84
- expect(mock_setPlugins).toHaveBeenCalledWith(
85
- plugins,
86
- plugin_configurations,
87
- callback
88
- );
85
+ expect(mock_setPlugins).toHaveBeenCalledWith(
86
+ plugins,
87
+ plugin_configurations,
88
+ callback
89
+ );
90
+ });
89
91
  });
90
- });
91
92
 
92
- describe("startAnalyticsTimedEvent", () => {
93
- beforeEach(clearAllMocks);
93
+ describe("startAnalyticsTimedEvent", () => {
94
+ beforeEach(clearAllMocks);
94
95
 
95
- it("skips the event if it is not valid", () => {
96
- analyticsManager.startAnalyticsTimedEvent(42);
97
- expect(mock_postTimedEvent).not.toHaveBeenCalled();
96
+ it("skips the event if it is not valid", () => {
97
+ analyticsManager.startAnalyticsTimedEvent(42);
98
+ expect(mock_postTimedEvent).not.toHaveBeenCalled();
99
+ });
98
100
  });
99
- });
100
101
 
101
- describe("endAnalyticsTimedEvent", () => {
102
- beforeEach(clearAllMocks);
102
+ describe("endAnalyticsTimedEvent", () => {
103
+ beforeEach(clearAllMocks);
103
104
 
104
- it("emits a time_on_screen event and adds duration to payload", () => {
105
- analyticsManager.endAnalyticsTimedEvent(eventName, payload);
106
- const newEventName = "time_on_screen";
107
- payload.duration = "N/A";
108
- expect(mock_postEvent).toHaveBeenCalledWith(newEventName, payload);
109
- });
105
+ it("emits a time_on_screen event and adds duration to payload", () => {
106
+ analyticsManager.endAnalyticsTimedEvent(eventName, payload);
107
+ const newEventName = "time_on_screen";
108
+ payload.duration = "N/A";
109
+ expect(mock_postEvent).toHaveBeenCalledWith(newEventName, payload);
110
+ });
110
111
 
111
- it("skips the event if it is not valid", () => {
112
- analyticsManager.endAnalyticsTimedEvent(42);
113
- expect(mock_postEvent).not.toHaveBeenCalled();
112
+ it("skips the event if it is not valid", () => {
113
+ analyticsManager.endAnalyticsTimedEvent(42);
114
+ expect(mock_postEvent).not.toHaveBeenCalled();
115
+ });
114
116
  });
115
117
  });
@@ -26,7 +26,7 @@ export const useHookAnalytics = (props): void => {
26
26
  if (screenData?.hookPlugin?.module?.runInBackground) {
27
27
  sendScreenEvent({ analyticsScreenData });
28
28
  }
29
- }, [hookPlugin.screen_id]);
29
+ }, [sendScreenEvent, screenData, hookPlugin.screen_id]);
30
30
  };
31
31
 
32
32
  export const useScreenAnalytics = (props) => {
@@ -270,7 +270,9 @@ export function HooksManager({
270
270
  });
271
271
 
272
272
  const isHookInHomescreen = payload?.home;
273
- const isHookFlowBlocker = hookPlugin?.module?.isFlowBlocker();
273
+
274
+ const isHookFlowBlocker =
275
+ hookPlugin?.module?.isFlowBlocker?.() ?? false;
274
276
 
275
277
  // TODO: We technically need to also support the case where there is more than one hook in the stack,
276
278
  // if needed we could add differentiation explicit user cancellation (when we should go to background) and other types of cancellation
@@ -1,9 +1,20 @@
1
1
  import * as R from "ramda";
2
+ import {
3
+ isNilOrEmpty,
4
+ isNotNil,
5
+ } from "@applicaster/zapp-react-native-utils/reactUtils/helpers";
6
+
2
7
  import { Tree } from "./treeDataStructure/Tree";
3
8
  import { findFocusableNode, findPriorityItem } from "./treeDataStructure/Utils";
4
9
  import { subscriber } from "../../functionUtils";
5
10
  import * as FOCUS_EVENTS from "./events";
6
11
  import { coreLogger } from "../../logger";
12
+ import {
13
+ waitForActiveScreen,
14
+ waitForContent,
15
+ getContentNode,
16
+ getActiveScreen,
17
+ } from "./utils";
7
18
 
8
19
  const logger = coreLogger.addSubsystem("focusManager");
9
20
 
@@ -46,7 +57,7 @@ export const focusManager = (function () {
46
57
  let lastFocusedId = null;
47
58
  let initialFocusOnContent = null;
48
59
  let focusDisabled = false;
49
- let focusAutoEnabled = true;
60
+ let focusAutoEnabled = false;
50
61
  const eventHandler = subscriber();
51
62
 
52
63
  function disableAutomaticFocus() {
@@ -93,7 +104,7 @@ export const focusManager = (function () {
93
104
  !currentFocusable.isGroup &&
94
105
  currentFocusable.isMounted()
95
106
  ) {
96
- currentFocusable.blur(currentFocusable, direction);
107
+ currentFocusable.blur(direction);
97
108
  currentFocusable.hasLostFocus(getCurrentFocus(), direction);
98
109
  }
99
110
  }
@@ -219,38 +230,55 @@ export const focusManager = (function () {
219
230
  /**
220
231
  * sets the initial focus when the screen loads, or when focus is lost
221
232
  */
222
- function setInitialFocus(lastAddedParentNode?: any) {
233
+ function setInitialFocus(lastAddedParentNode?: any, fallback?: () => void) {
223
234
  const preferredFocus = findPriorityItem(
224
235
  lastAddedParentNode?.children || focusableTree.root.children
225
236
  );
226
237
 
227
238
  logger.log({ message: "setInitialFocus", data: { preferredFocus } });
228
239
 
229
- if (preferredFocus && preferredFocus.length > 0) {
230
- const focusCandidate = preferredFocus[0];
231
- const focusableItem = focusCandidate.component;
232
-
233
- if (
234
- focusableItem &&
235
- !focusableItem.isGroup &&
236
- isFocusEnabled(focusableItem)
237
- ) {
238
- if (initialFocusOnContent) {
239
- blur();
240
- initialFocusOnContent = false;
241
- }
240
+ if (isNilOrEmpty(preferredFocus)) {
241
+ fallback?.();
242
+ return;
243
+ }
242
244
 
243
- logger.log({
244
- message: "setInitialFocus – currentFocusNode",
245
- data: {
246
- focusableItem,
247
- },
248
- });
245
+ const focusCandidate = preferredFocus[0];
246
+ const focusableItem = focusCandidate.component;
249
247
 
250
- currentFocusNode = focusCandidate;
251
- focusableItem.willReceiveFocus(focusableItem, null);
252
- focusableItem && setFocus(focusCandidate.id, null);
248
+ const currentFocusedId = R.path(["props", "id"], getCurrentFocus());
249
+ const nextFocusedId = R.path(["props", "id"], focusableItem);
250
+
251
+ if (currentFocusedId === nextFocusedId) {
252
+ // we are going to move focus to the same node(already focused) -> nothing to do
253
+ return;
254
+ }
255
+
256
+ if (preferredFocus.length === 1 && focusableItem && focusableItem.isGroup) {
257
+ // we've found only one FocusableGroup as a candidate to focus, try to use fallback
258
+ fallback?.();
259
+ return;
260
+ }
261
+
262
+ if (
263
+ focusableItem &&
264
+ !focusableItem.isGroup &&
265
+ isFocusEnabled(focusableItem)
266
+ ) {
267
+ if (initialFocusOnContent) {
268
+ blur();
269
+ initialFocusOnContent = false;
253
270
  }
271
+
272
+ logger.log({
273
+ message: "setInitialFocus – currentFocusNode",
274
+ data: {
275
+ focusableItem,
276
+ },
277
+ });
278
+
279
+ currentFocusNode = focusCandidate;
280
+ focusableItem.willReceiveFocus(focusableItem, null);
281
+ focusableItem && setFocus(focusCandidate.id, null);
254
282
  }
255
283
  }
256
284
 
@@ -323,6 +351,8 @@ export const focusManager = (function () {
323
351
  * @returns {React.Component} the registered component
324
352
  */
325
353
  function registerGroup(id, component) {
354
+ // console.log("debug_2", "FM registerGroup", { id, component });
355
+
326
356
  focusableTree.addNode({ id, component });
327
357
 
328
358
  return component;
@@ -338,6 +368,8 @@ export const focusManager = (function () {
338
368
  * @returns {React.Component} the registered component
339
369
  */
340
370
  function registerItem(id, component) {
371
+ // console.log("debug_2", "FM registerItem", { id, component });
372
+
341
373
  focusableTree.addNode({ id, component });
342
374
 
343
375
  if (!currentFocusNode && id === lastFocusedId && !id.includes("menu")) {
@@ -354,6 +386,8 @@ export const focusManager = (function () {
354
386
  * @param {String} id of the group to unregister
355
387
  */
356
388
  function unregisterGroup(id) {
389
+ // console.log("debug_2", "FM unregisterGroup", { id });
390
+
357
391
  focusableTree.findAndRemoveNode(id);
358
392
  }
359
393
 
@@ -365,6 +399,8 @@ export const focusManager = (function () {
365
399
  * @param {String} id of the Focusable to unregister
366
400
  */
367
401
  function unregisterItem(id) {
402
+ // console.log("debug_2", "FM unregisterItem", { id });
403
+
368
404
  focusableTree.findAndRemoveNode(id);
369
405
 
370
406
  if (currentFocusNode && currentFocusNode.id === id) {
@@ -403,6 +439,8 @@ export const focusManager = (function () {
403
439
  }
404
440
 
405
441
  function setupInitialFocus(id: string, focusOnContent?: boolean) {
442
+ console.log("debug_2", "FM - setupInitialFocus", { id, focusOnContent });
443
+
406
444
  if (focusOnContent) {
407
445
  initialFocusOnContent = focusOnContent;
408
446
  } else {
@@ -410,6 +448,58 @@ export const focusManager = (function () {
410
448
  }
411
449
  }
412
450
 
451
+ async function setFocusOnMenu(currentRoute: string) {
452
+ try {
453
+ await waitForActiveScreen(currentRoute, focusableTree);
454
+
455
+ const navbar = R.find(
456
+ (child) => R.startsWith("navbar", child.id),
457
+ getActiveScreen(focusableTree).children
458
+ );
459
+
460
+ const selectedMenuItemId = R.find(
461
+ (child) => child.component.props.selected,
462
+ navbar?.children || []
463
+ )?.id;
464
+
465
+ if (selectedMenuItemId) {
466
+ // set focus on selected menu item
467
+ setFocus(selectedMenuItemId);
468
+ return;
469
+ }
470
+
471
+ const firstMenuItemId = R.path(["children", 0, "id"], navbar);
472
+
473
+ if (firstMenuItemId) {
474
+ // set focus on first menu item
475
+ setFocus(firstMenuItemId);
476
+ }
477
+ } catch (error) {
478
+ // failed to put focus on top-menu => log it
479
+ }
480
+ }
481
+
482
+ async function setFocusOnContent(currentRoute: string) {
483
+ // we need to put focus on content here
484
+ // For doing it we have to wait registration of first focusable node into focusableTree
485
+ // this is the temporary hack with waiting until content node starts having children
486
+ try {
487
+ await waitForActiveScreen(currentRoute, focusableTree);
488
+ await waitForContent(focusableTree);
489
+
490
+ const content = getContentNode(focusableTree);
491
+
492
+ const fallback = () => {
493
+ throw Error("Failed to land focus on content");
494
+ };
495
+
496
+ setInitialFocus(content, fallback);
497
+ } catch (e) {
498
+ // we've failed to put initial focus on content, put focus on menu as a last chance
499
+ setFocusOnMenu(currentRoute);
500
+ }
501
+ }
502
+
413
503
  /**
414
504
  * moves the focus in a provided direction
415
505
  * @public
@@ -422,6 +512,13 @@ export const focusManager = (function () {
422
512
  currentFocusNode.component.willLoseFocus(currentFocusNode, direction);
423
513
  const nextFocusable = findFocusableNode(direction, currentFocusNode);
424
514
 
515
+ // console.log("debug_2", "FM moveFocus", {
516
+ // direction: direction?.value,
517
+ // currentFocusNode,
518
+ // focusableTree,
519
+ // nextFocusable,
520
+ // });
521
+
425
522
  if (nextFocusable?.component?.isMounted?.()) {
426
523
  const nextRect = nextFocusable.component.getRect();
427
524
 
@@ -518,5 +615,7 @@ export const focusManager = (function () {
518
615
  enableAutomaticFocus,
519
616
  getGroupRootById,
520
617
  isGroupItemFocused,
618
+ setFocusOnMenu,
619
+ setFocusOnContent,
521
620
  };
522
621
  })();
@@ -1,4 +1,4 @@
1
- import * as R from "ramda";
1
+ import { allPass, pathOr, startsWith, isNil } from "ramda";
2
2
  import { isNilOrEmpty } from "@applicaster/zapp-react-native-utils/reactUtils/helpers";
3
3
 
4
4
  import { rejectItemByPath } from "../../../../objectUtils";
@@ -143,7 +143,49 @@ const hasChildren = (target) =>
143
143
 
144
144
  const hasComponent = (target) => !!target?.component;
145
145
 
146
- const hasComponentAndChildren = R.allPass([hasComponent, hasChildren]);
146
+ const hasComponentAndChildren = allPass([hasComponent, hasChildren]);
147
+
148
+ const positiveOrZero = (value) => value >= 0;
149
+
150
+ const excludeFromFocusSearching = pathOr(false, [
151
+ "component",
152
+ "props",
153
+ "excludeFromFocusSearching",
154
+ ]);
155
+
156
+ // const isAllScreens = all(excludeFromFocusSearching);
157
+
158
+ const isNavBar = (target) => startsWith("navbar", target?.id);
159
+
160
+ const ignoreMenuIfMultipleTargets = (nodes) => {
161
+ if (nodes.length > 1 && nodes.map((n) => n?.target?.id).includes("navbar")) {
162
+ return nodes.filter((n) => !isNavBar(n?.target));
163
+ }
164
+
165
+ return nodes;
166
+ };
167
+
168
+ const toDistance = ({ target, direction, comparisonRect }) => {
169
+ const componentRect = target.component?.getRect();
170
+
171
+ const orientationDistance = measureDistance(
172
+ direction,
173
+ comparisonRect,
174
+ componentRect
175
+ );
176
+
177
+ if (orientationDistance === -1) {
178
+ // ignores offscreen targets when moving up/down
179
+ return null;
180
+ }
181
+
182
+ const euclidDistance = measureEuclidianDistanceToCenter(
183
+ comparisonRect,
184
+ componentRect
185
+ );
186
+
187
+ return orientationDistance > 0 ? euclidDistance : orientationDistance;
188
+ };
147
189
 
148
190
  /**
149
191
  * Get target in shortest distance in specified direction
@@ -173,43 +215,32 @@ export function getTargetInDirection(
173
215
 
174
216
  const distances = targets
175
217
  .filter(hasComponentAndChildren)
176
- .map((target) => {
177
- const componentRect = target.component?.getRect();
178
-
179
- const distance = measureDistance(
180
- direction,
181
- comparisonRect,
182
- componentRect
183
- );
184
-
185
- if (distance === -1) {
186
- // ignores offscreen targets when moving up/down
187
- return null;
188
- }
218
+ .filter((target) => !excludeFromFocusSearching(target))
219
+ .map((target) => ({
220
+ distance: toDistance({ target, direction, comparisonRect }),
221
+ target,
222
+ }))
223
+ .filter(({ distance }) => positiveOrZero(distance));
189
224
 
190
- return {
191
- distance:
192
- measureDistance(direction, comparisonRect, componentRect) +
193
- measureEuclidianDistanceToCenter(comparisonRect, componentRect) /
194
- 10000,
195
- target,
196
- };
197
- })
198
- .filter((obj) => obj && obj.distance >= 0);
225
+ // if (isAllScreens(targets)) {
226
+ // return last(targets);
227
+ // }
199
228
 
200
229
  const match = distances
201
230
  .sort((a, b) => a.distance - b.distance) // sorts the distances
202
231
  .filter(ignoreMenuIfMultipleTargets)[0]; // filters out multiple targets if necessary // gets the first element
203
232
 
204
- return distances && distances.length > 0 ? match.target : null;
205
- }
206
-
207
- function ignoreMenuIfMultipleTargets(nodes) {
208
- if (nodes.length > 1 && nodes.map((n) => n?.target?.id).includes("navbar")) {
209
- return nodes.filter((n) => n?.target?.id !== "navbar");
210
- }
211
-
212
- return nodes;
233
+ // console.log("debug_2", "FM getTargetInDirection", {
234
+ // direction: direction?.value,
235
+ // currentNode,
236
+ // targetNodes,
237
+ // targets,
238
+ // comparisonRect,
239
+ // distances,
240
+ // match,
241
+ // });
242
+
243
+ return match?.target;
213
244
  }
214
245
 
215
246
  /**
@@ -300,3 +331,36 @@ export function hasPriorityInDirection(direction, component) {
300
331
 
301
332
  return (prioritiseFocusOn & mappedDirection) > 0;
302
333
  }
334
+
335
+ export const getNodeBeforeRoot = (node) => {
336
+ if (isNil(node)) {
337
+ return undefined;
338
+ }
339
+
340
+ if (node.parent.id === "root") {
341
+ return node;
342
+ }
343
+
344
+ return getNodeBeforeRoot(node.parent);
345
+ };
346
+
347
+ export const isAllowedToMoveTo = (currentNode, nextNode) => {
348
+ const screenForCurrentNode = getNodeBeforeRoot(currentNode);
349
+ const screenForNextNode = getNodeBeforeRoot(nextNode);
350
+
351
+ return screenForCurrentNode.id === screenForNextNode.id;
352
+
353
+ // console.log("debug_2", "isAllowedToMoveTo", {
354
+ // nodeBeforeRoot,
355
+ // });
356
+
357
+ // if (nodeBeforeRoot?.id === "navbar") {
358
+ // return true;
359
+ // }
360
+
361
+ // if (nodeBeforeRoot?.component?.props?.isActive) {
362
+ // return true;
363
+ // }
364
+
365
+ // return false;
366
+ };