From 5b6631624d20837e6a17d45ee6be3cee06aee6e7 Mon Sep 17 00:00:00 2001 From: zimplexing Date: Wed, 13 Aug 2025 19:13:18 +0800 Subject: [PATCH] feat(home): enhance HomeScreen with API configuration checks and error handling --- app/index.tsx | 32 +++++++++++++++++++++++++++----- constants/UpdateConfig.ts | 4 ++-- stores/homeStore.ts | 5 +++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/index.tsx b/app/index.tsx index e386cd0..2df7da2 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -10,6 +10,7 @@ import { Search, Settings, LogOut, Heart } from "lucide-react-native"; import { StyledButton } from "@/components/StyledButton"; import useHomeStore, { RowItem, Category } from "@/stores/homeStore"; import useAuthStore from "@/stores/authStore"; +import { useSettingsStore } from "@/stores/settingsStore"; import CustomScrollView from "@/components/CustomScrollView"; import { useResponsiveLayout } from "@/hooks/useResponsiveLayout"; import { getCommonResponsiveStyles } from "@/utils/ResponsiveStyles"; @@ -40,8 +41,10 @@ export default function HomeScreen() { loadMoreData, selectCategory, refreshPlayRecords, + clearError, } = useHomeStore(); const { isLoggedIn, logout } = useAuthStore(); + const { apiBaseUrl } = useSettingsStore(); useFocusEffect( useCallback(() => { @@ -50,20 +53,33 @@ export default function HomeScreen() { ); useEffect(() => { - if (selectedCategory && !selectedCategory.tags) { + // 只有在 apiBaseUrl 存在时才调用 fetchInitialData,避免时序问题 + if (selectedCategory && !selectedCategory.tags && apiBaseUrl) { fetchInitialData(); } else if (selectedCategory?.tags && !selectedCategory.tag) { const defaultTag = selectedCategory.tags[0]; setSelectedTag(defaultTag); selectCategory({ ...selectedCategory, tag: defaultTag }); } - }, [selectedCategory, fetchInitialData, selectCategory]); + }, [selectedCategory, fetchInitialData, selectCategory, apiBaseUrl]); useEffect(() => { - if (selectedCategory && selectedCategory.tag) { + // 只有在 apiBaseUrl 存在时才调用 fetchInitialData,避免时序问题 + if (selectedCategory && selectedCategory.tag && apiBaseUrl) { fetchInitialData(); } - }, [fetchInitialData, selectedCategory, selectedCategory.tag]); + }, [fetchInitialData, selectedCategory, selectedCategory.tag, apiBaseUrl]); + + // 检查是否需要显示API配置提示 + const shouldShowApiConfig = !apiBaseUrl && selectedCategory && !selectedCategory.tags; + + // 清除错误状态,当API未配置时 + useEffect(() => { + if (shouldShowApiConfig && error) { + // 如果需要显示API配置提示,清除之前的错误状态 + clearError(); + } + }, [shouldShowApiConfig, error, clearError]); useEffect(() => { if (!loading && contentData.length > 0) { @@ -262,7 +278,13 @@ export default function HomeScreen() { )} {/* 内容网格 */} - {loading ? ( + {shouldShowApiConfig ? ( + + + 请点击右上角设置按钮,配置您的服务器地址 + + + ) : loading ? ( diff --git a/constants/UpdateConfig.ts b/constants/UpdateConfig.ts index 083b8e6..b224af3 100644 --- a/constants/UpdateConfig.ts +++ b/constants/UpdateConfig.ts @@ -6,8 +6,8 @@ export const UPDATE_CONFIG = { CHECK_INTERVAL: 12 * 60 * 60 * 1000, // 12小时 // GitHub相关URL - GITHUB_RAW_URL: 'https://raw.githubusercontent.com/zimplexing/OrionTV/refs/heads/master/package.json', - GITHUB_RELEASE_URL_TEMPLATE: 'https://github.com/zimplexing/OrionTV/releases/download/v{version}/orionTV.{version}.apk', + GITHUB_RAW_URL: 'https://ghfast.top/https://raw.githubusercontent.com/zimplexing/OrionTV/refs/heads/master/package.json', + GITHUB_RELEASE_URL_TEMPLATE: 'https://ghfast.top/https://github.com/zimplexing/OrionTV/releases/download/v{version}/orionTV.{version}.apk', // 是否显示更新日志 SHOW_RELEASE_NOTES: true, diff --git a/stores/homeStore.ts b/stores/homeStore.ts index 3939e90..bc808d8 100644 --- a/stores/homeStore.ts +++ b/stores/homeStore.ts @@ -68,6 +68,7 @@ interface HomeState { loadMoreData: () => Promise; selectCategory: (category: Category) => void; refreshPlayRecords: () => Promise; + clearError: () => void; } // 内存缓存,应用生命周期内有效 @@ -261,6 +262,10 @@ const useHomeStore = create((set, get) => ({ get().fetchInitialData(); }, + + clearError: () => { + set({ error: null }); + }, })); export default useHomeStore;