@applicaster/zapp-react-native-utils 13.0.0-alpha.1632352572 → 13.0.0-alpha.1909212433

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.
@@ -0,0 +1,130 @@
1
+ const defaultPlatforms = {
2
+ tvos: "tvOS",
3
+ android_tv: "Android TV",
4
+ lg_tv: "LG TV",
5
+ samsung_tv: "Samsung TV",
6
+ };
7
+
8
+ const global_defaults = {
9
+ fontSize: 20,
10
+ lineHeight: 40,
11
+ letterSpacing: 0.4,
12
+ };
13
+
14
+ const generateFontConfiguration = (platforms = defaultPlatforms) => {
15
+ const platformEntries = Object.entries(platforms);
16
+ const defaultsEntries = Object.entries(global_defaults);
17
+ const splitCamelCase = (str) => str.replace(/([a-z](?=[A-Z]))/g, "$1_");
18
+
19
+ return platformEntries.reduce((acc, [platformKey, platformValue]) => {
20
+ acc.push(
21
+ ...defaultsEntries.reduce((acc2, [valueKey, defaultValue]) => {
22
+ acc2.push({
23
+ type: "number_input",
24
+ label: `${platformValue} ${splitCamelCase(valueKey).replace(
25
+ "_",
26
+ " "
27
+ )}`,
28
+ key: `${platformKey}_${splitCamelCase(valueKey)}`.toLowerCase(),
29
+ initial_value: defaultValue,
30
+ });
31
+
32
+ return acc2;
33
+ }, [])
34
+ );
35
+
36
+ return acc;
37
+ }, []);
38
+ };
39
+
40
+ const conditional_rule = (key, condition_value) => ({
41
+ key,
42
+ condition_value,
43
+ });
44
+
45
+ const createConditionalConfig = (rules, matchAll = false) => ({
46
+ rules: matchAll ? "all_conditions" : "conditional",
47
+ conditional_fields: rules.map((field) => conditional_rule(...field)),
48
+ });
49
+
50
+ const conditional_horizontal_type = createConditionalConfig([
51
+ ["styles/screen_picker_type", "horizontal"],
52
+ ]);
53
+
54
+ const conditional_vertical_type = createConditionalConfig([
55
+ ["styles/screen_picker_type", "vertical"],
56
+ ]);
57
+
58
+ const conditional_horizontal_type_item_fixed = createConditionalConfig(
59
+ [
60
+ ["styles/screen_picker_type", "horizontal"],
61
+ ["styles/screen_picker_item_size", "fixed"],
62
+ ],
63
+ true
64
+ );
65
+
66
+ const conditional_horizontal_type_item_dynamic = createConditionalConfig(
67
+ [
68
+ ["styles/screen_picker_type", "horizontal"],
69
+ ["screen_picker_horizontal_display_type", "dynamic"],
70
+ ],
71
+ true
72
+ );
73
+
74
+ const conditional_horizontal_display_type_fixed = createConditionalConfig(
75
+ [
76
+ ["styles/screen_picker_type", "horizontal"],
77
+ ["styles/screen_picker_horizontal_display_type", "fixed"],
78
+ ],
79
+ true
80
+ );
81
+
82
+ const conditional_horizontal_asset_on = createConditionalConfig(
83
+ [
84
+ ["styles/screen_picker_type", "horizontal"],
85
+ ["styles/screen_picker_asset_switch", true],
86
+ ],
87
+ true
88
+ );
89
+
90
+ const createVariationFields = ({
91
+ prefix = "screen_picker",
92
+ key,
93
+ type = "number_input",
94
+ label = "",
95
+ variants: [a, b],
96
+ }) => {
97
+ return [
98
+ {
99
+ type,
100
+ key: `${prefix}${prefix.length > 0 ? "_" : ""}${
101
+ a?.key ?? "vertical"
102
+ }_${key}`,
103
+ label: a?.label ?? label,
104
+ initial_value: a.initial_value,
105
+ ...conditional_vertical_type,
106
+ },
107
+ {
108
+ type,
109
+ key: `${prefix}${prefix.length > 0 ? "_" : ""}${
110
+ b.key ?? "horizontal"
111
+ }_${key}`,
112
+ label: b?.label ?? label,
113
+ initial_value: b.initial_value,
114
+ ...conditional_horizontal_type,
115
+ },
116
+ ];
117
+ };
118
+
119
+ module.exports = {
120
+ generateFontConfiguration,
121
+ createVariationFields,
122
+ createConditionalConfig,
123
+ conditional_rule,
124
+ conditional_horizontal_type,
125
+ conditional_vertical_type,
126
+ conditional_horizontal_type_item_fixed,
127
+ conditional_horizontal_type_item_dynamic,
128
+ conditional_horizontal_display_type_fixed,
129
+ conditional_horizontal_asset_on,
130
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "13.0.0-alpha.1632352572",
3
+ "version": "13.0.0-alpha.1909212433",
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": "13.0.0-alpha.1632352572",
31
+ "@applicaster/applicaster-types": "13.0.0-alpha.1909212433",
32
32
  "buffer": "^5.2.1",
33
33
  "camelize": "^1.0.0",
34
34
  "create-react-context": "^0.2.3",
@@ -0,0 +1,116 @@
1
+ import { act, renderHook } from "@testing-library/react-hooks";
2
+ import { playerManager } from "@applicaster/zapp-react-native-utils/appUtils/playerManager";
3
+
4
+ import { ON_HOLD_INTERVAL, SEEK_TYPE, SKIP_TIME_BASE } from "../const";
5
+
6
+ import { useAutoSeek } from "../useAutoSeek";
7
+
8
+ jest.useFakeTimers();
9
+
10
+ const rewind = jest.fn();
11
+ const forward = jest.fn();
12
+
13
+ jest
14
+ .spyOn(playerManager, "getInstanceController")
15
+ // @ts-ignore
16
+ .mockImplementation(() => ({ rewind, forward }));
17
+
18
+ describe("useAutoSeek", () => {
19
+ beforeEach(() => {
20
+ rewind.mockReset();
21
+ forward.mockReset();
22
+ });
23
+
24
+ describe("autoSeekStart", () => {
25
+ it("should call forward method on playerInstance", () => {
26
+ const { result } = renderHook(() => useAutoSeek());
27
+
28
+ act(() => {
29
+ result.current.autoSeekStart(SEEK_TYPE.FORWARD);
30
+ });
31
+
32
+ expect(forward).toBeCalled();
33
+ result.current.autoSeekStop();
34
+ });
35
+
36
+ it("should call rewind method on playerManager", () => {
37
+ const { result } = renderHook(() => useAutoSeek());
38
+
39
+ act(() => {
40
+ result.current.autoSeekStart(SEEK_TYPE.REWIND);
41
+ });
42
+
43
+ expect(rewind).toBeCalled();
44
+ result.current.autoSeekStop();
45
+ });
46
+
47
+ it("should call rewind with default skip time", () => {
48
+ const { result } = renderHook(() => useAutoSeek());
49
+
50
+ act(() => {
51
+ result.current.autoSeekStart(SEEK_TYPE.REWIND);
52
+ });
53
+
54
+ expect(rewind).toBeCalledWith(SKIP_TIME_BASE);
55
+ result.current.autoSeekStop();
56
+ });
57
+
58
+ it("should call forward with default skip time", () => {
59
+ const { result } = renderHook(() => useAutoSeek());
60
+
61
+ act(() => {
62
+ result.current.autoSeekStart(SEEK_TYPE.FORWARD);
63
+ });
64
+
65
+ expect(forward).toBeCalledWith(SKIP_TIME_BASE);
66
+ result.current.autoSeekStop();
67
+ });
68
+
69
+ it("should call rewind in intervals set in configuration", () => {
70
+ const { result } = renderHook(() => useAutoSeek());
71
+
72
+ act(() => {
73
+ result.current.autoSeekStart(SEEK_TYPE.REWIND);
74
+ });
75
+
76
+ expect(rewind).toBeCalled();
77
+ jest.advanceTimersByTime(ON_HOLD_INTERVAL * 3);
78
+ expect(rewind).toBeCalledTimes(4);
79
+ result.current.autoSeekStop();
80
+ });
81
+
82
+ it("should call forward in intervals set in configuration", () => {
83
+ const { result } = renderHook(() => useAutoSeek());
84
+
85
+ act(() => {
86
+ result.current.autoSeekStart(SEEK_TYPE.FORWARD);
87
+ });
88
+
89
+ expect(forward).toBeCalled();
90
+ jest.advanceTimersByTime(ON_HOLD_INTERVAL * 3);
91
+ expect(forward).toBeCalledTimes(4);
92
+ result.current.autoSeekStop();
93
+ });
94
+ });
95
+
96
+ describe("autoSeekStop", () => {
97
+ it("should stop calling playerManager method after autoSeekStop is called", () => {
98
+ const { result } = renderHook(() => useAutoSeek());
99
+
100
+ act(() => {
101
+ result.current.autoSeekStart(SEEK_TYPE.FORWARD);
102
+ });
103
+
104
+ expect(forward).toBeCalled();
105
+ jest.advanceTimersByTime(ON_HOLD_INTERVAL * 3);
106
+ expect(forward).toBeCalledTimes(4);
107
+
108
+ act(() => {
109
+ result.current.autoSeekStop();
110
+ });
111
+
112
+ jest.advanceTimersByTime(ON_HOLD_INTERVAL * 3);
113
+ expect(forward).toBeCalledTimes(4);
114
+ });
115
+ });
116
+ });
@@ -0,0 +1,94 @@
1
+ import { renderHook } from "@testing-library/react-hooks";
2
+ import { playerManager } from "@applicaster/zapp-react-native-utils/appUtils/playerManager";
3
+ import { useTapSeek } from "../useTapSeek";
4
+
5
+ import { ON_PRESS_RESET_DURATION, SEEK_TYPE, SKIP_TIME_BASE } from "../const";
6
+
7
+ const rewind = jest.fn();
8
+ const forward = jest.fn();
9
+
10
+ jest
11
+ .spyOn(playerManager, "getInstanceController")
12
+ // @ts-ignore
13
+ .mockImplementation(() => ({ rewind, forward }));
14
+
15
+ describe("useTapSeek", () => {
16
+ beforeEach(() => {
17
+ rewind.mockReset();
18
+ forward.mockReset();
19
+ });
20
+
21
+ describe("forward", () => {
22
+ it("should call forward method on playerManager", () => {
23
+ const { result } = renderHook(() => useTapSeek());
24
+
25
+ result.current(SEEK_TYPE.FORWARD);
26
+ expect(forward).toBeCalled();
27
+ });
28
+
29
+ it("should call forward with default skip time", () => {
30
+ const { result } = renderHook(() => useTapSeek());
31
+
32
+ result.current(SEEK_TYPE.FORWARD);
33
+ expect(forward).toBeCalledWith(SKIP_TIME_BASE);
34
+ });
35
+
36
+ it("should multiply skip time for every call", async () => {
37
+ const { result } = renderHook(() => useTapSeek());
38
+
39
+ result.current(SEEK_TYPE.FORWARD);
40
+ expect(forward).toBeCalledWith(SKIP_TIME_BASE);
41
+ result.current(SEEK_TYPE.FORWARD);
42
+ expect(forward).toBeCalledWith(SKIP_TIME_BASE * 2);
43
+ });
44
+
45
+ it("should reset multiplier if waiting more then resetDuration between press actions", () => {
46
+ const { result, waitFor } = renderHook(() => useTapSeek());
47
+
48
+ result.current(SEEK_TYPE.FORWARD);
49
+ expect(forward).toBeCalledWith(SKIP_TIME_BASE);
50
+ waitFor(jest.fn(), { timeout: ON_PRESS_RESET_DURATION });
51
+ result.current(SEEK_TYPE.FORWARD);
52
+ expect(forward).toBeCalledWith(SKIP_TIME_BASE);
53
+ });
54
+ });
55
+
56
+ describe("rewind", () => {
57
+ it("should call rewind method on playerManager", () => {
58
+ const { result } = renderHook(() => useTapSeek());
59
+
60
+ result.current(SEEK_TYPE.REWIND);
61
+ expect(rewind).toBeCalled();
62
+ });
63
+
64
+ it("should call rewind with default skip time", () => {
65
+ const { result } = renderHook(() => useTapSeek());
66
+
67
+ result.current(SEEK_TYPE.REWIND);
68
+ expect(rewind).toBeCalledWith(SKIP_TIME_BASE);
69
+ });
70
+
71
+ it("should multiply skip time for every call", async () => {
72
+ const { result } = renderHook(() => useTapSeek());
73
+
74
+ result.current(SEEK_TYPE.REWIND);
75
+ expect(rewind).toBeCalledWith(SKIP_TIME_BASE);
76
+ result.current(SEEK_TYPE.REWIND);
77
+ expect(rewind).toBeCalledWith(SKIP_TIME_BASE * 2);
78
+ });
79
+ });
80
+
81
+ describe("mixed usage", () => {
82
+ it("should reset timer if different type of event is emitted", () => {
83
+ const { result } = renderHook(() => useTapSeek());
84
+ result.current(SEEK_TYPE.REWIND);
85
+ expect(rewind).toBeCalledWith(SKIP_TIME_BASE);
86
+
87
+ result.current(SEEK_TYPE.REWIND);
88
+ expect(rewind).toBeCalledWith(SKIP_TIME_BASE * 2);
89
+
90
+ result.current(SEEK_TYPE.FORWARD);
91
+ expect(forward).toBeCalledWith(SKIP_TIME_BASE);
92
+ });
93
+ });
94
+ });
@@ -4,7 +4,7 @@ export const ON_PRESS_RESET_DURATION = 2000;
4
4
 
5
5
  export const ON_HOLD_INTERVAL = 500;
6
6
 
7
- export const MAX_SEEK_TIME = 60;
7
+ export const MAX_SEEK_TIME = 600;
8
8
 
9
9
  export enum SEEK_TYPE {
10
10
  FORWARD = "forward",
@@ -1 +1,7 @@
1
1
  export { useLanguageData } from "./useLanguageData";
2
+
3
+ export { useAutoSeek } from "./useAutoSeek";
4
+
5
+ export { useTapSeek } from "./useTapSeek";
6
+
7
+ export { useSeek, withUseSeek } from "./useSeek";
@@ -0,0 +1,87 @@
1
+ import React, { useEffect, useState } from "react";
2
+ import { usePrevious } from "@applicaster/zapp-react-native-utils/reactHooks/utils";
3
+ import { playerManager } from "@applicaster/zapp-react-native-utils/appUtils/playerManager";
4
+ import {
5
+ MAX_SEEK_TIME,
6
+ ON_HOLD_INTERVAL,
7
+ SEEK_TYPE,
8
+ SKIP_TIME_BASE,
9
+ } from "./const";
10
+ import { noop } from "../../functionUtils";
11
+
12
+ export const useAutoSeek = (resetHUDTimer = noop) => {
13
+ const [isRunning, setIsRunning] = useState<boolean>(false);
14
+ const [direction, setDirection] = useState<Nullable<SEEK_TYPE>>(null);
15
+ const [multiplier, setMultiplier] = useState(1);
16
+ const intervalTimer = React.useRef(null);
17
+ const prevRunning = usePrevious(isRunning);
18
+
19
+ const clearIntervalTimer = () => {
20
+ try {
21
+ clearInterval(intervalTimer.current);
22
+ } catch (err) {
23
+ intervalTimer.current = null;
24
+ }
25
+ };
26
+
27
+ const setIntervalTimer = (cb) => {
28
+ clearIntervalTimer();
29
+ intervalTimer.current = setInterval(cb, ON_HOLD_INTERVAL);
30
+ };
31
+
32
+ const resetState = () => {
33
+ setIsRunning(false);
34
+ setDirection(null);
35
+ setMultiplier(1);
36
+ clearIntervalTimer();
37
+ };
38
+
39
+ useEffect(() => {
40
+ const player = playerManager.getInstanceController();
41
+
42
+ if (!player) return;
43
+
44
+ if (direction) {
45
+ player[direction](Math.min(MAX_SEEK_TIME, SKIP_TIME_BASE * multiplier));
46
+ resetHUDTimer();
47
+ }
48
+ }, [multiplier, direction]);
49
+
50
+ useEffect(() => {
51
+ if (isRunning && isRunning !== prevRunning) {
52
+ setIntervalTimer(() => {
53
+ setMultiplier((prev) => {
54
+ return prev + 1;
55
+ });
56
+ });
57
+ } else if (!isRunning && isRunning !== prevRunning) {
58
+ resetState();
59
+ }
60
+
61
+ return () => {
62
+ clearIntervalTimer();
63
+ };
64
+ }, [isRunning, direction]);
65
+
66
+ const autoSeekStop = React.useCallback(() => {
67
+ resetState();
68
+ }, []);
69
+
70
+ const autoSeekStart = React.useCallback(
71
+ (eventType: SEEK_TYPE) => {
72
+ // Check if there is another session running
73
+ if (isRunning) {
74
+ autoSeekStop();
75
+ }
76
+
77
+ setDirection(eventType);
78
+ setIsRunning(true);
79
+ },
80
+ [isRunning]
81
+ );
82
+
83
+ return React.useMemo(
84
+ () => ({ autoSeekStart, autoSeekStop }),
85
+ [autoSeekStart, autoSeekStop]
86
+ );
87
+ };
@@ -0,0 +1,66 @@
1
+ import React from "react";
2
+ import { SEEK_TYPE } from "./const";
3
+ import { useTapSeek } from "./useTapSeek";
4
+ import { useAutoSeek } from "./useAutoSeek";
5
+ import { noop } from "../../functionUtils";
6
+
7
+ export const useSeek = (resetHUDTimer = noop) => {
8
+ const handlePressEvent = useTapSeek();
9
+ const { autoSeekStart, autoSeekStop } = useAutoSeek(resetHUDTimer);
10
+
11
+ const onForwardPress = () => {
12
+ handlePressEvent(SEEK_TYPE.FORWARD);
13
+ };
14
+
15
+ const onRewindPress = () => {
16
+ handlePressEvent(SEEK_TYPE.REWIND);
17
+ };
18
+
19
+ const onForwardPressIn = () => {
20
+ autoSeekStart(SEEK_TYPE.FORWARD);
21
+ };
22
+
23
+ const onForwardPressOut = () => {
24
+ autoSeekStop();
25
+ };
26
+
27
+ const onRewindPressIn = () => {
28
+ autoSeekStart(SEEK_TYPE.REWIND);
29
+ };
30
+
31
+ const onRewindLongPress = () => {
32
+ autoSeekStart(SEEK_TYPE.REWIND);
33
+ };
34
+
35
+ const onForwardLongPress = () => {
36
+ autoSeekStart(SEEK_TYPE.FORWARD);
37
+ };
38
+
39
+ const onRewindPressOut = () => {
40
+ autoSeekStop();
41
+ };
42
+
43
+ return React.useMemo(
44
+ () => ({
45
+ onForwardPress,
46
+ onRewindPress,
47
+ onForwardPressIn,
48
+ onForwardPressOut,
49
+ onRewindPressIn,
50
+ onRewindPressOut,
51
+ onRewindLongPress,
52
+ onForwardLongPress,
53
+ }),
54
+ [handlePressEvent, autoSeekStart]
55
+ );
56
+ };
57
+
58
+ export const withUseSeek = (Component) =>
59
+ Object.assign(
60
+ (props) => {
61
+ const seekMethods = useSeek(props?.resetHUDTimer);
62
+
63
+ return <Component {...props} seekMethods={seekMethods} />;
64
+ },
65
+ { displayName: "WithUseSeek" }
66
+ );
@@ -0,0 +1,58 @@
1
+ import { useCallback, useRef } from "react";
2
+ import * as R from "ramda";
3
+ import {
4
+ MAX_SEEK_TIME,
5
+ ON_PRESS_RESET_DURATION,
6
+ SEEK_TYPE,
7
+ SKIP_TIME_BASE,
8
+ } from "./const";
9
+ import { playerManager } from "@applicaster/zapp-react-native-utils/appUtils";
10
+
11
+ type SeekType = SEEK_TYPE | null;
12
+
13
+ const getSkipTime = (multiplier) => {
14
+ const skipTime = R.min(MAX_SEEK_TIME, SKIP_TIME_BASE * multiplier);
15
+
16
+ return skipTime;
17
+ };
18
+
19
+ export const useTapSeek = () => {
20
+ const onPressMultiplier = useRef(1);
21
+ const lastPressTime = useRef(0);
22
+ const currentOnPressEventType = useRef<SeekType>(null);
23
+
24
+ const resetMultiplier = useCallback(() => {
25
+ onPressMultiplier.current = 1;
26
+ }, []);
27
+
28
+ const handlePressEvent = useCallback(
29
+ (eventType) => {
30
+ const player = playerManager.getInstanceController();
31
+ if (!player) return;
32
+
33
+ const now = Date.now();
34
+ const timeSinceLastPress = now - lastPressTime.current;
35
+ const isSameEventType = eventType === currentOnPressEventType.current;
36
+
37
+ const isWithinResetDuration =
38
+ timeSinceLastPress <= ON_PRESS_RESET_DURATION;
39
+
40
+ const shouldIncreaseMultiplier = isSameEventType && isWithinResetDuration;
41
+
42
+ if (shouldIncreaseMultiplier) {
43
+ onPressMultiplier.current += 1;
44
+ } else {
45
+ resetMultiplier();
46
+ }
47
+
48
+ const seekTime = getSkipTime(onPressMultiplier.current);
49
+ player[eventType](seekTime);
50
+
51
+ lastPressTime.current = now;
52
+ currentOnPressEventType.current = eventType;
53
+ },
54
+ [resetMultiplier]
55
+ );
56
+
57
+ return handlePressEvent;
58
+ };
@@ -1,68 +0,0 @@
1
- export type SeekKeyHandlerListener = {
2
- onRewindPress: () => boolean;
3
- onForwardPress: () => boolean;
4
- onRewindPressOut: () => void;
5
- onForwardPressOut: () => void;
6
- };
7
-
8
- export class KeyInputHandler {
9
- private static instance: KeyInputHandler;
10
-
11
- public static getInstance() {
12
- if (!KeyInputHandler.instance) {
13
- KeyInputHandler.instance = new KeyInputHandler();
14
- }
15
-
16
- return KeyInputHandler.instance;
17
- }
18
-
19
- constructor() {
20
- this.listeners = [];
21
- }
22
-
23
- private listeners: SeekKeyHandlerListener[] = [];
24
-
25
- addListener = (listener: SeekKeyHandlerListener) => {
26
- this.listeners.splice(0, 0, listener);
27
-
28
- return () => this.removeListener(listener);
29
- };
30
-
31
- removeListener = (listener: SeekKeyHandlerListener) => {
32
- this.listeners = this.listeners.filter((l) => l !== listener);
33
- };
34
-
35
- onRewindPress = () => {
36
- for (const element of this.listeners) {
37
- if (element.onRewindPress()) {
38
- break;
39
- }
40
- }
41
- };
42
-
43
- onForwardPress = () => {
44
- for (const element of this.listeners) {
45
- if (element.onForwardPress()) {
46
- break;
47
- }
48
- }
49
- };
50
-
51
- // TODO: Event should be send to listeners who handled down event
52
- onRewindPressOut = () => {
53
- this.listeners.forEach((listener) => listener.onRewindPressOut());
54
- };
55
-
56
- onForwardPressOut = () => {
57
- this.listeners.forEach((listener) => listener.onForwardPressOut());
58
- };
59
-
60
- getSeekMethods = () => {
61
- return {
62
- onForwardPress: this.onForwardPress,
63
- onRewindPress: this.onRewindPress,
64
- onForwardPressOut: this.onForwardPressOut,
65
- onRewindPressOut: this.onRewindPressOut,
66
- };
67
- };
68
- }
@@ -1,7 +0,0 @@
1
- import { SEEK_TYPE } from "../const";
2
-
3
- export interface ISeekController {
4
- onStartSeek(seekType: SEEK_TYPE): void;
5
- onSeekEnd?(): void;
6
- reset(): void;
7
- }