@applicaster/zapp-react-native-utils 12.0.2 → 12.0.3-alpha.1451754086

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,9 @@ import { AccessibilityRole } from "react-native";
6
6
 
7
7
  export class AccessibilityManager {
8
8
  private static _instance: AccessibilityManager | null = null;
9
+ private headingTimeout: ReturnType<typeof setTimeout> | null = null;
10
+ private WORDS_PER_MINUTE = 160;
11
+ private MINIMUM_PAUSE = 500;
9
12
  private state$ = new BehaviorSubject<AccessibilityState>({
10
13
  screenReaderEnabled: false,
11
14
  reduceMotionEnabled: false,
@@ -20,6 +23,7 @@ export class AccessibilityManager {
20
23
 
21
24
  private ttsManager = TTSManager.getInstance();
22
25
  private localizations: { [key: string]: string } = {};
26
+ private headingQueue: string[] = [];
23
27
 
24
28
  private constructor() {}
25
29
 
@@ -52,6 +56,28 @@ export class AccessibilityManager {
52
56
  return this.state$.asObservable();
53
57
  }
54
58
 
59
+ /** Calculates the reading time for a given text
60
+ * This method is a bit of a hack because we don't have a callback, or promise from VIZIO API
61
+ * @param text - The text to calculate the reading time for
62
+ * @returns The reading time in milliseconds
63
+ */
64
+ private calculateReadingTime(text: string): number {
65
+ const words = text.trim().split(/\s+/).length;
66
+
67
+ return Math.max(
68
+ this.MINIMUM_PAUSE,
69
+ (words / this.WORDS_PER_MINUTE) * 60 * 1000
70
+ );
71
+ }
72
+
73
+ /**
74
+ * Adds a heading to the queue, headings will be read before the next text
75
+ * Each heading will be read once and removed from the queue
76
+ */
77
+ public addHeading(heading: string) {
78
+ this.headingQueue.push(heading);
79
+ }
80
+
55
81
  /**
56
82
  * text you want to be read, if you want to use localized text pass keyOfLocalizedText instead
57
83
  * keyOfLocalizedText is the key to the localized text
@@ -85,7 +111,22 @@ export class AccessibilityManager {
85
111
  textToRead = localizedMessage;
86
112
  }
87
113
 
88
- this.ttsManager && this.ttsManager.readText(textToRead);
114
+ if (this.headingQueue.length > 0) {
115
+ const heading = this.headingQueue.shift()!;
116
+ this.ttsManager?.readText(heading);
117
+
118
+ if (this.headingTimeout) {
119
+ clearTimeout(this.headingTimeout);
120
+ }
121
+
122
+ const pauseTime = this.calculateReadingTime(heading);
123
+
124
+ this.headingTimeout = setTimeout(() => {
125
+ this.ttsManager?.readText(textToRead);
126
+ }, pauseTime);
127
+ } else {
128
+ this.ttsManager?.readText(textToRead);
129
+ }
89
130
 
90
131
  this.announcements$.next({
91
132
  message: textToRead,
@@ -11,8 +11,8 @@ import {
11
11
  haveSameParentBeforeRoot,
12
12
  } from "./treeDataStructure/Utils";
13
13
  import { subscriber } from "../../functionUtils";
14
- import * as FOCUS_EVENTS from "./events";
15
14
  import { coreLogger } from "../../logger";
15
+ import { ACTION } from "./utils/enums";
16
16
  const logger = coreLogger.addSubsystem("focusManager");
17
17
 
18
18
  const isFocusEnabled = (focusableItem): boolean => {
@@ -167,13 +167,82 @@ export const focusManager = (function () {
167
167
  }
168
168
 
169
169
  /**
170
- * returns the current focusable's group
171
- * @private
172
- * @returns {React.Component} the current focus
170
+ * Computes the difference in parent groups between two nodes.
171
+ * It returns the IDs of parents that are present in the current node but not in the previous node,
172
+ * as well as the IDs of parents that are present in the previous node but not in the current node.
173
+ *
174
+ * @param {string} currentNodeID - The ID of the current node.
175
+ * @param {string} previousNodeID - The ID of the previous node.
176
+ * @returns {Object} An object containing the differences in parent IDs:
177
+ * - {Array<string>} currentNodeParentsIDs: Parent IDs present in the current node but not in the previous node.
178
+ * - {Array<string>} previousNodeParentsIDs: Parent IDs present in the previous node but not in the current node.
173
179
  */
174
- function getCurrentGroup() {
175
- return getGroupById(R.path(["props", "groupId"], getCurrentFocus()));
176
- }
180
+ const getParentDiffs = (currentNodeID, previousNodeID) => {
181
+ const currentNodeParentsIDs =
182
+ focusableTree.getParentGroupIds(currentNodeID);
183
+
184
+ const previousNodeParentsIDs =
185
+ focusableTree.getParentGroupIds(previousNodeID);
186
+
187
+ return {
188
+ currentNodeParentsIDs: R.difference(
189
+ currentNodeParentsIDs,
190
+ previousNodeParentsIDs
191
+ ),
192
+ previousNodeParentsIDs: R.difference(
193
+ previousNodeParentsIDs,
194
+ currentNodeParentsIDs
195
+ ),
196
+ };
197
+ };
198
+
199
+ /**
200
+ * Updates the focus state of nodes identified by their IDs.
201
+ * For each ID, if `setFocus` is true, the node will receive focus;
202
+ * otherwise, it will lose focus.
203
+ *
204
+ * @param {Array<string>} ids - An array of node IDs to update.
205
+ * @param {boolean} setFocus - A flag indicating whether to set focus (true) or blur (false) on the nodes.
206
+ */
207
+ const updateNodeFocus = (ids, action) => {
208
+ if (!ids || ids.length === 0) {
209
+ return; // Nothing to do
210
+ }
211
+
212
+ const startNode = focusableTree.findInTree(ids[0]);
213
+
214
+ if (!startNode) {
215
+ return; // Start node not found
216
+ }
217
+
218
+ const endNodeId = ids[ids.length - 1]; // Get the ID of the end node
219
+
220
+ let currentNode = startNode;
221
+
222
+ // Function to apply the action (focus or blur)
223
+ const applyAction = (node) => {
224
+ if (node && node.component) {
225
+ if (action === "focus") {
226
+ node.component.setFocus();
227
+ } else if (action === "blur") {
228
+ node.component.setBlur();
229
+ }
230
+ }
231
+ };
232
+
233
+ // Apply action to the starting node
234
+ applyAction(currentNode);
235
+
236
+ // Traverse upwards
237
+ while (currentNode && currentNode.id !== endNodeId) {
238
+ currentNode = currentNode.parent;
239
+
240
+ if (currentNode) {
241
+ // check if current node exists, as parent might be root
242
+ applyAction(currentNode);
243
+ }
244
+ }
245
+ };
177
246
 
178
247
  /**
179
248
  * Sets the current focus to the provided id. If there is an existing focus, it will
@@ -181,34 +250,29 @@ export const focusManager = (function () {
181
250
  * @private
182
251
  * @param {String} id of the focusable to set as currentFocus
183
252
  * @param {Object} direction of the navigation, which led to this focus change
184
- * @param {Object} options
185
- * @param {Boolean} options.groupFocusedChanged tells wether the focus has moved
186
253
  * to another group or not. defaults to false
187
254
  */
188
- function setFocus(
189
- id: string,
190
- direction?: FocusManager.Web.Direction,
191
- options?: Partial<{ groupFocusedChanged: boolean }>
192
- ) {
255
+ function setFocus(id: string, direction?: FocusManager.Web.Direction) {
193
256
  if (focusDisabled) return false;
194
257
 
195
- const { groupFocusedChanged = false } = options || {};
196
-
197
258
  // due to optimisiation it's recommanded to set currentFocusNode before setFocus
198
259
  // is called, so there will be no delays on tree search.
199
260
  // If currentFocusNode is null or undefined we still want it to findInTree
200
261
  if (currentFocusNode?.id !== id) {
262
+ const { currentNodeParentsIDs, previousNodeParentsIDs } = getParentDiffs(
263
+ id,
264
+ currentFocusNode?.id
265
+ );
266
+
267
+ // Set focus on current node parents and blur on previous node parents
268
+ updateNodeFocus(currentNodeParentsIDs, ACTION.FOCUS);
269
+ updateNodeFocus(previousNodeParentsIDs, ACTION.BLUR);
270
+
201
271
  currentFocusNode = focusableTree.findInTree(id);
202
272
  }
203
273
 
204
274
  setLastFocusOnParentNode(currentFocusNode);
205
275
 
206
- const currentGroup = getCurrentGroup();
207
-
208
- if (groupFocusedChanged && currentGroup) {
209
- currentGroup.hasLostFocus(currentGroup, direction);
210
- }
211
-
212
276
  focus(direction);
213
277
  }
214
278
 
@@ -251,6 +315,7 @@ export const focusManager = (function () {
251
315
  !focusableItem.isGroup &&
252
316
  isFocusEnabled(focusableItem)
253
317
  ) {
318
+ // TODO: Move this to setFocus method
254
319
  blur();
255
320
 
256
321
  logger.log({
@@ -261,7 +326,7 @@ export const focusManager = (function () {
261
326
  });
262
327
 
263
328
  currentFocusNode = focusCandidate;
264
- focusableItem.willReceiveFocus(focusableItem, null);
329
+
265
330
  focusableItem && setFocus(focusCandidate.id, null);
266
331
 
267
332
  return { success: true };
@@ -448,20 +513,10 @@ export const focusManager = (function () {
448
513
  const nextRect = nextFocusable.component.getRect();
449
514
 
450
515
  if (nextRect && nextRect.width > 0) {
451
- nextFocusable.component.willReceiveFocus(
452
- nextFocusable.component,
453
- direction
454
- );
455
-
516
+ // TODO: Move this to setFocus method
456
517
  blur(direction);
457
518
 
458
- const groupChanged =
459
- nextFocusable.component.props.groupId !==
460
- currentFocusNode?.component?.props?.groupId;
461
-
462
- setFocus(nextFocusable.id, direction, {
463
- groupFocusedChanged: groupChanged,
464
- });
519
+ setFocus(nextFocusable.id, direction);
465
520
  }
466
521
  } else {
467
522
  currentFocusNode.component.failedLostFocus(currentFocusNode, direction);
@@ -481,34 +536,6 @@ export const focusManager = (function () {
481
536
 
482
537
  const { on, invokeHandler, removeHandler } = eventHandler;
483
538
 
484
- on(FOCUS_EVENTS.FOCUS, (focusable, direction) => {
485
- if (focusable.props.groupId) {
486
- const parent = focusableTree.findInTree(focusable.props.groupId);
487
- parent?.component?.onFocus(focusable.component, direction);
488
- }
489
- });
490
-
491
- on(FOCUS_EVENTS.BLUR, (focusable, direction) => {
492
- if (focusable.props.groupId) {
493
- const parent = focusableTree.findInTree(focusable.props.groupId);
494
- parent?.component?.onBlur(focusable.component, direction);
495
- }
496
- });
497
-
498
- on(FOCUS_EVENTS.HAS_LOST_FOCUS, (focusable, direction) => {
499
- if (focusable.props.groupId) {
500
- const parent = focusableTree.findInTree(focusable.props.groupId);
501
- parent?.component?.hasLostFocus(parent.component, direction);
502
- }
503
- });
504
-
505
- on(FOCUS_EVENTS.WILL_RECEIVE_FOCUS, (focusable, direction) => {
506
- if (focusable.props.groupId) {
507
- const parent = focusableTree.findInTree(focusable.props.groupId);
508
- parent?.component?.willReceiveFocus(focusable.component, direction);
509
- }
510
- });
511
-
512
539
  /**
513
540
  * this is the list of the functions available externally
514
541
  * when importing the focus manager
@@ -232,4 +232,27 @@ export class Tree {
232
232
 
233
233
  return retVal;
234
234
  }
235
+
236
+ /**
237
+ * Gets the IDs of all parent groups for a given node id.
238
+ * @public
239
+ * @param {String} nodeId - The id of the node to get parent IDs for.
240
+ * @returns {Array} An array of parent group IDs or an empty array if the node is not found.
241
+ */
242
+ getParentGroupIds(nodeId) {
243
+ const parentIds = [];
244
+ let currentNode = this.findInTree(nodeId);
245
+
246
+ if (!currentNode) {
247
+ return []; // Node not found, return an empty array
248
+ }
249
+
250
+ // Traverse up the tree, collecting parent IDs
251
+ while (currentNode.parent && currentNode.parent.id !== "root") {
252
+ parentIds.push(currentNode.parent.id);
253
+ currentNode = currentNode.parent;
254
+ }
255
+
256
+ return parentIds;
257
+ }
235
258
  }
@@ -0,0 +1,4 @@
1
+ export enum ACTION {
2
+ FOCUS = "focus",
3
+ BLUR = "blur",
4
+ }
@@ -1,8 +1,8 @@
1
1
  import { isNotNil } from "@applicaster/zapp-react-native-utils/reactUtils/helpers";
2
- import { find, last, startsWith, pathOr } from "ramda";
2
+ import { find, last, pathOr, startsWith } from "ramda";
3
3
  import {
4
- QUICK_BRICK_NAVBAR,
5
4
  QUICK_BRICK_CONTENT,
5
+ QUICK_BRICK_NAVBAR,
6
6
  } from "@applicaster/quick-brick-core/const";
7
7
 
8
8
  // run check each 300 ms
@@ -66,7 +66,8 @@ export const getContentNode = (focusableTree) => {
66
66
  const content = find(
67
67
  (child) =>
68
68
  startsWith(QUICK_BRICK_CONTENT, child.id) ||
69
- startsWith("player", child.id),
69
+ startsWith("player", child.id) ||
70
+ startsWith("/hooks", child?.parent?.id), // For startup hooks case
70
71
  getActiveScreenChildren(focusableTree)
71
72
  );
72
73
 
@@ -23,3 +23,17 @@ export const PLATFORMS = {
23
23
  export const PLATFORM_KEYS = {
24
24
  deviceClosedCaptioningEnabled: "device_closed_captioning_enabled",
25
25
  };
26
+
27
+ export enum ZappPlatform {
28
+ Android = "android",
29
+ AndroidTV = "android_tv",
30
+ AmazonFireTV = "amazon",
31
+ iOS = "ios",
32
+ tvOS = "tvos",
33
+ Roku = "roku",
34
+ Tizen = "tizen",
35
+ Web = "web",
36
+ WebOS = "webos",
37
+ Vizio = "vizio",
38
+ Unknown = "unknown",
39
+ }
@@ -6,8 +6,10 @@ import {
6
6
  isSamsungPlatform,
7
7
  } from "@applicaster/zapp-react-dom-app/App/Loader/utils/platform";
8
8
 
9
- import { PLATFORM_KEYS, PLATFORMS } from "./const";
9
+ import { PLATFORM_KEYS, PLATFORMS, ZappPlatform } from "./const";
10
10
  import { createLogger, utilsLogger } from "../../logger";
11
+ import { getPlatform } from "../../reactUtils";
12
+ import { appStore } from "@applicaster/zapp-react-native-redux/AppStore";
11
13
 
12
14
  declare global {
13
15
  interface Window {
@@ -29,6 +31,20 @@ const { log_debug } = createLogger({
29
31
  parent: utilsLogger,
30
32
  });
31
33
 
34
+ export const getZappPlatform = (): ZappPlatform => {
35
+ const platform = appStore.get("appData")?.platform;
36
+
37
+ switch (platform) {
38
+ case "amazon_fire_tv":
39
+ return ZappPlatform.AmazonFireTV;
40
+ case ZappPlatform.Vizio:
41
+ return ZappPlatform.Vizio;
42
+ default:
43
+ // TODO: Use only ZappPlatform enum
44
+ return getPlatform() as ZappPlatform;
45
+ }
46
+ };
47
+
32
48
  export const getUserAgent = () => window?.navigator?.userAgent;
33
49
 
34
50
  /** VIZIO UTILS */
@@ -169,8 +169,12 @@ export class PlayerManager {
169
169
  this.addPlayer(id, playerController);
170
170
 
171
171
  if (playerController.playerRole === PlayerRole.Primary) {
172
- if (playerController.playerPluginId !== "QuickBrickPlayerPlugin") {
173
- RNPlayerManager?.["QuickBrickPlayerPlugin"]?.stopBackgroundPlayback?.();
172
+ if (RNPlayerManager) {
173
+ Object.keys(RNPlayerManager).forEach((pluginId) => {
174
+ if (pluginId !== playerController.playerPluginId) {
175
+ RNPlayerManager[pluginId]?.stopBackgroundPlayback?.();
176
+ }
177
+ });
174
178
  }
175
179
 
176
180
  playerController.addListener({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "12.0.2",
3
+ "version": "12.0.3-alpha.1451754086",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "homepage": "https://github.com/applicaster/quickbrick#readme",
30
30
  "dependencies": {
31
- "@applicaster/applicaster-types": "12.0.2",
31
+ "@applicaster/applicaster-types": "12.0.3-alpha.1451754086",
32
32
  "buffer": "^5.2.1",
33
33
  "camelize": "^1.0.0",
34
34
  "create-react-context": "^0.2.3",
@@ -87,7 +87,7 @@ export const useFeedLoader = ({
87
87
  );
88
88
  }
89
89
  }
90
- }, [callableFeedUrl, currentFeed?.data]);
90
+ }, [callableFeedUrl, currentFeed?.data?.next]);
91
91
 
92
92
  useEffect(() => {
93
93
  if (