增加联赛选择模态框
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { HomeHeader } from "@/components/home-header";
|
import { HomeHeader } from "@/components/home-header";
|
||||||
|
import { LeagueModal } from "@/components/league-modal";
|
||||||
import { MatchCard } from "@/components/match-card";
|
import { MatchCard } from "@/components/match-card";
|
||||||
import { SelectionModal } from "@/components/selection-modal";
|
import { SelectionModal } from "@/components/selection-modal";
|
||||||
import { CalendarModal } from "@/components/simple-calendar";
|
import { CalendarModal } from "@/components/simple-calendar";
|
||||||
@@ -7,8 +8,8 @@ import { ThemedView } from "@/components/themed-view";
|
|||||||
import { IconSymbol } from "@/components/ui/icon-symbol";
|
import { IconSymbol } from "@/components/ui/icon-symbol";
|
||||||
import { Colors } from "@/constants/theme";
|
import { Colors } from "@/constants/theme";
|
||||||
import { useTheme } from "@/context/ThemeContext";
|
import { useTheme } from "@/context/ThemeContext";
|
||||||
import { fetchSports, fetchTodayMatches } from "@/lib/api";
|
import { fetchLeagues, fetchSports, fetchTodayMatches } from "@/lib/api";
|
||||||
import { Match, Sport } from "@/types/api";
|
import { League, Match, Sport } from "@/types/api";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import {
|
import {
|
||||||
@@ -27,6 +28,7 @@ export default function HomeScreen() {
|
|||||||
const filterBg = isDark ? "#2C2C2E" : "#F2F2F7";
|
const filterBg = isDark ? "#2C2C2E" : "#F2F2F7";
|
||||||
|
|
||||||
const [sports, setSports] = useState<Sport[]>([]);
|
const [sports, setSports] = useState<Sport[]>([]);
|
||||||
|
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);
|
||||||
|
|
||||||
@@ -34,14 +36,18 @@ export default function HomeScreen() {
|
|||||||
// 默认足球
|
// 默认足球
|
||||||
const [selectedSportId, setSelectedSportId] = useState<number | null>(1);
|
const [selectedSportId, setSelectedSportId] = useState<number | null>(1);
|
||||||
const [selectedDate, setSelectedDate] = useState(new Date());
|
const [selectedDate, setSelectedDate] = useState(new Date());
|
||||||
|
const [selectedLeagueKey, setSelectedLeagueKey] = useState<string | null>(null);
|
||||||
|
const [filterMode, setFilterMode] = useState<"time" | "league">("time"); // 时间或联赛模式
|
||||||
|
|
||||||
// Modal Visibilities
|
// Modal Visibilities
|
||||||
const [showSportModal, setShowSportModal] = useState(false);
|
const [showSportModal, setShowSportModal] = useState(false);
|
||||||
const [showCalendarModal, setShowCalendarModal] = useState(false);
|
const [showCalendarModal, setShowCalendarModal] = useState(false);
|
||||||
|
const [showLeagueModal, setShowLeagueModal] = useState(false);
|
||||||
|
|
||||||
// Load Sports
|
// Load Sports and Leagues
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadSports();
|
loadSports();
|
||||||
|
loadLeagues();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Load Matches when sport or date changes
|
// Load Matches when sport or date changes
|
||||||
@@ -51,6 +57,13 @@ export default function HomeScreen() {
|
|||||||
}
|
}
|
||||||
}, [selectedSportId, selectedDate]);
|
}, [selectedSportId, selectedDate]);
|
||||||
|
|
||||||
|
// Load Leagues when sport changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedSportId !== null) {
|
||||||
|
loadLeagues();
|
||||||
|
}
|
||||||
|
}, [selectedSportId]);
|
||||||
|
|
||||||
const loadSports = async () => {
|
const loadSports = async () => {
|
||||||
try {
|
try {
|
||||||
const apiList = await fetchSports();
|
const apiList = await fetchSports();
|
||||||
@@ -102,6 +115,18 @@ export default function HomeScreen() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 加载联赛
|
||||||
|
const loadLeagues = async () => {
|
||||||
|
try {
|
||||||
|
if (selectedSportId !== null) {
|
||||||
|
const list = await fetchLeagues(selectedSportId, "");
|
||||||
|
setLeagues(list);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const loadMatches = async (sportId: number) => {
|
const loadMatches = async (sportId: number) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
@@ -134,14 +159,26 @@ export default function HomeScreen() {
|
|||||||
return t(`sports.${sportKey}`, { defaultValue: sport.name });
|
return t(`sports.${sportKey}`, { defaultValue: sport.name });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleLeagueSelect = (leagueKey: string) => {
|
||||||
|
setSelectedLeagueKey(leagueKey);
|
||||||
|
console.log("Selected league:", leagueKey);
|
||||||
|
};
|
||||||
|
|
||||||
const renderHeader = () => (
|
const renderHeader = () => (
|
||||||
<View style={styles.filterContainer}>
|
<View style={styles.filterContainer}>
|
||||||
{/* Time Filter (Mock) */}
|
{/* Time/League Filter Toggle */}
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
style={[styles.filterBtn, { backgroundColor: filterBg }]}
|
style={[styles.filterBtn, { backgroundColor: filterBg }]}
|
||||||
|
onPress={() => setFilterMode(filterMode === "time" ? "league" : "time")}
|
||||||
>
|
>
|
||||||
<IconSymbol name="time-outline" size={18} color={iconColor} />
|
<IconSymbol
|
||||||
<ThemedText style={styles.filterText}>{t("home.time")}</ThemedText>
|
name={filterMode === "time" ? "time-outline" : "trophy-outline"}
|
||||||
|
size={18}
|
||||||
|
color={iconColor}
|
||||||
|
/>
|
||||||
|
<ThemedText style={styles.filterText}>
|
||||||
|
{filterMode === "time" ? t("home.time") : t("home.league")}
|
||||||
|
</ThemedText>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
|
|
||||||
{/* Sport Selector */}
|
{/* Sport Selector */}
|
||||||
@@ -159,7 +196,8 @@ export default function HomeScreen() {
|
|||||||
</ThemedText>
|
</ThemedText>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
|
|
||||||
{/* Date Selector */}
|
{/* Date/League Selector */}
|
||||||
|
{filterMode === "time" ? (
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
style={[styles.filterBtn, { backgroundColor: filterBg }]}
|
style={[styles.filterBtn, { backgroundColor: filterBg }]}
|
||||||
onPress={() => setShowCalendarModal(true)}
|
onPress={() => setShowCalendarModal(true)}
|
||||||
@@ -172,6 +210,19 @@ export default function HomeScreen() {
|
|||||||
{selectedDate.getMinutes().toString().padStart(2, "0")}
|
{selectedDate.getMinutes().toString().padStart(2, "0")}
|
||||||
</ThemedText>
|
</ThemedText>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
|
) : (
|
||||||
|
<TouchableOpacity
|
||||||
|
style={[styles.filterBtn, { backgroundColor: filterBg }]}
|
||||||
|
onPress={() => setShowLeagueModal(true)}
|
||||||
|
>
|
||||||
|
<IconSymbol name="trophy-outline" size={18} color={iconColor} />
|
||||||
|
<ThemedText style={styles.filterText} numberOfLines={1}>
|
||||||
|
{selectedLeagueKey
|
||||||
|
? leagues.find(l => l.key === selectedLeagueKey)?.name || t("home.select_league")
|
||||||
|
: t("home.select_league")}
|
||||||
|
</ThemedText>
|
||||||
|
</TouchableOpacity>
|
||||||
|
)}
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -234,6 +285,14 @@ export default function HomeScreen() {
|
|||||||
selectedDate={selectedDate}
|
selectedDate={selectedDate}
|
||||||
onSelectDate={setSelectedDate}
|
onSelectDate={setSelectedDate}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<LeagueModal
|
||||||
|
visible={showLeagueModal}
|
||||||
|
onClose={() => setShowLeagueModal(false)}
|
||||||
|
leagues={leagues}
|
||||||
|
selectedLeagueKey={selectedLeagueKey}
|
||||||
|
onSelect={handleLeagueSelect}
|
||||||
|
/>
|
||||||
</ThemedView>
|
</ThemedView>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
209
components/league-modal.tsx
Normal file
209
components/league-modal.tsx
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
import { ThemedText } from "@/components/themed-text";
|
||||||
|
import { IconSymbol } from "@/components/ui/icon-symbol";
|
||||||
|
import { Colors } from "@/constants/theme";
|
||||||
|
import { useTheme } from "@/context/ThemeContext";
|
||||||
|
import { League } from "@/types/api";
|
||||||
|
import { Image } from "expo-image";
|
||||||
|
import React from "react";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { Modal, Pressable, ScrollView, StyleSheet, View } from "react-native";
|
||||||
|
import { SafeAreaView } from "react-native-safe-area-context";
|
||||||
|
|
||||||
|
interface LeagueModalProps {
|
||||||
|
visible: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
leagues: League[];
|
||||||
|
selectedLeagueKey: string | null;
|
||||||
|
onSelect: (leagueKey: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function LeagueModal({
|
||||||
|
visible,
|
||||||
|
onClose,
|
||||||
|
leagues,
|
||||||
|
selectedLeagueKey,
|
||||||
|
onSelect,
|
||||||
|
}: LeagueModalProps) {
|
||||||
|
const { theme } = useTheme();
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const isDark = theme === "dark";
|
||||||
|
const bg = isDark ? "#1C1C1E" : "#FFFFFF";
|
||||||
|
const text = isDark ? "#FFFFFF" : "#000000";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
visible={visible}
|
||||||
|
transparent
|
||||||
|
animationType="slide"
|
||||||
|
onRequestClose={onClose}
|
||||||
|
>
|
||||||
|
<Pressable style={styles.overlay} onPress={onClose}>
|
||||||
|
<View />
|
||||||
|
</Pressable>
|
||||||
|
|
||||||
|
<View style={[styles.sheet, { backgroundColor: bg }]}>
|
||||||
|
<SafeAreaView edges={["bottom"]}>
|
||||||
|
<View style={styles.header}>
|
||||||
|
<ThemedText type="subtitle" style={styles.title}>
|
||||||
|
{t("home.select_league")}
|
||||||
|
</ThemedText>
|
||||||
|
<Pressable onPress={onClose} style={styles.closeButton}>
|
||||||
|
<IconSymbol name="close" size={24} color={text} />
|
||||||
|
</Pressable>
|
||||||
|
</View>
|
||||||
|
<ScrollView style={styles.scrollView} showsVerticalScrollIndicator={false}>
|
||||||
|
{leagues.length === 0 ? (
|
||||||
|
<View style={styles.emptyContainer}>
|
||||||
|
<ThemedText style={styles.emptyText}>
|
||||||
|
{t("home.no_leagues")}
|
||||||
|
</ThemedText>
|
||||||
|
</View>
|
||||||
|
) : (
|
||||||
|
leagues.map((league) => {
|
||||||
|
const isSelected = selectedLeagueKey === league.key;
|
||||||
|
const optionBg = isDark ? "#2C2C2E" : "#F5F5F5";
|
||||||
|
const iconBg = isDark ? "#3A3A3C" : "#E5E5EA";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Pressable
|
||||||
|
key={league.id}
|
||||||
|
style={[
|
||||||
|
styles.option,
|
||||||
|
{ backgroundColor: optionBg },
|
||||||
|
]}
|
||||||
|
onPress={() => {
|
||||||
|
onSelect(league.key);
|
||||||
|
onClose();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{/* 左侧图标 */}
|
||||||
|
<View style={[styles.iconContainer, { backgroundColor: iconBg }]}>
|
||||||
|
{league.logo ? (
|
||||||
|
<Image
|
||||||
|
source={{ uri: league.logo }}
|
||||||
|
style={styles.leagueLogo}
|
||||||
|
contentFit="contain"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<IconSymbol name="trophy-outline" size={24} color={text} />
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
|
||||||
|
{/* 中间内容 */}
|
||||||
|
<View style={styles.content}>
|
||||||
|
<ThemedText
|
||||||
|
style={[
|
||||||
|
styles.leagueName,
|
||||||
|
{ fontWeight: isSelected ? "600" : "400" },
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
{league.name}
|
||||||
|
</ThemedText>
|
||||||
|
{league.countryName && (
|
||||||
|
<ThemedText style={styles.countryName}>
|
||||||
|
{league.countryName}
|
||||||
|
</ThemedText>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
|
||||||
|
{/* 右侧指示点 */}
|
||||||
|
<View
|
||||||
|
style={[
|
||||||
|
styles.indicator,
|
||||||
|
{
|
||||||
|
backgroundColor: isSelected
|
||||||
|
? Colors.light.tint
|
||||||
|
: isDark
|
||||||
|
? "#666666"
|
||||||
|
: "#CCCCCC",
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
</Pressable>
|
||||||
|
);
|
||||||
|
})
|
||||||
|
)}
|
||||||
|
</ScrollView>
|
||||||
|
</SafeAreaView>
|
||||||
|
</View>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const styles = StyleSheet.create({
|
||||||
|
overlay: {
|
||||||
|
flex: 1,
|
||||||
|
backgroundColor: "rgba(0,0,0,0.5)",
|
||||||
|
},
|
||||||
|
sheet: {
|
||||||
|
borderTopLeftRadius: 20,
|
||||||
|
borderTopRightRadius: 20,
|
||||||
|
padding: 20,
|
||||||
|
height: "55%",
|
||||||
|
},
|
||||||
|
header: {
|
||||||
|
flexDirection: "row",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
alignItems: "center",
|
||||||
|
marginBottom: 20,
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
fontSize: 18,
|
||||||
|
fontWeight: "600",
|
||||||
|
},
|
||||||
|
closeButton: {
|
||||||
|
width: 32,
|
||||||
|
height: 32,
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
},
|
||||||
|
scrollView: {
|
||||||
|
maxHeight: 400,
|
||||||
|
},
|
||||||
|
emptyContainer: {
|
||||||
|
padding: 40,
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
},
|
||||||
|
emptyText: {
|
||||||
|
fontSize: 16,
|
||||||
|
opacity: 0.6,
|
||||||
|
},
|
||||||
|
option: {
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
paddingVertical: 16,
|
||||||
|
paddingHorizontal: 16,
|
||||||
|
marginBottom: 12,
|
||||||
|
borderRadius: 12,
|
||||||
|
},
|
||||||
|
iconContainer: {
|
||||||
|
width: 48,
|
||||||
|
height: 48,
|
||||||
|
borderRadius: 24,
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
marginRight: 12,
|
||||||
|
},
|
||||||
|
leagueLogo: {
|
||||||
|
width: 32,
|
||||||
|
height: 32,
|
||||||
|
},
|
||||||
|
content: {
|
||||||
|
flex: 1,
|
||||||
|
},
|
||||||
|
leagueName: {
|
||||||
|
fontSize: 16,
|
||||||
|
marginBottom: 4,
|
||||||
|
},
|
||||||
|
countryName: {
|
||||||
|
fontSize: 12,
|
||||||
|
opacity: 0.6,
|
||||||
|
},
|
||||||
|
indicator: {
|
||||||
|
width: 8,
|
||||||
|
height: 8,
|
||||||
|
borderRadius: 4,
|
||||||
|
marginLeft: 12,
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -20,9 +20,12 @@
|
|||||||
"home": {
|
"home": {
|
||||||
"title": "ScoreNow",
|
"title": "ScoreNow",
|
||||||
"time": "Time",
|
"time": "Time",
|
||||||
|
"league": "League",
|
||||||
"select_sport": "Sport",
|
"select_sport": "Sport",
|
||||||
|
"select_league": "Select League",
|
||||||
"loading": "Loading...",
|
"loading": "Loading...",
|
||||||
"no_matches": "No matches found."
|
"no_matches": "No matches found.",
|
||||||
|
"no_leagues": "No leagues available"
|
||||||
},
|
},
|
||||||
"profile": {
|
"profile": {
|
||||||
"title": "My Profile",
|
"title": "My Profile",
|
||||||
|
|||||||
@@ -20,9 +20,12 @@
|
|||||||
"home": {
|
"home": {
|
||||||
"title": "ScoreNow",
|
"title": "ScoreNow",
|
||||||
"time": "时间",
|
"time": "时间",
|
||||||
|
"league": "联赛",
|
||||||
"select_sport": "运动",
|
"select_sport": "运动",
|
||||||
|
"select_league": "选择联赛",
|
||||||
"loading": "加载中...",
|
"loading": "加载中...",
|
||||||
"no_matches": "暂无比赛"
|
"no_matches": "暂无比赛",
|
||||||
|
"no_leagues": "暂无联赛"
|
||||||
},
|
},
|
||||||
"profile": {
|
"profile": {
|
||||||
"title": "我的",
|
"title": "我的",
|
||||||
|
|||||||
Reference in New Issue
Block a user