@applicaster/quick-brick-core 16.0.0-rc.11 → 16.0.0-rc.110
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 +123 -0
- package/App/ActionsProvider/__tests__/LegacyActionsRegistryAdapter.test.tsx +64 -0
- package/App/DeepLinking/URLSchemeHandler/SchemeHandlerHooks/__tests__/useOpenSchemeHandler.test.tsx +25 -15
- package/App/ModalProvider/ModalBottomSheet/DraggableBottomSheet/__tests__/index.test.tsx +88 -0
- package/App/ModalProvider/ModalBottomSheet/DraggableBottomSheet/index.tsx +30 -56
- package/App/ModalProvider/ModalBottomSheet/ModalBottomSheetFrame.tsx +9 -2
- 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/ModalProvider/ModalBottomSheet/index.tsx +5 -2
- package/App/ModalProvider/ModalContent.tsx +3 -0
- package/App/ModalProvider/__tests__/ModalContent.test.tsx +39 -0
- package/App/ModalProvider/__tests__/index.test.tsx +99 -0
- package/App/ModalProvider/index.tsx +39 -14
- package/App/NavigationProvider/NavigationProvider.tsx +57 -5
- package/App/NavigationProvider/__tests__/navigationProvider.test.tsx +56 -0
- package/App/NavigationProvider/__tests__/utils.test.ts +31 -1
- package/App/NavigationProvider/utils.ts +14 -0
- package/App/NetworkStatusProvider/NetworkStatusProvider.tsx +32 -4
- package/App/NetworkStatusProvider/ResumeVerificationController.ts +282 -0
- package/App/NetworkStatusProvider/__tests__/NetworkStatusProvider.test.tsx +152 -1
- package/App/NetworkStatusProvider/__tests__/ResumeVerificationController.test.ts +471 -0
- package/App/NetworkStatusProvider/__tests__/resumeVerification.test.ts +126 -0
- package/App/NetworkStatusProvider/__tests__/useResumeVerifiedNetInfo.test.ts +189 -0
- package/App/NetworkStatusProvider/resumeVerification.ts +54 -0
- package/App/NetworkStatusProvider/useResumeVerifiedNetInfo.ts +33 -0
- package/App/NotificationToastRenderer/NotificationToastManager.ts +214 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.tsx +134 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.tv.tsx +10 -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 +233 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.web.test.tsx +90 -0
- package/App/NotificationToastRenderer/__tests__/resolveConfirmationToastStyle.test.ts +118 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationHeight.test.tsx +58 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationToastState.test.ts +112 -0
- package/App/NotificationToastRenderer/index.tsx +9 -0
- package/App/NotificationToastRenderer/resolveConfirmationToastStyle.ts +33 -0
- package/App/NotificationToastRenderer/useNotificationHeight.tsx +14 -0
- package/App/NotificationToastRenderer/useNotificationToastState.tsx +12 -0
- package/App/index.tsx +8 -5
- package/package.json +8 -8
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { renderHook, act } from "@testing-library/react-native";
|
|
3
|
+
import { AppState, Platform } from "react-native";
|
|
4
|
+
|
|
5
|
+
import { useResumeVerifiedNetInfo } from "../useResumeVerifiedNetInfo";
|
|
6
|
+
import { RESUME_VERIFICATION_DELAY_MS } from "../resumeVerification";
|
|
7
|
+
import { isOnline } from "../utils";
|
|
8
|
+
|
|
9
|
+
const mockLogger = {
|
|
10
|
+
warning: jest.fn(),
|
|
11
|
+
debug: jest.fn(),
|
|
12
|
+
info: jest.fn(),
|
|
13
|
+
log: jest.fn(),
|
|
14
|
+
error: jest.fn(),
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// addSubsystem runs at module load, before mockLogger is initialised: delegate lazily
|
|
18
|
+
jest.mock("../../logger", () => ({
|
|
19
|
+
coreAppLogger: {
|
|
20
|
+
addSubsystem: () => ({
|
|
21
|
+
warning: (event) => mockLogger.warning(event),
|
|
22
|
+
debug: (event) => mockLogger.debug(event),
|
|
23
|
+
info: (event) => mockLogger.info(event),
|
|
24
|
+
log: (event) => mockLogger.log(event),
|
|
25
|
+
error: (event) => mockLogger.error(event),
|
|
26
|
+
}),
|
|
27
|
+
},
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
jest.mock("react-native/Libraries/AppState/AppState", () => {
|
|
31
|
+
const { EventEmitter } = require("events");
|
|
32
|
+
const emitter = new EventEmitter();
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
emitter,
|
|
36
|
+
currentState: "active",
|
|
37
|
+
addEventListener: (type, handler) => {
|
|
38
|
+
emitter.addListener(type, handler);
|
|
39
|
+
|
|
40
|
+
return { remove: () => emitter.removeListener(type, handler) };
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const ONLINE = {
|
|
46
|
+
type: "wifi",
|
|
47
|
+
details: { isConnectionExpensive: false },
|
|
48
|
+
isConnected: true,
|
|
49
|
+
isInternetReachable: true,
|
|
50
|
+
deviceStatus: null,
|
|
51
|
+
} as any;
|
|
52
|
+
|
|
53
|
+
const OFFLINE = {
|
|
54
|
+
type: "wifi",
|
|
55
|
+
details: { isConnectionExpensive: false },
|
|
56
|
+
isConnected: false,
|
|
57
|
+
isInternetReachable: false,
|
|
58
|
+
deviceStatus: null,
|
|
59
|
+
} as any;
|
|
60
|
+
|
|
61
|
+
const mockAppState = AppState as any;
|
|
62
|
+
|
|
63
|
+
const changeAppState = (state: string) =>
|
|
64
|
+
act(() => {
|
|
65
|
+
mockAppState.currentState = state;
|
|
66
|
+
mockAppState.emitter.emit("change", state);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const advance = (ms: number) =>
|
|
70
|
+
act(() => {
|
|
71
|
+
jest.advanceTimersByTime(ms);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const setup = (initial = ONLINE, options = {}) => {
|
|
75
|
+
const hook = renderHook(({ info }) => useResumeVerifiedNetInfo(info), {
|
|
76
|
+
initialProps: { info: initial },
|
|
77
|
+
...options,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const setLive = (info) => hook.rerender({ info });
|
|
81
|
+
const publishedOnline = () => isOnline(hook.result.current.netInfo) === true;
|
|
82
|
+
|
|
83
|
+
return { ...hook, setLive, publishedOnline };
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const infoMessages = () =>
|
|
87
|
+
mockLogger.info.mock.calls.map(([event]) => event.message);
|
|
88
|
+
|
|
89
|
+
describe("useResumeVerifiedNetInfo", () => {
|
|
90
|
+
const originalOS = Platform.OS;
|
|
91
|
+
|
|
92
|
+
beforeEach(() => {
|
|
93
|
+
jest.useFakeTimers();
|
|
94
|
+
Platform.OS = "ios";
|
|
95
|
+
mockAppState.currentState = "active";
|
|
96
|
+
mockAppState.emitter.removeAllListeners();
|
|
97
|
+
jest.clearAllMocks();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
afterEach(() => {
|
|
101
|
+
jest.useRealTimers();
|
|
102
|
+
Platform.OS = originalOS;
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("publishes live changes immediately while active", () => {
|
|
106
|
+
const { setLive, result } = setup(ONLINE);
|
|
107
|
+
|
|
108
|
+
setLive(OFFLINE);
|
|
109
|
+
|
|
110
|
+
expect(result.current.netInfo).toBe(OFFLINE);
|
|
111
|
+
expect(result.current.stillOfflineAfterResumeCount).toBe(0);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("holds the state in background and publishes the verified state 5s after resume", () => {
|
|
115
|
+
const { setLive, publishedOnline } = setup(ONLINE);
|
|
116
|
+
|
|
117
|
+
changeAppState("background");
|
|
118
|
+
setLive(OFFLINE);
|
|
119
|
+
changeAppState("active");
|
|
120
|
+
|
|
121
|
+
advance(RESUME_VERIFICATION_DELAY_MS - 1);
|
|
122
|
+
expect(publishedOnline()).toBe(true);
|
|
123
|
+
|
|
124
|
+
advance(1);
|
|
125
|
+
expect(publishedOnline()).toBe(false);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("logs the resume verification at info level (delegates to the controller)", () => {
|
|
129
|
+
setup(ONLINE);
|
|
130
|
+
|
|
131
|
+
changeAppState("background");
|
|
132
|
+
changeAppState("active");
|
|
133
|
+
advance(RESUME_VERIFICATION_DELAY_MS);
|
|
134
|
+
|
|
135
|
+
expect(infoMessages()).toEqual(
|
|
136
|
+
expect.arrayContaining([
|
|
137
|
+
"NetworkStatus: resume verification started",
|
|
138
|
+
"NetworkStatus: resume verification finished",
|
|
139
|
+
])
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(mockLogger.warning).not.toHaveBeenCalled();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("exposes stillOfflineAfterResumeCount after an offline resume", () => {
|
|
146
|
+
const { result } = setup(OFFLINE);
|
|
147
|
+
|
|
148
|
+
changeAppState("background");
|
|
149
|
+
changeAppState("active");
|
|
150
|
+
advance(RESUME_VERIFICATION_DELAY_MS);
|
|
151
|
+
|
|
152
|
+
expect(result.current.stillOfflineAfterResumeCount).toBe(1);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("stops the controller on unmount (no AppState listener, pending window never finishes)", () => {
|
|
156
|
+
const { setLive, unmount } = setup(ONLINE);
|
|
157
|
+
|
|
158
|
+
changeAppState("background");
|
|
159
|
+
changeAppState("active");
|
|
160
|
+
setLive(OFFLINE);
|
|
161
|
+
|
|
162
|
+
unmount();
|
|
163
|
+
advance(RESUME_VERIFICATION_DELAY_MS);
|
|
164
|
+
|
|
165
|
+
expect(mockAppState.emitter.listenerCount("change")).toBe(0);
|
|
166
|
+
|
|
167
|
+
expect(infoMessages()).not.toContain(
|
|
168
|
+
"NetworkStatus: resume verification finished"
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("works under React.StrictMode (double-invoked effects)", () => {
|
|
173
|
+
const { setLive, publishedOnline } = setup(ONLINE, {
|
|
174
|
+
wrapper: React.StrictMode,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
expect(mockAppState.emitter.listenerCount("change")).toBe(1);
|
|
178
|
+
|
|
179
|
+
changeAppState("background");
|
|
180
|
+
changeAppState("active");
|
|
181
|
+
setLive(OFFLINE);
|
|
182
|
+
|
|
183
|
+
advance(RESUME_VERIFICATION_DELAY_MS - 1);
|
|
184
|
+
expect(publishedOnline()).toBe(true);
|
|
185
|
+
|
|
186
|
+
advance(1);
|
|
187
|
+
expect(publishedOnline()).toBe(false);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Platform } from "react-native";
|
|
2
|
+
import { NetInfoState } from "@react-native-community/netinfo";
|
|
3
|
+
import { isConnectionInfoReady, isOnline } from "./utils";
|
|
4
|
+
|
|
5
|
+
type ConnectionInfo = NetInfoState & { deviceStatus?: NetStatus };
|
|
6
|
+
|
|
7
|
+
export type ResumeDecision =
|
|
8
|
+
| "still_online"
|
|
9
|
+
| "went_offline"
|
|
10
|
+
| "back_online"
|
|
11
|
+
| "still_offline";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Time to wait after the app returns to foreground before trusting NetInfo.
|
|
15
|
+
* On iOS the JS reachability check (NetInfo internetReachability) is suspended
|
|
16
|
+
* with the app and reports a stale failure right after resume/unlock.
|
|
17
|
+
*/
|
|
18
|
+
export const RESUME_VERIFICATION_DELAY_MS = 5000;
|
|
19
|
+
|
|
20
|
+
// Only "background" suspends JS (and NetInfo's reachability check). "inactive"
|
|
21
|
+
// (Control Center, Notification Center, system alerts) keeps JS running, and is
|
|
22
|
+
// where users toggle airplane mode / Wi-Fi, so changes there must pass through.
|
|
23
|
+
export const isBackgroundAppState = (state?: string | null): boolean =>
|
|
24
|
+
state === "background";
|
|
25
|
+
|
|
26
|
+
// Mobile only: tvOS / Android TV report Platform.OS "ios" / "android" too
|
|
27
|
+
export const isResumeVerificationEnabled = (): boolean =>
|
|
28
|
+
(Platform.OS === "ios" || Platform.OS === "android") && !Platform.isTV;
|
|
29
|
+
|
|
30
|
+
export const getResumeDecision = (
|
|
31
|
+
wasOnline: boolean,
|
|
32
|
+
isOnlineNow: boolean
|
|
33
|
+
): ResumeDecision => {
|
|
34
|
+
if (wasOnline) {
|
|
35
|
+
return isOnlineNow ? "still_online" : "went_offline";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return isOnlineNow ? "back_online" : "still_offline";
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Log-safe summary: details holds ipAddress/subnet/carrier, never log it as is
|
|
42
|
+
export const describeNetInfo = (info: ConnectionInfo | null) => {
|
|
43
|
+
const safeInfo = (info ?? {}) as ConnectionInfo;
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
online: isOnline(safeInfo) === true,
|
|
47
|
+
isReady: !!isConnectionInfoReady(safeInfo),
|
|
48
|
+
type: safeInfo.type,
|
|
49
|
+
isConnected: safeInfo.isConnected,
|
|
50
|
+
isInternetReachable: safeInfo.isInternetReachable,
|
|
51
|
+
deviceOnline: safeInfo.deviceStatus?.online,
|
|
52
|
+
at: new Date().toISOString(),
|
|
53
|
+
};
|
|
54
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ConnectionInfo,
|
|
5
|
+
ResumeVerificationController,
|
|
6
|
+
ResumeVerifiedState,
|
|
7
|
+
} from "./ResumeVerificationController";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Network status verified after the app returns from background,
|
|
11
|
+
* see ResumeVerificationController.
|
|
12
|
+
* liveNetInfo must keep its reference until it really changes (memoize it),
|
|
13
|
+
* every new reference is treated as a network update.
|
|
14
|
+
*/
|
|
15
|
+
export function useResumeVerifiedNetInfo<T extends ConnectionInfo>(
|
|
16
|
+
liveNetInfo: T
|
|
17
|
+
): ResumeVerifiedState<T> {
|
|
18
|
+
const [controller] = React.useState(
|
|
19
|
+
() => new ResumeVerificationController(liveNetInfo)
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
React.useLayoutEffect(() => {
|
|
23
|
+
controller.start();
|
|
24
|
+
|
|
25
|
+
return () => controller.stop();
|
|
26
|
+
}, [controller]);
|
|
27
|
+
|
|
28
|
+
React.useLayoutEffect(() => {
|
|
29
|
+
controller.setNetInfo(liveNetInfo);
|
|
30
|
+
}, [controller, liveNetInfo]);
|
|
31
|
+
|
|
32
|
+
return React.useSyncExternalStore(controller.subscribe, controller.getState);
|
|
33
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
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 const CONFIRMATION_TOAST_SOURCE = "confirmation";
|
|
30
|
+
|
|
31
|
+
export interface ToastPayload {
|
|
32
|
+
id?: string;
|
|
33
|
+
message: string;
|
|
34
|
+
extraMessage?: string; // secondary line; TV renderers show it as a subtitle, mobile ignores it
|
|
35
|
+
timeout?: number; // ms; defaults to DEFAULT_TOAST_TIMEOUT (4000) if omitted
|
|
36
|
+
style?: ToastStyle;
|
|
37
|
+
// stamped by the showToast action; Theme confirmation styles apply when
|
|
38
|
+
// source === CONFIRMATION_TOAST_SOURCE ("confirmation")
|
|
39
|
+
source?: string;
|
|
40
|
+
[key: string]: unknown;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class NotificationToastManager {
|
|
44
|
+
private static instance: NotificationToastManager;
|
|
45
|
+
|
|
46
|
+
private eventHandler = subscriber();
|
|
47
|
+
private queue: ToastPayload[] = [];
|
|
48
|
+
private hideTimer: ReturnType<typeof setTimeout> | null = null;
|
|
49
|
+
private isShowing = false;
|
|
50
|
+
private nextId = 0;
|
|
51
|
+
|
|
52
|
+
private constructor() {
|
|
53
|
+
// subscribe to the shared event bus so other packages can trigger toasts
|
|
54
|
+
subscriberFor(
|
|
55
|
+
NOTIFICATION_TOAST_EVENTS.SHOW_TOAST,
|
|
56
|
+
this.handleShowToastEvent
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
subscriberFor(NOTIFICATION_TOAST_EVENTS.DISMISS, this.handleDismiss);
|
|
60
|
+
|
|
61
|
+
this.getCurrentState = this.getCurrentState.bind(this);
|
|
62
|
+
this.subscribe = this.subscribe.bind(this);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static getInstance(): NotificationToastManager {
|
|
66
|
+
if (!NotificationToastManager.instance) {
|
|
67
|
+
NotificationToastManager.instance = new NotificationToastManager();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return NotificationToastManager.instance;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
on(event: string, callback: (...args: any[]) => void) {
|
|
74
|
+
this.eventHandler.on(event, callback);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
removeHandler(event: string, callback: (...args: any[]) => void) {
|
|
78
|
+
this.eventHandler.removeHandler(event, callback);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
showToast(payload: ToastPayload) {
|
|
82
|
+
this.handleShowToast(payload);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
dismiss() {
|
|
86
|
+
this.handleDismiss();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getCurrentState() {
|
|
90
|
+
return this.queue[0] ?? null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
subscribe(callback) {
|
|
94
|
+
this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
|
|
95
|
+
|
|
96
|
+
this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
|
|
97
|
+
|
|
98
|
+
return () => {
|
|
99
|
+
this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
|
|
100
|
+
|
|
101
|
+
this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// subscriberFor's shared bus can deliver the raw payload (component emitters,
|
|
106
|
+
// see useSubscriberFor) or an args array (postEvent-style callers) - accept both.
|
|
107
|
+
private handleShowToastEvent = (payload: unknown) => {
|
|
108
|
+
if (!payload) {
|
|
109
|
+
console.warn(
|
|
110
|
+
"NotificationToastManager: Invalid showToast payload:",
|
|
111
|
+
payload
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const unwrappedPayload = unwrapEventPayload(payload) as ToastPayload;
|
|
118
|
+
|
|
119
|
+
if (!unwrappedPayload || typeof unwrappedPayload.message !== "string") {
|
|
120
|
+
console.warn(
|
|
121
|
+
"NotificationToastManager: Invalid showToast payload:",
|
|
122
|
+
payload
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
this.handleShowToast(unwrappedPayload);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
private handleShowToast = (payload: ToastPayload) => {
|
|
132
|
+
const item = {
|
|
133
|
+
...payload,
|
|
134
|
+
id: payload.id ?? String(this.nextId++),
|
|
135
|
+
timeout: payload.timeout ?? DEFAULT_TOAST_TIMEOUT,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const queuedIndex = this.queue.findIndex((queued) => queued.id === item.id);
|
|
139
|
+
|
|
140
|
+
if (queuedIndex === -1) {
|
|
141
|
+
this.queue.push(item);
|
|
142
|
+
} else if (queuedIndex === 0 && this.isShowing) {
|
|
143
|
+
// this id is the toast currently on screen - swap its content in place
|
|
144
|
+
// (new one appears in front of it) and dismiss the old timer/content
|
|
145
|
+
this.clearTimer();
|
|
146
|
+
this.queue[0] = item;
|
|
147
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, item);
|
|
148
|
+
|
|
149
|
+
if (item.timeout) {
|
|
150
|
+
this.hideTimer = setTimeout(() => this.finishCurrent(), item.timeout);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return;
|
|
154
|
+
} else {
|
|
155
|
+
// still queued, not yet shown - update it in place instead of duplicating
|
|
156
|
+
this.queue[queuedIndex] = item;
|
|
157
|
+
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!this.isShowing) {
|
|
162
|
+
this.processNext();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
private handleDismiss = () => {
|
|
167
|
+
if (!this.isShowing) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.finishCurrent();
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
private processNext() {
|
|
175
|
+
const next = this.queue[0];
|
|
176
|
+
|
|
177
|
+
if (!next) {
|
|
178
|
+
this.isShowing = false;
|
|
179
|
+
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
this.isShowing = true;
|
|
184
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, next);
|
|
185
|
+
|
|
186
|
+
if (next.timeout) {
|
|
187
|
+
this.hideTimer = setTimeout(() => this.finishCurrent(), next.timeout);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private finishCurrent() {
|
|
192
|
+
this.clearTimer();
|
|
193
|
+
this.queue.shift();
|
|
194
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.HIDE);
|
|
195
|
+
this.isShowing = false;
|
|
196
|
+
this.processNext();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private clearTimer() {
|
|
200
|
+
if (this.hideTimer) {
|
|
201
|
+
clearTimeout(this.hideTimer);
|
|
202
|
+
this.hideTimer = null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public reset(): void {
|
|
207
|
+
this.clearTimer();
|
|
208
|
+
this.queue = [];
|
|
209
|
+
this.isShowing = false;
|
|
210
|
+
this.nextId = 0;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export const notificationToastManager = NotificationToastManager.getInstance();
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import React, { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { View, Text, Animated, StyleSheet, ViewStyle } from "react-native";
|
|
3
|
+
import {
|
|
4
|
+
Directions,
|
|
5
|
+
Gesture,
|
|
6
|
+
GestureDetector,
|
|
7
|
+
} from "react-native-gesture-handler";
|
|
8
|
+
import { useTheme } from "@applicaster/zapp-react-native-utils/theme";
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
CONFIRMATION_TOAST_SOURCE,
|
|
12
|
+
notificationToastManager,
|
|
13
|
+
} from "./NotificationToastManager";
|
|
14
|
+
import { useNotificationToastState } from "./useNotificationToastState";
|
|
15
|
+
import { useNotificationHeight } from "./useNotificationHeight";
|
|
16
|
+
import { resolveConfirmationToastStyle } from "./resolveConfirmationToastStyle";
|
|
17
|
+
|
|
18
|
+
const DEFAULT_TOAST_BACKGROUND_COLOR = "#000";
|
|
19
|
+
const DEFAULT_TOAST_MESSAGE_COLOR = "#fff";
|
|
20
|
+
|
|
21
|
+
const styles = StyleSheet.create({
|
|
22
|
+
notificationView: {
|
|
23
|
+
flex: 1,
|
|
24
|
+
position: "absolute",
|
|
25
|
+
alignItems: "center",
|
|
26
|
+
justifyContent: "center",
|
|
27
|
+
top: 0,
|
|
28
|
+
left: 0,
|
|
29
|
+
right: 0,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
type NumberBoolean = 0 | 1;
|
|
34
|
+
|
|
35
|
+
export const NotificationToastRenderer = ({ children }) => {
|
|
36
|
+
const { statusHeight, notificationHeight } = useNotificationHeight();
|
|
37
|
+
const theme = useTheme<BaseThemePropertiesMobile>();
|
|
38
|
+
|
|
39
|
+
const toast = useNotificationToastState();
|
|
40
|
+
|
|
41
|
+
const [notificationOpacity, setNotificationOpacity] =
|
|
42
|
+
useState<NumberBoolean>(0);
|
|
43
|
+
|
|
44
|
+
const yPosition = useRef(new Animated.Value(0)).current;
|
|
45
|
+
|
|
46
|
+
const resolvedStyle =
|
|
47
|
+
toast?.source === CONFIRMATION_TOAST_SOURCE
|
|
48
|
+
? resolveConfirmationToastStyle(theme, toast.style)
|
|
49
|
+
: toast?.style;
|
|
50
|
+
|
|
51
|
+
const backgroundColor =
|
|
52
|
+
resolvedStyle?.backgroundColor ?? DEFAULT_TOAST_BACKGROUND_COLOR;
|
|
53
|
+
|
|
54
|
+
const color = resolvedStyle?.color ?? DEFAULT_TOAST_MESSAGE_COLOR;
|
|
55
|
+
|
|
56
|
+
const textStyles = {
|
|
57
|
+
color,
|
|
58
|
+
fontFamily: resolvedStyle?.fontFamily,
|
|
59
|
+
fontSize: resolvedStyle?.fontSize,
|
|
60
|
+
lineHeight: resolvedStyle?.lineHeight,
|
|
61
|
+
letterSpacing: resolvedStyle?.letterSpacing,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const animatedViewStyles: ViewStyle = {
|
|
65
|
+
...styles.notificationView,
|
|
66
|
+
transform: [
|
|
67
|
+
{
|
|
68
|
+
translateY: yPosition.interpolate({
|
|
69
|
+
inputRange: [0, 1],
|
|
70
|
+
outputRange: [-notificationHeight, 0],
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
opacity: notificationOpacity,
|
|
75
|
+
height: notificationHeight,
|
|
76
|
+
width: "100%",
|
|
77
|
+
|
|
78
|
+
backgroundColor,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const flingGesture = useMemo(
|
|
82
|
+
() =>
|
|
83
|
+
Gesture.Fling()
|
|
84
|
+
.direction(Directions.UP)
|
|
85
|
+
.enabled(!!toast)
|
|
86
|
+
.runOnJS(true)
|
|
87
|
+
.onEnd((_event, success) => {
|
|
88
|
+
if (success) {
|
|
89
|
+
notificationToastManager.dismiss();
|
|
90
|
+
}
|
|
91
|
+
}),
|
|
92
|
+
[toast]
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (toast) {
|
|
97
|
+
setNotificationOpacity(1);
|
|
98
|
+
|
|
99
|
+
Animated.timing(yPosition, {
|
|
100
|
+
toValue: 1,
|
|
101
|
+
useNativeDriver: true,
|
|
102
|
+
duration: 300,
|
|
103
|
+
}).start();
|
|
104
|
+
} else {
|
|
105
|
+
Animated.timing(yPosition, {
|
|
106
|
+
toValue: 0,
|
|
107
|
+
useNativeDriver: true,
|
|
108
|
+
duration: 300,
|
|
109
|
+
}).start(() => setNotificationOpacity(0));
|
|
110
|
+
}
|
|
111
|
+
}, [toast]);
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<View
|
|
115
|
+
testID="NotificationToastRenderer"
|
|
116
|
+
style={StyleSheet.absoluteFill}
|
|
117
|
+
pointerEvents="box-none"
|
|
118
|
+
>
|
|
119
|
+
{children}
|
|
120
|
+
<GestureDetector gesture={flingGesture}>
|
|
121
|
+
<Animated.View
|
|
122
|
+
testID="NotificationToastRenderer-fling"
|
|
123
|
+
pointerEvents={toast ? "auto" : "none"}
|
|
124
|
+
style={animatedViewStyles}
|
|
125
|
+
>
|
|
126
|
+
<View style={{ height: statusHeight, backgroundColor }} />
|
|
127
|
+
<Text testID="NotificationToastRenderer-message" style={textStyles}>
|
|
128
|
+
{toast?.message}
|
|
129
|
+
</Text>
|
|
130
|
+
</Animated.View>
|
|
131
|
+
</GestureDetector>
|
|
132
|
+
</View>
|
|
133
|
+
);
|
|
134
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* NotificationToastRenderer for TV platforms.
|
|
5
|
+
* Currently, it does not render anything except for the children passed to it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const NotificationToastRenderer = ({ children }) => {
|
|
9
|
+
return <>{children}</>;
|
|
10
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { notificationToastManager } from "./NotificationToastManager";
|
|
4
|
+
import { useNotificationToastState } from "./useNotificationToastState";
|
|
5
|
+
|
|
6
|
+
const styles: Record<string, React.CSSProperties> = {
|
|
7
|
+
body: {
|
|
8
|
+
position: "absolute",
|
|
9
|
+
bottom: 0,
|
|
10
|
+
left: 0,
|
|
11
|
+
right: 0,
|
|
12
|
+
width: 1920,
|
|
13
|
+
height: 200,
|
|
14
|
+
background:
|
|
15
|
+
"linear-gradient(180deg, rgba(17,17,17,0.9) 0%, rgba(17,17,17,1) 49%, rgba(0,0,0,0.85) 100%)",
|
|
16
|
+
display: "flex",
|
|
17
|
+
alignItems: "center",
|
|
18
|
+
justifyContent: "center",
|
|
19
|
+
flexDirection: "column",
|
|
20
|
+
color: "#fff",
|
|
21
|
+
paddingBottom: 15,
|
|
22
|
+
},
|
|
23
|
+
title: {
|
|
24
|
+
fontSize: 28,
|
|
25
|
+
margin: 0,
|
|
26
|
+
padding: 0,
|
|
27
|
+
},
|
|
28
|
+
subtitle: {
|
|
29
|
+
fontSize: 20,
|
|
30
|
+
margin: 0,
|
|
31
|
+
padding: 0,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const NotificationToastRenderer = ({ children }) => {
|
|
36
|
+
const toast = useNotificationToastState();
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div
|
|
40
|
+
data-testid="NotificationToastRenderer"
|
|
41
|
+
onClick={() => notificationToastManager.dismiss()}
|
|
42
|
+
>
|
|
43
|
+
{children}
|
|
44
|
+
{toast ? (
|
|
45
|
+
<div
|
|
46
|
+
data-testid="NotificationToastRenderer-touchable"
|
|
47
|
+
style={styles.body}
|
|
48
|
+
>
|
|
49
|
+
<div style={styles.title}>
|
|
50
|
+
<h2 data-testid="NotificationToastRenderer-message">
|
|
51
|
+
{toast.message}
|
|
52
|
+
</h2>
|
|
53
|
+
</div>
|
|
54
|
+
{toast.extraMessage ? (
|
|
55
|
+
<h4 style={styles.subtitle}>{toast.extraMessage}</h4>
|
|
56
|
+
) : null}
|
|
57
|
+
</div>
|
|
58
|
+
) : null}
|
|
59
|
+
</div>
|
|
60
|
+
);
|
|
61
|
+
};
|