From b5fd763c7e3e1a7991675012201caf243b5145ce Mon Sep 17 00:00:00 2001 From: yuchenglong Date: Fri, 16 Jan 2026 16:15:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E6=AF=94=E8=B5=9B=E5=92=8C=E7=90=83?= =?UTF-8?q?=E9=98=9F=E6=B7=BB=E5=8A=A0=E6=94=B6=E8=97=8F=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E6=9B=B4=E6=96=B0=E7=8A=B6=E6=80=81=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=92=8C=E5=88=87=E6=8D=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/live-detail/live-score-header.tsx | 128 ++++++++++++++++-- components/match-detail/score-header.tsx | 134 +++++++++++++++++-- lib/api.ts | 3 + 3 files changed, 240 insertions(+), 25 deletions(-) diff --git a/components/live-detail/live-score-header.tsx b/components/live-detail/live-score-header.tsx index 3433373..604eca6 100644 --- a/components/live-detail/live-score-header.tsx +++ b/components/live-detail/live-score-header.tsx @@ -38,6 +38,11 @@ export function LiveScoreHeader({ match, topInset }: LiveScoreHeaderProps) { const [isFav, setIsFav] = useState(false); const [favLoading, setFavLoading] = useState(false); + const [isHomeFav, setIsHomeFav] = useState(false); + const [isAwayFav, setIsAwayFav] = useState(false); + const [homeFavLoading, setHomeFavLoading] = useState(false); + const [awayFavLoading, setAwayFavLoading] = useState(false); + // 检查收藏状态 React.useEffect(() => { const loadFavStatus = async () => { @@ -51,6 +56,36 @@ export function LiveScoreHeader({ match, topInset }: LiveScoreHeaderProps) { loadFavStatus(); }, [match.event_key]); + // 检查主队收藏状态 + React.useEffect(() => { + const loadHomeFav = async () => { + try { + const res = await checkFavorite("team", match.home_team_key.toString()); + setIsHomeFav(res.isFavorite); + } catch (error) { + console.error("Check home team favorite status error:", error); + } + }; + if (match.home_team_key) { + loadHomeFav(); + } + }, [match.home_team_key]); + + // 检查客队收藏状态 + React.useEffect(() => { + const loadAwayFav = async () => { + try { + const res = await checkFavorite("team", match.away_team_key.toString()); + setIsAwayFav(res.isFavorite); + } catch (error) { + console.error("Check away team favorite status error:", error); + } + }; + if (match.away_team_key) { + loadAwayFav(); + } + }, [match.away_team_key]); + const toggleFavorite = async () => { if (favLoading) return; setFavLoading(true); @@ -76,6 +111,38 @@ export function LiveScoreHeader({ match, topInset }: LiveScoreHeaderProps) { setFavLoading(false); } }; + const toggleTeamFavorite = async (teamKey: number, isHome: boolean) => { + const isTeamFav = isHome ? isHomeFav : isAwayFav; + const setLoading = isHome ? setHomeFavLoading : setAwayFavLoading; + const setFav = isHome ? setIsHomeFav : setIsAwayFav; + const loading = isHome ? homeFavLoading : awayFavLoading; + + if (loading) return; + setLoading(true); + + const newFavState = !isTeamFav; + try { + if (newFavState) { + await addFavorite({ + teamId: teamKey, + type: "team", + typeId: teamKey.toString(), + notify: true, + }); + } else { + await removeFavorite({ + type: "team", + typeId: teamKey.toString(), + }); + } + setFav(newFavState); + } catch (error) { + console.error("Toggle team favorite error:", error); + } finally { + setLoading(false); + } + }; + const lastServerMatchRef = React.useRef( `${match.event_status}-${match.event_time}` ); @@ -163,11 +230,24 @@ export function LiveScoreHeader({ match, topInset }: LiveScoreHeaderProps) { {/* Score Section */} - - + + toggleTeamFavorite(match.home_team_key, true)} + disabled={homeFavLoading} + style={styles.starBtnLeft} + > + + + + + {match.event_home_team} @@ -202,11 +282,24 @@ export function LiveScoreHeader({ match, topInset }: LiveScoreHeaderProps) { - - + + + + + toggleTeamFavorite(match.away_team_key, false)} + disabled={awayFavLoading} + style={styles.starBtnRight} + > + + {match.event_away_team} @@ -261,13 +354,26 @@ const styles = StyleSheet.create({ alignItems: "center", width: "30%", }, + teamLogoRow: { + flexDirection: "row", + alignItems: "center", + justifyContent: "center", + marginBottom: 10, + }, logoContainer: { width: 70, height: 70, - marginBottom: 10, justifyContent: "center", alignItems: "center", }, + starBtnLeft: { + padding: 8, + marginRight: -4, + }, + starBtnRight: { + padding: 8, + marginLeft: -4, + }, teamLogo: { width: 60, height: 60, diff --git a/components/match-detail/score-header.tsx b/components/match-detail/score-header.tsx index 50aa96f..f7faa75 100644 --- a/components/match-detail/score-header.tsx +++ b/components/match-detail/score-header.tsx @@ -22,14 +22,19 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) { const [isFav, setIsFav] = useState(false); const [favLoading, setFavLoading] = useState(false); - // 检查收藏状态 + const [isHomeFav, setIsHomeFav] = useState(false); + const [isAwayFav, setIsAwayFav] = useState(false); + const [homeFavLoading, setHomeFavLoading] = useState(false); + const [awayFavLoading, setAwayFavLoading] = useState(false); + + // 检查比赛收藏状态 React.useEffect(() => { const loadFavStatus = async () => { try { const res = await checkFavorite("match", match.eventKey.toString()); setIsFav(res.isFavorite); } catch (error) { - console.error("Check favorite status error:", error); + console.error("Check match favorite status error:", error); } }; if (match.eventKey) { @@ -37,6 +42,36 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) { } }, [match.eventKey]); + // 检查主队收藏状态 + React.useEffect(() => { + const loadHomeFav = async () => { + try { + const res = await checkFavorite("team", match.homeTeamKey.toString()); + setIsHomeFav(res.isFavorite); + } catch (error) { + console.error("Check home team favorite status error:", error); + } + }; + if (match.homeTeamKey) { + loadHomeFav(); + } + }, [match.homeTeamKey]); + + // 检查客队收藏状态 + React.useEffect(() => { + const loadAwayFav = async () => { + try { + const res = await checkFavorite("team", match.awayTeamKey.toString()); + setIsAwayFav(res.isFavorite); + } catch (error) { + console.error("Check away team favorite status error:", error); + } + }; + if (match.awayTeamKey) { + loadAwayFav(); + } + }, [match.awayTeamKey]); + const toggleFavorite = async () => { if (favLoading) return; setFavLoading(true); @@ -44,7 +79,7 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) { try { if (newFavState) { await addFavorite({ - matchId: Number(match.eventKey), + matchId: Number(match.eventKey), type: "match", typeId: match.eventKey.toString(), notify: true, @@ -63,6 +98,38 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) { } }; + const toggleTeamFavorite = async (teamKey: string, isHome: boolean) => { + const isTeamFav = isHome ? isHomeFav : isAwayFav; + const setLoading = isHome ? setHomeFavLoading : setAwayFavLoading; + const setFav = isHome ? setIsHomeFav : setIsAwayFav; + const loading = isHome ? homeFavLoading : awayFavLoading; + + if (loading) return; + setLoading(true); + + const newFavState = !isTeamFav; + try { + if (newFavState) { + await addFavorite({ + teamId: Number(teamKey), + type: "team", + typeId: teamKey.toString(), + notify: true, + }); + } else { + await removeFavorite({ + type: "team", + typeId: teamKey.toString(), + }); + } + setFav(newFavState); + } catch (error) { + console.error("Toggle team favorite error:", error); + } finally { + setLoading(false); + } + }; + return ( - - + + toggleTeamFavorite(match.homeTeamKey, true)} + disabled={homeFavLoading} + style={styles.starBtnLeft} + > + + + + + {match.eventHomeTeam} @@ -133,11 +213,24 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) { - - + + + + + toggleTeamFavorite(match.awayTeamKey, false)} + disabled={awayFavLoading} + style={styles.starBtnRight} + > + + {match.eventAwayTeam} @@ -202,13 +295,26 @@ const styles = StyleSheet.create({ flex: 1, alignItems: "center", }, + teamLogoRow: { + flexDirection: "row", + alignItems: "center", + justifyContent: "center", + marginBottom: 10, + }, logoContainer: { width: 70, height: 70, - marginBottom: 10, justifyContent: "center", alignItems: "center", }, + starBtnLeft: { + padding: 8, + marginRight: -4, + }, + starBtnRight: { + padding: 8, + marginLeft: -4, + }, teamLogo: { width: 60, height: 60, diff --git a/lib/api.ts b/lib/api.ts index 743e68c..61b99de 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -431,6 +431,7 @@ export const fetchUserProfile = async (): Promise => { export const addFavorite = async (request: FavoriteRequest): Promise => { try { + // console.log("Adding favorite with request:", request); const response = await apiClient.post>( API_ENDPOINTS.FAVORITES, request @@ -450,6 +451,7 @@ export const removeFavorite = async (request: { typeId: string; }): Promise => { try { + console.log("Removing favorite with request:", request); const response = await apiClient.delete>( API_ENDPOINTS.FAVORITES, { data: request } @@ -498,6 +500,7 @@ export const fetchFavorites = async ( } ); if (response.data.code === 0) { + console.log("Fetched favorites:", JSON.stringify(response.data.data)); return response.data.data; } throw new Error(response.data.message);