@applicaster/zapp-react-native-utils 12.0.0-alpha.4337072244 → 12.0.0-alpha.4342457208

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 (58) hide show
  1. package/analyticsUtils/AnalyticPlayerListener.ts +141 -0
  2. package/analyticsUtils/AnalyticsEvents/index.ts +7 -0
  3. package/analyticsUtils/AnalyticsEvents/sendShareEvent.ts +21 -0
  4. package/analyticsUtils/PlayerAnalyticsManager.ts +90 -0
  5. package/analyticsUtils/analyticsPlugins.ts +25 -0
  6. package/analyticsUtils/events.ts +6 -0
  7. package/analyticsUtils/index.tsx +6 -0
  8. package/analyticsUtils/{analyticsTracker.ts → playerAnalyticsTracker.ts} +19 -10
  9. package/appUtils/accessibilityManager/const.ts +50 -0
  10. package/appUtils/accessibilityManager/hooks.ts +40 -0
  11. package/appUtils/accessibilityManager/index.ts +166 -0
  12. package/appUtils/accessibilityManager/logger.ts +5 -0
  13. package/appUtils/focusManager/__tests__/__snapshots__/focusManager.test.js.snap +1 -2
  14. package/appUtils/focusManager/__tests__/index.test.ts +1 -2
  15. package/appUtils/focusManager/index.ts +7 -73
  16. package/appUtils/focusManager/treeDataStructure/Utils/index.js +6 -5
  17. package/appUtils/focusManager/utils/index.ts +8 -2
  18. package/appUtils/focusManagerAux/index.ts +76 -0
  19. package/appUtils/focusManagerAux/utils/index.ts +100 -0
  20. package/appUtils/index.js +2 -0
  21. package/appUtils/keyCodes/keys.web.js +8 -0
  22. package/appUtils/keyCodes/keysByPlatform.js +11 -1
  23. package/appUtils/platform/const.ts +25 -0
  24. package/appUtils/platform/index.ts +3 -0
  25. package/appUtils/platform/platformUtils.ts +217 -0
  26. package/appUtils/playerManager/conts.ts +1 -0
  27. package/appUtils/playerManager/index.ts +20 -32
  28. package/appUtils/playerManager/nativePlayerManager.ts +12 -0
  29. package/appUtils/playerManager/player.ts +9 -3
  30. package/appUtils/playerManager/playerHooks/usePlayerCurrentTime.tsx +57 -0
  31. package/appUtils/playerManager/playerNative.ts +6 -15
  32. package/appUtils/playerManager/usePlayerControllerSetup.tsx +8 -2
  33. package/appUtils/playerManager/usePlayerState.tsx +19 -34
  34. package/booleanUtils/index.ts +4 -4
  35. package/cellUtils/index.ts +38 -5
  36. package/functionUtils/index.ts +38 -0
  37. package/index.d.ts +15 -0
  38. package/keyboardState/index.ts +0 -0
  39. package/navigationUtils/index.ts +48 -0
  40. package/numberUtils/__tests__/toNumber.test.ts +2 -2
  41. package/package.json +2 -2
  42. package/playerUtils/imaScriptDom.ts +59 -0
  43. package/pluginUtils/index.ts +9 -0
  44. package/reactHooks/cell-click/__tests__/index.test.js +2 -1
  45. package/reactHooks/cell-click/index.ts +1 -1
  46. package/reactHooks/debugging/__tests__/index.test.js +81 -11
  47. package/reactHooks/feed/__tests__/useFeedRefresh.test.tsx +6 -2
  48. package/reactHooks/feed/useFeedRefresh.tsx +9 -7
  49. package/reactHooks/layout/useDimensions/useDimensions.ts +2 -0
  50. package/reactHooks/navigation/__tests__/index.test.tsx +11 -8
  51. package/reactHooks/navigation/index.ts +46 -68
  52. package/reactHooks/player/useAutoSeek.tsx +9 -7
  53. package/reactHooks/resolvers/useCellResolver.ts +5 -1
  54. package/reactHooks/screen/useScreenContext.ts +1 -2
  55. package/reactUtils/index.ts +104 -0
  56. package/appUtils/focusManager/FocusableEvents.ts +0 -8
  57. package/appUtils/focusManager/treeDataStructure/Utils/__tests__/Utils_android.test.js +0 -1094
  58. package/appUtils/focusManager/treeDataStructure/Utils/index.android.js +0 -261
@@ -8,18 +8,11 @@ import { Tree } from "./treeDataStructure/Tree";
8
8
  import {
9
9
  findFocusableNode,
10
10
  findPriorityItem,
11
- isTransitionHappening,
11
+ haveSameParentBeforeRoot,
12
12
  } from "./treeDataStructure/Utils";
13
13
  import { subscriber } from "../../functionUtils";
14
14
  import * as FOCUS_EVENTS from "./events";
15
15
  import { coreLogger } from "../../logger";
16
- import {
17
- waitForActiveScreen,
18
- waitForContent,
19
- getContentNode,
20
- getNavbarNode,
21
- } from "./utils";
22
-
23
16
  const logger = coreLogger.addSubsystem("focusManager");
24
17
 
25
18
  const isFocusEnabled = (focusableItem): boolean => {
@@ -416,77 +409,19 @@ export const focusManager = (function () {
416
409
  group ? unregisterGroup(id) : unregisterItem(id);
417
410
  }
418
411
 
419
- async function setFocusOnMenu(currentRoute: string) {
420
- try {
421
- await waitForActiveScreen(currentRoute, focusableTree);
422
-
423
- const navbar = getNavbarNode(focusableTree);
424
-
425
- const selectedMenuItemId = R.find(
426
- (child) => child.component.props.selected,
427
- navbar.children
428
- )?.id;
429
-
430
- if (selectedMenuItemId) {
431
- // set focus on selected menu item
432
- setFocus(selectedMenuItemId);
433
-
434
- return;
435
- }
436
-
437
- const firstMenuItemId = R.path(["children", 0, "id"], navbar);
438
-
439
- if (firstMenuItemId) {
440
- // set focus on first menu item
441
- setFocus(firstMenuItemId);
442
- }
443
- } catch (error) {
444
- logger.log({
445
- message: "setFocusOnMenu – failed to put focus on top-menu",
446
- data: {
447
- currentRoute,
448
- error,
449
- },
450
- });
451
- }
452
- }
453
-
454
- async function setFocusOnContent(currentRoute: string) {
455
- // we need to put focus on content here
456
- // For doing it we have to wait registration of first focusable node into focusableTree
457
- // we wait until content node starts having children
458
- try {
459
- await waitForActiveScreen(currentRoute, focusableTree);
460
- await waitForContent(focusableTree);
461
-
462
- const content = getContentNode(focusableTree);
463
-
464
- const { success } = setInitialFocus(content);
465
-
466
- if (!success) {
467
- // we've failed to put initial focus on content, re-run "setFocusOnContent" again as a fallback
468
- throw Error(
469
- "we've failed to put initial focus on content, re-run 'setFocusOnContent' again as a fallback"
470
- );
471
- }
472
- } catch (error) {
473
- setFocusOnContent(currentRoute);
474
- }
475
- }
476
-
477
- function isTransitionInProgress() {
412
+ function isCurrentFocusOnTheTopScreen() {
478
413
  const routes = R.pathOr([], ["root", "children"], focusableTree);
479
414
 
480
415
  if (routes.length <= 1) {
481
416
  // there is only one or zero routes
482
- return false;
417
+ return true;
483
418
  }
484
419
 
485
- return isTransitionHappening(currentFocusNode, R.last(routes));
420
+ return haveSameParentBeforeRoot(currentFocusNode, R.last(routes));
486
421
  }
487
422
 
488
423
  function recoverFocus() {
489
- if (isTransitionInProgress()) {
424
+ if (!isCurrentFocusOnTheTopScreen()) {
490
425
  // We've failed to set focused node on the new screen => run focus recovery
491
426
  setInitialFocus();
492
427
  }
@@ -498,7 +433,7 @@ export const focusManager = (function () {
498
433
  * @param {Object} direction of the scroll
499
434
  */
500
435
  function moveFocus(direction) {
501
- if (isTransitionInProgress()) {
436
+ if (!isCurrentFocusOnTheTopScreen()) {
502
437
  // We are opening new screen. We need to ignore movement of focus.
503
438
  return false;
504
439
  }
@@ -600,8 +535,7 @@ export const focusManager = (function () {
600
535
  focusableTree,
601
536
  getGroupRootById,
602
537
  isGroupItemFocused,
603
- setFocusOnMenu,
604
- setFocusOnContent,
605
538
  recoverFocus,
539
+ isCurrentFocusOnTheTopScreen,
606
540
  };
607
541
  })();
@@ -1,6 +1,7 @@
1
1
  import { allPass, pathOr, startsWith, isNil } from "ramda";
2
2
  import { isNilOrEmpty } from "@applicaster/zapp-react-native-utils/reactUtils/helpers";
3
3
  import { isZero } from "@applicaster/zapp-react-native-utils/numberUtils";
4
+ import { QUICK_BRICK_NAVBAR } from "@applicaster/quick-brick-core/const";
4
5
 
5
6
  import { rejectItemByPath } from "../../../../objectUtils";
6
7
  import {
@@ -154,10 +155,10 @@ const excludeFromFocusSearching = pathOr(false, [
154
155
  "excludeFromFocusSearching",
155
156
  ]);
156
157
 
157
- const isNavBar = (target) => startsWith("navbar", target?.id);
158
+ const isNavBar = (target) => startsWith(QUICK_BRICK_NAVBAR, target?.id);
158
159
 
159
160
  const ignoreMenuIfMultipleTargets = (nodes) => {
160
- if (nodes.length > 1 && nodes.map((n) => n?.target?.id).includes("navbar")) {
161
+ if (nodes.length > 1) {
161
162
  return nodes.filter((n) => !isNavBar(n?.target));
162
163
  }
163
164
 
@@ -205,7 +206,7 @@ export function getTargetInDirection(
205
206
  const overrideTarget = currentNode?.component?.props[overridePropName];
206
207
 
207
208
  if (overrideTarget) {
208
- const target = targetNodes.find((n) => n.id === overrideTarget);
209
+ const target = targetNodes.find((n) => n?.id === overrideTarget);
209
210
 
210
211
  if (target) return target;
211
212
  }
@@ -333,9 +334,9 @@ export const getNodeBeforeRoot = (node) => {
333
334
  return getNodeBeforeRoot(node.parent);
334
335
  };
335
336
 
336
- export const isTransitionHappening = (currentNode, nextNode) => {
337
+ export const haveSameParentBeforeRoot = (currentNode, nextNode) => {
337
338
  const routeForCurrentNode = getNodeBeforeRoot(currentNode);
338
339
  const routeForNextNode = getNodeBeforeRoot(nextNode);
339
340
 
340
- return routeForCurrentNode?.id !== routeForNextNode?.id;
341
+ return routeForCurrentNode?.id === routeForNextNode?.id;
341
342
  };
@@ -1,5 +1,9 @@
1
1
  import { isNotNil } from "@applicaster/zapp-react-native-utils/reactUtils/helpers";
2
2
  import { find, last, startsWith, pathOr } from "ramda";
3
+ import {
4
+ QUICK_BRICK_NAVBAR,
5
+ QUICK_BRICK_CONTENT,
6
+ } from "@applicaster/quick-brick-core/const";
3
7
 
4
8
  // run check each 300 ms
5
9
  // run this check too often could lead to performance penalty on low-end devices
@@ -60,7 +64,9 @@ const getActiveScreenChildren = (focusableTree) => {
60
64
 
61
65
  export const getContentNode = (focusableTree) => {
62
66
  const content = find(
63
- (child) => startsWith("content", child.id),
67
+ (child) =>
68
+ startsWith(QUICK_BRICK_CONTENT, child.id) ||
69
+ startsWith("player", child.id),
64
70
  getActiveScreenChildren(focusableTree)
65
71
  );
66
72
 
@@ -69,7 +75,7 @@ export const getContentNode = (focusableTree) => {
69
75
 
70
76
  export const getNavbarNode = (focusableTree) => {
71
77
  const navbar = find(
72
- (child) => startsWith("navbar", child.id),
78
+ (child) => startsWith(QUICK_BRICK_NAVBAR, child.id),
73
79
  getActiveScreenChildren(focusableTree)
74
80
  );
75
81
 
@@ -0,0 +1,76 @@
1
+ import { focusManager } from "@applicaster/zapp-react-native-utils/appUtils/focusManager";
2
+ import { find, path } from "ramda";
3
+
4
+ import {
5
+ waitForActiveScreen,
6
+ waitForContent,
7
+ getContentNode,
8
+ getNavbarNode,
9
+ } from "./utils";
10
+
11
+ import { coreLogger } from "../../logger";
12
+
13
+ const logger = coreLogger.addSubsystem("focusManagerAux");
14
+
15
+ export async function setFocusOnMenu(currentRoute: string) {
16
+ const { setFocus, focusableTree } = focusManager;
17
+
18
+ try {
19
+ await waitForActiveScreen(currentRoute, focusableTree);
20
+
21
+ const navbar = getNavbarNode(focusableTree);
22
+
23
+ const selectedMenuItemId = find(
24
+ (child) => child.component.props.selected,
25
+ navbar.children
26
+ )?.id;
27
+
28
+ if (selectedMenuItemId) {
29
+ // set focus on selected menu item
30
+ setFocus(selectedMenuItemId);
31
+
32
+ return;
33
+ }
34
+
35
+ const firstMenuItemId = path(["children", 0, "id"], navbar);
36
+
37
+ if (firstMenuItemId) {
38
+ // set focus on first menu item
39
+ setFocus(firstMenuItemId);
40
+ }
41
+ } catch (error) {
42
+ logger.log({
43
+ message: "setFocusOnMenu – failed to put focus on top-menu",
44
+ data: {
45
+ currentRoute,
46
+ error,
47
+ },
48
+ });
49
+ }
50
+ }
51
+
52
+ export async function setFocusOnContent(currentRoute: string) {
53
+ // we need to put focus on content here
54
+ // For doing it we have to wait registration of first focusable node into focusableTree
55
+ // we wait until content node starts having children
56
+
57
+ const { setInitialFocus, focusableTree } = focusManager;
58
+
59
+ try {
60
+ await waitForActiveScreen(currentRoute, focusableTree);
61
+ await waitForContent(focusableTree);
62
+
63
+ const content = getContentNode(focusableTree);
64
+
65
+ const { success } = setInitialFocus(content);
66
+
67
+ if (!success) {
68
+ // we've failed to put initial focus on content, re-run "setFocusOnContent" again as a fallback
69
+ throw Error(
70
+ "we've failed to put initial focus on content, re-run 'setFocusOnContent' again as a fallback"
71
+ );
72
+ }
73
+ } catch (error) {
74
+ setFocusOnContent(currentRoute);
75
+ }
76
+ }
@@ -0,0 +1,100 @@
1
+ import { isNotNil } from "@applicaster/zapp-react-native-utils/reactUtils/helpers";
2
+ import { find, last, startsWith, pathOr } from "ramda";
3
+ import {
4
+ QUICK_BRICK_NAVBAR,
5
+ QUICK_BRICK_CONTENT,
6
+ } from "@applicaster/quick-brick-core/const";
7
+
8
+ // run check each 300 ms
9
+ // run this check too often could lead to performance penalty on low-end devices
10
+ const HOW_OFTEN_TO_CHECK_CONDITION = 300; // ms
11
+
12
+ type Props = {
13
+ maxTimeout: number;
14
+ conditionFn: () => boolean;
15
+ };
16
+
17
+ export const waitUntil = ({ maxTimeout, conditionFn }: Props) => {
18
+ let interval;
19
+ let timeout;
20
+
21
+ return new Promise((resolve, reject) => {
22
+ // run interval check
23
+ interval = setInterval(() => {
24
+ if (conditionFn()) {
25
+ clearInterval(interval);
26
+ clearTimeout(timeout);
27
+
28
+ resolve(undefined);
29
+ }
30
+ }, HOW_OFTEN_TO_CHECK_CONDITION);
31
+
32
+ // max timeout when we need to stop interval check
33
+ timeout = setTimeout(() => {
34
+ clearInterval(interval);
35
+ clearTimeout(timeout);
36
+
37
+ const error = Error(
38
+ "Failed to wait satisfy condition. Max timeout is reached."
39
+ );
40
+
41
+ reject(error);
42
+ }, maxTimeout);
43
+ });
44
+ };
45
+
46
+ export const waitForActiveScreen = (currentRoute: string, focusableTree) => {
47
+ const isActiveScreenAvailableToFocus = (): boolean => {
48
+ const routes = focusableTree.root.children;
49
+
50
+ const route = find((route) => route.id === currentRoute, routes);
51
+
52
+ return isNotNil(route);
53
+ };
54
+
55
+ return waitUntil({
56
+ maxTimeout: HOW_OFTEN_TO_CHECK_CONDITION * 10, // ms
57
+ conditionFn: isActiveScreenAvailableToFocus,
58
+ });
59
+ };
60
+
61
+ const getActiveScreenChildren = (focusableTree) => {
62
+ return pathOr([], ["children"], last(focusableTree.root.children));
63
+ };
64
+
65
+ export const getContentNode = (focusableTree) => {
66
+ const content = find(
67
+ (child) =>
68
+ startsWith(QUICK_BRICK_CONTENT, child.id) ||
69
+ startsWith("player", child.id),
70
+ getActiveScreenChildren(focusableTree)
71
+ );
72
+
73
+ return content;
74
+ };
75
+
76
+ export const getNavbarNode = (focusableTree) => {
77
+ const navbar = find(
78
+ (child) => startsWith(QUICK_BRICK_NAVBAR, child.id),
79
+ getActiveScreenChildren(focusableTree)
80
+ );
81
+
82
+ return navbar;
83
+ };
84
+
85
+ export const waitForContent = (focusableTree) => {
86
+ const contentHasAnyChildren = (): boolean => {
87
+ const countOfChildren = pathOr(
88
+ 0,
89
+ ["children", "length"],
90
+ getContentNode(focusableTree)
91
+ );
92
+
93
+ return countOfChildren > 0;
94
+ };
95
+
96
+ return waitUntil({
97
+ maxTimeout: HOW_OFTEN_TO_CHECK_CONDITION * 10, // ms
98
+ conditionFn: contentHasAnyChildren,
99
+ });
100
+ };
package/appUtils/index.js CHANGED
@@ -9,6 +9,8 @@ export { KEYS, ARROW_KEYS } from "./keyCodes/keys";
9
9
 
10
10
  export { playerManager } from "./playerManager";
11
11
 
12
+ export { accessibilityManager } from "./accessibilityManager";
13
+
12
14
  export function deprecationMessage(fn, replacement) {
13
15
  if (__DEV__ && process.env.NODE_ENV !== "test") {
14
16
  console.warn("/** DEPRECATION WARNING **/");
@@ -1,6 +1,7 @@
1
1
  /* global tizen */
2
2
  const isTizen = typeof tizen !== "undefined";
3
3
 
4
+ import { isVizioPlatform } from "@applicaster/zapp-react-native-utils/reactUtils";
4
5
  import * as platformKeys from "./keysByPlatform";
5
6
  import { Platform } from "react-native";
6
7
 
@@ -21,6 +22,13 @@ export const ARROW_KEYS = [
21
22
  KEYS.ArrowRight,
22
23
  ];
23
24
 
25
+ /**
26
+ * Platform.OS currently identifies Vizio as LG_TV, therefore we are using isVizioPlatform to identify platform and add keys.
27
+ */
28
+ if (isVizioPlatform()) {
29
+ Object.assign(KEYS, platformKeys.vizio);
30
+ }
31
+
24
32
  /**
25
33
  * In TizenOS we need to register all of the keys that we are going to use.
26
34
  * You need to register using the name Tizen has for the key.
@@ -102,4 +102,14 @@ const tvos = {
102
102
  Menu: { code: "Menu", eventType: "menu" },
103
103
  };
104
104
 
105
- export { lg_tv, samsung_tv, web, tvos, android_tv };
105
+ const vizio = {
106
+ ...samsung_tv,
107
+ Exit: { code: "Exit", keyCode: 27 },
108
+ PageUp: { code: "PageUp", keyCode: 33 },
109
+ PageDown: { code: "PageDown", keyCode: 34 },
110
+ MediaTrackNext: { code: "MediaTrackNext", keyCode: 418 },
111
+ MediaTrackPrevious: { code: "MediaTrackPrevious", keyCode: 419 },
112
+ PrevCh: { code: "PrevCh", keyCode: 500 },
113
+ };
114
+
115
+ export { lg_tv, samsung_tv, web, tvos, android_tv, vizio };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Platforms found in userAgent string, please note that simulators
3
+ * might not have the same agents that the devices have
4
+ */
5
+ export const PLATFORMS = {
6
+ webos: "Web0S",
7
+ tizen: "Tizen",
8
+ samsung: "SMART-TV",
9
+ mac: "Mac",
10
+ win: "Win",
11
+ linux: "Linux",
12
+ iphone: "iPhone",
13
+ android: "Android",
14
+ mobile: "Mobile",
15
+ vizio: "VIZIO",
16
+ smartcast: "SmartCast",
17
+ conjure: "Conjure",
18
+ };
19
+
20
+ /**
21
+ * Keys used to store the platform / device state in the session storage
22
+ */
23
+ export const PLATFORM_KEYS = {
24
+ deviceClosedCaptioningEnabled: "device_closed_captioning_enabled",
25
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./platformUtils";
2
+
3
+ export * from "./const";
@@ -0,0 +1,217 @@
1
+ import { BehaviorSubject } from "rxjs";
2
+
3
+ import { sessionStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/SessionStorage";
4
+ import {
5
+ isLgPlatform,
6
+ isSamsungPlatform,
7
+ } from "@applicaster/zapp-react-dom-app/App/Loader/utils/platform";
8
+
9
+ import { PLATFORM_KEYS, PLATFORMS } from "./const";
10
+ import { createLogger, utilsLogger } from "../../logger";
11
+
12
+ declare global {
13
+ interface Window {
14
+ VIZIO?: {
15
+ exitApplication: () => void;
16
+ Chromevox: {
17
+ play: (text: string) => void;
18
+ };
19
+ setClosedCaptionHandler: (
20
+ callback: (isCCEnabled: boolean) => void
21
+ ) => void;
22
+ };
23
+ }
24
+ }
25
+
26
+ const { log_debug } = createLogger({
27
+ category: "General",
28
+ subsystem: "PlatformUtils",
29
+ parent: utilsLogger,
30
+ });
31
+
32
+ export const getUserAgent = () =>
33
+ window?.navigator?.userAgent || global?.navigator?.userAgent;
34
+
35
+ /** VIZIO UTILS */
36
+
37
+ /**
38
+ * Checks if we are on the Vizio platform by using the userAgent string
39
+ *
40
+ * Use this method when you need to identify the platform on runtime
41
+ * before the VIZIO Companion Library has loaded
42
+ */
43
+ export const isVizioPlatform = () => {
44
+ const userAgent = getUserAgent();
45
+
46
+ const isVizioAgent =
47
+ userAgent.includes(PLATFORMS.vizio) ||
48
+ userAgent.includes(PLATFORMS.smartcast) ||
49
+ userAgent.includes(PLATFORMS.conjure);
50
+
51
+ return isVizioAgent;
52
+ };
53
+
54
+ /**
55
+ * Checks if we are on the Vizio platform by checking for the global VIZIO object
56
+ * and the VIZIO Companion Library. This method does not use the userAgent string,
57
+ * because it mainly checks whether we have access to apis, which may not be available yet
58
+ */
59
+ export const hasVizioAPIs = () => {
60
+ const hasAPIs =
61
+ typeof window.VIZIO !== "undefined" &&
62
+ window?.applicaster?.vizioLibraryDidLoad;
63
+
64
+ return hasAPIs;
65
+ };
66
+
67
+ /**
68
+ * Manages device based closed captioning state
69
+ * Returns an observable that emits the current state and updates whenever it changes
70
+ */
71
+ export class ClosedCaptioningManager {
72
+ private ccState$ = new BehaviorSubject<boolean>(false);
73
+ private static ccManagerInstance: ClosedCaptioningManager;
74
+
75
+ private constructor() {
76
+ this.initialize();
77
+ }
78
+
79
+ static getInstance(): ClosedCaptioningManager {
80
+ if (!ClosedCaptioningManager.ccManagerInstance) {
81
+ ClosedCaptioningManager.ccManagerInstance = new ClosedCaptioningManager();
82
+ }
83
+
84
+ return ClosedCaptioningManager.ccManagerInstance;
85
+ }
86
+
87
+ async initialize() {
88
+ const existingState = await sessionStorage.getItem(
89
+ PLATFORM_KEYS.deviceClosedCaptioningEnabled
90
+ );
91
+
92
+ log_debug("Initializing closed captioning manager", { existingState });
93
+
94
+ if (existingState !== null) {
95
+ this.ccState$.next(existingState === "true" || existingState === true);
96
+ }
97
+
98
+ if (hasVizioAPIs()) {
99
+ window.VIZIO!.setClosedCaptionHandler((isCCEnabled) => {
100
+ log_debug("CC state changed", { isCCEnabled });
101
+
102
+ sessionStorage.setItem(
103
+ PLATFORM_KEYS.deviceClosedCaptioningEnabled,
104
+ isCCEnabled
105
+ );
106
+
107
+ this.ccState$.next(isCCEnabled);
108
+ });
109
+ }
110
+ }
111
+
112
+ getCurrentState(): boolean {
113
+ return this.ccState$.getValue();
114
+ }
115
+
116
+ getStateAsObservable() {
117
+ return this.ccState$.asObservable();
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Subscribe to closed captioning state changes
123
+ * @returns Observable<boolean>
124
+ * @example
125
+ * const subscription = getClosedCaptionState().subscribe(
126
+ * isEnabled => setClosedCaptionState(isEnabled)
127
+ * );
128
+ * subscription.unsubscribe(), []);
129
+ */
130
+ export const getClosedCaptionState = () => {
131
+ const ccManager = ClosedCaptioningManager.getInstance();
132
+
133
+ return ccManager.getStateAsObservable();
134
+ };
135
+
136
+ /**
137
+ * Manages device based closed captioning state
138
+ * Returns an observable that emits the current state and updates whenever it changes
139
+ */
140
+ export class TTSManager {
141
+ private ttsState$ = new BehaviorSubject<boolean>(false);
142
+ private static ttsManagerInstance: TTSManager;
143
+
144
+ private constructor() {
145
+ this.initialize();
146
+ }
147
+
148
+ static getInstance(): TTSManager {
149
+ if (!TTSManager.ttsManagerInstance) {
150
+ TTSManager.ttsManagerInstance = new TTSManager();
151
+ }
152
+
153
+ return TTSManager.ttsManagerInstance;
154
+ }
155
+
156
+ async initialize() {
157
+ if (!isVizioPlatform()) return;
158
+
159
+ document.addEventListener(
160
+ "VIZIO_TTS_ENABLED",
161
+ () => {
162
+ this.ttsState$.next(true);
163
+ },
164
+ false
165
+ );
166
+
167
+ document.addEventListener(
168
+ "VIZIO_TTS_DISABLED",
169
+ () => {
170
+ this.ttsState$.next(false);
171
+ },
172
+ false
173
+ );
174
+ }
175
+
176
+ getCurrentState(): boolean {
177
+ return this.ttsState$.getValue();
178
+ }
179
+
180
+ getStateAsObservable() {
181
+ return this.ttsState$.asObservable();
182
+ }
183
+
184
+ readText(text: string) {
185
+ if (isSamsungPlatform() && window.speechSynthesis) {
186
+ const utterance = new SpeechSynthesisUtterance(text);
187
+ window.speechSynthesis.speak(utterance);
188
+ }
189
+
190
+ if (isLgPlatform() && window.webOS?.service) {
191
+ try {
192
+ window.webOS.service.request("luna://com.webos.service.tts", {
193
+ method: "speak",
194
+ onFailure(error: any) {
195
+ log_debug("There was a failure setting up webOS TTS service", {
196
+ error,
197
+ });
198
+ },
199
+ onSuccess(response: any) {
200
+ log_debug("webOS TTS service is configured successfully", {
201
+ response,
202
+ });
203
+ },
204
+ parameters: {
205
+ text,
206
+ },
207
+ });
208
+ } catch (error) {
209
+ log_debug("webOS TTS service error", { error });
210
+ }
211
+ }
212
+
213
+ if (!window.VIZIO?.Chromevox) return;
214
+
215
+ window.VIZIO.Chromevox.play(text);
216
+ }
217
+ }
@@ -7,6 +7,7 @@ export enum SharedPlayerCallBacksKeys {
7
7
  OnPlayerPause = "onPlayerPause",
8
8
  OnLoad = "onLoad",
9
9
  OnError = "onError",
10
+ OnVideoError = "onVideoError",
10
11
  OnVideoEnd = "onVideoEnd",
11
12
  OnPlaybackRateChange = "onPlaybackRateChange",
12
13
  OnVideoLoad = "onVideoLoad",