All files / app/(tabs)/scanner/components/results OcrResultView.tsx

90.9% Statements 30/33
80.76% Branches 21/26
100% Functions 5/5
90.9% Lines 30/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345                                                    1x               13x 13x 13x 13x 13x 13x     13x 1x 1x 1x 1x 1x       1x         13x 2x 2x       13x 1x 1x       13x 1x         1x 1x 1x       13x 13x 13x   13x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 1x      
/**
 * OCR 识别结果展示组件
 * 展示识别的文本内容,提供生成 AI 报告的入口
 * 支持手动编辑识别文本
 */
import { memo, useState } from 'react';
import { Alert, StyleSheet, TextInput } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import * as Clipboard from 'expo-clipboard';
import { Card, ScrollView, Spinner, Text, XStack, YStack } from 'tamagui';
import { Button } from '@/src/design-system/components';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import { Colors } from '@/src/constants/theme';
import { useThemeAwareColorScheme } from '@/src/hooks/useThemeAwareColorScheme';
import type { OcrResult } from '@/src/services/api';
// @ts-ignore: expo-clipboard may not have type declarations
 
interface OcrResultViewProps {
  ocrResult: OcrResult;
  photoUri?: string | null;
  isGeneratingReport?: boolean;
  onGenerateReport: () => void;
  onRetake: () => void;
  onClose: () => void;
}
 
export const OcrResultView = memo(function OcrResultView({
  ocrResult,
  photoUri,
  isGeneratingReport,
  onGenerateReport,
  onRetake,
  onClose,
}: OcrResultViewProps) {
  const insets = useSafeAreaInsets();
  const colorScheme = useThemeAwareColorScheme();
  const colors = Colors[colorScheme];
  const [isCopying, setIsCopying] = useState(false);
  const [isEditing, setIsEditing] = useState(false);
  const [editedText, setEditedText] = useState(ocrResult.text);
 
  // 复制文本到剪贴板
  const handleCopyText = async () => {
    try {
      setIsCopying(true);
      const textToCopy = isEditing ? editedText : ocrResult.text;
      await Clipboard.setStringAsync(textToCopy);
      Alert.alert('✅ 已复制', '识别文本已复制到剪贴板');
    } catch (error) {
      Alert.alert('❌ 复制失败', '无法复制到剪贴板');
    } finally {
      setIsCopying(false);
    }
  };
 
  // 开启编辑模式
  const handleStartEdit = () => {
    setIsEditing(true);
    setEditedText(ocrResult.text);
  };
 
  // 取消编辑
  const handleCancelEdit = () => {
    setIsEditing(false);
    setEditedText(ocrResult.text);
  };
 
  // 保存编辑
  const handleSaveEdit = () => {
    Iif (editedText.trim() === '') {
      Alert.alert('提示', '文本内容不能为空');
      return;
    }
    // 更新 ocrResult
    ocrResult.text = editedText;
    setIsEditing(false);
    Alert.alert('✅ 已保存', '识别文本已更新');
  };
 
  // 计算文本统计信息
  const currentText = isEditing ? editedText : ocrResult.text;
  const textLength = currentText.length;
  const wordCount = currentText.split(/\s+/).filter(Boolean).length;
 
  return (
    <YStack flex={1} backgroundColor={colors.background} paddingTop={insets.top}>
      {/* 顶部标题栏 */}
      <XStack
        paddingHorizontal="$4"
        paddingVertical="$3"
        alignItems="center"
        justifyContent="space-between"
        borderBottomWidth={1}
        borderBottomColor={(colors.icon + '20') as any}
        backgroundColor={colors.background}
      >
        <XStack alignItems="center" gap="$2">
          <IconSymbol name="doc.text.viewfinder" size={24} color={colors.tint} />
          <Text fontSize="$7" fontWeight="bold" color={colors.text}>
            识别结果
          </Text>
        </XStack>
        <Button size="sm" variant="ghost" rounded onPress={onClose}>
          <IconSymbol name="xmark" size={20} color={colors.icon} />
        </Button>
      </XStack>
 
      <ScrollView flex={1} showsVerticalScrollIndicator={false}>
        <YStack padding="$4" gap="$4">
          {/* 识别状态卡片 */}
          <Card
            padding="$4"
            backgroundColor={(colors.tint + '10') as any}
            borderRadius="$4"
            borderWidth={1}
            borderColor={(colors.tint + '30') as any}
            bordered
          >
            <XStack alignItems="center" gap="$3">
              <YStack
                width={48}
                height={48}
                borderRadius="$10"
                backgroundColor={colors.tint}
                alignItems="center"
                justifyContent="center"
              >
                <IconSymbol name="checkmark.circle.fill" size={28} color="white" />
              </YStack>
              <YStack flex={1}>
                <Text fontSize="$5" fontWeight="600" color={colors.text}>
                  识别完成
                </Text>
                <Text fontSize="$3" color={colors.icon}>
                  共识别 {textLength} 个字符,{wordCount} 个词
                </Text>
              </YStack>
            </XStack>
          </Card>
 
          {/* 识别文本内容 */}
          <Card padding="$4" backgroundColor={colors.background} borderRadius="$4" bordered>
            <YStack gap="$3">
              <XStack alignItems="center" justifyContent="space-between">
                <Text fontSize="$5" fontWeight="600" color={colors.text}>
                  识别文本
                </Text>
                <XStack gap="$2.5">
                  {!isEditing ? (
                    <>
                      <Button
                        size="$4"
                        height={38}
                        paddingHorizontal="$3.5"
                        chromeless
                        onPress={handleStartEdit}
                        icon={<IconSymbol name="pencil" size={18} color={colors.tint} />}
                      >
                        <Text fontSize={15} fontWeight="600" color={colors.tint}>
                          编辑
                        </Text>
                      </Button>
                      <Button
                        size="$4"
                        height={38}
                        paddingHorizontal="$3.5"
                        chromeless
                        onPress={handleCopyText}
                        disabled={isCopying}
                        icon={
                          isCopying ? (
                            <Spinner size="small" color={colors.tint} />
                          ) : (
                            <IconSymbol name="doc.on.doc" size={18} color={colors.tint} />
                          )
                        }
                      >
                        <Text fontSize={15} fontWeight="600" color={colors.tint}>
                          复制
                        </Text>
                      </Button>
                    </>
                  ) : (
                    <>
                      <Button
                        size="$4"
                        height={38}
                        paddingHorizontal="$3.5"
                        chromeless
                        onPress={handleCancelEdit}
                        icon={<IconSymbol name="xmark" size={18} color={colors.icon} />}
                      >
                        <Text fontSize={15} fontWeight="600" color={colors.icon}>
                          取消
                        </Text>
                      </Button>
                      <Button
                        size="$4"
                        height={38}
                        paddingHorizontal="$3.5"
                        backgroundColor={colors.tint}
                        color="white"
                        onPress={handleSaveEdit}
                        icon={<IconSymbol name="checkmark" size={18} color="white" />}
                      >
                        <Text fontSize={15} fontWeight="600" color="white">
                          保存
                        </Text>
                      </Button>
                    </>
                  )}
                </XStack>
              </XStack>
 
              {/* 文本内容 - 支持编辑 */}
              {isEditing ? (
                <Card
                  backgroundColor={colors.background as any}
                  padding="$3"
                  borderRadius="$3"
                  borderWidth={2}
                  borderColor={colors.tint as any}
                >
                  <TextInput
                    value={editedText}
                    onChangeText={setEditedText}
                    multiline
                    numberOfLines={10}
                    style={{
                      fontSize: 15,
                      color: colors.text,
                      lineHeight: 24,
                      minHeight: 200,
                      textAlignVertical: 'top',
                    }}
                    placeholder="请输入或编辑识别的文本..."
                    placeholderTextColor={colors.icon + '60'}
                  />
                </Card>
              ) : (
                <Card
                  backgroundColor={(colors.icon + '05') as any}
                  padding="$3"
                  borderRadius="$3"
                  borderWidth={1}
                  borderColor={(colors.icon + '20') as any}
                >
                  <Text
                    fontSize="$4"
                    color={colors.text}
                    lineHeight={24}
                    fontFamily="$body"
                    selectable
                  >
                    {currentText || '未识别到文本内容'}
                  </Text>
                </Card>
              )}
            </YStack>
          </Card>
 
          {/* 提示信息 */}
          {!isEditing && (
            <Card
              padding="$3.5"
              backgroundColor={(colors.icon + '05') as any}
              borderRadius="$4"
              borderLeftWidth={4}
              borderLeftColor={colors.tint as any}
            >
              <XStack gap="$2.5" alignItems="flex-start">
                <IconSymbol name="lightbulb.fill" size={20} color={colors.tint} />
                <YStack flex={1}>
                  <Text fontSize="$3" color={colors.text} lineHeight={22}>
                    💡 您可以点击"编辑"按钮修改识别结果,然后点击"生成 AI 报告"进行智能分析。
                  </Text>
                </YStack>
              </XStack>
            </Card>
          )}
 
          {/* 操作按钮组 */}
          <YStack gap="$3" marginTop="$2" paddingBottom={insets.bottom || 24}>
            <Button
              size="$5"
              height={54}
              backgroundColor={colors.tint}
              color="white"
              onPress={onGenerateReport}
              disabled={isGeneratingReport || isEditing}
              opacity={isEditing ? 0.5 : 1}
              icon={
                isGeneratingReport ? (
                  <Spinner size="small" color="white" />
                ) : (
                  <IconSymbol name="sparkles" size={22} color="white" />
                )
              }
            >
              <Text fontSize="$5" fontWeight="700" color="white">
                {isGeneratingReport ? '分析中...' : '生成 AI 报告'}
              </Text>
            </Button>
 
            <XStack gap="$3">
              <Button
                flex={1}
                size="$4"
                height={44}
                variant="outlined"
                onPress={onRetake}
                borderColor={(colors.icon + '30') as any}
                color={colors.text}
                icon={<IconSymbol name="camera.fill" size={18} color={colors.icon} />}
              >
                <Text fontSize="$4" color={colors.text}>
                  重新拍照
                </Text>
              </Button>
 
              <Button
                flex={1}
                size="$4"
                height={44}
                chromeless
                onPress={onClose}
                color={colors.icon}
                icon={<IconSymbol name="arrow.left" size={18} color={colors.icon} />}
              >
                <Text fontSize="$4" color={colors.icon}>
                  返回首页
                </Text>
              </Button>
            </XStack>
          </YStack>
        </YStack>
      </ScrollView>
    </YStack>
  );
});
 
const styles = StyleSheet.create({
  // 预留样式,如果需要特殊处理可以在这里添加
});