添加收藏
This commit is contained in:
@@ -9,7 +9,12 @@ import { IconSymbol } from "@/components/ui/icon-symbol";
|
||||
import { Colors } from "@/constants/theme";
|
||||
import { useAppState } from "@/context/AppStateContext";
|
||||
import { useTheme } from "@/context/ThemeContext";
|
||||
import { fetchLeagues, fetchSports, fetchTodayMatches } from "@/lib/api";
|
||||
import {
|
||||
checkFavorite,
|
||||
fetchLeagues,
|
||||
fetchSports,
|
||||
fetchTodayMatches,
|
||||
} from "@/lib/api";
|
||||
import { League, Match, Sport } from "@/types/api";
|
||||
import React, { useEffect, useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
@@ -310,7 +315,27 @@ export default function HomeScreen() {
|
||||
selectedDate,
|
||||
deviceTimeZone
|
||||
);
|
||||
setMatches(list);
|
||||
|
||||
// 直接传递 match.id 查询是否收藏,并更新列表状态
|
||||
const listWithFavStatus = await Promise.all(
|
||||
list.map(async (m) => {
|
||||
try {
|
||||
// 查询比赛是否已被收藏
|
||||
const favRes = await checkFavorite("match", m.id);
|
||||
return { ...m, fav: favRes.isFavorite };
|
||||
} catch (error) {
|
||||
console.error(`Check favorite failed for match ${m.id}:`, error);
|
||||
return m;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
// 将收藏的比赛置顶
|
||||
const sortedList = [...listWithFavStatus].sort((a, b) => {
|
||||
if (a.fav === b.fav) return 0;
|
||||
return a.fav ? -1 : 1;
|
||||
});
|
||||
setMatches(sortedList);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
} finally {
|
||||
@@ -318,6 +343,18 @@ export default function HomeScreen() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleFavoriteToggle = (matchId: string, isFav: boolean) => {
|
||||
setMatches((prev) => {
|
||||
const updated = prev.map((m) =>
|
||||
m.id === matchId ? { ...m, fav: isFav } : m
|
||||
);
|
||||
return [...updated].sort((a, b) => {
|
||||
if (a.fav === b.fav) return 0;
|
||||
return a.fav ? -1 : 1;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const currentSport = sports.find((s) => s.id === selectedSportId);
|
||||
|
||||
// 获取当前运动的国际化名称
|
||||
@@ -452,7 +489,9 @@ export default function HomeScreen() {
|
||||
<FlatList
|
||||
data={matches}
|
||||
keyExtractor={(item) => item.id}
|
||||
renderItem={({ item }) => <MatchCard match={item} />}
|
||||
renderItem={({ item }) => (
|
||||
<MatchCard match={item} onFavoriteToggle={handleFavoriteToggle} />
|
||||
)}
|
||||
contentContainerStyle={styles.listContent}
|
||||
ListEmptyComponent={
|
||||
<View style={styles.center}>
|
||||
|
||||
Reference in New Issue
Block a user