|
1 | | -import React, { useCallback } from 'react'; |
2 | | -import FlashMessage, { showMessage } from 'react-native-flash-message'; |
3 | | -import { View, StyleSheet, Platform } from 'react-native'; |
4 | | - |
5 | | -import IconIon from 'react-native-vector-icons/Ionicons'; |
6 | | -import IconAnt from 'react-native-vector-icons/AntDesign'; |
7 | | -import IconMC from 'react-native-vector-icons/MaterialCommunityIcons'; |
8 | | -import type { MessageType, Icon } from 'react-native-flash-message'; |
9 | 1 | import ThemedStyles from './src/styles/ThemedStyles'; |
| 2 | +import { ToastConfig } from '@msantang78/react-native-styled-toast/dist/Toast'; |
| 3 | + |
| 4 | +let toast: undefined | ((config: ToastConfig) => void); |
| 5 | + |
| 6 | +export function registerToast(t) { |
| 7 | + toast = t; |
| 8 | +} |
| 9 | + |
| 10 | +type MessageType = 'success' | 'info' | 'warning' | 'danger'; |
| 11 | + |
| 12 | +function getIcon(type: MessageType): any { |
| 13 | + switch (type) { |
| 14 | + case 'success': |
| 15 | + return { |
| 16 | + iconColor: '#59A05E', |
| 17 | + iconFamily: 'Ionicons', |
| 18 | + iconName: 'md-checkmark', |
| 19 | + }; |
| 20 | + case 'warning': |
| 21 | + return { |
| 22 | + iconColor: '#D49538', |
| 23 | + iconFamily: 'Ionicons', |
| 24 | + iconName: 'ios-warning', |
| 25 | + }; |
| 26 | + case 'info': |
| 27 | + return { |
| 28 | + iconColor: '#5282A7', |
| 29 | + iconFamily: 'EvilIcons', |
| 30 | + iconName: 'exclamation', |
| 31 | + }; |
| 32 | + case 'danger': |
| 33 | + return { |
| 34 | + iconColor: '#CA4A34', |
| 35 | + iconFamily: 'MaterialCommunityIcons', |
| 36 | + iconName: 'alert-octagon', |
| 37 | + }; |
| 38 | + } |
| 39 | + return {}; |
| 40 | +} |
10 | 41 |
|
11 | 42 | /** |
12 | | - * Show a notification message to the user |
| 43 | + * Show a notification message to the user |
13 | 44 | * @param message |
14 | 45 | * @param type |
15 | | - * @param duration use 0 for permanent message |
| 46 | + * @param duration |
| 47 | + * @param subMessage |
| 48 | + * @param shouldVibrate |
16 | 49 | */ |
17 | 50 | export const showNotification = ( |
18 | 51 | message: string, |
19 | 52 | type: MessageType = 'info', |
20 | 53 | duration: number = 2800, |
21 | | - position: 'top' | 'bottom' | 'center' | undefined = 'bottom', |
| 54 | + subMessage?: string, |
| 55 | + shouldVibrate = false, |
| 56 | + onPress?: () => void, |
22 | 57 | ) => { |
23 | | - showMessage({ |
24 | | - floating: true, |
25 | | - position, |
26 | | - message, |
27 | | - icon: type, |
28 | | - duration, |
29 | | - backgroundColor: '#FFFFFF', |
30 | | - //@ts-ignore style parameter is not defined on the type |
31 | | - style: styles.container, |
32 | | - titleStyle: styles.title, |
33 | | - color: '#7D7D82', |
34 | | - type, |
35 | | - }); |
36 | | -}; |
| 58 | + if (toast) { |
| 59 | + toast({ |
| 60 | + closeIconColor: ThemedStyles.getColor('SecondaryText'), |
| 61 | + message, |
| 62 | + onPress, |
| 63 | + hideAccent: true, |
| 64 | + shouldVibrate, |
| 65 | + allowFontScaling: false, |
| 66 | + // hideCloseIcon: true, |
| 67 | + duration, |
| 68 | + subMessage, |
| 69 | + ...getIcon(type), |
| 70 | + closeIconSize: 18, |
| 71 | + messageProps: { |
| 72 | + fontFamily: 'Roboto-Medium', |
| 73 | + fontSize: 15, |
| 74 | + }, |
| 75 | + iconSize: 24, |
| 76 | + toastStyles: { |
| 77 | + borderRadius: 4, |
| 78 | + }, |
| 79 | + shadow: { |
| 80 | + shadowColor: '#000000', |
| 81 | + shadowOffset: { |
| 82 | + width: 0, |
| 83 | + height: 1, |
| 84 | + }, |
| 85 | + shadowOpacity: 0.5, |
| 86 | + shadowRadius: 2, |
37 | 87 |
|
38 | | -/** |
39 | | - * Icon renderer |
40 | | - * @param icon |
41 | | - */ |
42 | | -const renderNotificationIcon = (icon: Icon = 'success') => { |
43 | | - const theme = ThemedStyles.style; |
44 | | - switch (icon) { |
45 | | - case 'success': |
46 | | - return ( |
47 | | - <View style={[styles.success, theme.bgSuccessBackground]}> |
48 | | - <IconIon name="md-checkmark" color="white" size={25} /> |
49 | | - </View> |
50 | | - ); |
51 | | - case 'info': |
52 | | - return ( |
53 | | - <View style={[styles.info, theme.bgInfoBackground]}> |
54 | | - <IconAnt name="exclamationcircleo" color="white" size={25} /> |
55 | | - </View> |
56 | | - ); |
57 | | - case 'warning': |
58 | | - return ( |
59 | | - <View style={[styles.warning, theme.bgWarningBackground]}> |
60 | | - <IconIon name="ios-warning" color="white" size={25} /> |
61 | | - </View> |
62 | | - ); |
63 | | - case 'danger': |
64 | | - return ( |
65 | | - <View style={[styles.danger, theme.bgDangerBackground]}> |
66 | | - <IconMC name="alert-octagon" color="white" size={25} /> |
67 | | - </View> |
68 | | - ); |
| 88 | + elevation: 4, |
| 89 | + }, |
| 90 | + }); |
69 | 91 | } |
70 | | - return null; |
71 | 92 | }; |
72 | | - |
73 | | -/** |
74 | | - * App messages component |
75 | | - */ |
76 | | -const AppMessages = () => { |
77 | | - const renderNotification = useCallback( |
78 | | - (message: any) => |
79 | | - message.renderCustomContent ? message.renderCustomContent() : null, |
80 | | - [], |
81 | | - ); |
82 | | - return ( |
83 | | - <FlashMessage |
84 | | - //@ts-ignore renderCustomContent prop is not defined on the type |
85 | | - renderCustomContent={renderNotification} |
86 | | - renderFlashMessageIcon={renderNotificationIcon} |
87 | | - /> |
88 | | - ); |
89 | | -}; |
90 | | - |
91 | | -const styles = StyleSheet.create({ |
92 | | - info: { |
93 | | - height: '100%', |
94 | | - aspectRatio: 1, |
95 | | - justifyContent: 'center', |
96 | | - alignItems: 'center', |
97 | | - }, |
98 | | - success: { |
99 | | - height: '100%', |
100 | | - aspectRatio: 1, |
101 | | - justifyContent: 'center', |
102 | | - alignItems: 'center', |
103 | | - }, |
104 | | - danger: { |
105 | | - height: '100%', |
106 | | - aspectRatio: 1, |
107 | | - justifyContent: 'center', |
108 | | - alignItems: 'center', |
109 | | - }, |
110 | | - warning: { |
111 | | - height: '100%', |
112 | | - aspectRatio: 1, |
113 | | - justifyContent: 'center', |
114 | | - alignItems: 'center', |
115 | | - }, |
116 | | - title: { |
117 | | - fontSize: 17, |
118 | | - padding: 15, |
119 | | - paddingRight: 15, |
120 | | - marginRight: 55, |
121 | | - alignSelf: 'center', |
122 | | - flexWrap: 'wrap', |
123 | | - }, |
124 | | - container: { |
125 | | - shadowColor: 'black', |
126 | | - shadowOpacity: Platform.select({ ios: 0.2, android: 0.3 }), |
127 | | - shadowOffset: { width: 0, height: 5 }, |
128 | | - shadowRadius: 10, |
129 | | - elevation: 15, |
130 | | - alignItems: 'center', |
131 | | - minHeight: 70, |
132 | | - borderRadius: 0, |
133 | | - paddingHorizontal: 0, |
134 | | - paddingVertical: 0, |
135 | | - marginTop: 0, |
136 | | - marginLeft: 0, |
137 | | - marginRight: 0, |
138 | | - marginBottom: 0, |
139 | | - }, |
140 | | - messageHorizontalLine: { |
141 | | - marginLeft: -20, |
142 | | - marginRight: -20, |
143 | | - }, |
144 | | - messageVerticalLine: { |
145 | | - marginTop: -10, |
146 | | - marginBottom: -14, |
147 | | - }, |
148 | | -}); |
149 | | - |
150 | | -export default AppMessages; |
0 commit comments