@applicaster/zapp-react-native-utils 13.0.8 → 13.0.9-alpha.1049520314

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 (32) hide show
  1. package/actionsExecutor/ActionExecutorContext.tsx +60 -84
  2. package/actionsExecutor/ScreenActions.ts +164 -0
  3. package/actionsExecutor/StorageActions.ts +110 -0
  4. package/actionsExecutor/feedDecorator.ts +171 -0
  5. package/actionsExecutor/screenResolver.ts +11 -0
  6. package/appUtils/HooksManager/Hook.ts +4 -4
  7. package/appUtils/HooksManager/index.ts +11 -1
  8. package/appUtils/accessibilityManager/const.ts +13 -0
  9. package/appUtils/accessibilityManager/hooks.ts +35 -1
  10. package/appUtils/accessibilityManager/index.ts +149 -28
  11. package/appUtils/accessibilityManager/utils.ts +24 -0
  12. package/appUtils/contextKeysManager/contextResolver.ts +42 -1
  13. package/appUtils/platform/platformUtils.ts +31 -1
  14. package/index.d.ts +3 -0
  15. package/package.json +2 -2
  16. package/playerUtils/PlayerTTS/PlayerTTS.ts +359 -0
  17. package/playerUtils/PlayerTTS/index.ts +1 -0
  18. package/playerUtils/usePlayerTTS.ts +21 -0
  19. package/reactHooks/cell-click/__tests__/index.test.js +3 -0
  20. package/reactHooks/cell-click/index.ts +8 -1
  21. package/reactHooks/feed/__tests__/useBatchLoading.test.tsx +3 -0
  22. package/reactHooks/feed/__tests__/useFeedLoader.test.tsx +71 -31
  23. package/reactHooks/feed/index.ts +2 -0
  24. package/reactHooks/feed/useBatchLoading.ts +13 -6
  25. package/reactHooks/feed/useFeedLoader.tsx +35 -33
  26. package/reactHooks/feed/useLoadPipesDataDispatch.ts +55 -0
  27. package/reactHooks/navigation/useRoute.ts +7 -2
  28. package/reactHooks/navigation/useScreenStateStore.ts +8 -0
  29. package/storage/ScreenSingleValueProvider.ts +208 -0
  30. package/storage/ScreenStateMultiSelectProvider.ts +293 -0
  31. package/storage/StorageMultiSelectProvider.ts +192 -0
  32. package/storage/StorageSingleSelectProvider.ts +108 -0
@@ -1,13 +1,12 @@
1
1
  import { complement, compose, isNil, map, min, prop, take, uniq } from "ramda";
2
- import { useDispatch } from "react-redux";
3
2
  import * as React from "react";
4
3
  import { useZappPipesFeeds } from "@applicaster/zapp-react-native-redux/hooks";
5
- import { loadPipesData } from "@applicaster/zapp-react-native-redux/ZappPipes";
6
4
  import { isNilOrEmpty } from "../../reactUtils/helpers";
7
5
  import { ZappPipesSearchContext } from "@applicaster/zapp-react-native-ui-components/Contexts";
8
6
  import {
9
7
  getInflatedDataSourceUrl,
10
8
  getSearchContext,
9
+ useLoadPipesDataDispatch,
11
10
  } from "@applicaster/zapp-react-native-utils/reactHooks";
12
11
  import { isGallery } from "@applicaster/zapp-react-native-utils/componentsUtils";
13
12
  import { useScreenContext } from "../screen/useScreenContext";
@@ -63,7 +62,6 @@ export const useBatchLoading = (
63
62
  componentsToRender: { data?: ZappDataSource; component_type: string }[],
64
63
  options: Options
65
64
  ) => {
66
- const dispatch = useDispatch();
67
65
  const { screen: screenContext, entry: entryContext } = useScreenContext();
68
66
  const [searchContext] = ZappPipesSearchContext.useZappPipesContext();
69
67
  const [hasEverBeenReady, setHasEverBeenReady] = React.useState(false);
@@ -120,6 +118,8 @@ export const useBatchLoading = (
120
118
 
121
119
  const feeds = useZappPipesFeeds(feedUrls);
122
120
 
121
+ const loadPipesDataDispatcher = useLoadPipesDataDispatch();
122
+
123
123
  // dispatch loadPipesData for each feed that is not loaded
124
124
  const runBatchLoading = React.useCallback(() => {
125
125
  batchComponents.forEach((rawData: any) => {
@@ -138,13 +138,20 @@ export const useBatchLoading = (
138
138
 
139
139
  if (mappedFeedUrl) {
140
140
  // 4. load data
141
- return dispatch(
142
- loadPipesData(mappedFeedUrl, { riverId: options.riverId })
141
+ return loadPipesDataDispatcher(
142
+ mappedFeedUrl,
143
+ {
144
+ riverId: options.riverId,
145
+ },
146
+ {
147
+ withResolvers: true,
148
+ withScreenRouteMapping: true,
149
+ }
143
150
  );
144
151
  }
145
152
  }
146
153
  });
147
- }, [feedUrls, feeds]);
154
+ }, [feedUrls, feeds, loadPipesDataDispatcher]);
148
155
 
149
156
  React.useEffect(() => {
150
157
  runBatchLoading();
@@ -1,13 +1,11 @@
1
1
  import React, { useEffect } from "react";
2
- import { useDispatch } from "react-redux";
3
2
 
4
- import { loadPipesData } from "@applicaster/zapp-react-native-redux/ZappPipes";
5
3
  import { useZappPipesFeed } from "@applicaster/zapp-react-native-redux/hooks";
6
4
 
7
5
  import { reactHooksLogger } from "../logger";
8
6
  import { shouldDispatchData, useIsInitialRender } from "../utils";
9
7
  import { useInflatedUrl } from "./useInflatedUrl";
10
- import { useRoute } from "../navigation";
8
+ import { useLoadPipesDataDispatch } from "./useLoadPipesDataDispatch";
11
9
 
12
10
  const logger = reactHooksLogger.addSubsystem("useFeedLoader");
13
11
 
@@ -49,29 +47,26 @@ export const useFeedLoader = ({
49
47
  }, []);
50
48
 
51
49
  const isInitialRender = useIsInitialRender();
52
- const dispatch = useDispatch();
53
- const { screenData } = useRoute();
54
50
 
55
51
  const callableFeedUrl = useInflatedUrl({ feedUrl, mapping });
56
52
 
57
53
  const currentFeed = useZappPipesFeed(callableFeedUrl);
58
54
 
59
- const riverId =
60
- (screenData as LegacyNavigationScreenData)?.targetScreen?.id ??
61
- screenData?.id;
55
+ const loadPipesDataDispatcher = useLoadPipesDataDispatch();
62
56
 
63
57
  const reloadData = React.useCallback<ReloadDataFunction>(
64
58
  (silentRefresh = true, callback) => {
65
- if (callableFeedUrl) {
66
- dispatch(
67
- loadPipesData(callableFeedUrl, {
68
- clearCache: true,
69
- silentRefresh,
70
- callback,
71
- riverId,
72
- })
73
- );
74
- }
59
+ loadPipesDataDispatcher(
60
+ callableFeedUrl,
61
+ {
62
+ clearCache: true,
63
+ silentRefresh,
64
+ callback,
65
+ },
66
+ {
67
+ withResolvers: true,
68
+ }
69
+ );
75
70
  },
76
71
  [callableFeedUrl]
77
72
  );
@@ -80,15 +75,16 @@ export const useFeedLoader = ({
80
75
  if (callableFeedUrl) {
81
76
  const nextFeed = currentFeed?.data?.next;
82
77
 
83
- if (nextFeed) {
84
- dispatch(
85
- loadPipesData(nextFeed, {
86
- silentRefresh: true,
87
- parentFeed: callableFeedUrl,
88
- riverId,
89
- })
90
- );
91
- }
78
+ loadPipesDataDispatcher(
79
+ nextFeed,
80
+ {
81
+ silentRefresh: true,
82
+ parentFeed: callableFeedUrl,
83
+ },
84
+ {
85
+ withResolvers: true,
86
+ }
87
+ );
92
88
  }
93
89
  }, [callableFeedUrl, currentFeed?.data?.next]);
94
90
 
@@ -97,12 +93,16 @@ export const useFeedLoader = ({
97
93
  shouldDispatchData(callableFeedUrl, currentFeed, pipesOptions.clearCache)
98
94
  ) {
99
95
  if (callableFeedUrl && !pipesOptions.skipLoading) {
100
- dispatch(
101
- loadPipesData(callableFeedUrl, {
96
+ loadPipesDataDispatcher(
97
+ callableFeedUrl,
98
+ {
102
99
  ...pipesOptions,
103
100
  clearCache: true,
104
- riverId,
105
- })
101
+ },
102
+ {
103
+ withResolvers: true,
104
+ withScreenRouteMapping: true,
105
+ }
106
106
  );
107
107
  } else if (!callableFeedUrl) {
108
108
  logger.info({
@@ -131,9 +131,11 @@ export const useFeedLoader = ({
131
131
  // Reload feed when feedUrl changes, unless skipLoading is true
132
132
  useEffect(() => {
133
133
  if (!isInitialRender && callableFeedUrl && !pipesOptions.skipLoading) {
134
- dispatch(loadPipesData(callableFeedUrl, { ...pipesOptions, riverId }));
134
+ loadPipesDataDispatcher(callableFeedUrl, pipesOptions, {
135
+ withResolvers: true,
136
+ });
135
137
  }
136
- }, [callableFeedUrl]);
138
+ }, [callableFeedUrl, isInitialRender, pipesOptions.skipLoading]);
137
139
 
138
140
  return React.useMemo(() => {
139
141
  if (!callableFeedUrl || !feedUrl) {
@@ -0,0 +1,55 @@
1
+ import * as ZappPipes from "@applicaster/zapp-react-native-redux/ZappPipes";
2
+ import { applyScreenRouteDefaults } from "@applicaster/zapp-react-native-redux/ZappPipes/feedProcessor";
3
+ import React from "react";
4
+ import { useScreenResolvers } from "../../actionsExecutor/screenResolver";
5
+ import { useRoute } from "../navigation";
6
+ import { useScreenStateStore } from "../navigation/useScreenStateStore";
7
+ import { useDispatch } from "react-redux";
8
+
9
+ export const useLoadPipesDataDispatch = () => {
10
+ const screenStateStore = useScreenStateStore();
11
+ const resolvers = useScreenResolvers();
12
+ const dispatch = useDispatch();
13
+
14
+ const { pathname, screenData } = useRoute();
15
+
16
+ const riverId =
17
+ (screenData as LegacyNavigationScreenData)?.targetScreen?.id ??
18
+ screenData?.id;
19
+
20
+ const onLoadCB = (options) => (data, _err) => {
21
+ options?.callback?.();
22
+
23
+ if (data) {
24
+ applyScreenRouteDefaults(data, screenStateStore, pathname);
25
+ }
26
+ };
27
+
28
+ return React.useCallback(
29
+ (
30
+ url: string,
31
+ options = {},
32
+ {
33
+ withResolvers = false,
34
+ withScreenRouteMapping = false,
35
+ }: {
36
+ withResolvers?: boolean;
37
+ withScreenRouteMapping?: boolean;
38
+ } = {}
39
+ ) => {
40
+ if (url) {
41
+ dispatch(
42
+ ZappPipes.loadPipesData(url, {
43
+ riverId,
44
+ resolvers: withResolvers ? resolvers : undefined,
45
+ ...options,
46
+ callback: withScreenRouteMapping
47
+ ? onLoadCB(options)
48
+ : options?.callback,
49
+ })
50
+ );
51
+ }
52
+ },
53
+ []
54
+ );
55
+ };
@@ -28,14 +28,19 @@ const isHookPathname = (pathname: string) => /^\/hooks\//.test(pathname);
28
28
 
29
29
  type VariousScreenData = LegacyNavigationScreenData | ZappRiver | ZappEntry;
30
30
 
31
- export const useRoute = (): {
31
+ export const useRoute = (
32
+ useLegacy = true
33
+ ): {
32
34
  screenData: VariousScreenData;
33
35
  pathname: string;
34
36
  } => {
35
37
  const pathname = usePathname() || "";
36
38
  const navigator = useNavigation();
39
+ const screenContext = useContext(ScreenDataContext);
37
40
 
38
- const screenDataContext = legacyScreenData(useContext(ScreenDataContext));
41
+ const screenDataContext = useLegacy
42
+ ? legacyScreenData(screenContext)
43
+ : screenContext;
39
44
 
40
45
  const { plugins, contentTypes, rivers } = usePickFromState([
41
46
  "plugins",
@@ -0,0 +1,8 @@
1
+ import { useScreenContextV2 } from "../screen/useScreenContext";
2
+ import { useMemo } from "react";
3
+
4
+ export const useScreenStateStore = () => {
5
+ const _stateStore = useScreenContextV2()._stateStore;
6
+
7
+ return useMemo(() => _stateStore, []);
8
+ };
@@ -0,0 +1,208 @@
1
+ import { BehaviorSubject } from "rxjs";
2
+ import { SingleValueProvider } from "./StorageSingleSelectProvider";
3
+ import { createLogger } from "../logger";
4
+ import { bridgeLogger } from "../../zapp-react-native-bridge/logger";
5
+ import { useScreenStateStore } from "../reactHooks/navigation/useScreenStateStore";
6
+
7
+ export const { log_debug, log_error } = createLogger({
8
+ category: "ScreenSingleValueProvider",
9
+ subsystem: "zapp-react-native-bridge",
10
+ parent: bridgeLogger,
11
+ });
12
+
13
+ export class ScreenSingleValueProvider implements SingleValueProvider {
14
+ // @ts-ignore
15
+ private valueSubject: BehaviorSubject<string | null>;
16
+ private screenStateStoreUnsubscribe: (() => void) | null = null;
17
+
18
+ private static providers: Record<
19
+ string,
20
+ Record<string, SingleValueProvider>
21
+ > = {};
22
+
23
+ public static getProvider(
24
+ key: string,
25
+ screenRoute: string,
26
+ screenStateStore: ReturnType<typeof useScreenStateStore>
27
+ ): SingleValueProvider {
28
+ if (!key) {
29
+ throw new Error("ScreenSingleValueProvider: Key is required");
30
+ }
31
+
32
+ if (!screenRoute) {
33
+ throw new Error("ScreenSingleValueProvider: Screen route is required");
34
+ }
35
+
36
+ if (!screenStateStore) {
37
+ throw new Error(
38
+ "ScreenSingleValueProvider: screen state store is required"
39
+ );
40
+ }
41
+
42
+ if (!this.providers[screenRoute]) {
43
+ this.providers[screenRoute] = {};
44
+ }
45
+
46
+ try {
47
+ if (!this.providers[screenRoute][key]) {
48
+ this.providers[screenRoute][key] = new ScreenSingleValueProvider(
49
+ key,
50
+ screenRoute,
51
+ screenStateStore
52
+ );
53
+ } else {
54
+ (
55
+ this.providers[screenRoute][key] as ScreenSingleValueProvider
56
+ ).updateStore(screenStateStore);
57
+ }
58
+ } catch (error) {
59
+ log_error("Failed to create provider", { key, screenRoute, error });
60
+ throw error;
61
+ }
62
+
63
+ return this.providers[screenRoute][key];
64
+ }
65
+
66
+ // @ts-ignore
67
+ private constructor(
68
+ private key: string,
69
+ route: string,
70
+ private screenStateStore: ReturnType<typeof useScreenStateStore>
71
+ ) {
72
+ if (!key) {
73
+ throw new Error("ScreenSingleValueProvider: Key is required");
74
+ }
75
+
76
+ if (!screenStateStore) {
77
+ throw new Error(
78
+ "ScreenSingleValueProvider: screen state store is required"
79
+ );
80
+ }
81
+
82
+ this.key = key;
83
+ this.valueSubject = new BehaviorSubject<string | null>(null);
84
+
85
+ this.setupScreenStateSubscription();
86
+
87
+ void this.getValueAsync();
88
+
89
+ log_debug("ScreenSingleValueProvider: Initializing", { key, route });
90
+ }
91
+
92
+ private updateStore(
93
+ screenStateStore: ReturnType<typeof useScreenStateStore>
94
+ ): void {
95
+ this.cleanup();
96
+ this.screenStateStore = screenStateStore;
97
+ this.setupScreenStateSubscription();
98
+ void this.getValueAsync();
99
+ }
100
+
101
+ private setupScreenStateSubscription(): void {
102
+ this.screenStateStoreUnsubscribe = this.screenStateStore.subscribe(
103
+ (state) => ({
104
+ value: state.data[this.key],
105
+ exists: this.key in state.data,
106
+ })
107
+ // (current, previous) => {
108
+ // if (!current.exists && previous.exists) {
109
+ // log_debug(
110
+ // `ScreenSingleValueProvider: Key deleted from store: ${this.key}`
111
+ // );
112
+
113
+ // // TODO: If we need to handle deletion, we can do it here
114
+ // }
115
+
116
+ // if (current.value !== previous.value) {
117
+ // this.valueSubject.next(current.value || null);
118
+
119
+ // log_debug(`ScreenSingleValueProvider: Key updated: ${this.key}`, {
120
+ // previous: previous.value,
121
+ // current: current.value,
122
+ // });
123
+ // }
124
+ // }
125
+ );
126
+
127
+ log_debug(
128
+ `ScreenSingleValueProvider: screen state store subscription setup for key: ${this.key}`
129
+ );
130
+ }
131
+
132
+ private cleanup(): void {
133
+ if (this.screenStateStoreUnsubscribe) {
134
+ this.screenStateStoreUnsubscribe();
135
+ this.screenStateStoreUnsubscribe = null;
136
+
137
+ log_debug(
138
+ `ScreenSingleValueProvider: Unsubscribed from screen state store for key: ${this.key}`
139
+ );
140
+ }
141
+ }
142
+
143
+ clearValue(): Promise<void> {
144
+ this.screenStateStore.getState().removeValue(this.key);
145
+ this.valueSubject.next(null);
146
+
147
+ log_debug(`clearValue: value cleared for key: ${this.key}`);
148
+
149
+ return Promise.resolve();
150
+ }
151
+
152
+ getObservable(): BehaviorSubject<string | null> {
153
+ return this.valueSubject;
154
+ }
155
+
156
+ getValue(): string | null {
157
+ return this.valueSubject.value;
158
+ }
159
+
160
+ getValueAsync(): Promise<string | null> {
161
+ const currentValue = this.screenStateStore.getState().data[this.key];
162
+ const value = currentValue || null;
163
+ const selected = this.getValue();
164
+ const valuesAreEqual = value === selected;
165
+
166
+ const bothEmpty =
167
+ (value === null || value === "") &&
168
+ (selected === null || selected === "");
169
+
170
+ if (!this.valueSubject.closed && !valuesAreEqual && !bothEmpty) {
171
+ this.valueSubject.next(value);
172
+ }
173
+
174
+ return Promise.resolve(value);
175
+ }
176
+
177
+ setValue(value: string): Promise<void> {
178
+ this.screenStateStore.getState().setValue(this.key, value);
179
+ this.valueSubject.next(value);
180
+
181
+ log_debug(`setValue: value set for key: ${this.key}`, { value });
182
+
183
+ return Promise.resolve();
184
+ }
185
+
186
+ // TODO: remove if not needed
187
+
188
+ public static cleanupRoute(screenRoute: string): void {
189
+ if (this.providers[screenRoute]) {
190
+ Object.values(this.providers[screenRoute]).forEach((provider) => {
191
+ (provider as ScreenSingleValueProvider).destroy();
192
+ });
193
+
194
+ delete this.providers[screenRoute];
195
+ }
196
+ }
197
+
198
+ // TODO: remove if not needed
199
+
200
+ public destroy(): void {
201
+ log_debug(
202
+ `ScreenSingleValueProvider: Destroying provider for key: ${this.key}`
203
+ );
204
+
205
+ this.valueSubject.complete();
206
+ this.cleanup();
207
+ }
208
+ }