diff --git a/app/(tabs)/memo-list.tsx b/app/(tabs)/memo-list.tsx index f93d5d4..55a2ec3 100644 --- a/app/(tabs)/memo-list.tsx +++ b/app/(tabs)/memo-list.tsx @@ -3,7 +3,7 @@ import UploaderProgress from "@/components/file-upload/upload-progress/uploader- import AskNavbar from "@/components/layout/ask"; import { endUploadSessionInDb, syncUploadSessionState } from "@/features/appState/appStateSlice"; import { triggerManualUpload } from "@/lib/background-uploader/automatic"; -import { getUploadTasksSince, UploadTask } from "@/lib/db"; +import { exist_pending_tasks, getUploadTasksSince, UploadTask } from "@/lib/db"; import { fetchApi } from "@/lib/server-api-util"; import { useAppDispatch, useAppSelector } from "@/store"; import { Chat } from "@/types/ask"; @@ -54,8 +54,11 @@ const MemoList = () => { let isActive = true; let interval: any = null; - const manageUploadState = async () => { - if (!isActive) return; + const manageUploadState = async (restore_session: boolean = false) => { + if (!isActive) { + console.log('MemoList manageUploadState is not active'); + return; + } // 首先,同步Redux中的会话开始时间 const action = await dispatch(syncUploadSessionState()); @@ -86,25 +89,35 @@ const MemoList = () => { } }; - const triggerAndMonitor = async () => { - console.log('MemoList focused, triggering foreground media upload check.'); - const now = new Date(); - const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000); + const initializeUploadProcess = async () => { + // First, check if a session is already active. + const action = await dispatch(syncUploadSessionState()); + const existingSessionStartTime = action.payload as number | null; + const existPendingTasks = await exist_pending_tasks(); - // This now returns the start time string or null - const sessionStartTimeStr = await triggerManualUpload(oneDayAgo, now); - - // Only start monitoring if a session was actually started. - if (sessionStartTimeStr) { - console.log(`Upload session started with time: ${sessionStartTimeStr}, beginning to monitor...`); - // Immediately check the state once, then the interval will take over. - // This ensures the progress bar appears instantly. - manageUploadState(); + if (existingSessionStartTime && existPendingTasks) { + console.log('MemoList focused, existing session found. Monitoring progress.'); + // If a session exists, just start monitoring. + manageUploadState(true); // Initial check interval = setInterval(manageUploadState, 2000); + } else { + // If no session, then try to trigger a new upload. + console.log('MemoList focused, no existing session. Triggering foreground media upload check.'); + const now = new Date(); + const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000); + + const newSessionStartTimeStr = await triggerManualUpload(oneDayAgo, now); + + if (newSessionStartTimeStr) { + console.log(`New upload session started with time: ${newSessionStartTimeStr}, beginning to monitor...`); + // A new session was started, so start monitoring. + manageUploadState(); // Initial check + interval = setInterval(manageUploadState, 2000); + } } }; - triggerAndMonitor(); + initializeUploadProcess(); return () => { isActive = false; @@ -115,11 +128,25 @@ const MemoList = () => { }, [dispatch]) ); - return ( - + const renderHeader = () => ( + <> + {process.env.NODE_ENV === 'development' && router.push('/debug')} + > + + 进入db调试页面 + + } + + {/* 顶部标题和上传按钮 */} + + Memo List + + {/* 上传进度展示区域 */} - {process.env.NODE_ENV === 'development' && uploadSessionStartTime && ( - + {uploadSessionStartTime && progressInfo.total > 0 && ( + { /> )} + + ); + return ( + {/* */} - router.push('/debug')} - > - - 进入db调试页面 - - - - {/* 顶部标题和上传按钮 */} - - Memo List - - {/* 历史对话 */} item.session_id} ItemSeparatorComponent={() => ( diff --git a/lib/db.ts b/lib/db.ts index fd817c7..5aadded 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -111,6 +111,13 @@ export async function getUploadTasksSince(timestamp: number): Promise { + const rows = await db.getAllAsync( + 'SELECT * FROM upload_tasks WHERE status = "pending" OR status = "uploading"' + ); + return rows.length > 0; +} + // 检查一组文件URI,返回那些在数据库中不存在或是未成功上传的文件的URI export async function filterExistingFiles(fileUris: string[]): Promise { if (fileUris.length === 0) {