@applicaster/zapp-react-native-utils 14.0.0-alpha.1101330035 → 14.0.0-alpha.1152359078

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 (96) hide show
  1. package/actionsExecutor/ActionExecutorContext.tsx +0 -1
  2. package/actionsExecutor/ScreenActions.ts +20 -19
  3. package/analyticsUtils/AnalyticPlayerListener.ts +5 -2
  4. package/analyticsUtils/AnalyticsEvents/sendHeaderClickEvent.ts +1 -1
  5. package/analyticsUtils/AnalyticsEvents/sendMenuClickEvent.ts +2 -1
  6. package/analyticsUtils/__tests__/analyticsUtils.test.js +0 -11
  7. package/analyticsUtils/index.tsx +3 -4
  8. package/analyticsUtils/manager.ts +1 -1
  9. package/analyticsUtils/playerAnalyticsTracker.ts +2 -1
  10. package/appUtils/HooksManager/Hook.ts +4 -4
  11. package/appUtils/HooksManager/index.ts +11 -1
  12. package/appUtils/accessibilityManager/const.ts +13 -0
  13. package/appUtils/accessibilityManager/hooks.ts +35 -1
  14. package/appUtils/accessibilityManager/index.ts +150 -29
  15. package/appUtils/accessibilityManager/utils.ts +24 -0
  16. package/appUtils/contextKeysManager/contextResolver.ts +29 -1
  17. package/appUtils/focusManager/__tests__/__snapshots__/focusManager.test.js.snap +8 -0
  18. package/appUtils/focusManager/__tests__/focusManager.test.js +1 -1
  19. package/appUtils/focusManager/events.ts +2 -0
  20. package/appUtils/focusManager/index.ios.ts +27 -0
  21. package/appUtils/focusManager/index.ts +86 -11
  22. package/appUtils/focusManager/treeDataStructure/Tree/index.js +1 -1
  23. package/appUtils/focusManagerAux/utils/index.ts +112 -3
  24. package/appUtils/focusManagerAux/utils/utils.ios.ts +35 -0
  25. package/appUtils/platform/platformUtils.ts +33 -3
  26. package/appUtils/playerManager/OverlayObserver/OverlaysObserver.ts +94 -4
  27. package/appUtils/playerManager/OverlayObserver/utils.ts +32 -20
  28. package/appUtils/playerManager/conts.ts +21 -0
  29. package/arrayUtils/__tests__/allTruthy.test.ts +24 -0
  30. package/arrayUtils/__tests__/anyThruthy.test.ts +24 -0
  31. package/arrayUtils/index.ts +6 -1
  32. package/componentsUtils/__tests__/isTabsScreen.test.ts +38 -0
  33. package/componentsUtils/index.ts +4 -1
  34. package/configurationUtils/__tests__/manifestKeyParser.test.ts +0 -1
  35. package/configurationUtils/index.ts +1 -1
  36. package/focusManager/FocusManager.ts +78 -4
  37. package/focusManager/aux/index.ts +98 -0
  38. package/focusManager/utils.ts +12 -6
  39. package/index.d.ts +1 -10
  40. package/manifestUtils/defaultManifestConfigurations/player.js +188 -2
  41. package/manifestUtils/index.js +4 -0
  42. package/manifestUtils/keys.js +33 -0
  43. package/manifestUtils/sharedConfiguration/screenPicker/stylesFields.js +6 -0
  44. package/manifestUtils/sharedConfiguration/screenPicker/utils.js +1 -0
  45. package/navigationUtils/__tests__/mapContentTypesToRivers.test.ts +130 -0
  46. package/navigationUtils/index.ts +26 -21
  47. package/package.json +2 -3
  48. package/playerUtils/PlayerTTS/PlayerTTS.ts +359 -0
  49. package/playerUtils/PlayerTTS/index.ts +1 -0
  50. package/playerUtils/getPlayerActionButtons.ts +1 -1
  51. package/playerUtils/usePlayerTTS.ts +21 -0
  52. package/reactHooks/autoscrolling/__tests__/useTrackedView.test.tsx +15 -14
  53. package/reactHooks/cell-click/__tests__/index.test.js +3 -0
  54. package/reactHooks/debugging/__tests__/index.test.js +0 -1
  55. package/reactHooks/feed/__tests__/useBatchLoading.test.tsx +47 -90
  56. package/reactHooks/feed/__tests__/useFeedLoader.test.tsx +57 -37
  57. package/reactHooks/feed/index.ts +2 -0
  58. package/reactHooks/feed/useBatchLoading.ts +15 -8
  59. package/reactHooks/feed/useFeedLoader.tsx +39 -53
  60. package/reactHooks/feed/useInflatedUrl.ts +23 -29
  61. package/reactHooks/feed/useLoadPipesDataDispatch.ts +63 -0
  62. package/reactHooks/feed/usePipesCacheReset.ts +2 -2
  63. package/reactHooks/flatList/useSequentialRenderItem.tsx +3 -3
  64. package/reactHooks/layout/__tests__/index.test.tsx +3 -1
  65. package/reactHooks/layout/index.ts +1 -1
  66. package/reactHooks/layout/useDimensions/__tests__/useDimensions.test.ts +34 -36
  67. package/reactHooks/layout/useDimensions/useDimensions.ts +2 -3
  68. package/reactHooks/layout/useLayoutVersion.ts +5 -5
  69. package/reactHooks/navigation/index.ts +5 -7
  70. package/reactHooks/navigation/useScreenStateStore.ts +3 -3
  71. package/reactHooks/resolvers/__tests__/useCellResolver.test.tsx +4 -0
  72. package/reactHooks/state/index.ts +1 -1
  73. package/reactHooks/state/useHomeRiver.ts +4 -2
  74. package/reactHooks/state/useRivers.ts +7 -8
  75. package/screenPickerUtils/index.ts +13 -0
  76. package/storage/ScreenSingleValueProvider.ts +25 -22
  77. package/storage/ScreenStateMultiSelectProvider.ts +26 -23
  78. package/testUtils/index.tsx +7 -8
  79. package/time/BackgroundTimer.ts +1 -1
  80. package/utils/__tests__/endsWith.test.ts +30 -0
  81. package/utils/__tests__/find.test.ts +36 -0
  82. package/utils/__tests__/mapAccum.test.ts +73 -0
  83. package/utils/__tests__/omit.test.ts +19 -0
  84. package/utils/__tests__/path.test.ts +33 -0
  85. package/utils/__tests__/pathOr.test.ts +37 -0
  86. package/utils/__tests__/startsWith.test.ts +30 -0
  87. package/utils/__tests__/take.test.ts +40 -0
  88. package/utils/endsWith.ts +9 -0
  89. package/utils/find.ts +3 -0
  90. package/utils/index.ts +23 -1
  91. package/utils/mapAccum.ts +23 -0
  92. package/utils/omit.ts +5 -0
  93. package/utils/path.ts +5 -0
  94. package/utils/pathOr.ts +5 -0
  95. package/utils/startsWith.ts +9 -0
  96. package/utils/take.ts +5 -0
@@ -5,6 +5,7 @@ import { createLogger, utilsLogger } from "../../../logger";
5
5
  import { appStore } from "@applicaster/zapp-react-native-redux/AppStore";
6
6
  import {
7
7
  findPluginByIdentifier,
8
+ loadFeedEntry,
8
9
  loadFeedAndPrefetchThumbnailImage,
9
10
  parseTimeToSeconds,
10
11
  retrieveFeedUrl,
@@ -26,6 +27,19 @@ type ChapterMarkerOriginal = {
26
27
  actions: ActionChapter[];
27
28
  };
28
29
 
30
+ export type LiveMetadataEvent = {
31
+ programId: string;
32
+ assetId: string;
33
+ title: string;
34
+ programStartTime: string;
35
+ programEndTime: string;
36
+ };
37
+
38
+ export type LiveMetadataConfig = {
39
+ updateUrl: string;
40
+ updateInterval: number;
41
+ };
42
+
29
43
  export type TitleSummaryEvent = {
30
44
  title: string | number;
31
45
  summary: string | number;
@@ -50,11 +64,13 @@ export type PlayNextState = PlayNextConfig & {
50
64
  export class OverlaysObserver {
51
65
  readonly chapterSubject: BehaviorSubject<ChapterMarkerEvent>;
52
66
  private playNextSubject: BehaviorSubject<PlayNextState>;
67
+ private liveMetadataSubject: BehaviorSubject<LiveMetadataEvent>;
53
68
  private titleSummarySubject: BehaviorSubject<TitleSummaryEvent>;
54
69
  private feedUrl: string;
55
70
  private reloadData: () => void;
56
71
  private updateTitleAndDescription: (data: any) => void;
57
72
  private feedDataInterval: any;
73
+ private liveMetadataUpdateInterval: any;
58
74
  private releasePlayerObserver?: () => void;
59
75
  readonly entry: ZappEntry;
60
76
  private chapterMarkerEvents: ChapterMarkerEvent[];
@@ -66,13 +82,17 @@ export class OverlaysObserver {
66
82
  this.chapterSubject = new BehaviorSubject(null);
67
83
  this.playNextSubject = new BehaviorSubject(null);
68
84
 
85
+ this.player = player;
86
+ this.entry = player.getEntry();
87
+
69
88
  this.titleSummarySubject = new BehaviorSubject<TitleSummaryEvent>({
70
- title: player.getEntry()?.title || "",
71
- summary: player.getEntry()?.summary || "",
89
+ title: this.entry?.title || "",
90
+ summary: this.entry?.summary || "",
72
91
  });
73
92
 
74
- this.entry = player.getEntry();
75
- this.player = player;
93
+ this.liveMetadataSubject = new BehaviorSubject<LiveMetadataEvent>(
94
+ this.entry?.extensions?.liveMetadata || null
95
+ );
76
96
 
77
97
  this.chapterMarkerEvents = this.prepareChapterMarkers();
78
98
  this.releasePlayerObserver = this.subscribeToPlayerEvents();
@@ -80,7 +100,9 @@ export class OverlaysObserver {
80
100
  this.reloadData = () => {};
81
101
  this.updateTitleAndDescription = () => {};
82
102
  this.feedDataInterval = null;
103
+ this.liveMetadataUpdateInterval = null;
83
104
  void this.preparePlayNext();
105
+ void this.prepareLiveMetadata();
84
106
  }
85
107
 
86
108
  private setupFeedDataInterval(interval: number) {
@@ -98,6 +120,13 @@ export class OverlaysObserver {
98
120
  }
99
121
  }
100
122
 
123
+ public clearLiveMetadataUpdateInterval() {
124
+ if (this.liveMetadataUpdateInterval) {
125
+ clearInterval(this.liveMetadataUpdateInterval);
126
+ this.liveMetadataUpdateInterval = null;
127
+ }
128
+ }
129
+
101
130
  public setFeedDataHandlers(
102
131
  feedUrl: string,
103
132
  reloadData: () => void,
@@ -197,6 +226,48 @@ export class OverlaysObserver {
197
226
  }
198
227
  };
199
228
 
229
+ prepareLiveMetadata = async () => {
230
+ if (!this.player?.isLive?.()) {
231
+ log_debug("prepareLiveMetadata: Player is not live. Skipping...");
232
+
233
+ return;
234
+ }
235
+
236
+ const config: LiveMetadataConfig =
237
+ this.entry?.extensions?.liveMetadataConfig;
238
+
239
+ if (!config?.updateUrl || !config?.updateInterval) {
240
+ log_debug(
241
+ "prepareLiveMetadata: Live metadata configuration is not available. Skipping...",
242
+ { config }
243
+ );
244
+
245
+ return;
246
+ }
247
+
248
+ const reloadData = async () => {
249
+ try {
250
+ const entry = await loadFeedEntry(config.updateUrl, this.entry);
251
+
252
+ this.onLiveMetadataUpdated(entry?.extensions?.liveMetadata || null);
253
+ } catch (error) {
254
+ log_error("prepareLiveMetadata: Metadata fetching failed", {
255
+ error,
256
+ config,
257
+ });
258
+ }
259
+ };
260
+
261
+ log_debug("prepareLiveMetadata: Setting up live metadata observer update", {
262
+ config,
263
+ });
264
+
265
+ const interval = Number(config?.updateInterval) || 60;
266
+
267
+ this.clearLiveMetadataUpdateInterval();
268
+ this.liveMetadataUpdateInterval = setInterval(reloadData, interval * 1000);
269
+ };
270
+
200
271
  // TODO: Hack for video end, will be replaced with playlist prev/next in the future
201
272
  getPlayNextEntry = () =>
202
273
  !this.isCanceledByUser ? this.playNextConfig?.entry : null;
@@ -317,9 +388,11 @@ export class OverlaysObserver {
317
388
  onPlayerClose = () => {
318
389
  this.chapterSubject.complete();
319
390
  this.playNextSubject.complete();
391
+ this.liveMetadataSubject.complete();
320
392
  this.titleSummarySubject.complete();
321
393
  this.releasePlayerObserver?.();
322
394
  this.clearFeedDataInterval();
395
+ this.clearLiveMetadataUpdateInterval();
323
396
  this.releasePlayerObserver = null;
324
397
  };
325
398
 
@@ -352,4 +425,21 @@ export class OverlaysObserver {
352
425
  (prev, curr) => prev?.triggerTime === curr?.triggerTime
353
426
  )
354
427
  );
428
+
429
+ private onLiveMetadataUpdated = (liveMetadataEvent: LiveMetadataEvent) => {
430
+ this.liveMetadataSubject.next(liveMetadataEvent);
431
+ };
432
+
433
+ public getLiveMetadataObservable = (): Observable<LiveMetadataEvent> => {
434
+ return this.liveMetadataSubject.pipe(
435
+ distinctUntilChanged(
436
+ (prev, curr) =>
437
+ prev?.programId === curr?.programId && prev?.assetId === curr?.assetId
438
+ )
439
+ );
440
+ };
441
+
442
+ public getLiveMetadataValue = (): LiveMetadataEvent => {
443
+ return this.liveMetadataSubject.value;
444
+ };
355
445
  }
@@ -92,22 +92,26 @@ export const prefetchImage = (playableItem: ZappEntry, config: any = {}) => {
92
92
  }
93
93
  };
94
94
 
95
- export const loadFeedAndPrefetchThumbnailImage = async (
96
- playNextFeedUrl: string,
97
- entry: ZappEntry,
98
- playNextPlugin
99
- ) => {
95
+ /**
96
+ * Loads a feed entry from the given feed URL using the provided Zapp entry as context.
97
+ *
98
+ * @param {string} feedUrl - The URL of the feed to load the entry from.
99
+ * @param {ZappEntry} entry - The Zapp entry to use as context for the request.
100
+ * @returns {Promise<ZappEntry>} A promise that resolves to the loaded Zapp entry.
101
+ * @throws {Error} If the feed loading fails or no entry is found in the response.
102
+ */
103
+ export const loadFeedEntry = async (feedUrl: string, entry: ZappEntry) => {
100
104
  const requestBuilder = new RequestBuilder()
101
105
  .setEntryContext(entry)
102
106
  .setScreenContext({} as ZappRiver)
103
- .setUrl(playNextFeedUrl);
107
+ .setUrl(feedUrl);
104
108
 
105
109
  const responseObject = await requestBuilder.call<ZappEntry>();
106
110
  const responseHelper = new PipesClientResponseHelper(responseObject);
107
111
 
108
112
  if (responseHelper.error) {
109
113
  log_error(
110
- `loadFeedAndPrefetchThumbnailImage: loading failed with error: ${responseHelper.error.message}. Play next observer, will not be executed`,
114
+ `loadFeedEntry: loading failed with error: ${responseHelper.error.message}. Observer will not be executed`,
111
115
  {
112
116
  response: responseHelper.getLogsData(),
113
117
  }
@@ -116,29 +120,37 @@ export const loadFeedAndPrefetchThumbnailImage = async (
116
120
  throw responseHelper.error;
117
121
  } else {
118
122
  log_info(
119
- `loadFeedAndPrefetchThumbnailImage: Play next url was successfully loaded for url: ${playNextFeedUrl}. Prefetching image`,
123
+ `loadFeedEntry: Feed was successfully loaded for url: ${feedUrl}`,
120
124
  responseHelper.getLogsData()
121
125
  );
122
126
 
123
- const playNextEntry = responseHelper.responseData?.entry[0];
127
+ const entry = responseHelper.responseData?.entry[0];
124
128
 
125
- if (!playNextEntry) {
126
- log_error(
127
- "loadFeedAndPrefetchThumbnailImage: Can not retrieve play next entry, feed was loaded but no entry was found",
128
- responseHelper.getLogsData()
129
- );
129
+ if (!entry) {
130
+ const error =
131
+ "loadFeedEntry: Can not retrieve entry, feed was loaded but no entry was found";
130
132
 
131
- throw new Error(
132
- "Can not retrieve play next entry, feed was loaded but no entry was found"
133
- );
134
- }
133
+ log_error(error, responseHelper.getLogsData());
135
134
 
136
- prefetchImage(playNextEntry, playNextPlugin?.configuration);
135
+ throw new Error(error);
136
+ }
137
137
 
138
- return playNextEntry;
138
+ return entry;
139
139
  }
140
140
  };
141
141
 
142
+ export const loadFeedAndPrefetchThumbnailImage = async (
143
+ playNextFeedUrl: string,
144
+ entry: ZappEntry,
145
+ playNextPlugin
146
+ ) => {
147
+ const playNextEntry = await loadFeedEntry(playNextFeedUrl, entry);
148
+
149
+ prefetchImage(playNextEntry, playNextPlugin?.configuration);
150
+
151
+ return playNextEntry;
152
+ };
153
+
142
154
  export const findPluginByIdentifier = (
143
155
  identifier: string,
144
156
  plugins: ZappPlugin[]
@@ -2,6 +2,27 @@ export const userPreferencesNamespace = "user_preferences";
2
2
 
3
3
  export const skipActionType = "show_skip";
4
4
 
5
+ export class PlayerError
6
+ extends Error
7
+ implements QuickBrickPlayer.PlayerErrorI
8
+ {
9
+ description: string;
10
+
11
+ constructor(message: string, description: string) {
12
+ super(message);
13
+ this.description = description;
14
+
15
+ Object.setPrototypeOf(this, PlayerError.prototype);
16
+ }
17
+
18
+ toObject() {
19
+ return {
20
+ error: this.message,
21
+ message: this.description,
22
+ };
23
+ }
24
+ }
25
+
5
26
  export enum SharedPlayerCallBacksKeys {
6
27
  OnPlayerResume = "onPlayerResume",
7
28
  OnPlayerPause = "onPlayerPause",
@@ -0,0 +1,24 @@
1
+ import { allTruthy } from "..";
2
+
3
+ describe("allTruthy", () => {
4
+ it("should return true when all values are true", () => {
5
+ expect(allTruthy([true, true, true])).toBe(true);
6
+ });
7
+
8
+ it("should return false when at least one value is false", () => {
9
+ expect(allTruthy([true, false, true])).toBe(false);
10
+ });
11
+
12
+ it("should return false when all values are false", () => {
13
+ expect(allTruthy([false, false, false])).toBe(false);
14
+ });
15
+
16
+ it("should return false for an empty array", () => {
17
+ expect(allTruthy([])).toBe(false);
18
+ });
19
+
20
+ it("should handle single-element arrays correctly", () => {
21
+ expect(allTruthy([true])).toBe(true);
22
+ expect(allTruthy([false])).toBe(false);
23
+ });
24
+ });
@@ -0,0 +1,24 @@
1
+ import { anyTruthy } from "..";
2
+
3
+ describe("anyTruthy", () => {
4
+ it("should return true when at least one value is true", () => {
5
+ expect(anyTruthy([false, true, false])).toBe(true);
6
+ });
7
+
8
+ it("should return false when all values are false", () => {
9
+ expect(anyTruthy([false, false, false])).toBe(false);
10
+ });
11
+
12
+ it("should return true when all values are true", () => {
13
+ expect(anyTruthy([true, true, true])).toBe(true);
14
+ });
15
+
16
+ it("should return false for an empty array", () => {
17
+ expect(anyTruthy([])).toBe(false);
18
+ });
19
+
20
+ it("should handle single-element arrays correctly", () => {
21
+ expect(anyTruthy([true])).toBe(true);
22
+ expect(anyTruthy([false])).toBe(false);
23
+ });
24
+ });
@@ -93,7 +93,7 @@ export const isIndexInRange = (index: number, length: number): boolean => {
93
93
  export const makeListOfIndexes = (size: number): number[] =>
94
94
  Array.from({ length: size }, (_, index) => index);
95
95
 
96
- export const makeListOf = (value: unknown, size: number): number[] => {
96
+ export const makeListOf = <T>(value: T, size: number): T[] => {
97
97
  return Array(size).fill(value);
98
98
  };
99
99
 
@@ -116,3 +116,8 @@ export const sample = (xs: unknown[]): unknown => {
116
116
 
117
117
  return xs[index];
118
118
  };
119
+
120
+ export const allTruthy = (xs: boolean[]) =>
121
+ isFilledArray(xs) && xs.every(Boolean);
122
+
123
+ export const anyTruthy = (xs: boolean[]) => xs.some(Boolean);
@@ -0,0 +1,38 @@
1
+ import { isTabsScreen } from "..";
2
+
3
+ describe("isTabsScreen", () => {
4
+ it("should return true if the component type is 'screen-picker-qb-tv' and tabs_screen=true", () => {
5
+ const item = { component_type: "screen-picker-qb-tv", tabs_screen: true };
6
+ expect(isTabsScreen(item)).toBe(true);
7
+ });
8
+
9
+ it("should return true if the component type is 'screen-picker-qb-tv' and tabs_screen=false", () => {
10
+ const item = { component_type: "screen-picker-qb-tv", tabs_screen: false };
11
+ expect(isTabsScreen(item)).toBe(false);
12
+ });
13
+
14
+ it("should return false if the component type is not 'screen-picker-qb-tv'", () => {
15
+ const item = { component_type: "other-component" };
16
+ expect(isTabsScreen(item)).toBe(false);
17
+ });
18
+
19
+ it("should return false if the component type is undefined", () => {
20
+ const item = { component_type: undefined };
21
+ expect(isTabsScreen(item)).toBe(false);
22
+ });
23
+
24
+ it("should return false if the item is null", () => {
25
+ const item = null;
26
+ expect(isTabsScreen(item)).toBe(false);
27
+ });
28
+
29
+ it("should return false if the item is undefined", () => {
30
+ const item = undefined;
31
+ expect(isTabsScreen(item)).toBe(false);
32
+ });
33
+
34
+ it("should return false if the item is an empty object", () => {
35
+ const item = {};
36
+ expect(isTabsScreen(item)).toBe(false);
37
+ });
38
+ });
@@ -5,7 +5,7 @@ const EMPTY_GROUP_COMPONENT = "empty_group_component";
5
5
 
6
6
  const GALLERY = "gallery-qb";
7
7
 
8
- const SCREEN_PICKER = "screen-picker-qb-tv";
8
+ export const SCREEN_PICKER = "screen-picker-qb-tv";
9
9
 
10
10
  const HORIZONTAL_LIST = "horizontal_list_qb";
11
11
 
@@ -37,3 +37,6 @@ export const isEmptyGroup = (item): boolean =>
37
37
  export const isGroupInfo = (item): boolean =>
38
38
  item?.component_type === GROUP_INFO ||
39
39
  item?.component_type === GROUP_INFO_OLD;
40
+
41
+ export const isTabsScreen = (item): boolean =>
42
+ isScreenPicker(item) && item?.tabs_screen;
@@ -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
  }));
@@ -399,7 +399,7 @@ export const populateConfigurationValues =
399
399
  flattenAndPopulateFields(fields, configuration, skipDefaults)
400
400
  );
401
401
 
402
- export const getAccesabilityProps = (item: ZappEntry) => ({
402
+ export const getAccessibilityProps = (item: ZappEntry) => ({
403
403
  accessible: item?.extensions?.accessibility,
404
404
  accessibilityLabel: item?.extensions?.accessibility?.label || item?.title,
405
405
  accessibilityHint: item?.extensions?.accessibility?.hint,
@@ -2,11 +2,24 @@ import { path } from "ramda";
2
2
  import { isString } from "@applicaster/zapp-react-native-utils/stringUtils";
3
3
  import * as FOCUS_EVENTS from "@applicaster/zapp-react-native-utils/appUtils/focusManager/events";
4
4
 
5
+ import {
6
+ QUICK_BRICK_CONTENT,
7
+ QUICK_BRICK_NAVBAR,
8
+ } from "@applicaster/quick-brick-core/const";
9
+
5
10
  import { logger } from "./logger";
6
11
  import { TreeNode } from "./TreeNode";
7
12
  import { Tree } from "./Tree";
8
13
  import { subscriber } from "../functionUtils";
9
14
  import { getFocusableId, toFocusDirection } from "./utils";
15
+ import {
16
+ findSelectedMenuId,
17
+ isTabsScreenContentFocused,
18
+ findSelectedTabId,
19
+ contextWithoutScrolling,
20
+ } from "./aux";
21
+
22
+ export { contextWithoutScrolling } from "./aux";
10
23
 
11
24
  export {
12
25
  toFocusDirection,
@@ -221,7 +234,8 @@ class FocusManager {
221
234
 
222
235
  private setNextFocus(
223
236
  nextFocus: FocusManager.TouchableReactRef,
224
- options?: FocusManager.Android.CallbackOptions
237
+ options?: FocusManager.Android.CallbackOptions,
238
+ context?: FocusManager.FocusContext
225
239
  ) {
226
240
  if (nextFocus?.current?.props?.blockFocus) {
227
241
  return;
@@ -250,7 +264,7 @@ class FocusManager {
250
264
 
251
265
  FocusManager.instance.setPreviousNavigationDirection(options ?? null);
252
266
 
253
- nextFocus?.current?.onFocus?.(nextFocus.current, options ?? {});
267
+ nextFocus?.current?.onFocus?.(nextFocus.current, options ?? {}, context);
254
268
  }
255
269
  }
256
270
 
@@ -291,7 +305,8 @@ class FocusManager {
291
305
 
292
306
  setFocus(
293
307
  newFocus: FocusManager.TouchableReactRef | string,
294
- options?: FocusManager.Android.CallbackOptions
308
+ options?: FocusManager.Android.CallbackOptions,
309
+ context?: FocusManager.FocusContext
295
310
  ) {
296
311
  // Checks if element is focusable
297
312
  const { isFocusable, error } = FocusManager.isFocusable(newFocus);
@@ -316,7 +331,7 @@ class FocusManager {
316
331
  }
317
332
 
318
333
  if (newFocusRef) {
319
- FocusManager.instance.setNextFocus(newFocusRef, options);
334
+ FocusManager.instance.setNextFocus(newFocusRef, options, context);
320
335
  }
321
336
  }
322
337
  }
@@ -351,6 +366,11 @@ class FocusManager {
351
366
  FocusManager.instance.focused.onBlur(FocusManager.instance.focused, {});
352
367
  }
353
368
 
369
+ // send reset event to some handler to reset their internal state, before real reset happens
370
+ this.eventHandler?.invokeHandler?.(FOCUS_EVENTS.RESET, {
371
+ focusedId: FocusManager.instance.focusedId,
372
+ });
373
+
354
374
  FocusManager.instance.setFocusLocal({ current: null });
355
375
  }
356
376
 
@@ -417,6 +437,60 @@ class FocusManager {
417
437
  throw new Error(`Group with id ${id} not found`);
418
438
  }
419
439
  }
440
+
441
+ isFocusOnMenu(): boolean {
442
+ return this.isFocusableChildOf(
443
+ FocusManager.instance.focusedId,
444
+ QUICK_BRICK_NAVBAR
445
+ );
446
+ }
447
+
448
+ isFocusOnContent(): boolean {
449
+ return this.isFocusableChildOf(
450
+ FocusManager.instance.focusedId,
451
+ QUICK_BRICK_CONTENT
452
+ );
453
+ }
454
+
455
+ private landFocusToWithoutScrolling = (id) => {
456
+ if (id) {
457
+ // set focus on selected menu item
458
+ const direction = undefined;
459
+
460
+ const context: FocusManager.FocusContext =
461
+ contextWithoutScrolling("back");
462
+
463
+ logger.log({ message: "landFocusToWithoutScrolling", data: { id } });
464
+
465
+ this.setFocus(id, direction, context);
466
+ }
467
+ };
468
+
469
+ // Move focus to appropriate top navigation tab with context
470
+ focusOnSelectedTab(index: number): void {
471
+ const selectedTabId = findSelectedTabId(this.tree, index);
472
+
473
+ // Set focus with back button context to tabs-menu
474
+ this.landFocusToWithoutScrolling(selectedTabId);
475
+ }
476
+
477
+ // Move focus to appropriate top navigation tab with context
478
+ focusOnSelectedTopMenuItem(index: number, sectionKey: string): void {
479
+ const selectedMenuItemId = findSelectedMenuId(this.tree, {
480
+ index,
481
+ sectionKey,
482
+ });
483
+
484
+ // Set focus with back button context to top-menu
485
+ this.landFocusToWithoutScrolling(selectedMenuItemId);
486
+ }
487
+
488
+ isTabsScreenContentFocused(): boolean {
489
+ return isTabsScreenContentFocused(
490
+ this.tree,
491
+ FocusManager.instance.focusedId
492
+ );
493
+ }
420
494
  }
421
495
 
422
496
  export const focusManager = FocusManager.getInstance();
@@ -0,0 +1,98 @@
1
+ import { isNil, pathOr } from "@applicaster/zapp-react-native-utils/utils";
2
+
3
+ import {
4
+ QUICK_BRICK_CONTENT,
5
+ QUICK_BRICK_NAVBAR,
6
+ QUICK_BRICK_NAVBAR_SECTIONS,
7
+ } from "@applicaster/quick-brick-core/const";
8
+
9
+ const isNavBar = (node) => QUICK_BRICK_NAVBAR === node?.id;
10
+ const isContent = (node) => QUICK_BRICK_CONTENT === node?.id;
11
+
12
+ // SCREEN_PICKER_SELECTOR_CONTAINER(we assume there is only one SCREEN_PICKER)
13
+ let screenPickerSelectorContainerId;
14
+
15
+ export const onRegisterScreenPickerSelectorContainer = (id) => {
16
+ screenPickerSelectorContainerId = id;
17
+ };
18
+
19
+ export const onUnregisterScreenPickerSelectorContainer = (id) => {
20
+ // reset screenSelectorId on unregistration
21
+ if (screenPickerSelectorContainerId === id) {
22
+ screenPickerSelectorContainerId = undefined;
23
+ }
24
+ };
25
+ // SCREEN_PICKER_SELECTOR_CONTAINER
26
+
27
+ // SCREEN_PICKER_CONTENT_CONTAINER(we assume there is only one SCREEN_PICKER)
28
+ let screenPickerContentContainerId;
29
+
30
+ export const onRegisterScreenPickerContentContainer = (id) => {
31
+ screenPickerContentContainerId = id;
32
+ };
33
+
34
+ export const onUnregisterScreenPickerContentContainer = (id) => {
35
+ // reset screenSelectorId on unregistration
36
+ if (screenPickerContentContainerId === id) {
37
+ screenPickerContentContainerId = undefined;
38
+ }
39
+ };
40
+
41
+ const isScreenPickerContentContainer = (node) =>
42
+ screenPickerContentContainerId === node?.id;
43
+
44
+ // SCREEN_PICKER_CONTENT_CONTAINER
45
+
46
+ export const findSelectedMenuId = (
47
+ focusableTree,
48
+ { index, sectionKey }: { index: number; sectionKey: string }
49
+ ) => {
50
+ const sectionName = QUICK_BRICK_NAVBAR_SECTIONS[sectionKey];
51
+
52
+ return pathOr(
53
+ undefined,
54
+ ["children", index, "id"],
55
+ focusableTree.find(sectionName)
56
+ );
57
+ };
58
+
59
+ export const findSelectedTabId = (focusableTree, index: number): string => {
60
+ const screenSelectorContainerNode = focusableTree.find(
61
+ screenPickerSelectorContainerId
62
+ );
63
+
64
+ const selectedTabId = screenSelectorContainerNode.children[index]?.id;
65
+
66
+ return selectedTabId;
67
+ };
68
+
69
+ export const isTabsScreenContentFocused = (focusableTree, id) => {
70
+ const node = focusableTree.find(id);
71
+
72
+ if (isNil(node)) {
73
+ return false;
74
+ }
75
+
76
+ if (isNavBar(node)) {
77
+ return false;
78
+ }
79
+
80
+ if (isContent(node)) {
81
+ return false;
82
+ }
83
+
84
+ if (isScreenPickerContentContainer(node)) {
85
+ return true;
86
+ }
87
+
88
+ return isTabsScreenContentFocused(focusableTree, node.parentId);
89
+ };
90
+
91
+ export const contextWithoutScrolling = (
92
+ source: FocusManager.FocusContext["source"]
93
+ ): FocusManager.FocusContext => {
94
+ return {
95
+ source,
96
+ preserveScroll: true,
97
+ };
98
+ };