添加下拉刷新功能,优化比赛加载逻辑

This commit is contained in:
yuchenglong
2026-01-21 10:09:14 +08:00
parent 28f83ae871
commit 1842cf93fb

View File

@@ -24,6 +24,7 @@ import { useTranslation } from "react-i18next";
import { import {
ActivityIndicator, ActivityIndicator,
FlatList, FlatList,
RefreshControl,
StyleSheet, StyleSheet,
TouchableOpacity, TouchableOpacity,
View, View,
@@ -44,6 +45,7 @@ export default function HomeScreen() {
const [leagues, setLeagues] = useState<League[]>([]); const [leagues, setLeagues] = useState<League[]>([]);
const [matches, setMatches] = useState<Match[]>([]); const [matches, setMatches] = useState<Match[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false);
const [loadingLeagues, setLoadingLeagues] = useState(false); const [loadingLeagues, setLoadingLeagues] = useState(false);
const [now, setNow] = useState(() => new Date()); const [now, setNow] = useState(() => new Date());
const [liveLeagueIdByMatchId, setLiveLeagueIdByMatchId] = useState< const [liveLeagueIdByMatchId, setLiveLeagueIdByMatchId] = useState<
@@ -321,8 +323,8 @@ export default function HomeScreen() {
} }
}; };
const loadMatches = async (sportId: number) => { const loadMatches = async (sportId: number, isRefresh = false) => {
setLoading(true); if (!isRefresh) setLoading(true);
try { try {
const res = await fetchTodayMatches({ const res = await fetchTodayMatches({
sportId, sportId,
@@ -441,10 +443,17 @@ export default function HomeScreen() {
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} finally { } finally {
setLoading(false); if (!isRefresh) setLoading(false);
} }
}; };
const onRefresh = async () => {
if (!selectedSportId) return;
setRefreshing(true);
await loadMatches(selectedSportId, true);
setRefreshing(false);
};
const loadMoreMatches = async () => { const loadMoreMatches = async () => {
if (loadingMore || loading) return; if (loadingMore || loading) return;
if (!selectedSportId) return; if (!selectedSportId) return;
@@ -604,6 +613,9 @@ export default function HomeScreen() {
renderItem={({ item }) => ( renderItem={({ item }) => (
<MatchCard match={item} onFavoriteToggle={handleFavoriteToggle} /> <MatchCard match={item} onFavoriteToggle={handleFavoriteToggle} />
)} )}
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}
onEndReached={loadMoreMatches} onEndReached={loadMoreMatches}
onEndReachedThreshold={0.4} onEndReachedThreshold={0.4}
ListFooterComponent={ ListFooterComponent={