|
| 1 | +import type { ToastConfig } from '@/context/ToastContext' |
| 2 | +import { merge } from '@flow/core' |
| 3 | +import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' |
| 4 | +import { Animated, Dimensions, StyleSheet, Text, View } from 'react-native' |
| 5 | +import { useTheme } from 'react-native-paper' |
| 6 | +import { useSafeAreaInsets } from 'react-native-safe-area-context' |
| 7 | +import { ToastContext } from '@/context/ToastContext' |
| 8 | + |
| 9 | +const { width: screenWidth, height: screenHeight } = Dimensions.get('window') |
| 10 | + |
| 11 | +export function ToastProvider({ children }: { children: React.ReactNode }) { |
| 12 | + const [visible, setVisible] = useState(false) |
| 13 | + const [config, setConfig] = useState<ToastConfig>({ |
| 14 | + message: '', |
| 15 | + type: 'info', |
| 16 | + duration: 3000, |
| 17 | + position: 'bottom', |
| 18 | + }) |
| 19 | + |
| 20 | + const opacity = useRef(new Animated.Value(0)).current |
| 21 | + const translateY = useRef(new Animated.Value(50)).current |
| 22 | + const scale = useRef(new Animated.Value(0.9)).current |
| 23 | + const timeoutRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined) |
| 24 | + |
| 25 | + const insets = useSafeAreaInsets() |
| 26 | + const theme = useTheme() |
| 27 | + |
| 28 | + const hideToast = useCallback(() => { |
| 29 | + // 执行退出动画 |
| 30 | + Animated.parallel([ |
| 31 | + Animated.timing(opacity, { |
| 32 | + toValue: 0, |
| 33 | + duration: 200, |
| 34 | + useNativeDriver: true, |
| 35 | + }), |
| 36 | + Animated.timing(translateY, { |
| 37 | + toValue: config.position === 'top' ? -50 : 50, |
| 38 | + duration: 200, |
| 39 | + useNativeDriver: true, |
| 40 | + }), |
| 41 | + Animated.timing(scale, { |
| 42 | + toValue: 0.9, |
| 43 | + duration: 200, |
| 44 | + useNativeDriver: true, |
| 45 | + }), |
| 46 | + ]).start(() => { |
| 47 | + setVisible(false) |
| 48 | + }) |
| 49 | + |
| 50 | + if (timeoutRef.current) { |
| 51 | + clearTimeout(timeoutRef.current) |
| 52 | + } |
| 53 | + }, [opacity, translateY, scale, config.position]) |
| 54 | + |
| 55 | + const showToast = useCallback((newConfig: ToastConfig) => { |
| 56 | + // 清除之前的定时器 |
| 57 | + if (timeoutRef.current) { |
| 58 | + clearTimeout(timeoutRef.current) |
| 59 | + } |
| 60 | + |
| 61 | + const defaultConfig: ToastConfig = { |
| 62 | + duration: 3000, |
| 63 | + type: 'info', |
| 64 | + position: 'bottom', |
| 65 | + message: '', |
| 66 | + } |
| 67 | + |
| 68 | + const finalConfig = merge(defaultConfig, newConfig) |
| 69 | + |
| 70 | + setConfig(finalConfig) |
| 71 | + setVisible(true) |
| 72 | + |
| 73 | + // 重置动画值 |
| 74 | + opacity.setValue(0) |
| 75 | + translateY.setValue(finalConfig.position === 'top' ? -50 : 50) |
| 76 | + scale.setValue(0.9) |
| 77 | + |
| 78 | + // 执行进入动画 |
| 79 | + Animated.parallel([ |
| 80 | + Animated.timing(opacity, { |
| 81 | + toValue: 1, |
| 82 | + duration: 300, |
| 83 | + useNativeDriver: true, |
| 84 | + }), |
| 85 | + Animated.timing(translateY, { |
| 86 | + toValue: 0, |
| 87 | + duration: 300, |
| 88 | + useNativeDriver: true, |
| 89 | + }), |
| 90 | + Animated.spring(scale, { |
| 91 | + toValue: 1, |
| 92 | + tension: 100, |
| 93 | + friction: 8, |
| 94 | + useNativeDriver: true, |
| 95 | + }), |
| 96 | + ]).start() |
| 97 | + |
| 98 | + // 设置自动隐藏 |
| 99 | + if (finalConfig.duration && finalConfig.duration > 0) { |
| 100 | + timeoutRef.current = setTimeout(() => { |
| 101 | + hideToast() |
| 102 | + }, finalConfig.duration) |
| 103 | + } |
| 104 | + }, [opacity, translateY, scale, hideToast]) |
| 105 | + |
| 106 | + useEffect(() => { |
| 107 | + return () => { |
| 108 | + if (timeoutRef.current) { |
| 109 | + clearTimeout(timeoutRef.current) |
| 110 | + } |
| 111 | + } |
| 112 | + }, []) |
| 113 | + |
| 114 | + const getToastStyle = () => { |
| 115 | + const baseStyle = { |
| 116 | + position: 'absolute' as const, |
| 117 | + left: 16, |
| 118 | + right: 16, |
| 119 | + zIndex: 9999, |
| 120 | + } |
| 121 | + |
| 122 | + switch (config.position) { |
| 123 | + case 'top': |
| 124 | + return { |
| 125 | + ...baseStyle, |
| 126 | + top: insets.top + 16, |
| 127 | + } |
| 128 | + case 'center': |
| 129 | + return { |
| 130 | + ...baseStyle, |
| 131 | + top: (screenHeight - 56) / 2, // 56 是大概的 toast 高度 |
| 132 | + } |
| 133 | + case 'bottom': |
| 134 | + default: |
| 135 | + return { |
| 136 | + ...baseStyle, |
| 137 | + bottom: insets.bottom + 16, |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + const getToastColors = () => { |
| 143 | + const { colors } = theme |
| 144 | + |
| 145 | + switch (config.type) { |
| 146 | + case 'success': |
| 147 | + return { |
| 148 | + background: colors.primaryContainer, |
| 149 | + text: colors.onPrimaryContainer, |
| 150 | + } |
| 151 | + case 'error': |
| 152 | + return { |
| 153 | + background: colors.errorContainer, |
| 154 | + text: colors.onErrorContainer, |
| 155 | + } |
| 156 | + case 'warning': |
| 157 | + return { |
| 158 | + background: colors.secondaryContainer, |
| 159 | + text: colors.onSecondaryContainer, |
| 160 | + } |
| 161 | + case 'info': |
| 162 | + default: |
| 163 | + return { |
| 164 | + background: colors.tertiaryContainer, |
| 165 | + text: colors.onTertiaryContainer, |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + const toastColors = getToastColors() |
| 171 | + const contextValue = useMemo(() => ({ showToast, hideToast }), [showToast, hideToast]) |
| 172 | + |
| 173 | + return ( |
| 174 | + <ToastContext value={contextValue}> |
| 175 | + {children} |
| 176 | + {visible && ( |
| 177 | + <Animated.View |
| 178 | + style={[ |
| 179 | + getToastStyle(), |
| 180 | + { |
| 181 | + opacity, |
| 182 | + transform: [{ translateY }, { scale }], |
| 183 | + }, |
| 184 | + ]} |
| 185 | + pointerEvents="none" |
| 186 | + > |
| 187 | + <View |
| 188 | + style={[ |
| 189 | + styles.toastContainer, |
| 190 | + { |
| 191 | + backgroundColor: toastColors.background, |
| 192 | + shadowColor: theme.colors.shadow, |
| 193 | + }, |
| 194 | + ]} |
| 195 | + > |
| 196 | + <Text |
| 197 | + style={[ |
| 198 | + styles.toastText, |
| 199 | + { |
| 200 | + color: toastColors.text, |
| 201 | + }, |
| 202 | + ]} |
| 203 | + numberOfLines={2} |
| 204 | + > |
| 205 | + {config.message} |
| 206 | + </Text> |
| 207 | + </View> |
| 208 | + </Animated.View> |
| 209 | + )} |
| 210 | + </ToastContext> |
| 211 | + ) |
| 212 | +} |
| 213 | + |
| 214 | +const styles = StyleSheet.create({ |
| 215 | + toastContainer: { |
| 216 | + paddingHorizontal: 16, |
| 217 | + paddingVertical: 12, |
| 218 | + borderRadius: 8, |
| 219 | + maxWidth: screenWidth - 32, |
| 220 | + alignSelf: 'center', |
| 221 | + // Material Design 阴影 |
| 222 | + shadowOffset: { |
| 223 | + width: 0, |
| 224 | + height: 2, |
| 225 | + }, |
| 226 | + shadowOpacity: 0.25, |
| 227 | + shadowRadius: 4, |
| 228 | + elevation: 5, |
| 229 | + }, |
| 230 | + toastText: { |
| 231 | + fontSize: 14, |
| 232 | + fontWeight: '500', |
| 233 | + textAlign: 'center', |
| 234 | + lineHeight: 20, |
| 235 | + }, |
| 236 | +}) |
0 commit comments