@applicaster/quick-brick-core 15.1.5 → 15.1.6-alpha.3988508055
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/App/ActionsProvider/ActionsProvider.tsx +31 -9
- package/App/ActionsProvider/LegacyActionsRegistryAdapter.tsx +107 -0
- package/App/ModalProvider/ModalBottomSheet/ModalBottomSheetFrame.tsx +9 -1
- package/App/ModalProvider/ModalBottomSheet/hooks/__tests__/useKeyboardHeight.android.test.ts +105 -0
- package/App/ModalProvider/ModalBottomSheet/hooks/__tests__/useKeyboardHeight.test.ts +102 -0
- package/App/ModalProvider/ModalBottomSheet/hooks/index.ts +1 -0
- package/App/ModalProvider/ModalBottomSheet/hooks/useKeyboardHeight.ts +30 -0
- package/App/NotificationToastRenderer/NotificationToastManager.ts +209 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.tsx +102 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.web.tsx +61 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastManager.test.ts +237 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.test.tsx +85 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.web.test.tsx +90 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationHeight.test.tsx +58 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationToastState.test.ts +112 -0
- package/App/NotificationToastRenderer/index.tsx +5 -0
- package/App/NotificationToastRenderer/useNotificationHeight.tsx +13 -0
- package/App/NotificationToastRenderer/useNotificationToastState.tsx +12 -0
- package/package.json +8 -8
|
@@ -3,6 +3,14 @@ import * as R from "ramda";
|
|
|
3
3
|
|
|
4
4
|
import { ActionsContext } from "@applicaster/zapp-react-native-ui-components/Contexts/ActionsContext";
|
|
5
5
|
import { isNilOrEmpty } from "@applicaster/zapp-react-native-utils/reactUtils/helpers";
|
|
6
|
+
import { createLogger } from "@applicaster/zapp-react-native-utils/logger";
|
|
7
|
+
|
|
8
|
+
import { LegacyActionsRegistryAdapter } from "./LegacyActionsRegistryAdapter";
|
|
9
|
+
|
|
10
|
+
const { log_debug } = createLogger({
|
|
11
|
+
subsystem: "ActionsProvider",
|
|
12
|
+
category: "ActionRegistration",
|
|
13
|
+
});
|
|
6
14
|
|
|
7
15
|
type Props = {
|
|
8
16
|
children: React.ReactNode;
|
|
@@ -36,19 +44,17 @@ const abstractIdentifier = (actionPlugins: Plugin[]): PluginsIdMap =>
|
|
|
36
44
|
* Component returning children.
|
|
37
45
|
* Can be used to create Component from children
|
|
38
46
|
*/
|
|
39
|
-
const ChildrenWrapper = ({
|
|
40
|
-
children
|
|
41
|
-
}: {
|
|
42
|
-
children: React.ReactElement<any>;
|
|
43
|
-
}) => {
|
|
44
|
-
return children;
|
|
47
|
+
const ChildrenWrapper = ({ children }: { children: React.ReactNode }) => {
|
|
48
|
+
return <>{children}</>;
|
|
45
49
|
};
|
|
46
50
|
|
|
47
51
|
/**
|
|
48
52
|
* It wraps children in multiple context providers
|
|
49
53
|
* @param contexts - Array of React Context Providers
|
|
50
54
|
*/
|
|
51
|
-
const contextComposer = (
|
|
55
|
+
const contextComposer = (
|
|
56
|
+
contexts: Plugin[]
|
|
57
|
+
): React.ComponentType<React.PropsWithChildren> =>
|
|
52
58
|
R.compose(...contexts)(ChildrenWrapper);
|
|
53
59
|
|
|
54
60
|
/**
|
|
@@ -69,6 +75,19 @@ export function ActionsProvider(props: Props) {
|
|
|
69
75
|
[plugins]
|
|
70
76
|
);
|
|
71
77
|
|
|
78
|
+
React.useEffect(() => {
|
|
79
|
+
const count = Object.keys(actionPlugins).length;
|
|
80
|
+
|
|
81
|
+
if (count > 0) {
|
|
82
|
+
log_debug(
|
|
83
|
+
`ActionsProvider: composed ${count} action plugin(s) into context`,
|
|
84
|
+
{ identifiers: Object.keys(actionPlugins) }
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
log_debug("ActionsProvider: no action plugins to compose");
|
|
88
|
+
}
|
|
89
|
+
}, [actionPlugins]);
|
|
90
|
+
|
|
72
91
|
const contextProviders = React.useMemo((): [Plugin] | [] => {
|
|
73
92
|
try {
|
|
74
93
|
const providers = R.compose(
|
|
@@ -88,7 +107,7 @@ export function ActionsProvider(props: Props) {
|
|
|
88
107
|
? contextComposer(contextProviders)
|
|
89
108
|
: React.Fragment,
|
|
90
109
|
[]
|
|
91
|
-
)
|
|
110
|
+
) as React.ComponentType<React.PropsWithChildren>;
|
|
92
111
|
|
|
93
112
|
const contextValue = React.useMemo(
|
|
94
113
|
() => ({ actions: actionPlugins }),
|
|
@@ -97,7 +116,10 @@ export function ActionsProvider(props: Props) {
|
|
|
97
116
|
|
|
98
117
|
return (
|
|
99
118
|
<ActionsContext.Provider value={contextValue}>
|
|
100
|
-
<Context>
|
|
119
|
+
<Context>
|
|
120
|
+
<LegacyActionsRegistryAdapter actionPlugins={actionPlugins} />
|
|
121
|
+
{children}
|
|
122
|
+
</Context>
|
|
101
123
|
</ActionsContext.Provider>
|
|
102
124
|
);
|
|
103
125
|
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import * as R from "ramda";
|
|
3
|
+
|
|
4
|
+
import { uiActionsRegistry } from "@applicaster/zapp-react-native-utils/uiActionsRegistrator";
|
|
5
|
+
import { createLogger } from "@applicaster/zapp-react-native-utils/logger";
|
|
6
|
+
|
|
7
|
+
const { log_debug, log_warning } = createLogger({
|
|
8
|
+
subsystem: "LegacyActionsRegistryAdapter",
|
|
9
|
+
category: "ActionRegistration",
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
type LegacyActionPlugin = Record<string, any> & {
|
|
13
|
+
identifier: string;
|
|
14
|
+
module: {
|
|
15
|
+
context?: React.Context<any>;
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Bridges a single legacy action plugin (one that exposes `module.context`)
|
|
22
|
+
* into the `uiActionsRegistry`.
|
|
23
|
+
*
|
|
24
|
+
* Reactivity note: legacy stateful actions rely on React Context re-renders to
|
|
25
|
+
* propagate their latest value to subscribed UI (via `useActions`). We keep
|
|
26
|
+
* that mechanism intact and only *mirror* the current value into the registry
|
|
27
|
+
* through a ref, so registry-based consumers always read the freshest value at
|
|
28
|
+
* call time without changing legacy reactivity.
|
|
29
|
+
*/
|
|
30
|
+
function LegacyActionBridge({ plugin }: { plugin: LegacyActionPlugin }) {
|
|
31
|
+
// `plugin.module.context` is stable for the lifetime of the plugin, so this
|
|
32
|
+
// single `useContext` call is safe and rules-of-hooks compliant.
|
|
33
|
+
const value = React.useContext(plugin.module.context as React.Context<any>);
|
|
34
|
+
|
|
35
|
+
const valueRef = React.useRef(value);
|
|
36
|
+
valueRef.current = value;
|
|
37
|
+
|
|
38
|
+
React.useEffect(() => {
|
|
39
|
+
if (value == null) {
|
|
40
|
+
log_warning(
|
|
41
|
+
`LegacyActionBridge: context value for "${plugin.identifier}" is null/undefined – registering provider with empty list until context is available`,
|
|
42
|
+
{ identifier: plugin.identifier }
|
|
43
|
+
);
|
|
44
|
+
} else {
|
|
45
|
+
log_debug(
|
|
46
|
+
`LegacyActionBridge: context value ready for "${plugin.identifier}", registering in UIActionsRegistry`,
|
|
47
|
+
{ identifier: plugin.identifier }
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const unregister = uiActionsRegistry.registerActionProvider(
|
|
52
|
+
plugin.identifier,
|
|
53
|
+
() =>
|
|
54
|
+
valueRef.current
|
|
55
|
+
? [{ identifier: plugin.identifier, action: valueRef.current }]
|
|
56
|
+
: []
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
return () => {
|
|
60
|
+
log_debug(
|
|
61
|
+
`LegacyActionBridge: unmounting bridge for "${plugin.identifier}"`,
|
|
62
|
+
{ identifier: plugin.identifier }
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
unregister();
|
|
66
|
+
};
|
|
67
|
+
}, [plugin.identifier]);
|
|
68
|
+
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
type Props = {
|
|
73
|
+
actionPlugins: Record<string, LegacyActionPlugin>;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Registers every legacy action plugin's live context value into the
|
|
78
|
+
* `uiActionsRegistry`. It must be rendered *inside* the composed context
|
|
79
|
+
* providers (see `ActionsProvider`) so that each plugin's context is populated.
|
|
80
|
+
*
|
|
81
|
+
* This is the compatibility layer that lets new, registry-based consumers read
|
|
82
|
+
* actions coming from old plugins that still export `contextProvider`/`context`.
|
|
83
|
+
*/
|
|
84
|
+
export function LegacyActionsRegistryAdapter({ actionPlugins }: Props) {
|
|
85
|
+
const bridgedPlugins = React.useMemo(
|
|
86
|
+
() =>
|
|
87
|
+
R.values(actionPlugins).filter(
|
|
88
|
+
(plugin) => plugin?.module?.context != null
|
|
89
|
+
),
|
|
90
|
+
[actionPlugins]
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
log_debug(
|
|
94
|
+
`LegacyActionsRegistryAdapter: bridging ${bridgedPlugins.length} legacy action plugin(s)`,
|
|
95
|
+
{ identifiers: bridgedPlugins.map((p) => p.identifier) }
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<>
|
|
100
|
+
{bridgedPlugins.map((plugin) => (
|
|
101
|
+
<LegacyActionBridge key={plugin.identifier} plugin={plugin} />
|
|
102
|
+
))}
|
|
103
|
+
</>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
LegacyActionsRegistryAdapter.displayName = "LegacyActionsRegistryAdapter";
|
|
@@ -14,6 +14,8 @@ import {
|
|
|
14
14
|
platformSelect,
|
|
15
15
|
} from "@applicaster/zapp-react-native-utils/reactUtils";
|
|
16
16
|
|
|
17
|
+
import { useKeyboardHeight } from "./hooks";
|
|
18
|
+
|
|
17
19
|
type Props = {
|
|
18
20
|
children: React.ReactChild;
|
|
19
21
|
backgroundColor: string;
|
|
@@ -104,6 +106,8 @@ export function ModalBottomSheetFrame(props: Props) {
|
|
|
104
106
|
|
|
105
107
|
const sheetHeight = useGetSheetHeight(height, maxHeight);
|
|
106
108
|
|
|
109
|
+
const keyboardHeight = useKeyboardHeight();
|
|
110
|
+
|
|
107
111
|
return (
|
|
108
112
|
<DraggableBottomSheet
|
|
109
113
|
sheetHeight={sheetHeight}
|
|
@@ -113,7 +117,11 @@ export function ModalBottomSheetFrame(props: Props) {
|
|
|
113
117
|
header={(dragging) => <Handle {...handleProps} focused={dragging} />}
|
|
114
118
|
maxWidth={maxWidth}
|
|
115
119
|
{...dimensions}
|
|
116
|
-
height={
|
|
120
|
+
height={
|
|
121
|
+
safeAreaDims.height -
|
|
122
|
+
(Platform.OS === "ios" ? bottom : 0) -
|
|
123
|
+
keyboardHeight
|
|
124
|
+
}
|
|
117
125
|
>
|
|
118
126
|
<SafeAreaView
|
|
119
127
|
edges={["left", "right", "bottom"]}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react-native";
|
|
2
|
+
import { Keyboard } from "react-native";
|
|
3
|
+
|
|
4
|
+
jest.mock("@applicaster/zapp-react-native-utils/reactUtils", () => ({
|
|
5
|
+
isApplePlatform: () => false,
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
// Import after mocks so module-level isApple resolves to false.
|
|
9
|
+
// eslint-disable-next-line import/first
|
|
10
|
+
import { useKeyboardHeight } from "../useKeyboardHeight";
|
|
11
|
+
|
|
12
|
+
type KeyboardListener = (event?: {
|
|
13
|
+
endCoordinates: { height: number };
|
|
14
|
+
}) => void;
|
|
15
|
+
|
|
16
|
+
function setupKeyboardListenerMock() {
|
|
17
|
+
const listeners: Record<string, KeyboardListener> = {};
|
|
18
|
+
const removeMocks: Record<string, jest.Mock> = {};
|
|
19
|
+
|
|
20
|
+
const addListenerSpy = jest
|
|
21
|
+
.spyOn(Keyboard, "addListener")
|
|
22
|
+
.mockImplementation((event: string, callback: KeyboardListener) => {
|
|
23
|
+
listeners[event] = callback;
|
|
24
|
+
|
|
25
|
+
const remove = jest.fn();
|
|
26
|
+
removeMocks[event] = remove;
|
|
27
|
+
|
|
28
|
+
return { remove };
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return { listeners, removeMocks, addListenerSpy };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe("useKeyboardHeight (non-Apple platforms)", () => {
|
|
35
|
+
let listeners: Record<string, KeyboardListener>;
|
|
36
|
+
let removeMocks: Record<string, jest.Mock>;
|
|
37
|
+
let addListenerSpy: jest.SpyInstance;
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
const mock = setupKeyboardListenerMock();
|
|
41
|
+
listeners = mock.listeners;
|
|
42
|
+
removeMocks = mock.removeMocks;
|
|
43
|
+
addListenerSpy = mock.addListenerSpy;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
addListenerSpy.mockRestore();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("returns 0 initially and subscribes to didShow / didHide events", () => {
|
|
51
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
52
|
+
|
|
53
|
+
expect(result.current).toBe(0);
|
|
54
|
+
expect(addListenerSpy).toHaveBeenCalledTimes(2);
|
|
55
|
+
|
|
56
|
+
expect(addListenerSpy).toHaveBeenCalledWith(
|
|
57
|
+
"keyboardDidShow",
|
|
58
|
+
expect.any(Function)
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
expect(addListenerSpy).toHaveBeenCalledWith(
|
|
62
|
+
"keyboardDidHide",
|
|
63
|
+
expect.any(Function)
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("updates height when keyboardDidShow fires", () => {
|
|
68
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
69
|
+
|
|
70
|
+
act(() => {
|
|
71
|
+
listeners.keyboardDidShow?.({
|
|
72
|
+
endCoordinates: { height: 412 },
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(result.current).toBe(412);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("resets height to 0 when keyboardDidHide fires", () => {
|
|
80
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
81
|
+
|
|
82
|
+
act(() => {
|
|
83
|
+
listeners.keyboardDidShow?.({
|
|
84
|
+
endCoordinates: { height: 412 },
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(result.current).toBe(412);
|
|
89
|
+
|
|
90
|
+
act(() => {
|
|
91
|
+
listeners.keyboardDidHide?.();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
expect(result.current).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("removes listeners on unmount", () => {
|
|
98
|
+
const { unmount } = renderHook(() => useKeyboardHeight());
|
|
99
|
+
|
|
100
|
+
unmount();
|
|
101
|
+
|
|
102
|
+
expect(removeMocks.keyboardDidShow).toHaveBeenCalledTimes(1);
|
|
103
|
+
expect(removeMocks.keyboardDidHide).toHaveBeenCalledTimes(1);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react-native";
|
|
2
|
+
import { Keyboard } from "react-native";
|
|
3
|
+
import { useKeyboardHeight } from "../useKeyboardHeight";
|
|
4
|
+
|
|
5
|
+
jest.mock("@applicaster/zapp-react-native-utils/reactUtils", () => ({
|
|
6
|
+
isApplePlatform: () => true,
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
type KeyboardListener = (event?: {
|
|
10
|
+
endCoordinates: { height: number };
|
|
11
|
+
}) => void;
|
|
12
|
+
|
|
13
|
+
function setupKeyboardListenerMock() {
|
|
14
|
+
const listeners: Record<string, KeyboardListener> = {};
|
|
15
|
+
const removeMocks: Record<string, jest.Mock> = {};
|
|
16
|
+
|
|
17
|
+
const addListenerSpy = jest
|
|
18
|
+
.spyOn(Keyboard, "addListener")
|
|
19
|
+
.mockImplementation((event: string, callback: KeyboardListener) => {
|
|
20
|
+
listeners[event] = callback;
|
|
21
|
+
|
|
22
|
+
const remove = jest.fn();
|
|
23
|
+
removeMocks[event] = remove;
|
|
24
|
+
|
|
25
|
+
return { remove };
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return { listeners, removeMocks, addListenerSpy };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("useKeyboardHeight (Apple platforms)", () => {
|
|
32
|
+
let listeners: Record<string, KeyboardListener>;
|
|
33
|
+
let removeMocks: Record<string, jest.Mock>;
|
|
34
|
+
let addListenerSpy: jest.SpyInstance;
|
|
35
|
+
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
const mock = setupKeyboardListenerMock();
|
|
38
|
+
listeners = mock.listeners;
|
|
39
|
+
removeMocks = mock.removeMocks;
|
|
40
|
+
addListenerSpy = mock.addListenerSpy;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
addListenerSpy.mockRestore();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("returns 0 initially and subscribes to willShow / willHide events", () => {
|
|
48
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
49
|
+
|
|
50
|
+
expect(result.current).toBe(0);
|
|
51
|
+
expect(addListenerSpy).toHaveBeenCalledTimes(2);
|
|
52
|
+
|
|
53
|
+
expect(addListenerSpy).toHaveBeenCalledWith(
|
|
54
|
+
"keyboardWillShow",
|
|
55
|
+
expect.any(Function)
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
expect(addListenerSpy).toHaveBeenCalledWith(
|
|
59
|
+
"keyboardWillHide",
|
|
60
|
+
expect.any(Function)
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("updates height when keyboardWillShow fires", () => {
|
|
65
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
66
|
+
|
|
67
|
+
act(() => {
|
|
68
|
+
listeners.keyboardWillShow?.({
|
|
69
|
+
endCoordinates: { height: 336 },
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
expect(result.current).toBe(336);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("resets height to 0 when keyboardWillHide fires", () => {
|
|
77
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
78
|
+
|
|
79
|
+
act(() => {
|
|
80
|
+
listeners.keyboardWillShow?.({
|
|
81
|
+
endCoordinates: { height: 280 },
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
expect(result.current).toBe(280);
|
|
86
|
+
|
|
87
|
+
act(() => {
|
|
88
|
+
listeners.keyboardWillHide?.();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
expect(result.current).toBe(0);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("removes listeners on unmount", () => {
|
|
95
|
+
const { unmount } = renderHook(() => useKeyboardHeight());
|
|
96
|
+
|
|
97
|
+
unmount();
|
|
98
|
+
|
|
99
|
+
expect(removeMocks.keyboardWillShow).toHaveBeenCalledTimes(1);
|
|
100
|
+
expect(removeMocks.keyboardWillHide).toHaveBeenCalledTimes(1);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useKeyboardHeight } from "./useKeyboardHeight";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Keyboard, KeyboardEvent } from "react-native";
|
|
3
|
+
import { isApplePlatform } from "@applicaster/zapp-react-native-utils/reactUtils";
|
|
4
|
+
|
|
5
|
+
const isApple = isApplePlatform();
|
|
6
|
+
|
|
7
|
+
export function useKeyboardHeight() {
|
|
8
|
+
const [keyboardHeight, setKeyboardHeight] = React.useState(0);
|
|
9
|
+
|
|
10
|
+
React.useEffect(() => {
|
|
11
|
+
// iOS supports smooth 'willShow' / 'willHide' events
|
|
12
|
+
const showEvent = isApple ? "keyboardWillShow" : "keyboardDidShow";
|
|
13
|
+
const hideEvent = isApple ? "keyboardWillHide" : "keyboardDidHide";
|
|
14
|
+
|
|
15
|
+
const showListener = Keyboard.addListener(showEvent, (e: KeyboardEvent) => {
|
|
16
|
+
setKeyboardHeight(e.endCoordinates.height);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const hideListener = Keyboard.addListener(hideEvent, () => {
|
|
20
|
+
setKeyboardHeight(0);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return () => {
|
|
24
|
+
showListener.remove();
|
|
25
|
+
hideListener.remove();
|
|
26
|
+
};
|
|
27
|
+
}, []);
|
|
28
|
+
|
|
29
|
+
return keyboardHeight;
|
|
30
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { subscriber } from "@applicaster/zapp-react-native-utils/functionUtils";
|
|
2
|
+
import { subscriberFor } from "@applicaster/zapp-react-native-utils/reactHooks/useSubscriberFor";
|
|
3
|
+
|
|
4
|
+
// events accepted on the shared cross-package event bus (see useSubscriberFor),
|
|
5
|
+
// so packages that must not depend on quick-brick-core (e.g. OfflineHandler in
|
|
6
|
+
// zapp-react-native-ui-components) can trigger a toast by event name alone,
|
|
7
|
+
// without importing notificationToastManager directly.
|
|
8
|
+
const unwrapEventPayload = (payload: unknown) =>
|
|
9
|
+
Array.isArray(payload) ? payload[0] : payload;
|
|
10
|
+
|
|
11
|
+
export const NOTIFICATION_TOAST_EVENTS = {
|
|
12
|
+
SHOW_TOAST: "showToast", // in: request to enqueue a toast
|
|
13
|
+
DISMISS: "dismiss", // in: renderer/user dismissed the visible toast early
|
|
14
|
+
SHOW: "show", // out: renderer should display this toast
|
|
15
|
+
HIDE: "hide", // out: renderer should hide the current toast
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
export interface ToastStyle {
|
|
19
|
+
backgroundColor?: string;
|
|
20
|
+
color?: string;
|
|
21
|
+
fontFamily?: string;
|
|
22
|
+
fontSize?: number;
|
|
23
|
+
lineHeight?: number;
|
|
24
|
+
letterSpacing?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const DEFAULT_TOAST_TIMEOUT = 4000;
|
|
28
|
+
|
|
29
|
+
export interface ToastPayload {
|
|
30
|
+
id?: string;
|
|
31
|
+
message: string;
|
|
32
|
+
extraMessage?: string; // secondary line; TV renderers show it as a subtitle, mobile ignores it
|
|
33
|
+
timeout?: number; // ms; defaults to DEFAULT_TOAST_TIMEOUT (4000) if omitted
|
|
34
|
+
style?: ToastStyle;
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
class NotificationToastManager {
|
|
39
|
+
private static instance: NotificationToastManager;
|
|
40
|
+
|
|
41
|
+
private eventHandler = subscriber();
|
|
42
|
+
private queue: ToastPayload[] = [];
|
|
43
|
+
private hideTimer: ReturnType<typeof setTimeout> | null = null;
|
|
44
|
+
private isShowing = false;
|
|
45
|
+
private nextId = 0;
|
|
46
|
+
|
|
47
|
+
private constructor() {
|
|
48
|
+
// subscribe to the shared event bus so other packages can trigger toasts
|
|
49
|
+
subscriberFor(
|
|
50
|
+
NOTIFICATION_TOAST_EVENTS.SHOW_TOAST,
|
|
51
|
+
this.handleShowToastEvent
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
subscriberFor(NOTIFICATION_TOAST_EVENTS.DISMISS, this.handleDismiss);
|
|
55
|
+
|
|
56
|
+
this.getCurrentState = this.getCurrentState.bind(this);
|
|
57
|
+
this.subscribe = this.subscribe.bind(this);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static getInstance(): NotificationToastManager {
|
|
61
|
+
if (!NotificationToastManager.instance) {
|
|
62
|
+
NotificationToastManager.instance = new NotificationToastManager();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return NotificationToastManager.instance;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
on(event: string, callback: (...args: any[]) => void) {
|
|
69
|
+
this.eventHandler.on(event, callback);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
removeHandler(event: string, callback: (...args: any[]) => void) {
|
|
73
|
+
this.eventHandler.removeHandler(event, callback);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
showToast(payload: ToastPayload) {
|
|
77
|
+
this.handleShowToast(payload);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
dismiss() {
|
|
81
|
+
this.handleDismiss();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
getCurrentState() {
|
|
85
|
+
return this.queue[0] ?? null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
subscribe(callback) {
|
|
89
|
+
this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
|
|
90
|
+
|
|
91
|
+
this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
|
|
92
|
+
|
|
93
|
+
return () => {
|
|
94
|
+
this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
|
|
95
|
+
|
|
96
|
+
this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// subscriberFor's shared bus can deliver the raw payload (component emitters,
|
|
101
|
+
// see useSubscriberFor) or an args array (postEvent-style callers) - accept both.
|
|
102
|
+
private handleShowToastEvent = (payload: unknown) => {
|
|
103
|
+
if (!payload) {
|
|
104
|
+
console.warn(
|
|
105
|
+
"NotificationToastManager: Invalid showToast payload:",
|
|
106
|
+
payload
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const unwrappedPayload = unwrapEventPayload(payload) as ToastPayload;
|
|
113
|
+
|
|
114
|
+
if (!unwrappedPayload || typeof unwrappedPayload.message !== "string") {
|
|
115
|
+
console.warn(
|
|
116
|
+
"NotificationToastManager: Invalid showToast payload:",
|
|
117
|
+
payload
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
this.handleShowToast(unwrappedPayload);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
private handleShowToast = (payload: ToastPayload) => {
|
|
127
|
+
const item = {
|
|
128
|
+
...payload,
|
|
129
|
+
id: payload.id ?? String(this.nextId++),
|
|
130
|
+
timeout: payload.timeout ?? DEFAULT_TOAST_TIMEOUT,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const queuedIndex = this.queue.findIndex((queued) => queued.id === item.id);
|
|
134
|
+
|
|
135
|
+
if (queuedIndex === -1) {
|
|
136
|
+
this.queue.push(item);
|
|
137
|
+
} else if (queuedIndex === 0 && this.isShowing) {
|
|
138
|
+
// this id is the toast currently on screen - swap its content in place
|
|
139
|
+
// (new one appears in front of it) and dismiss the old timer/content
|
|
140
|
+
this.clearTimer();
|
|
141
|
+
this.queue[0] = item;
|
|
142
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, item);
|
|
143
|
+
|
|
144
|
+
if (item.timeout) {
|
|
145
|
+
this.hideTimer = setTimeout(() => this.finishCurrent(), item.timeout);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return;
|
|
149
|
+
} else {
|
|
150
|
+
// still queued, not yet shown - update it in place instead of duplicating
|
|
151
|
+
this.queue[queuedIndex] = item;
|
|
152
|
+
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!this.isShowing) {
|
|
157
|
+
this.processNext();
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
private handleDismiss = () => {
|
|
162
|
+
if (!this.isShowing) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
this.finishCurrent();
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
private processNext() {
|
|
170
|
+
const next = this.queue[0];
|
|
171
|
+
|
|
172
|
+
if (!next) {
|
|
173
|
+
this.isShowing = false;
|
|
174
|
+
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
this.isShowing = true;
|
|
179
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, next);
|
|
180
|
+
|
|
181
|
+
if (next.timeout) {
|
|
182
|
+
this.hideTimer = setTimeout(() => this.finishCurrent(), next.timeout);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private finishCurrent() {
|
|
187
|
+
this.clearTimer();
|
|
188
|
+
this.queue.shift();
|
|
189
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.HIDE);
|
|
190
|
+
this.isShowing = false;
|
|
191
|
+
this.processNext();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private clearTimer() {
|
|
195
|
+
if (this.hideTimer) {
|
|
196
|
+
clearTimeout(this.hideTimer);
|
|
197
|
+
this.hideTimer = null;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public reset(): void {
|
|
202
|
+
this.clearTimer();
|
|
203
|
+
this.queue = [];
|
|
204
|
+
this.isShowing = false;
|
|
205
|
+
this.nextId = 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export const notificationToastManager = NotificationToastManager.getInstance();
|