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

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