import { ThemedView } from '@/components/themed-view'; import { VideoView, useVideoPlayer } from 'expo-video'; import * as MediaLibrary from 'expo-media-library'; import { Platform } from 'react-native'; import { ThemedText } from '@/components/themed-text'; // ResizeMode 兼容映射 - 移除 'stretch',expo-video 不支持 const ResizeMode = { CONTAIN: 'contain' as const, COVER: 'cover' as const, STRETCH: 'fill' as const, // 使用 'fill' 替代 'stretch' }; import { router, useLocalSearchParams } from 'expo-router'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { Copy, Download, Maximize2, Plus, X, ArrowLeft, } from 'lucide-react'; import React, { useEffect, useState } from 'react'; import { Alert, Dimensions, Image, ImageStyle, Modal, ScrollView, StyleSheet, StyleProp, Text, TouchableOpacity, View, ViewStyle, } from 'react-native'; const { width: screenWidth } = Dimensions.get('window'); const HERO_HEIGHT = screenWidth * 0.58; const DETAIL_HEIGHT = screenWidth * 0.95; interface ResultWithTemplate { id: string; userId: string; templateId: string; type: 'TEXT' | 'IMAGE' | 'VIDEO'; resultUrl: string[]; status: 'pending' | 'running' | 'completed' | 'failed'; creditsCost?: number; creditsTransactionId?: string; createdAt: string; updatedAt: string; templateName?: string; templateThumbnail?: string; templateDescription?: string; } export default function ResultPage() { const { id } = useLocalSearchParams<{ id: string }>(); const insets = useSafeAreaInsets(); const [result, setResult] = useState(null); const [fullscreenVisible, setFullscreenVisible] = useState(false); const [fullscreenContent, setFullscreenContent] = useState<{ type: 'image' | 'video'; url: string; } | null>(null); const [saving, setSaving] = useState(false); useEffect(() => { loadResult(); }, [id]); const loadResult = async () => { try { const typeParam = typeof id === 'string' ? id.split('_')[1] : 'text'; const resultId = typeof id === 'string' ? id : 'res1_text'; const getResultByType = () => { switch (typeParam) { case 'image': return { id: resultId, userId: 'user1', templateId: 'tpl2', type: 'IMAGE' as const, resultUrl: ['https://picsum.photos/800/600?random=1'], status: 'completed' as const, creditsCost: 20, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), templateName: 'AI图片生成', templateThumbnail: 'https://picsum.photos/60/60?random=2', templateDescription: '使用AI生成高质量图片', }; case 'video': return { id: resultId, userId: 'user1', templateId: 'tpl3', type: 'VIDEO' as const, resultUrl: ['https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4'], status: 'completed' as const, creditsCost: 50, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), templateName: 'AI视频生成', templateThumbnail: 'https://picsum.photos/60/60?random=3', templateDescription: '基于文本生成视频内容', }; default: return { id: resultId, userId: 'user1', templateId: 'tpl1', type: 'TEXT' as const, resultUrl: [ '这是一段AI生成的营销文案,专门为您的产品精心打造。它突出了产品的核心优势,采用了吸引人的语言风格,能够有效提升用户的购买欲望和品牌认知度。通过深入分析目标受众的需求和痛点,我们精心设计了这份文案,旨在最大化品牌影响力和转化效果。', ], status: 'completed' as const, creditsCost: 10, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), templateName: 'AI文案生成', templateThumbnail: 'https://picsum.photos/60/60?random=1', templateDescription: '基于关键词生成营销文案', }; } }; setResult(getResultByType()); } catch (error) { Alert.alert('错误', '无法加载结果详情'); } }; const handleRerun = () => { router.push(`/templates/${result?.templateId}/form`); }; const handleSaveToGallery = async () => { if (!result || result.type === 'TEXT') return; try { setSaving(true); const { status } = await MediaLibrary.requestPermissionsAsync(); if (status !== 'granted') { Alert.alert('权限请求', '需要媒体库权限才能保存文件'); return; } Alert.alert('保存成功', `${result.type === 'IMAGE' ? '图片' : '视频'}已保存到相册`); } catch (error) { Alert.alert('保存失败', '无法保存文件,请检查网络连接'); } finally { setSaving(false); } }; const handleCopyText = async () => { if (!result || result.type !== 'TEXT') return; try { await navigator.clipboard?.writeText?.(result.resultUrl[0]); Alert.alert('复制成功', '文本已复制到剪贴板'); } catch (error) { Alert.alert('复制失败', '无法复制文本'); } }; const openFullscreen = (type: 'image' | 'video', url: string) => { setFullscreenContent({ type, url }); setFullscreenVisible(true); }; const handleDownloadPress = () => { if (!result) return; if (result.type === 'TEXT') { handleCopyText(); return; } handleSaveToGallery(); }; const renderVisualMedia = ( url: string, mediaStyles: { image: StyleProp; video: StyleProp } ) => { if (!url) return null; if (result?.type === 'VIDEO') { // 简化实现:使用原生 HTML5 video 或 Image 作为回退 if (Platform.OS === 'web') { return (