From 38ba5d0825204979ed1ded2f3e337cfe8792a7e0 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:15:40 +0800 Subject: [PATCH] Import apps/android/src/screens/device-screens.tsx --- apps/android/src/screens/device-screens.tsx | 610 ++++++++++++++++++++ 1 file changed, 610 insertions(+) create mode 100644 apps/android/src/screens/device-screens.tsx diff --git a/apps/android/src/screens/device-screens.tsx b/apps/android/src/screens/device-screens.tsx new file mode 100644 index 0000000..bfb8032 --- /dev/null +++ b/apps/android/src/screens/device-screens.tsx @@ -0,0 +1,610 @@ +import { useEffect, useRef, useState } from 'react' +import { AnimatedEllipsis } from '../ui/animated-ellipsis' +import { ActivityIndicator, Alert, BackHandler, StyleSheet, Text, View } from 'react-native' +import { ArrowLeft, Check, ChevronRight, Laptop, MessageSquareText, Monitor, Pencil, Server, ShieldCheck, Trash2, type LucideIcon } from 'lucide-react-native' +import { useAppStore } from '../state/store' +import type { ConnectionProbeTransport, ConnectionStage, RemoteDevice } from '../types' +import { + BlurHeaderFrame, + Button, + GroupCard, + GroupRow, + IconButton, + KeyValue, + Screen, + SectionTitle, + TopBar, +} from '../ui/components' +import { floatingShadow, radius, spacing, type } from '../ui/theme' +import { useTheme, type ThemeColors } from '../ui/theme-context' +import { useResponsiveLayout } from '../ui/responsive' +import { useThemedStyles } from '../ui/use-themed-styles' +import { strings as zhCN } from '../locales/i18n' +import { useSafeAreaInsets } from 'react-native-safe-area-context' +import { DeviceRenameModal } from './device-rename-modal' +import { ConnectionHero } from './connection-hero' + +function confirmDeviceUnbind(device: RemoteDevice): Promise { + return new Promise(resolve => { + let settled = false + const finish = (confirmed: boolean) => { + if (settled) return + settled = true + resolve(confirmed) + } + Alert.alert(zhCN.devices.forgetTitle(device.name), zhCN.devices.forgetBody, [ + { text: zhCN.common.cancel, style: 'cancel', onPress: () => finish(false) }, + { text: zhCN.devices.forget, style: 'destructive', onPress: () => finish(true) }, + ], { cancelable: true, onDismiss: () => finish(false) }) + }) +} + +const SWIPE_ACTION_WIDTH = 96 + +/** Keep visible session lists honest: refresh execution status while mounted. */ +export function useLiveSessionStatus(enabled = true): void { + const phase = useAppStore(state => state.connection.phase) + useEffect(() => { + if (!enabled || phase !== 'connected') return + void useAppStore.getState().refreshSessionStatus() + const timer = setInterval(() => { void useAppStore.getState().refreshSessionStatus() }, 8_000) + return () => clearInterval(timer) + }, [phase, enabled]) +} + +const connectionStages = ['authenticating', 'transport', 'secure', 'loading'] as const satisfies readonly ConnectionStage[] +const CONNECTION_STUCK_MS = 3000 + +export function ConnectionScreen({ device, onBack, onConnected, embedded = false }: { + device: RemoteDevice + onBack: () => void + onConnected: () => void + embedded?: boolean +}) { + const selectedDevice = useAppStore(state => state.selectedDevice) + const connection = useAppStore(state => state.connection) + const connectionStage = useAppStore(state => state.connectionStage) + const connectionProbeOrder = useAppStore(state => state.connectionProbeOrder) + const connect = useAppStore(state => state.connectDevice) + const disconnect = useAppStore(state => state.disconnect) + const clearError = useAppStore(state => state.clearError) + const [attempt, setAttempt] = useState(0) + const [stuck, setStuck] = useState(false) + const launchedAttempt = useRef(-1) + const completedAttempt = useRef(-1) + const leaving = useRef(false) + const onConnectedRef = useRef(onConnected) + onConnectedRef.current = onConnected + + useEffect(() => () => { + leaving.current = true + if (embedded && launchedAttempt.current >= 0 && completedAttempt.current !== launchedAttempt.current) void disconnect() + }, [disconnect, embedded]) + + const insets = useSafeAreaInsets() + const { colors } = useTheme() + const styles = useThemedStyles(createStyles) + const { landscape, compactHeight } = useResponsiveLayout() + // The hero is the first block of a ScrollView; a compact landscape window + // wastes its scarce height on the old 40/32dp padding pair. + const compactHero = landscape || compactHeight + + useEffect(() => { + const current = useAppStore.getState() + if (current.selectedDevice?.deviceId === device.deviceId && current.connection.phase === 'connected') { + if (completedAttempt.current !== attempt) { + completedAttempt.current = attempt + onConnectedRef.current() + } + return + } + if (current.selectedDevice?.deviceId === device.deviceId + && (current.connection.phase === 'connecting' || current.connection.phase === 'reconnecting')) return + if (launchedAttempt.current === attempt) return + launchedAttempt.current = attempt + let active = true + void connect(device).then(async connected => { + if (!connected) return + // Match the Plugin hand-off: let assistive technology and the visible + // progress state announce completion before replacing this screen. + await new Promise(resolve => setTimeout(resolve, 220)) + if (active && !leaving.current && completedAttempt.current !== attempt) { + completedAttempt.current = attempt + onConnectedRef.current() + } + }) + return () => { active = false } + }, [attempt, connect, connection.phase, device, selectedDevice?.deviceId]) + + const currentStage = connectionStage ?? 'authenticating' + const selectedProbe = probeTransportForMode(connection.stats.mode) + const currentIndex = currentStage === 'ready' + ? connectionStages.length + : Math.max(0, connectionStages.indexOf(currentStage)) + const failed = selectedDevice?.deviceId === device.deviceId + && connection.phase === 'offline' + && connection.error !== undefined + const showBack = failed || stuck + + // Reveal cancel after a stage stalls, or immediately on failure. + useEffect(() => { + if (failed) { + setStuck(true) + return + } + if (currentStage === 'ready') { + setStuck(false) + return + } + setStuck(false) + const timer = setTimeout(() => setStuck(true), CONNECTION_STUCK_MS) + return () => clearTimeout(timer) + }, [attempt, currentStage, failed]) + + const cancel = () => { + leaving.current = true + void disconnect() + onBack() + } + const cancelRef = useRef(cancel) + cancelRef.current = cancel + + useEffect(() => { + const subscription = BackHandler.addEventListener('hardwareBackPress', () => { + if (!showBack) return true + cancelRef.current() + return true + }) + return () => subscription.remove() + }, [showBack]) + + const retry = () => { + clearError() + setAttempt(value => value + 1) + } + + return ( + + {showBack && !embedded && ( + + + + )} + + + {!embedded && connectionStages.indexOf('secure')} + ready={currentStage === 'ready'} + failed={failed} + />} + + + + + {failed ? zhCN.devices.connectionInterrupted : currentStage === 'ready' ? zhCN.devices.connectionReady : zhCN.devices.connecting} + {currentStage !== 'ready' && !failed && } + + + {device.name} + + + {zhCN.devices.harnessShort} + {device.harnessVersion ?? zhCN.devices.unknownVersion} + + + {zhCN.devices.pluginShort} + {device.clientVersion ?? zhCN.devices.unknownVersion} + + + + + + + {connectionStages.map((stage, index) => { + const done = index < currentIndex + const active = index === currentIndex && !failed + const stepFailed = index === currentIndex && failed + const copy = zhCN.devices.connectionSteps[stage] + return ( + + + + {done + ? + : active + ? + : } + + {index < connectionStages.length - 1 && } + + + {copy.title} + {(stage !== 'transport' || connectionProbeOrder.length === 0) && {copy.body}} + {stage === 'transport' && connectionProbeOrder.length > 0 && ( + + {connectionProbeOrder.map((transport, probeIndex) => ( + + {probeIndex > 0 && } + + {probeTransportDiagnosticLabel(transport)} + + + ))} + + )} + {stepFailed && connection.error !== undefined && {connection.error}} + + + ) + })} + + {failed && ( + + {zhCN.devices.connectionInterrupted} + {connection.error} +