@applicaster/zapp-react-native-utils 14.0.0-alpha.1430213104 → 14.0.0-alpha.1661204539
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.
- package/actionsExecutor/ActionExecutorContext.tsx +53 -82
- package/actionsExecutor/ScreenActions.ts +90 -0
- package/actionsExecutor/StorageActions.ts +110 -0
- package/actionsExecutor/screenResolver.ts +8 -0
- package/appUtils/contextKeysManager/contextResolver.ts +15 -1
- package/package.json +2 -2
- package/playerUtils/index.ts +0 -6
- package/reactHooks/cell-click/index.ts +5 -1
- package/reactHooks/feed/useFeedLoader.tsx +13 -6
- package/storage/ScreenSingleValueProvider.ts +92 -0
- package/storage/ScreenStateMultiSelectProvider.ts +119 -0
- package/storage/StorageMultiSelectProvider.ts +192 -0
- package/storage/StorageSingleSelectProvider.ts +108 -0
|
@@ -16,7 +16,6 @@ import { QUICK_BRICK_EVENTS } from "@applicaster/zapp-react-native-bridge/QuickB
|
|
|
16
16
|
import { showConfirmationDialog } from "../alertUtils";
|
|
17
17
|
import { createCloudEvent, sendCloudEvent } from "../cloudEventsUtils";
|
|
18
18
|
import { createLogger } from "../logger";
|
|
19
|
-
import { StorageMultiSelectProvider } from "@applicaster/zapp-react-native-bridge/ZappStorage/StorageMultiSelectProvider";
|
|
20
19
|
import { ACTIVE_LAYOUT_ID_STORAGE_KEY } from "@applicaster/quick-brick-core/App/remoteContextReloader/consts";
|
|
21
20
|
import { appStore } from "@applicaster/zapp-react-native-redux/AppStore";
|
|
22
21
|
import { loadPipesData } from "@applicaster/zapp-react-native-redux/ZappPipes";
|
|
@@ -25,15 +24,20 @@ import {
|
|
|
25
24
|
resolveObjectValues,
|
|
26
25
|
} from "../appUtils/contextKeysManager/contextResolver";
|
|
27
26
|
import { useNavigation } from "../reactHooks";
|
|
28
|
-
import { get } from "lodash";
|
|
29
27
|
|
|
30
28
|
import {
|
|
31
29
|
useContentTypes,
|
|
32
30
|
usePickFromState,
|
|
33
31
|
} from "@applicaster/zapp-react-native-redux/hooks";
|
|
34
|
-
import {
|
|
35
|
-
import { postEvent, useSubscriberFor } from "../reactHooks/useSubscriberFor";
|
|
32
|
+
import { useSubscriberFor } from "../reactHooks/useSubscriberFor";
|
|
36
33
|
import { APP_EVENTS } from "../appUtils/events";
|
|
34
|
+
import {
|
|
35
|
+
localStorageToggleFlag,
|
|
36
|
+
sessionStorageToggleFlag,
|
|
37
|
+
} from "./StorageActions";
|
|
38
|
+
|
|
39
|
+
import { ScreenSingleValueProvider } from "@applicaster/zapp-react-native-utils/storage/ScreenSingleValueProvider";
|
|
40
|
+
import { screenSetVariable, screenToggleFlag } from "./ScreenActions";
|
|
37
41
|
|
|
38
42
|
export const { log_error, log_info, log_debug } = createLogger({
|
|
39
43
|
subsystem: "ActionExecutorContext",
|
|
@@ -82,19 +86,6 @@ function findParentComponent(
|
|
|
82
86
|
return null;
|
|
83
87
|
}
|
|
84
88
|
|
|
85
|
-
// send all data just in case (like for message string formatting)
|
|
86
|
-
// Type is not exported for now
|
|
87
|
-
type MaxTagsReachedEvent = {
|
|
88
|
-
selectedItems: string[];
|
|
89
|
-
maxItems: number;
|
|
90
|
-
tag: string;
|
|
91
|
-
keyNamespace: string;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
async function onMaxTagsReached(data: MaxTagsReachedEvent) {
|
|
95
|
-
postEvent(TOGGLE_FLAG_MAX_ITEMS_REACHED_EVENT, [data]);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
89
|
const prepareDefaultActions = (actionExecutor) => {
|
|
99
90
|
actionExecutor.registerAction("localStorageSet", async (action) => {
|
|
100
91
|
const namespaces = action.options.content;
|
|
@@ -178,10 +169,16 @@ const prepareDefaultActions = (actionExecutor) => {
|
|
|
178
169
|
|
|
179
170
|
const entry = context?.entry || {};
|
|
180
171
|
const entryResolver = new EntryResolver(entry);
|
|
172
|
+
const route = context.screenRoute;
|
|
173
|
+
const screenData = ScreenSingleValueProvider.getState(route) as any;
|
|
174
|
+
const screenResolver = new EntryResolver(screenData || {});
|
|
181
175
|
|
|
182
176
|
const data =
|
|
183
177
|
options?.data && options.inflateData
|
|
184
|
-
? await resolveObjectValues(options.data, {
|
|
178
|
+
? await resolveObjectValues(options.data, {
|
|
179
|
+
entry: entryResolver,
|
|
180
|
+
screen: screenResolver,
|
|
181
|
+
})
|
|
185
182
|
: options?.data || entry;
|
|
186
183
|
|
|
187
184
|
const cloudEvent = await createCloudEvent({
|
|
@@ -216,80 +213,54 @@ const prepareDefaultActions = (actionExecutor) => {
|
|
|
216
213
|
});
|
|
217
214
|
|
|
218
215
|
actionExecutor.registerAction(
|
|
219
|
-
"
|
|
216
|
+
"sessionStorageToggleFlag",
|
|
220
217
|
async (
|
|
221
218
|
action: ActionType,
|
|
222
219
|
context?: Record<string, any>
|
|
223
220
|
): Promise<ActionResult> => {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
return ActionResult.Error;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const entry = context?.entry as ZappEntry;
|
|
233
|
-
|
|
234
|
-
if (!entry) {
|
|
235
|
-
log_error(
|
|
236
|
-
"handleAction: localStorageToggleFlag action missing entry. Entry is required to get the tag."
|
|
237
|
-
);
|
|
238
|
-
|
|
239
|
-
return ActionResult.Error;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
const tag = action.options?.selector
|
|
243
|
-
? get(entry, action.options.selector)
|
|
244
|
-
: (entry.extensions?.tag ?? entry.id);
|
|
245
|
-
|
|
246
|
-
const keyNamespace = action.options?.key;
|
|
247
|
-
|
|
248
|
-
if (keyNamespace && tag) {
|
|
249
|
-
const multiSelectProvider =
|
|
250
|
-
StorageMultiSelectProvider.getProvider(keyNamespace);
|
|
251
|
-
|
|
252
|
-
const selectedItems = await multiSelectProvider.getSelectedAsync();
|
|
253
|
-
const isTagInSelectedItems = selectedItems.includes(tag);
|
|
254
|
-
|
|
255
|
-
log_info(
|
|
256
|
-
`handleAction: localStorageToggleFlag event will ${
|
|
257
|
-
isTagInSelectedItems ? "remove" : "add"
|
|
258
|
-
} tag: ${tag} for keyNamespace: ${keyNamespace}, current selectedItems: ${selectedItems}`
|
|
259
|
-
);
|
|
260
|
-
|
|
261
|
-
if (selectedItems.includes(tag)) {
|
|
262
|
-
await multiSelectProvider.removeItem(tag);
|
|
263
|
-
} else {
|
|
264
|
-
const maxItems = action.options?.max_items;
|
|
221
|
+
return await sessionStorageToggleFlag(context, action);
|
|
222
|
+
}
|
|
223
|
+
);
|
|
265
224
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
225
|
+
actionExecutor.registerAction(
|
|
226
|
+
"localStorageToggleFlag",
|
|
227
|
+
async (
|
|
228
|
+
action: ActionType,
|
|
229
|
+
context?: Record<string, any>
|
|
230
|
+
): Promise<ActionResult> => {
|
|
231
|
+
return await localStorageToggleFlag(context, action);
|
|
232
|
+
}
|
|
233
|
+
);
|
|
270
234
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
235
|
+
actionExecutor.registerAction(
|
|
236
|
+
"screenSetVariable",
|
|
237
|
+
async (
|
|
238
|
+
action: ActionType,
|
|
239
|
+
context?: Record<string, any>
|
|
240
|
+
): Promise<ActionResult> => {
|
|
241
|
+
const route = context?.screenRoute;
|
|
242
|
+
const { key, value } = action.options;
|
|
243
|
+
screenSetVariable(route, key, value);
|
|
277
244
|
|
|
278
|
-
|
|
279
|
-
|
|
245
|
+
return Promise.resolve(ActionResult.Success);
|
|
246
|
+
}
|
|
247
|
+
);
|
|
280
248
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
249
|
+
actionExecutor.registerAction(
|
|
250
|
+
"screenToggleFlag",
|
|
251
|
+
async (
|
|
252
|
+
action: ActionType,
|
|
253
|
+
context?: Record<string, any>
|
|
254
|
+
): Promise<ActionResult> => {
|
|
255
|
+
const screenRoute = context?.screenRoute;
|
|
288
256
|
|
|
289
|
-
|
|
290
|
-
|
|
257
|
+
await screenToggleFlag(
|
|
258
|
+
screenRoute,
|
|
259
|
+
{ entry: context?.entry, options: action.options },
|
|
260
|
+
action
|
|
261
|
+
);
|
|
291
262
|
|
|
292
|
-
return ActionResult.Success;
|
|
263
|
+
return Promise.resolve(ActionResult.Success);
|
|
293
264
|
}
|
|
294
265
|
);
|
|
295
266
|
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
2
|
+
|
|
3
|
+
import { log_error, log_info } from "./ActionExecutorContext";
|
|
4
|
+
import { ActionResult } from "./ActionExecutor";
|
|
5
|
+
import { get } from "lodash";
|
|
6
|
+
|
|
7
|
+
import { onMaxTagsReached } from "./StorageActions";
|
|
8
|
+
import { ScreenMultiSelectProvider } from "../storage/ScreenStateMultiSelectProvider";
|
|
9
|
+
|
|
10
|
+
export const screenSetVariable = (
|
|
11
|
+
_screenRoute: string,
|
|
12
|
+
_key: string,
|
|
13
|
+
_value: string
|
|
14
|
+
) => {};
|
|
15
|
+
|
|
16
|
+
export const screenToggleFlag = async (
|
|
17
|
+
screenRoute: string,
|
|
18
|
+
context: Record<string, any>,
|
|
19
|
+
action: ActionType
|
|
20
|
+
) => {
|
|
21
|
+
if (!context) {
|
|
22
|
+
log_error("handleAction: localStorageToggleFlag action missing context");
|
|
23
|
+
|
|
24
|
+
return ActionResult.Error;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const entry = context?.entry as ZappEntry;
|
|
28
|
+
|
|
29
|
+
if (!entry) {
|
|
30
|
+
log_error(
|
|
31
|
+
"handleAction: localStorageToggleFlag action missing entry. Entry is required to get the tag."
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return ActionResult.Error;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const tag = action.options?.selector
|
|
38
|
+
? get(entry, action.options.selector)
|
|
39
|
+
: (entry.extensions?.tag ?? entry.id);
|
|
40
|
+
|
|
41
|
+
const keyNamespace = action.options?.key;
|
|
42
|
+
|
|
43
|
+
if (keyNamespace && tag) {
|
|
44
|
+
const multiSelectProvider = ScreenMultiSelectProvider.getProvider(
|
|
45
|
+
keyNamespace,
|
|
46
|
+
screenRoute
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const selectedItems = await multiSelectProvider.getSelectedAsync();
|
|
50
|
+
const isTagInSelectedItems = selectedItems.includes(tag);
|
|
51
|
+
|
|
52
|
+
log_info(
|
|
53
|
+
`handleAction: screenToggleFlag event will ${
|
|
54
|
+
isTagInSelectedItems ? "remove" : "add"
|
|
55
|
+
} tag: ${tag} for keyNamespace: ${keyNamespace}, current selectedItems: ${selectedItems}`
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
if (selectedItems.includes(tag)) {
|
|
59
|
+
await multiSelectProvider.removeItem(tag);
|
|
60
|
+
} else {
|
|
61
|
+
const maxItems = action.options?.max_items;
|
|
62
|
+
|
|
63
|
+
if (maxItems && selectedItems.length >= maxItems) {
|
|
64
|
+
log_info(
|
|
65
|
+
`handleAction: screenToggleFlag event reached max items limit: ${maxItems}, cannot add tag: ${tag}`
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
await onMaxTagsReached({
|
|
69
|
+
selectedItems,
|
|
70
|
+
maxItems,
|
|
71
|
+
tag,
|
|
72
|
+
keyNamespace,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return ActionResult.Cancel;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
await multiSelectProvider.addItem(tag);
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
log_error(
|
|
82
|
+
"handleAction: screenToggleFlag event missing keyNamespace or tag",
|
|
83
|
+
{ keyNamespace, tag }
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
return ActionResult.Error;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return ActionResult.Success;
|
|
90
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { ActionResult } from "./ActionExecutor";
|
|
2
|
+
import { get } from "lodash";
|
|
3
|
+
import { StorageMultiSelectProvider } from "@applicaster/zapp-react-native-utils/storage/StorageMultiSelectProvider";
|
|
4
|
+
import { log_error, log_info } from "./ActionExecutorContext";
|
|
5
|
+
import { postEvent } from "../reactHooks/useSubscriberFor";
|
|
6
|
+
import { TOGGLE_FLAG_MAX_ITEMS_REACHED_EVENT } from "./consts";
|
|
7
|
+
import { StorageType } from "../appUtils/contextKeysManager/consts";
|
|
8
|
+
|
|
9
|
+
// send all data just in case (like for message string formatting)
|
|
10
|
+
// Type is not exported for now
|
|
11
|
+
type MaxTagsReachedEvent = {
|
|
12
|
+
selectedItems: string[];
|
|
13
|
+
maxItems: number;
|
|
14
|
+
tag: string;
|
|
15
|
+
keyNamespace: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export async function onMaxTagsReached(data: MaxTagsReachedEvent) {
|
|
19
|
+
postEvent(TOGGLE_FLAG_MAX_ITEMS_REACHED_EVENT, [data]);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function storageToggleFlag(
|
|
23
|
+
context: Record<string, any>,
|
|
24
|
+
action: ActionType,
|
|
25
|
+
storageType: StorageType
|
|
26
|
+
) {
|
|
27
|
+
if (!context) {
|
|
28
|
+
log_error("handleAction: localStorageToggleFlag action missing context");
|
|
29
|
+
|
|
30
|
+
return ActionResult.Error;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const entry = context?.entry as ZappEntry;
|
|
34
|
+
|
|
35
|
+
if (!entry) {
|
|
36
|
+
log_error(
|
|
37
|
+
"handleAction: localStorageToggleFlag action missing entry. Entry is required to get the tag."
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
return ActionResult.Error;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const tag = action.options?.selector
|
|
44
|
+
? get(entry, action.options.selector)
|
|
45
|
+
: (entry.extensions?.tag ?? entry.id);
|
|
46
|
+
|
|
47
|
+
const keyNamespace = action.options?.key;
|
|
48
|
+
|
|
49
|
+
if (keyNamespace && tag) {
|
|
50
|
+
const multiSelectProvider = StorageMultiSelectProvider.getProvider(
|
|
51
|
+
keyNamespace,
|
|
52
|
+
storageType
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const selectedItems = await multiSelectProvider.getSelectedAsync();
|
|
56
|
+
const isTagInSelectedItems = selectedItems.includes(tag);
|
|
57
|
+
|
|
58
|
+
log_info(
|
|
59
|
+
`handleAction: localStorageToggleFlag event will ${
|
|
60
|
+
isTagInSelectedItems ? "remove" : "add"
|
|
61
|
+
} tag: ${tag} for keyNamespace: ${keyNamespace}, current selectedItems: ${selectedItems}`
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (selectedItems.includes(tag)) {
|
|
65
|
+
await multiSelectProvider.removeItem(tag);
|
|
66
|
+
} else {
|
|
67
|
+
const maxItems = action.options?.max_items;
|
|
68
|
+
|
|
69
|
+
if (maxItems && selectedItems.length >= maxItems) {
|
|
70
|
+
log_info(
|
|
71
|
+
`handleAction: localStorageToggleFlag event reached max items limit: ${maxItems}, cannot add tag: ${tag}`
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
await onMaxTagsReached({
|
|
75
|
+
selectedItems,
|
|
76
|
+
maxItems,
|
|
77
|
+
tag,
|
|
78
|
+
keyNamespace,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return ActionResult.Cancel;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await multiSelectProvider.addItem(tag);
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
log_error(
|
|
88
|
+
"handleAction: localStorageToggleFlag event missing keyNamespace or tag",
|
|
89
|
+
{ keyNamespace, tag }
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return ActionResult.Error;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return ActionResult.Success;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function sessionStorageToggleFlag(
|
|
99
|
+
context: Record<string, any>,
|
|
100
|
+
action: ActionType
|
|
101
|
+
) {
|
|
102
|
+
return storageToggleFlag(context, action, StorageType.session);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function localStorageToggleFlag(
|
|
106
|
+
context: Record<string, any>,
|
|
107
|
+
action: ActionType
|
|
108
|
+
) {
|
|
109
|
+
return storageToggleFlag(context, action, StorageType.local);
|
|
110
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { ScreenStateResolver } from "../appUtils/contextKeysManager/contextResolver";
|
|
3
|
+
|
|
4
|
+
export const useScreenResolvers = (screenRoute: string) => {
|
|
5
|
+
return useMemo(() => {
|
|
6
|
+
return { screen: new ScreenStateResolver(screenRoute) };
|
|
7
|
+
}, [screenRoute]);
|
|
8
|
+
};
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { ContextKeysManager } from "./index";
|
|
2
2
|
import * as R from "ramda";
|
|
3
|
+
import { screenStates } from "@applicaster/zapp-react-native-utils/storage/ScreenSingleValueProvider";
|
|
3
4
|
|
|
4
|
-
interface IResolver {
|
|
5
|
+
export interface IResolver {
|
|
5
6
|
resolve: (string) => Promise<string | number | object>;
|
|
6
7
|
}
|
|
7
8
|
|
|
9
|
+
// TODO: Rename to ObjectKeyResolver or similar
|
|
8
10
|
export class EntryResolver implements IResolver {
|
|
9
11
|
entry: ZappEntry;
|
|
10
12
|
|
|
@@ -21,6 +23,18 @@ export class EntryResolver implements IResolver {
|
|
|
21
23
|
}
|
|
22
24
|
}
|
|
23
25
|
|
|
26
|
+
// TODO: Move to proper place
|
|
27
|
+
|
|
28
|
+
export class ScreenStateResolver implements IResolver {
|
|
29
|
+
constructor(private route: string) {}
|
|
30
|
+
|
|
31
|
+
async resolve(key: string) {
|
|
32
|
+
const screenState = screenStates[this.route];
|
|
33
|
+
|
|
34
|
+
return screenState?.[key];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
24
38
|
export class ContextResolver implements IResolver {
|
|
25
39
|
resolve = async (compositeKey: string) =>
|
|
26
40
|
ContextKeysManager.instance.getKey(compositeKey);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "14.0.0-alpha.
|
|
3
|
+
"version": "14.0.0-alpha.1661204539",
|
|
4
4
|
"description": "Applicaster Zapp React Native utilities package",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@applicaster/applicaster-types": "14.0.0-alpha.
|
|
30
|
+
"@applicaster/applicaster-types": "14.0.0-alpha.1661204539",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
package/playerUtils/index.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import * as R from "ramda";
|
|
2
2
|
import { playerManager } from "@applicaster/zapp-react-native-utils/appUtils/playerManager";
|
|
3
3
|
import { isString } from "@applicaster/zapp-react-native-utils/typeGuards";
|
|
4
|
-
import { isFilledArray } from "@applicaster/zapp-react-native-utils/arrayUtils";
|
|
5
|
-
import { isTV } from "@applicaster/zapp-react-native-utils/reactUtils";
|
|
6
4
|
|
|
7
5
|
import { getBoolFromConfigValue } from "../configurationUtils";
|
|
8
6
|
|
|
@@ -91,7 +89,3 @@ export const isAudioItem = (item: Option<ZappEntry>) => {
|
|
|
91
89
|
|
|
92
90
|
return false;
|
|
93
91
|
};
|
|
94
|
-
|
|
95
|
-
export const isInlineTV = (screenData) => {
|
|
96
|
-
return isTV() && isFilledArray(screenData?.ui_components);
|
|
97
|
-
};
|
|
@@ -16,7 +16,7 @@ import { ActionExecutorContext } from "@applicaster/zapp-react-native-utils/acti
|
|
|
16
16
|
import { isFunction, noop } from "../../functionUtils";
|
|
17
17
|
import { useSendAnalyticsOnPress } from "../analytics";
|
|
18
18
|
import { logOnPress, warnEmptyContentType } from "./helpers";
|
|
19
|
-
import { useCurrentScreenData } from "../screen";
|
|
19
|
+
import { useCurrentScreenData, useScreenContext } from "../screen";
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* If onCellTap is defined execute the function and
|
|
@@ -46,6 +46,7 @@ export const useCellClick = ({
|
|
|
46
46
|
const onCellTap: Option<Function> = React.useContext(CellTapContext);
|
|
47
47
|
const actionExecutor = React.useContext(ActionExecutorContext);
|
|
48
48
|
const screenData = useCurrentScreenData();
|
|
49
|
+
const screenState = useScreenContext()?.options;
|
|
49
50
|
|
|
50
51
|
const cellSelectable = toBooleanWithDefaultTrue(
|
|
51
52
|
component?.rules?.component_cells_selectable
|
|
@@ -83,6 +84,8 @@ export const useCellClick = ({
|
|
|
83
84
|
await actionExecutor?.handleEntryActions(selectedItem, {
|
|
84
85
|
component,
|
|
85
86
|
screenData,
|
|
87
|
+
screenState,
|
|
88
|
+
screenRoute: pathname,
|
|
86
89
|
});
|
|
87
90
|
}
|
|
88
91
|
|
|
@@ -117,6 +120,7 @@ export const useCellClick = ({
|
|
|
117
120
|
push,
|
|
118
121
|
sendAnalyticsOnPress,
|
|
119
122
|
screenData,
|
|
123
|
+
screenState,
|
|
120
124
|
]
|
|
121
125
|
);
|
|
122
126
|
|
|
@@ -8,6 +8,7 @@ import { reactHooksLogger } from "../logger";
|
|
|
8
8
|
import { shouldDispatchData, useIsInitialRender } from "../utils";
|
|
9
9
|
import { useInflatedUrl } from "./useInflatedUrl";
|
|
10
10
|
import { useRoute } from "../navigation";
|
|
11
|
+
import { useScreenResolvers } from "@applicaster/zapp-react-native-utils/actionsExecutor/screenResolver";
|
|
11
12
|
|
|
12
13
|
const logger = reactHooksLogger.addSubsystem("useFeedLoader");
|
|
13
14
|
|
|
@@ -50,7 +51,8 @@ export const useFeedLoader = ({
|
|
|
50
51
|
|
|
51
52
|
const isInitialRender = useIsInitialRender();
|
|
52
53
|
const dispatch = useDispatch();
|
|
53
|
-
const { screenData } = useRoute();
|
|
54
|
+
const { screenData, pathname } = useRoute();
|
|
55
|
+
const resolvers = useScreenResolvers(pathname);
|
|
54
56
|
|
|
55
57
|
const callableFeedUrl = useInflatedUrl({ feedUrl, mapping });
|
|
56
58
|
|
|
@@ -69,11 +71,12 @@ export const useFeedLoader = ({
|
|
|
69
71
|
silentRefresh,
|
|
70
72
|
callback,
|
|
71
73
|
riverId,
|
|
74
|
+
resolvers,
|
|
72
75
|
})
|
|
73
76
|
);
|
|
74
77
|
}
|
|
75
78
|
},
|
|
76
|
-
[callableFeedUrl]
|
|
79
|
+
[callableFeedUrl, resolvers]
|
|
77
80
|
);
|
|
78
81
|
|
|
79
82
|
const loadNext: FeedLoaderResponse["loadNext"] = React.useCallback(() => {
|
|
@@ -86,11 +89,12 @@ export const useFeedLoader = ({
|
|
|
86
89
|
silentRefresh: true,
|
|
87
90
|
parentFeed: callableFeedUrl,
|
|
88
91
|
riverId,
|
|
92
|
+
resolvers,
|
|
89
93
|
})
|
|
90
94
|
);
|
|
91
95
|
}
|
|
92
96
|
}
|
|
93
|
-
}, [callableFeedUrl, currentFeed?.data?.next]);
|
|
97
|
+
}, [callableFeedUrl, currentFeed?.data?.next, resolvers]);
|
|
94
98
|
|
|
95
99
|
useEffect(() => {
|
|
96
100
|
if (
|
|
@@ -102,6 +106,7 @@ export const useFeedLoader = ({
|
|
|
102
106
|
...pipesOptions,
|
|
103
107
|
clearCache: true,
|
|
104
108
|
riverId,
|
|
109
|
+
resolvers,
|
|
105
110
|
})
|
|
106
111
|
);
|
|
107
112
|
} else if (!callableFeedUrl) {
|
|
@@ -126,14 +131,16 @@ export const useFeedLoader = ({
|
|
|
126
131
|
jsOnly: true,
|
|
127
132
|
});
|
|
128
133
|
}
|
|
129
|
-
}, []);
|
|
134
|
+
}, [resolvers]);
|
|
130
135
|
|
|
131
136
|
// Reload feed when feedUrl changes, unless skipLoading is true
|
|
132
137
|
useEffect(() => {
|
|
133
138
|
if (!isInitialRender && callableFeedUrl && !pipesOptions.skipLoading) {
|
|
134
|
-
dispatch(
|
|
139
|
+
dispatch(
|
|
140
|
+
loadPipesData(callableFeedUrl, { ...pipesOptions, riverId, resolvers })
|
|
141
|
+
);
|
|
135
142
|
}
|
|
136
|
-
}, [callableFeedUrl]);
|
|
143
|
+
}, [callableFeedUrl, resolvers]);
|
|
137
144
|
|
|
138
145
|
return React.useMemo(() => {
|
|
139
146
|
if (!callableFeedUrl || !feedUrl) {
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { BehaviorSubject } from "rxjs";
|
|
2
|
+
import { log_debug, SingleValueProvider } from "./StorageSingleSelectProvider";
|
|
3
|
+
|
|
4
|
+
export const screenStates: Record<string, Record<string, string>> = {};
|
|
5
|
+
|
|
6
|
+
export class ScreenSingleValueProvider implements SingleValueProvider {
|
|
7
|
+
private static providers: Record<
|
|
8
|
+
string,
|
|
9
|
+
Record<string, SingleValueProvider>
|
|
10
|
+
> = {};
|
|
11
|
+
|
|
12
|
+
// TODO: Screen State should be stored outside of this class and should be shared with screen multi-select provider
|
|
13
|
+
public static getState(screenRoute: string): Record<string, string> {
|
|
14
|
+
return screenStates[screenRoute] || {};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public static getProvider(
|
|
18
|
+
key: string,
|
|
19
|
+
screenRoute: string
|
|
20
|
+
): SingleValueProvider {
|
|
21
|
+
if (!key) {
|
|
22
|
+
throw new Error("ScreenSingleValueProvider: Key is required");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!screenRoute) {
|
|
26
|
+
throw new Error("ScreenSingleValueProvider: Screen route is required");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!this.providers[screenRoute]) {
|
|
30
|
+
this.providers[screenRoute] = {};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!this.providers[screenRoute][key]) {
|
|
34
|
+
this.providers[screenRoute][key] = new ScreenSingleValueProvider(
|
|
35
|
+
key,
|
|
36
|
+
screenRoute
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!screenStates[screenRoute]) {
|
|
41
|
+
screenStates[screenRoute] = {};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return this.providers[screenRoute][key];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
private valueSubject: BehaviorSubject<string | null>;
|
|
49
|
+
p;
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
private constructor(
|
|
52
|
+
private key: string,
|
|
53
|
+
private route: string
|
|
54
|
+
) {
|
|
55
|
+
if (!key) {
|
|
56
|
+
throw new Error("ScreenSingleValueProvider: Key is required");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// localStorage.addListener?.({ key, namespace }, this.reloadValue);
|
|
60
|
+
|
|
61
|
+
this.valueSubject = new BehaviorSubject<string | null>(null);
|
|
62
|
+
|
|
63
|
+
void this.getValueAsync();
|
|
64
|
+
log_debug("ScreenSingleValueProvider: Initializing");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
clearValue(): Promise<void> {
|
|
68
|
+
delete [this.route][this.key];
|
|
69
|
+
this.valueSubject.next(null);
|
|
70
|
+
|
|
71
|
+
return Promise.resolve();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
getObservable(): BehaviorSubject<string | null> {
|
|
75
|
+
return this.valueSubject;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
getValue(): string | null {
|
|
79
|
+
return this.valueSubject.value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getValueAsync(): Promise<string | null> {
|
|
83
|
+
return Promise.resolve(this.valueSubject.value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
setValue(value: string): Promise<void> {
|
|
87
|
+
screenStates[this.route][this.key] = value;
|
|
88
|
+
this.valueSubject.next(value);
|
|
89
|
+
|
|
90
|
+
return Promise.resolve();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { log_debug, MultiSelectProvider } from "./StorageMultiSelectProvider";
|
|
2
|
+
import { BehaviorSubject } from "rxjs";
|
|
3
|
+
import { screenStates } from "./ScreenSingleValueProvider";
|
|
4
|
+
|
|
5
|
+
export class ScreenMultiSelectProvider implements MultiSelectProvider {
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
private itemSubject: BehaviorSubject<string[] | null>;
|
|
8
|
+
|
|
9
|
+
private static providers: Record<
|
|
10
|
+
string,
|
|
11
|
+
Record<string, MultiSelectProvider>
|
|
12
|
+
> = {};
|
|
13
|
+
|
|
14
|
+
public static getProvider(
|
|
15
|
+
key: string,
|
|
16
|
+
screenRoute: string
|
|
17
|
+
): MultiSelectProvider {
|
|
18
|
+
if (!key) {
|
|
19
|
+
throw new Error("ScreenSingleValueProvider: Key is required");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!screenRoute) {
|
|
23
|
+
throw new Error("ScreenSingleValueProvider: Screen route is required");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!this.providers[screenRoute]) {
|
|
27
|
+
this.providers[screenRoute] = {};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!this.providers[screenRoute][key]) {
|
|
31
|
+
this.providers[screenRoute][key] = new ScreenMultiSelectProvider(
|
|
32
|
+
key,
|
|
33
|
+
screenRoute
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!screenStates[screenRoute]) {
|
|
38
|
+
screenStates[screenRoute] = {};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return this.providers[screenRoute][key];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
private constructor(
|
|
46
|
+
private key: string,
|
|
47
|
+
private route: string
|
|
48
|
+
) {
|
|
49
|
+
if (!key) {
|
|
50
|
+
throw new Error("StorageMultiSelectProvider: Key is required");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.key = key;
|
|
54
|
+
// this.storage.addListener?.({ key, namespace }, this.reloadItems);
|
|
55
|
+
|
|
56
|
+
this.itemSubject = new BehaviorSubject([]);
|
|
57
|
+
|
|
58
|
+
void this.getSelectedAsync();
|
|
59
|
+
log_debug("StorageMultiSelectProvider: Initializing");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private getSetOfCurrentItems() {
|
|
63
|
+
const currentResult: string = screenStates[this.route][this.key];
|
|
64
|
+
|
|
65
|
+
return new Set(currentResult ? currentResult.split(",") : []);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
addItem(item: string): Promise<void> {
|
|
69
|
+
const items = this.getSetOfCurrentItems();
|
|
70
|
+
items.add(item);
|
|
71
|
+
screenStates[this.route][this.key] = Array.from(items).join(",");
|
|
72
|
+
this.itemSubject.next(Array.from(items) || []);
|
|
73
|
+
|
|
74
|
+
log_debug(
|
|
75
|
+
`addItem: item added: ${item}, current items: ${Array.from(items)}`
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
return Promise.resolve();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
addItems(_items: string[]): Promise<void> {
|
|
82
|
+
return Promise.resolve();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public getObservable = (): BehaviorSubject<string[]> => this.itemSubject;
|
|
86
|
+
|
|
87
|
+
getSelectedAsync(): Promise<string[]> {
|
|
88
|
+
const values = screenStates[this.route][this.key]?.split(",");
|
|
89
|
+
this.itemSubject.next(values || []);
|
|
90
|
+
|
|
91
|
+
return Promise.resolve(values || []);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
getSelectedItems(): string[] {
|
|
95
|
+
return this.itemSubject.value;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
removeAllItems(): Promise<void> {
|
|
99
|
+
return Promise.resolve();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
removeItem(item: string): Promise<void> {
|
|
103
|
+
const items = this.getSetOfCurrentItems();
|
|
104
|
+
items.delete(item);
|
|
105
|
+
|
|
106
|
+
screenStates[this.route][this.key] = Array.from(items).join(",");
|
|
107
|
+
this.itemSubject.next(Array.from(items) || []);
|
|
108
|
+
|
|
109
|
+
return Promise.resolve();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
removeItems(_items: string[]): Promise<void> {
|
|
113
|
+
return Promise.resolve();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
setSelectedItems(_items: string[]): Promise<void> {
|
|
117
|
+
return Promise.resolve();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { bridgeLogger } from "../../zapp-react-native-bridge/logger";
|
|
2
|
+
import { BehaviorSubject } from "rxjs";
|
|
3
|
+
import { Storage } from "@applicaster/zapp-react-native-bridge/ZappStorage/Storage";
|
|
4
|
+
import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
5
|
+
import { sessionStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/SessionStorage";
|
|
6
|
+
import { StorageType } from "../appUtils/contextKeysManager/consts";
|
|
7
|
+
import { getNamespaceAndKey } from "../appUtils/contextKeysManager/utils";
|
|
8
|
+
import { createLogger } from "../logger";
|
|
9
|
+
|
|
10
|
+
export const { log_verbose, log_debug, log_warning, log_info, log_error } =
|
|
11
|
+
createLogger({
|
|
12
|
+
category: "StorageMultiSelectProvider",
|
|
13
|
+
subsystem: "zapp-react-native-bridge",
|
|
14
|
+
parent: bridgeLogger,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export interface MultiSelectProvider {
|
|
18
|
+
getObservable(): BehaviorSubject<string[]>;
|
|
19
|
+
|
|
20
|
+
addItem(item: string): Promise<void>;
|
|
21
|
+
|
|
22
|
+
addItems(items: string[]): Promise<void>;
|
|
23
|
+
|
|
24
|
+
setSelectedItems(items: string[]): Promise<void>;
|
|
25
|
+
|
|
26
|
+
removeItem(item: string): Promise<void>;
|
|
27
|
+
|
|
28
|
+
removeItems(items: string[]): Promise<void>;
|
|
29
|
+
|
|
30
|
+
removeAllItems(): Promise<void>;
|
|
31
|
+
|
|
32
|
+
getSelectedItems(): string[];
|
|
33
|
+
|
|
34
|
+
getSelectedAsync(): Promise<string[]>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class StorageMultiSelectProvider implements MultiSelectProvider {
|
|
38
|
+
// TODO: Unsubscribe and remove when there is no listeners
|
|
39
|
+
private static multiSelectProviders: Record<
|
|
40
|
+
StorageType,
|
|
41
|
+
Record<string, StorageMultiSelectProvider>
|
|
42
|
+
> = {
|
|
43
|
+
[StorageType.local]: {},
|
|
44
|
+
[StorageType.session]: {},
|
|
45
|
+
// TODO: not implemented yet
|
|
46
|
+
[StorageType.secure]: {},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
public static getProvider(
|
|
50
|
+
keyNamespace: string,
|
|
51
|
+
storage: StorageType = StorageType.local
|
|
52
|
+
): MultiSelectProvider {
|
|
53
|
+
if (!this.multiSelectProviders[storage][keyNamespace]) {
|
|
54
|
+
const storageImpl =
|
|
55
|
+
storage === StorageType.session ? sessionStorage : localStorage;
|
|
56
|
+
|
|
57
|
+
this.multiSelectProviders[storage][keyNamespace] =
|
|
58
|
+
new StorageMultiSelectProvider(keyNamespace, storageImpl);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return this.multiSelectProviders[storage][keyNamespace];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private itemSubject: BehaviorSubject<string[] | null>;
|
|
65
|
+
|
|
66
|
+
private readonly key: string;
|
|
67
|
+
private readonly namespace: string;
|
|
68
|
+
|
|
69
|
+
private constructor(
|
|
70
|
+
keyNamespace: string,
|
|
71
|
+
private storage: Storage
|
|
72
|
+
) {
|
|
73
|
+
const { namespace, key } = getNamespaceAndKey(keyNamespace);
|
|
74
|
+
|
|
75
|
+
if (!key) {
|
|
76
|
+
throw new Error("StorageMultiSelectProvider: Key is required");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
this.key = key;
|
|
80
|
+
this.namespace = namespace;
|
|
81
|
+
this.storage.addListener?.({ key, namespace }, this.reloadItems);
|
|
82
|
+
|
|
83
|
+
this.itemSubject = new BehaviorSubject([]);
|
|
84
|
+
|
|
85
|
+
void this.getSelectedAsync();
|
|
86
|
+
log_debug("StorageMultiSelectProvider: Initializing");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private reloadItems = async ({ value }: StorageListenerArgs) => {
|
|
90
|
+
const selectedItems = value ? value.split(",") : [];
|
|
91
|
+
|
|
92
|
+
log_debug(
|
|
93
|
+
`reloadItems: request to reload items for value: ${value}`,
|
|
94
|
+
selectedItems
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
this.itemSubject.next(selectedItems);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
public getObservable = (): BehaviorSubject<string[]> => this.itemSubject;
|
|
101
|
+
|
|
102
|
+
private async getSetOfCurrentItems() {
|
|
103
|
+
const currentResult: string = await this.storage.getItem(
|
|
104
|
+
this.key,
|
|
105
|
+
this.namespace
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
return new Set(currentResult ? currentResult.split(",") : []);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private async updateItemsAndNotifyObservers(currentItems: Set<string>) {
|
|
112
|
+
const arrayOfItems = Array.from(currentItems);
|
|
113
|
+
const newValue = arrayOfItems.join(",");
|
|
114
|
+
await this.storage.setItem(this.key, newValue, this.namespace);
|
|
115
|
+
|
|
116
|
+
this.itemSubject.next(arrayOfItems);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public addItem = async (item: string): Promise<void> => {
|
|
120
|
+
const currentItems = await this.getSetOfCurrentItems();
|
|
121
|
+
currentItems.add(item);
|
|
122
|
+
log_debug(`addItem: Adding new item: ${item}`);
|
|
123
|
+
|
|
124
|
+
await this.updateItemsAndNotifyObservers(currentItems);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
public addItems = async (items: string[]): Promise<void> => {
|
|
128
|
+
const currentItems = await this.getSetOfCurrentItems();
|
|
129
|
+
|
|
130
|
+
items.forEach((item) => currentItems.add(item));
|
|
131
|
+
|
|
132
|
+
log_debug(
|
|
133
|
+
`addItems: Adding new items: ${items.join(
|
|
134
|
+
","
|
|
135
|
+
)}, current items: ${Array.from(currentItems).join(",")}`
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
await this.updateItemsAndNotifyObservers(currentItems);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
public removeItem = async (item: string): Promise<void> => {
|
|
142
|
+
const currentItems = await this.getSetOfCurrentItems();
|
|
143
|
+
currentItems.delete(item);
|
|
144
|
+
|
|
145
|
+
log_debug(
|
|
146
|
+
`removeItem: Removing item: ${item}, current items: ${Array.from(
|
|
147
|
+
currentItems
|
|
148
|
+
).join(",")}`
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
await this.updateItemsAndNotifyObservers(currentItems);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
public removeItems = async (items: string[]): Promise<void> => {
|
|
155
|
+
const currentItems = await this.getSetOfCurrentItems();
|
|
156
|
+
|
|
157
|
+
items.forEach((item) => currentItems.delete(item));
|
|
158
|
+
|
|
159
|
+
log_debug(
|
|
160
|
+
`removeItems: Removing items: ${items.join(
|
|
161
|
+
","
|
|
162
|
+
)}, current items: ${Array.from(currentItems).join(",")}`
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
await this.updateItemsAndNotifyObservers(currentItems);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
public removeAllItems = async (): Promise<void> => {
|
|
169
|
+
await this.storage.removeItem(this.key, this.namespace);
|
|
170
|
+
log_debug(`removeAllItems: Removing all items, current items: ${[]}`);
|
|
171
|
+
|
|
172
|
+
this.itemSubject.next([]);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
public getSelectedAsync = async (): Promise<string[]> => {
|
|
176
|
+
const value = await this.storage.getItem(this.key, this.namespace);
|
|
177
|
+
const selectedItems = value ? value.split(",") : [];
|
|
178
|
+
this.itemSubject.next(selectedItems);
|
|
179
|
+
|
|
180
|
+
return selectedItems;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
public getSelectedItems = (): string[] => {
|
|
184
|
+
return this.itemSubject.getValue();
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
setSelectedItems = async (items: string[]): Promise<void> => {
|
|
188
|
+
log_debug(`setSelectedItems: Setting selected items: ${items.join(",")}`);
|
|
189
|
+
|
|
190
|
+
await this.updateItemsAndNotifyObservers(new Set(items));
|
|
191
|
+
};
|
|
192
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { bridgeLogger } from "../../zapp-react-native-bridge/logger";
|
|
2
|
+
import { BehaviorSubject } from "rxjs";
|
|
3
|
+
import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
4
|
+
import { getNamespaceAndKey } from "../appUtils/contextKeysManager/utils";
|
|
5
|
+
import { createLogger } from "../logger";
|
|
6
|
+
|
|
7
|
+
export const { log_verbose, log_debug, log_warning, log_info, log_error } =
|
|
8
|
+
createLogger({
|
|
9
|
+
category: "StorageSingleValueProvider",
|
|
10
|
+
subsystem: "zapp-react-native-bridge",
|
|
11
|
+
parent: bridgeLogger,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export interface SingleValueProvider {
|
|
15
|
+
getObservable(): BehaviorSubject<string | null>;
|
|
16
|
+
|
|
17
|
+
setValue(value: string): Promise<void>;
|
|
18
|
+
getValue(): string | null;
|
|
19
|
+
getValueAsync(): Promise<string | null>;
|
|
20
|
+
clearValue(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface StorageListenerArgs {
|
|
24
|
+
value: string | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class StorageSingleValueProvider implements SingleValueProvider {
|
|
28
|
+
// Static cache of providers by namespace
|
|
29
|
+
private static singleValueProviders: Record<
|
|
30
|
+
string,
|
|
31
|
+
StorageSingleValueProvider
|
|
32
|
+
> = {};
|
|
33
|
+
|
|
34
|
+
public static getProvider(keyNamespace: string): SingleValueProvider {
|
|
35
|
+
if (!this.singleValueProviders[keyNamespace]) {
|
|
36
|
+
this.singleValueProviders[keyNamespace] = new StorageSingleValueProvider(
|
|
37
|
+
keyNamespace
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return this.singleValueProviders[keyNamespace];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private valueSubject: BehaviorSubject<string | null>;
|
|
45
|
+
|
|
46
|
+
private readonly key: string;
|
|
47
|
+
private readonly namespace: string;
|
|
48
|
+
|
|
49
|
+
private constructor(keyNamespace: string) {
|
|
50
|
+
const { namespace, key } = getNamespaceAndKey(keyNamespace);
|
|
51
|
+
|
|
52
|
+
if (!key) {
|
|
53
|
+
throw new Error("StorageSingleValueProvider: Key is required");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
this.key = key;
|
|
57
|
+
this.namespace = namespace;
|
|
58
|
+
localStorage.addListener?.({ key, namespace }, this.reloadValue);
|
|
59
|
+
|
|
60
|
+
this.valueSubject = new BehaviorSubject<string | null>(null);
|
|
61
|
+
|
|
62
|
+
void this.getValueAsync();
|
|
63
|
+
log_debug("StorageSingleValueProvider: Initializing");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private reloadValue = async ({ value }: StorageListenerArgs) => {
|
|
67
|
+
log_debug(`reloadValue: request to reload value: ${value}`);
|
|
68
|
+
|
|
69
|
+
this.valueSubject.next(value);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
public getObservable = (): BehaviorSubject<string | null> =>
|
|
73
|
+
this.valueSubject;
|
|
74
|
+
|
|
75
|
+
private async updateValueAndNotifyObservers(value: string | null) {
|
|
76
|
+
if (value === null) {
|
|
77
|
+
await localStorage.removeItem(this.key, this.namespace);
|
|
78
|
+
} else {
|
|
79
|
+
await localStorage.setItem(this.key, value, this.namespace);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.valueSubject.next(value);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public setValue = async (value: string): Promise<void> => {
|
|
86
|
+
log_debug(`setValue: Setting new value: ${value}`);
|
|
87
|
+
|
|
88
|
+
await this.updateValueAndNotifyObservers(value);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
public clearValue = async (): Promise<void> => {
|
|
92
|
+
await localStorage.removeItem(this.key, this.namespace);
|
|
93
|
+
log_debug("clearValue: Removing value");
|
|
94
|
+
|
|
95
|
+
this.valueSubject.next(null);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
public getValueAsync = async (): Promise<string | null> => {
|
|
99
|
+
const value = await localStorage.getItem(this.key, this.namespace);
|
|
100
|
+
this.valueSubject.next(value);
|
|
101
|
+
|
|
102
|
+
return value;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
public getValue = (): string | null => {
|
|
106
|
+
return this.valueSubject.getValue();
|
|
107
|
+
};
|
|
108
|
+
}
|