添加收藏

This commit is contained in:
yuchenglong
2026-01-16 14:41:15 +08:00
parent 4bccf39a8b
commit d20080eaf3
11 changed files with 656 additions and 75 deletions

View File

@@ -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,26 @@ 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 +342,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);
const filteredMatches = useMemo(
@@ -457,7 +493,9 @@ export default function HomeScreen() {
<FlatList
data={filteredMatches}
keyExtractor={(item) => item.id}
renderItem={({ item }) => <MatchCard match={item} />}
renderItem={({ item }) => (
<MatchCard match={item} onFavoriteToggle={handleFavoriteToggle} />
)}
contentContainerStyle={styles.listContent}
ListEmptyComponent={
<View style={styles.center}>