Refactor components to use Zustand for state management

- Updated EpisodeSelectionModal to utilize Zustand for episode selection state.
- Refactored PlayerControls to manage playback state and controls using Zustand.
- Simplified SettingsModal to handle settings state with Zustand.
- Introduced homeStore for managing home screen categories and content data.
- Created playerStore for managing video playback and episode details.
- Added settingsStore for managing API settings and modal visibility.
- Updated package.json to include Zustand as a dependency.
- Cleaned up code formatting and improved readability across components.
This commit is contained in:
zimplexing
2025-07-06 20:45:42 +08:00
parent b2b667ae91
commit 08e24dd748
11 changed files with 592 additions and 585 deletions

View File

@@ -1,16 +1,11 @@
import { import { DarkTheme, DefaultTheme, ThemeProvider } from '@react-navigation/native';
DarkTheme, import { useFonts } from 'expo-font';
DefaultTheme, import { Stack } from 'expo-router';
ThemeProvider, import * as SplashScreen from 'expo-splash-screen';
} from "@react-navigation/native"; import { useEffect } from 'react';
import { useFonts } from "expo-font"; import { Platform, useColorScheme } from 'react-native';
import { Stack } from "expo-router";
import * as SplashScreen from "expo-splash-screen";
import { useEffect } from "react";
import { Platform } from "react-native";
import { useColorScheme } from "@/hooks/useColorScheme"; import { useSettingsStore } from '@/stores/settingsStore';
import { initializeApi } from "@/services/api";
// Prevent the splash screen from auto-hiding before asset loading is complete. // Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync(); SplashScreen.preventAutoHideAsync();
@@ -18,8 +13,13 @@ SplashScreen.preventAutoHideAsync();
export default function RootLayout() { export default function RootLayout() {
const colorScheme = useColorScheme(); const colorScheme = useColorScheme();
const [loaded, error] = useFonts({ const [loaded, error] = useFonts({
SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"), SpaceMono: require('../assets/fonts/SpaceMono-Regular.ttf'),
}); });
const initializeSettings = useSettingsStore(state => state.loadSettings);
useEffect(() => {
initializeSettings();
}, [initializeSettings]);
useEffect(() => { useEffect(() => {
if (loaded || error) { if (loaded || error) {
@@ -30,22 +30,16 @@ export default function RootLayout() {
} }
}, [loaded, error]); }, [loaded, error]);
useEffect(() => {
initializeApi();
}, []);
if (!loaded && !error) { if (!loaded && !error) {
return null; return null;
} }
return ( return (
<ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}> <ThemeProvider value={colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
<Stack> <Stack>
<Stack.Screen name="index" options={{ headerShown: false }} /> <Stack.Screen name="index" options={{ headerShown: false }} />
<Stack.Screen name="detail" options={{ headerShown: false }} /> <Stack.Screen name="detail" options={{ headerShown: false }} />
{Platform.OS !== "web" && ( {Platform.OS !== 'web' && <Stack.Screen name="play" options={{ headerShown: false }} />}
<Stack.Screen name="play" options={{ headerShown: false }} />
)}
<Stack.Screen name="search" options={{ headerShown: false }} /> <Stack.Screen name="search" options={{ headerShown: false }} />
<Stack.Screen name="+not-found" /> <Stack.Screen name="+not-found" />
</Stack> </Stack>

View File

@@ -1,50 +1,15 @@
import React, { useState, useEffect, useCallback, useRef } from 'react'; import React, { useEffect, useCallback, useRef } from 'react';
import { View, StyleSheet, ActivityIndicator, FlatList, Pressable, Dimensions } from 'react-native'; import { View, StyleSheet, ActivityIndicator, FlatList, Pressable, Dimensions } from 'react-native';
import { ThemedView } from '@/components/ThemedView'; import { ThemedView } from '@/components/ThemedView';
import { ThemedText } from '@/components/ThemedText'; import { ThemedText } from '@/components/ThemedText';
import { api } from '@/services/api'; import { api } from '@/services/api';
import { SearchResult } from '@/services/api';
import { PlayRecord } from '@/services/storage';
export type RowItem = (SearchResult | PlayRecord) & {
id: string;
source: string;
title: string;
poster: string;
progress?: number;
lastPlayed?: number;
episodeIndex?: number;
sourceName?: string;
totalEpisodes?: number;
year?: string;
rate?: string;
};
import VideoCard from '@/components/VideoCard.tv'; import VideoCard from '@/components/VideoCard.tv';
import { PlayRecordManager } from '@/services/storage';
import { useFocusEffect, useRouter } from 'expo-router'; import { useFocusEffect, useRouter } from 'expo-router';
import { useColorScheme } from 'react-native'; import { useColorScheme } from 'react-native';
import { Search, Settings } from 'lucide-react-native'; import { Search, Settings } from 'lucide-react-native';
import { SettingsModal } from '@/components/SettingsModal'; import { SettingsModal } from '@/components/SettingsModal';
import useHomeStore, { RowItem, Category } from '@/stores/homeStore';
// --- 类别定义 --- import { useSettingsStore } from '@/stores/settingsStore';
interface Category {
title: string;
type?: 'movie' | 'tv' | 'record';
tag?: string;
}
const initialCategories: Category[] = [
{ title: '最近播放', type: 'record' },
{ title: '热门剧集', type: 'tv', tag: '热门' },
{ title: '综艺', type: 'tv', tag: '综艺' },
{ title: '热门电影', type: 'movie', tag: '热门' },
{ title: '豆瓣 Top250', type: 'movie', tag: 'top250' },
{ title: '儿童', type: 'movie', tag: '少儿' },
{ title: '美剧', type: 'tv', tag: '美剧' },
{ title: '韩剧', type: 'tv', tag: '韩剧' },
{ title: '日剧', type: 'tv', tag: '日剧' },
{ title: '日漫', type: 'tv', tag: '日本动画' },
];
const NUM_COLUMNS = 5; const NUM_COLUMNS = 5;
const { width } = Dimensions.get('window'); const { width } = Dimensions.get('window');
@@ -53,146 +18,40 @@ const ITEM_WIDTH = width / NUM_COLUMNS - 24;
export default function HomeScreen() { export default function HomeScreen() {
const router = useRouter(); const router = useRouter();
const colorScheme = useColorScheme(); const colorScheme = useColorScheme();
const [categories, setCategories] = useState<Category[]>(initialCategories);
const [selectedCategory, setSelectedCategory] = useState<Category>(categories[0]);
const [contentData, setContentData] = useState<RowItem[]>([]);
const [loading, setLoading] = useState(true);
const [loadingMore, setLoadingMore] = useState(false);
const [error, setError] = useState<string | null>(null);
const [isSettingsVisible, setSettingsVisible] = useState(false);
const [pageStart, setPageStart] = useState(0);
const [hasMore, setHasMore] = useState(true);
const flatListRef = useRef<FlatList>(null); const flatListRef = useRef<FlatList>(null);
// --- 数据获取逻辑 --- const {
const fetchPlayRecords = async () => { categories,
const records = await PlayRecordManager.getAll(); selectedCategory,
return Object.entries(records) contentData,
.map(([key, record]) => { loading,
const [source, id] = key.split('+'); loadingMore,
return { error,
id, fetchInitialData,
source, loadMoreData,
title: record.title, selectCategory,
poster: record.cover, refreshPlayRecords,
progress: record.play_time / record.total_time, } = useHomeStore();
lastPlayed: record.save_time,
episodeIndex: record.index,
sourceName: record.source_name,
totalEpisodes: record.total_episodes,
} as RowItem;
})
.filter(record => record.progress !== undefined && record.progress > 0 && record.progress < 1)
.sort((a, b) => (b.lastPlayed || 0) - (a.lastPlayed || 0));
};
const fetchData = async (category: Category, start: number, preloadedRecords?: RowItem[]) => { const showSettingsModal = useSettingsStore(state => state.showModal);
if (category.type === 'record') {
const records = preloadedRecords ?? (await fetchPlayRecords());
if (records.length === 0 && categories.some(c => c.type === 'record')) {
// 如果没有播放记录,则移除"最近播放"分类并选择第一个真实分类
const newCategories = categories.filter(c => c.type !== 'record');
setCategories(newCategories);
if (newCategories.length > 0) {
handleCategorySelect(newCategories[0]);
}
} else {
setContentData(records);
setHasMore(false);
}
setLoading(false);
return;
}
if (!category.type || !category.tag) return;
setLoadingMore(start > 0);
setError(null);
try {
const result = await api.getDoubanData(category.type, category.tag, 20, start);
if (result.list.length === 0) {
setHasMore(false);
} else {
const newItems = result.list.map(item => ({
...item,
id: item.title, // 临时ID
source: 'douban',
})) as RowItem[];
setContentData(prev => (start === 0 ? newItems : [...prev, ...newItems]));
setPageStart(prev => prev + result.list.length);
setHasMore(true);
}
} catch (err: any) {
if (err.message === 'API_URL_NOT_SET') {
setError('请点击右上角设置按钮,配置您的 API 地址');
} else {
setError('加载失败,请重试');
}
} finally {
setLoading(false);
setLoadingMore(false);
}
};
// --- Effects ---
useFocusEffect( useFocusEffect(
useCallback(() => { useCallback(() => {
const manageRecordCategory = async () => { refreshPlayRecords();
const records = await fetchPlayRecords(); }, [refreshPlayRecords])
const hasRecords = records.length > 0;
setCategories(currentCategories => {
const recordCategoryExists = currentCategories.some(c => c.type === 'record');
if (hasRecords && !recordCategoryExists) {
// Add 'Recent Plays' if records exist and the tab doesn't
return [initialCategories[0], ...currentCategories];
}
return currentCategories;
});
// If 'Recent Plays' is selected, always refresh its data.
// This will also handle removing the tab if records have disappeared.
if (selectedCategory.type === 'record') {
loadInitialData(records);
}
};
manageRecordCategory();
}, [selectedCategory])
); );
useEffect(() => { useEffect(() => {
loadInitialData(); fetchInitialData();
}, [selectedCategory]);
const loadInitialData = (records?: RowItem[]) => {
setLoading(true);
setContentData([]);
setPageStart(0);
setHasMore(true);
flatListRef.current?.scrollToOffset({ animated: false, offset: 0 }); flatListRef.current?.scrollToOffset({ animated: false, offset: 0 });
fetchData(selectedCategory, 0, records); }, [selectedCategory, fetchInitialData]);
};
const loadMoreData = () => {
if (loading || loadingMore || !hasMore || selectedCategory.type === 'record') return;
fetchData(selectedCategory, pageStart);
};
const handleCategorySelect = (category: Category) => { const handleCategorySelect = (category: Category) => {
setSelectedCategory(category); selectCategory(category);
}; };
// --- 渲染组件 ---
const renderCategory = ({ item }: { item: Category }) => { const renderCategory = ({ item }: { item: Category }) => {
const isSelected = selectedCategory.title === item.title; const isSelected = selectedCategory?.title === item.title;
return ( return (
<Pressable <Pressable
style={({ focused }) => [ style={({ focused }) => [
@@ -221,7 +80,7 @@ export default function HomeScreen() {
sourceName={item.sourceName} sourceName={item.sourceName}
totalEpisodes={item.totalEpisodes} totalEpisodes={item.totalEpisodes}
api={api} api={api}
onRecordDeleted={loadInitialData} // For "Recent Plays" onRecordDeleted={fetchInitialData} // For "Recent Plays"
/> />
</View> </View>
); );
@@ -245,7 +104,7 @@ export default function HomeScreen() {
</Pressable> </Pressable>
<Pressable <Pressable
style={({ focused }) => [styles.searchButton, focused && styles.searchButtonFocused]} style={({ focused }) => [styles.searchButton, focused && styles.searchButtonFocused]}
onPress={() => setSettingsVisible(true)} onPress={showSettingsModal}
> >
<Settings color={colorScheme === 'dark' ? 'white' : 'black'} size={24} /> <Settings color={colorScheme === 'dark' ? 'white' : 'black'} size={24} />
</Pressable> </Pressable>
@@ -293,14 +152,7 @@ export default function HomeScreen() {
} }
/> />
)} )}
<SettingsModal <SettingsModal />
visible={isSettingsVisible}
onCancel={() => setSettingsVisible(false)}
onSave={() => {
setSettingsVisible(false);
loadInitialData();
}}
/>
</ThemedView> </ThemedView>
); );
} }

View File

@@ -1,49 +1,52 @@
import React, { useState, useRef } from "react"; import React, { useEffect, useRef } from 'react';
import { import { View, StyleSheet, TouchableOpacity, ActivityIndicator } from 'react-native';
View, import { useLocalSearchParams } from 'expo-router';
StyleSheet, import { Video, ResizeMode } from 'expo-av';
TouchableOpacity, import { useKeepAwake } from 'expo-keep-awake';
ActivityIndicator, import { ThemedView } from '@/components/ThemedView';
} from "react-native"; import { PlayerControls } from '@/components/PlayerControls';
import { useRouter } from "expo-router"; import { EpisodeSelectionModal } from '@/components/EpisodeSelectionModal';
import { Video, ResizeMode } from "expo-av"; import { NextEpisodeOverlay } from '@/components/NextEpisodeOverlay';
import { useKeepAwake } from "expo-keep-awake"; import { LoadingOverlay } from '@/components/LoadingOverlay';
import { ThemedView } from "@/components/ThemedView"; import usePlayerStore from '@/stores/playerStore';
import { PlayerControls } from "@/components/PlayerControls"; import { useTVRemoteHandler } from '@/hooks/useTVRemoteHandler';
import { EpisodeSelectionModal } from "@/components/EpisodeSelectionModal";
import { NextEpisodeOverlay } from "@/components/NextEpisodeOverlay";
import { LoadingOverlay } from "@/components/LoadingOverlay";
import { usePlaybackManager } from "@/hooks/usePlaybackManager";
import { useTVRemoteHandler } from "@/hooks/useTVRemoteHandler";
export default function PlayScreen() { export default function PlayScreen() {
const router = useRouter();
const videoRef = useRef<Video>(null); const videoRef = useRef<Video>(null);
useKeepAwake(); useKeepAwake();
const { source, id, episodeIndex } = useLocalSearchParams<{ source: string; id: string; episodeIndex: string }>();
const { const {
detail, detail,
episodes, episodes,
currentEpisodeIndex, currentEpisodeIndex,
status,
isLoading, isLoading,
setIsLoading, showControls,
showEpisodeModal,
showNextEpisodeOverlay, showNextEpisodeOverlay,
setVideoRef,
loadVideo,
playEpisode, playEpisode,
togglePlayPause, togglePlayPause,
seek, seek,
handlePlaybackStatusUpdate, handlePlaybackStatusUpdate,
setShowControls,
setShowEpisodeModal,
setShowNextEpisodeOverlay, setShowNextEpisodeOverlay,
} = usePlaybackManager(videoRef); reset,
} = usePlayerStore();
const [showControls, setShowControls] = useState(true); useEffect(() => {
const [showEpisodeModal, setShowEpisodeModal] = useState(false); setVideoRef(videoRef);
const [episodeGroupSize] = useState(30); if (source && id) {
const [selectedEpisodeGroup, setSelectedEpisodeGroup] = useState( loadVideo(source, id, parseInt(episodeIndex || '0', 10));
Math.floor(currentEpisodeIndex / episodeGroupSize) }
); return () => {
reset(); // Reset state when component unmounts
};
}, [source, id, episodeIndex, setVideoRef, loadVideo, reset]);
const { currentFocus, setCurrentFocus } = useTVRemoteHandler({ const { setCurrentFocus } = useTVRemoteHandler({
showControls, showControls,
setShowControls, setShowControls,
showEpisodeModal, showEpisodeModal,
@@ -57,46 +60,6 @@ export default function PlayScreen() {
}, },
}); });
const [isSeeking, setIsSeeking] = useState(false);
const [seekPosition, setSeekPosition] = useState(0);
const [progressPosition, setProgressPosition] = useState(0);
const formatTime = (milliseconds: number) => {
if (!milliseconds) return "00:00";
const totalSeconds = Math.floor(milliseconds / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes.toString().padStart(2, "0")}:${seconds
.toString()
.padStart(2, "0")}`;
};
const handleSeekStart = () => setIsSeeking(true);
const handleSeekMove = (event: { nativeEvent: { locationX: number } }) => {
if (!status?.isLoaded || !status.durationMillis) return;
const { locationX } = event.nativeEvent;
const progressBarWidth = 300;
const progress = Math.max(0, Math.min(locationX / progressBarWidth, 1));
setSeekPosition(progress);
};
const handleSeekRelease = (event: { nativeEvent: { locationX: number } }) => {
if (!videoRef.current || !status?.isLoaded || !status.durationMillis)
return;
const wasPlaying = status.isPlaying;
const { locationX } = event.nativeEvent;
const progressBarWidth = 300;
const progress = Math.max(0, Math.min(locationX / progressBarWidth, 1));
const newPosition = progress * status.durationMillis;
videoRef.current.setPositionAsync(newPosition).then(() => {
if (wasPlaying) {
videoRef.current?.playAsync();
}
});
setIsSeeking(false);
};
if (!detail && isLoading) { if (!detail && isLoading) {
return ( return (
<ThemedView style={[styles.container, styles.centered]}> <ThemedView style={[styles.container, styles.centered]}>
@@ -106,8 +69,6 @@ export default function PlayScreen() {
} }
const currentEpisode = episodes[currentEpisodeIndex]; const currentEpisode = episodes[currentEpisodeIndex];
const videoTitle = detail?.videoInfo?.title || "";
const hasNextEpisode = currentEpisodeIndex < episodes.length - 1;
return ( return (
<ThemedView style={styles.container}> <ThemedView style={styles.container}>
@@ -124,67 +85,28 @@ export default function PlayScreen() {
style={styles.videoPlayer} style={styles.videoPlayer}
source={{ uri: currentEpisode?.url }} source={{ uri: currentEpisode?.url }}
resizeMode={ResizeMode.CONTAIN} resizeMode={ResizeMode.CONTAIN}
onPlaybackStatusUpdate={(s) => { onPlaybackStatusUpdate={handlePlaybackStatusUpdate}
handlePlaybackStatusUpdate(s); onLoad={() => usePlayerStore.setState({ isLoading: false })}
if (s.isLoaded && !isSeeking) { onLoadStart={() => usePlayerStore.setState({ isLoading: true })}
setProgressPosition(s.positionMillis / (s.durationMillis || 1));
}
}}
onLoad={() => setIsLoading(false)}
onLoadStart={() => setIsLoading(true)}
useNativeControls={false} useNativeControls={false}
shouldPlay shouldPlay
/> />
{showControls && ( {showControls && <PlayerControls />}
<PlayerControls
videoTitle={videoTitle}
currentEpisodeTitle={currentEpisode?.title}
status={status}
isSeeking={isSeeking}
seekPosition={seekPosition}
progressPosition={progressPosition}
currentFocus={currentFocus}
hasNextEpisode={hasNextEpisode}
onSeekStart={handleSeekStart}
onSeekMove={handleSeekMove}
onSeekRelease={handleSeekRelease}
onSeek={seek}
onTogglePlayPause={togglePlayPause}
onPlayNextEpisode={() => playEpisode(currentEpisodeIndex + 1)}
onShowEpisodes={() => setShowEpisodeModal(true)}
formatTime={formatTime}
/>
)}
<LoadingOverlay visible={isLoading} /> <LoadingOverlay visible={isLoading} />
<NextEpisodeOverlay <NextEpisodeOverlay visible={showNextEpisodeOverlay} onCancel={() => setShowNextEpisodeOverlay(false)} />
visible={showNextEpisodeOverlay}
onCancel={() => setShowNextEpisodeOverlay(false)}
/>
</TouchableOpacity> </TouchableOpacity>
<EpisodeSelectionModal <EpisodeSelectionModal />
visible={showEpisodeModal}
episodes={episodes}
currentEpisodeIndex={currentEpisodeIndex}
episodeGroupSize={episodeGroupSize}
selectedEpisodeGroup={selectedEpisodeGroup}
setSelectedEpisodeGroup={setSelectedEpisodeGroup}
onSelectEpisode={(index) => {
playEpisode(index);
setShowEpisodeModal(false);
}}
onClose={() => setShowEpisodeModal(false)}
/>
</ThemedView> </ThemedView>
); );
} }
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { flex: 1, backgroundColor: "black" }, container: { flex: 1, backgroundColor: 'black' },
centered: { flex: 1, justifyContent: "center", alignItems: "center" }, centered: { flex: 1, justifyContent: 'center', alignItems: 'center' },
videoContainer: { videoContainer: {
...StyleSheet.absoluteFillObject, ...StyleSheet.absoluteFillObject,
}, },

View File

@@ -1,74 +1,56 @@
import React from "react"; import React from 'react';
import { import { View, Text, StyleSheet, Modal, FlatList, Pressable, TouchableOpacity } from 'react-native';
View,
Text, import usePlayerStore from '@/stores/playerStore';
StyleSheet, import { useState } from 'react';
Modal,
FlatList,
Pressable,
TouchableOpacity,
} from "react-native";
interface Episode { interface Episode {
title?: string; title?: string;
url: string; url: string;
} }
interface EpisodeSelectionModalProps { interface EpisodeSelectionModalProps {}
visible: boolean;
episodes: Episode[]; export const EpisodeSelectionModal: React.FC<EpisodeSelectionModalProps> = () => {
currentEpisodeIndex: number; const { showEpisodeModal, episodes, currentEpisodeIndex, playEpisode, setShowEpisodeModal } = usePlayerStore();
episodeGroupSize: number;
selectedEpisodeGroup: number; const [episodeGroupSize] = useState(30);
setSelectedEpisodeGroup: (group: number) => void; const [selectedEpisodeGroup, setSelectedEpisodeGroup] = useState(Math.floor(currentEpisodeIndex / episodeGroupSize));
onSelectEpisode: (index: number) => void;
onClose: () => void; const onSelectEpisode = (index: number) => {
} playEpisode(index);
setShowEpisodeModal(false);
};
const onClose = () => {
setShowEpisodeModal(false);
};
export const EpisodeSelectionModal: React.FC<EpisodeSelectionModalProps> = ({
visible,
episodes,
currentEpisodeIndex,
episodeGroupSize,
selectedEpisodeGroup,
setSelectedEpisodeGroup,
onSelectEpisode,
onClose,
}) => {
return ( return (
<Modal <Modal visible={showEpisodeModal} transparent={true} animationType="slide" onRequestClose={onClose}>
visible={visible}
transparent={true}
animationType="slide"
onRequestClose={onClose}
>
<View style={styles.modalContainer}> <View style={styles.modalContainer}>
<View style={styles.modalContent}> <View style={styles.modalContent}>
<Text style={styles.modalTitle}></Text> <Text style={styles.modalTitle}></Text>
{episodes.length > episodeGroupSize && ( {episodes.length > episodeGroupSize && (
<View style={styles.episodeGroupContainer}> <View style={styles.episodeGroupContainer}>
{Array.from( {Array.from({ length: Math.ceil(episodes.length / episodeGroupSize) }, (_, groupIndex) => (
{ length: Math.ceil(episodes.length / episodeGroupSize) }, <TouchableOpacity
(_, groupIndex) => ( key={groupIndex}
<TouchableOpacity style={[
key={groupIndex} styles.episodeGroupButton,
style={[ selectedEpisodeGroup === groupIndex && styles.episodeGroupButtonSelected,
styles.episodeGroupButton, ]}
selectedEpisodeGroup === groupIndex && onPress={() => setSelectedEpisodeGroup(groupIndex)}
styles.episodeGroupButtonSelected, >
]} <Text style={styles.episodeGroupButtonText}>
onPress={() => setSelectedEpisodeGroup(groupIndex)} {`${groupIndex * episodeGroupSize + 1}-${Math.min(
> (groupIndex + 1) * episodeGroupSize,
<Text style={styles.episodeGroupButtonText}> episodes.length
{`${groupIndex * episodeGroupSize + 1}-${Math.min( )}`}
(groupIndex + 1) * episodeGroupSize, </Text>
episodes.length </TouchableOpacity>
)}`} ))}
</Text>
</TouchableOpacity>
)
)}
</View> </View>
)} )}
<FlatList <FlatList
@@ -77,39 +59,27 @@ export const EpisodeSelectionModal: React.FC<EpisodeSelectionModalProps> = ({
(selectedEpisodeGroup + 1) * episodeGroupSize (selectedEpisodeGroup + 1) * episodeGroupSize
)} )}
numColumns={5} numColumns={5}
keyExtractor={(_, index) => keyExtractor={(_, index) => `episode-${selectedEpisodeGroup * episodeGroupSize + index}`}
`episode-${selectedEpisodeGroup * episodeGroupSize + index}`
}
renderItem={({ item, index }) => { renderItem={({ item, index }) => {
const absoluteIndex = const absoluteIndex = selectedEpisodeGroup * episodeGroupSize + index;
selectedEpisodeGroup * episodeGroupSize + index;
return ( return (
<Pressable <Pressable
style={({ focused }) => [ style={({ focused }) => [
styles.episodeItem, styles.episodeItem,
currentEpisodeIndex === absoluteIndex && currentEpisodeIndex === absoluteIndex && styles.episodeItemSelected,
styles.episodeItemSelected,
focused && styles.focusedButton, focused && styles.focusedButton,
]} ]}
onPress={() => onSelectEpisode(absoluteIndex)} onPress={() => onSelectEpisode(absoluteIndex)}
hasTVPreferredFocus={currentEpisodeIndex === absoluteIndex} hasTVPreferredFocus={currentEpisodeIndex === absoluteIndex}
> >
<Text style={styles.episodeItemText}> <Text style={styles.episodeItemText}>{item.title || `${absoluteIndex + 1}`}</Text>
{item.title || `${absoluteIndex + 1}`}
</Text>
</Pressable> </Pressable>
); );
}} }}
/> />
<Pressable <Pressable style={({ focused }) => [styles.closeButton, focused && styles.focusedButton]} onPress={onClose}>
style={({ focused }) => [ <Text style={{ color: 'white' }}></Text>
styles.closeButton,
focused && styles.focusedButton,
]}
onPress={onClose}
>
<Text style={{ color: "white" }}></Text>
</Pressable> </Pressable>
</View> </View>
</View> </View>
@@ -120,69 +90,69 @@ export const EpisodeSelectionModal: React.FC<EpisodeSelectionModalProps> = ({
const styles = StyleSheet.create({ const styles = StyleSheet.create({
modalContainer: { modalContainer: {
flex: 1, flex: 1,
flexDirection: "row", flexDirection: 'row',
justifyContent: "flex-end", justifyContent: 'flex-end',
backgroundColor: "transparent", backgroundColor: 'transparent',
}, },
modalContent: { modalContent: {
width: 400, width: 400,
height: "100%", height: '100%',
backgroundColor: "rgba(0, 0, 0, 0.85)", backgroundColor: 'rgba(0, 0, 0, 0.85)',
padding: 20, padding: 20,
}, },
modalTitle: { modalTitle: {
color: "white", color: 'white',
marginBottom: 20, marginBottom: 20,
textAlign: "center", textAlign: 'center',
fontSize: 18, fontSize: 18,
fontWeight: "bold", fontWeight: 'bold',
}, },
episodeItem: { episodeItem: {
backgroundColor: "#333", backgroundColor: '#333',
paddingVertical: 12, paddingVertical: 12,
borderRadius: 8, borderRadius: 8,
margin: 4, margin: 4,
flex: 1, flex: 1,
alignItems: "center", alignItems: 'center',
justifyContent: "center", justifyContent: 'center',
}, },
episodeItemSelected: { episodeItemSelected: {
backgroundColor: "#007bff", backgroundColor: '#007bff',
}, },
episodeItemText: { episodeItemText: {
color: "white", color: 'white',
fontSize: 14, fontSize: 14,
}, },
episodeGroupContainer: { episodeGroupContainer: {
flexDirection: "row", flexDirection: 'row',
flexWrap: "wrap", flexWrap: 'wrap',
justifyContent: "center", justifyContent: 'center',
marginBottom: 15, marginBottom: 15,
paddingHorizontal: 10, paddingHorizontal: 10,
}, },
episodeGroupButton: { episodeGroupButton: {
backgroundColor: "#444", backgroundColor: '#444',
paddingHorizontal: 12, paddingHorizontal: 12,
paddingVertical: 6, paddingVertical: 6,
borderRadius: 15, borderRadius: 15,
margin: 5, margin: 5,
}, },
episodeGroupButtonSelected: { episodeGroupButtonSelected: {
backgroundColor: "#007bff", backgroundColor: '#007bff',
}, },
episodeGroupButtonText: { episodeGroupButtonText: {
color: "white", color: 'white',
fontSize: 12, fontSize: 12,
}, },
closeButton: { closeButton: {
backgroundColor: "#333", backgroundColor: '#333',
padding: 15, padding: 15,
borderRadius: 8, borderRadius: 8,
alignItems: "center", alignItems: 'center',
marginTop: 20, marginTop: 20,
}, },
focusedButton: { focusedButton: {
backgroundColor: "rgba(119, 119, 119, 0.9)", backgroundColor: 'rgba(119, 119, 119, 0.9)',
transform: [{ scale: 1.1 }], transform: [{ scale: 1.1 }],
}, },
}); });

View File

@@ -1,76 +1,58 @@
import React from "react"; import React from 'react';
import { import { View, Text, StyleSheet, TouchableOpacity, Pressable } from 'react-native';
View, import { useRouter } from 'expo-router';
Text, import { AVPlaybackStatus } from 'expo-av';
StyleSheet, import { ArrowLeft, Pause, Play, SkipForward, List, ChevronsRight, ChevronsLeft } from 'lucide-react-native';
TouchableOpacity, import { ThemedText } from '@/components/ThemedText';
Pressable, import { MediaButton } from '@/components/MediaButton';
} from "react-native";
import { useRouter } from "expo-router";
import { AVPlaybackStatus } from "expo-av";
import {
ArrowLeft,
Pause,
Play,
SkipForward,
List,
ChevronsRight,
ChevronsLeft,
} from "lucide-react-native";
import { ThemedText } from "@/components/ThemedText";
import { MediaButton } from "@/components/MediaButton";
interface PlayerControlsProps { import usePlayerStore from '@/stores/playerStore';
videoTitle: string;
currentEpisodeTitle?: string;
status: AVPlaybackStatus | null;
isSeeking: boolean;
seekPosition: number;
progressPosition: number;
currentFocus: string | null;
hasNextEpisode: boolean;
onSeekStart: () => void;
onSeekMove: (event: { nativeEvent: { locationX: number } }) => void;
onSeekRelease: (event: { nativeEvent: { locationX: number } }) => void;
onSeek: (forward: boolean) => void;
onTogglePlayPause: () => void;
onPlayNextEpisode: () => void;
onShowEpisodes: () => void;
formatTime: (time: number) => string;
}
export const PlayerControls: React.FC<PlayerControlsProps> = ({ interface PlayerControlsProps {}
videoTitle,
currentEpisodeTitle, export const PlayerControls: React.FC<PlayerControlsProps> = () => {
status,
isSeeking,
seekPosition,
progressPosition,
currentFocus,
hasNextEpisode,
onSeekStart,
onSeekMove,
onSeekRelease,
onSeek,
onTogglePlayPause,
onPlayNextEpisode,
onShowEpisodes,
formatTime,
}) => {
const router = useRouter(); const router = useRouter();
const {
detail,
currentEpisodeIndex,
status,
isSeeking,
seekPosition,
progressPosition,
seek,
togglePlayPause,
playEpisode,
setShowEpisodeModal,
} = usePlayerStore();
const videoTitle = detail?.videoInfo?.title || '';
const currentEpisode = detail?.episodes[currentEpisodeIndex];
const currentEpisodeTitle = currentEpisode?.title;
const hasNextEpisode = currentEpisodeIndex < (detail?.episodes.length || 0) - 1;
const formatTime = (milliseconds: number) => {
if (!milliseconds) return '00:00';
const totalSeconds = Math.floor(milliseconds / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
};
const onPlayNextEpisode = () => {
if (hasNextEpisode) {
playEpisode(currentEpisodeIndex + 1);
}
};
return ( return (
<View style={styles.controlsOverlay}> <View style={styles.controlsOverlay}>
<View style={styles.topControls}> <View style={styles.topControls}>
<TouchableOpacity <TouchableOpacity style={styles.controlButton} onPress={() => router.back()}>
style={styles.controlButton}
onPress={() => router.back()}
>
<ArrowLeft color="white" size={24} /> <ArrowLeft color="white" size={24} />
</TouchableOpacity> </TouchableOpacity>
<Text style={styles.controlTitle}> <Text style={styles.controlTitle}>
{videoTitle} {currentEpisodeTitle ? `- ${currentEpisodeTitle}` : ""} {videoTitle} {currentEpisodeTitle ? `- ${currentEpisodeTitle}` : ''}
</Text> </Text>
</View> </View>
@@ -81,40 +63,25 @@ export const PlayerControls: React.FC<PlayerControlsProps> = ({
style={[ style={[
styles.progressBarFilled, styles.progressBarFilled,
{ {
width: `${ width: `${(isSeeking ? seekPosition : progressPosition) * 100}%`,
(isSeeking ? seekPosition : progressPosition) * 100
}%`,
}, },
]} ]}
/> />
<Pressable <Pressable style={styles.progressBarTouchable} />
style={styles.progressBarTouchable}
onPressIn={onSeekStart}
onTouchMove={onSeekMove}
onTouchEnd={onSeekRelease}
/>
</View> </View>
<ThemedText style={{ color: "white", marginTop: 5 }}> <ThemedText style={{ color: 'white', marginTop: 5 }}>
{status?.isLoaded {status?.isLoaded
? `${formatTime(status.positionMillis)} / ${formatTime( ? `${formatTime(status.positionMillis)} / ${formatTime(status.durationMillis || 0)}`
status.durationMillis || 0 : '00:00 / 00:00'}
)}`
: "00:00 / 00:00"}
</ThemedText> </ThemedText>
<View style={styles.bottomControls}> <View style={styles.bottomControls}>
<MediaButton <MediaButton onPress={() => seek(false)}>
onPress={() => onSeek(false)}
isFocused={currentFocus === "skipBack"}
>
<ChevronsLeft color="white" size={24} /> <ChevronsLeft color="white" size={24} />
</MediaButton> </MediaButton>
<MediaButton <MediaButton onPress={togglePlayPause}>
onPress={onTogglePlayPause}
isFocused={currentFocus === "playPause"}
>
{status?.isLoaded && status.isPlaying ? ( {status?.isLoaded && status.isPlaying ? (
<Pause color="white" size={24} /> <Pause color="white" size={24} />
) : ( ) : (
@@ -122,25 +89,15 @@ export const PlayerControls: React.FC<PlayerControlsProps> = ({
)} )}
</MediaButton> </MediaButton>
<MediaButton <MediaButton onPress={onPlayNextEpisode} isDisabled={!hasNextEpisode}>
onPress={onPlayNextEpisode} <SkipForward color={hasNextEpisode ? 'white' : '#666'} size={24} />
isFocused={currentFocus === "nextEpisode"}
isDisabled={!hasNextEpisode}
>
<SkipForward color={hasNextEpisode ? "white" : "#666"} size={24} />
</MediaButton> </MediaButton>
<MediaButton <MediaButton onPress={() => seek(true)}>
onPress={() => onSeek(true)}
isFocused={currentFocus === "skipForward"}
>
<ChevronsRight color="white" size={24} /> <ChevronsRight color="white" size={24} />
</MediaButton> </MediaButton>
<MediaButton <MediaButton onPress={() => setShowEpisodeModal(true)}>
onPress={onShowEpisodes}
isFocused={currentFocus === "episodes"}
>
<List color="white" size={24} /> <List color="white" size={24} />
</MediaButton> </MediaButton>
</View> </View>
@@ -152,58 +109,58 @@ export const PlayerControls: React.FC<PlayerControlsProps> = ({
const styles = StyleSheet.create({ const styles = StyleSheet.create({
controlsOverlay: { controlsOverlay: {
...StyleSheet.absoluteFillObject, ...StyleSheet.absoluteFillObject,
backgroundColor: "rgba(0, 0, 0, 0.4)", backgroundColor: 'rgba(0, 0, 0, 0.4)',
justifyContent: "space-between", justifyContent: 'space-between',
padding: 20, padding: 20,
}, },
topControls: { topControls: {
flexDirection: "row", flexDirection: 'row',
justifyContent: "space-between", justifyContent: 'space-between',
alignItems: "center", alignItems: 'center',
}, },
controlTitle: { controlTitle: {
color: "white", color: 'white',
fontSize: 16, fontSize: 16,
fontWeight: "bold", fontWeight: 'bold',
flex: 1, flex: 1,
textAlign: "center", textAlign: 'center',
marginHorizontal: 10, marginHorizontal: 10,
}, },
bottomControlsContainer: { bottomControlsContainer: {
width: "100%", width: '100%',
alignItems: "center", alignItems: 'center',
}, },
bottomControls: { bottomControls: {
flexDirection: "row", flexDirection: 'row',
justifyContent: "center", justifyContent: 'center',
alignItems: "center", alignItems: 'center',
gap: 10, gap: 10,
flexWrap: "wrap", flexWrap: 'wrap',
marginTop: 15, marginTop: 15,
}, },
progressBarContainer: { progressBarContainer: {
width: "100%", width: '100%',
height: 8, height: 8,
position: "relative", position: 'relative',
marginTop: 10, marginTop: 10,
}, },
progressBarBackground: { progressBarBackground: {
position: "absolute", position: 'absolute',
left: 0, left: 0,
right: 0, right: 0,
height: 8, height: 8,
backgroundColor: "rgba(255, 255, 255, 0.3)", backgroundColor: 'rgba(255, 255, 255, 0.3)',
borderRadius: 4, borderRadius: 4,
}, },
progressBarFilled: { progressBarFilled: {
position: "absolute", position: 'absolute',
left: 0, left: 0,
height: 8, height: 8,
backgroundColor: "#ff0000", backgroundColor: '#ff0000',
borderRadius: 4, borderRadius: 4,
}, },
progressBarTouchable: { progressBarTouchable: {
position: "absolute", position: 'absolute',
left: 0, left: 0,
right: 0, right: 0,
height: 30, height: 30,
@@ -212,20 +169,20 @@ const styles = StyleSheet.create({
}, },
controlButton: { controlButton: {
padding: 10, padding: 10,
flexDirection: "row", flexDirection: 'row',
alignItems: "center", alignItems: 'center',
}, },
topRightContainer: { topRightContainer: {
padding: 10, padding: 10,
alignItems: "center", alignItems: 'center',
justifyContent: "center", justifyContent: 'center',
minWidth: 44, // Match TouchableOpacity default size for alignment minWidth: 44, // Match TouchableOpacity default size for alignment
}, },
resolutionText: { resolutionText: {
color: "white", color: 'white',
fontSize: 16, fontSize: 16,
fontWeight: "bold", fontWeight: 'bold',
backgroundColor: "rgba(0,0,0,0.5)", backgroundColor: 'rgba(0,0,0,0.5)',
paddingHorizontal: 8, paddingHorizontal: 8,
paddingVertical: 4, paddingVertical: 4,
borderRadius: 6, borderRadius: 6,

View File

@@ -1,38 +1,28 @@
import React, { useState, useEffect, useRef } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { Modal, View, Text, TextInput, StyleSheet, Pressable, useColorScheme } from 'react-native'; import { Modal, View, Text, TextInput, StyleSheet, Pressable, useColorScheme } from 'react-native';
import { SettingsManager } from '@/services/storage';
import { api } from '@/services/api';
import { ThemedText } from './ThemedText'; import { ThemedText } from './ThemedText';
import { ThemedView } from './ThemedView'; import { ThemedView } from './ThemedView';
import { useSettingsStore } from '@/stores/settingsStore';
interface SettingsModalProps { export const SettingsModal: React.FC = () => {
visible: boolean; const { isModalVisible, hideModal, apiBaseUrl, setApiBaseUrl, saveSettings, loadSettings } = useSettingsStore();
onCancel: () => void;
onSave: () => void;
}
export const SettingsModal: React.FC<SettingsModalProps> = ({ visible, onCancel, onSave }) => {
const [apiUrl, setApiUrl] = useState('');
const [isInputFocused, setIsInputFocused] = useState(false); const [isInputFocused, setIsInputFocused] = useState(false);
const colorScheme = useColorScheme(); const colorScheme = useColorScheme();
const inputRef = useRef<TextInput>(null); const inputRef = useRef<TextInput>(null);
useEffect(() => { useEffect(() => {
if (visible) { if (isModalVisible) {
SettingsManager.get().then(settings => { loadSettings();
setApiUrl(settings.apiBaseUrl);
});
const timer = setTimeout(() => { const timer = setTimeout(() => {
inputRef.current?.focus(); inputRef.current?.focus();
}, 200); }, 200);
return () => clearTimeout(timer); return () => clearTimeout(timer);
} }
}, [visible]); }, [isModalVisible, loadSettings]);
const handleSave = async () => { const handleSave = () => {
await SettingsManager.save({ apiBaseUrl: apiUrl }); saveSettings();
api.setBaseUrl(apiUrl);
onSave();
}; };
const styles = StyleSheet.create({ const styles = StyleSheet.create({
@@ -107,15 +97,15 @@ export const SettingsModal: React.FC<SettingsModalProps> = ({ visible, onCancel,
}); });
return ( return (
<Modal animationType="fade" transparent={true} visible={visible} onRequestClose={onCancel}> <Modal animationType="fade" transparent={true} visible={isModalVisible} onRequestClose={hideModal}>
<View style={styles.modalContainer}> <View style={styles.modalContainer}>
<ThemedView style={styles.modalContent}> <ThemedView style={styles.modalContent}>
<ThemedText style={styles.title}></ThemedText> <ThemedText style={styles.title}></ThemedText>
<TextInput <TextInput
ref={inputRef} ref={inputRef}
style={[styles.input, isInputFocused && styles.inputFocused]} style={[styles.input, isInputFocused && styles.inputFocused]}
value={apiUrl} value={apiBaseUrl}
onChangeText={setApiUrl} onChangeText={setApiBaseUrl}
placeholder="输入 API 地址" placeholder="输入 API 地址"
placeholderTextColor={colorScheme === 'dark' ? '#888' : '#555'} placeholderTextColor={colorScheme === 'dark' ? '#888' : '#555'}
autoCapitalize="none" autoCapitalize="none"
@@ -126,7 +116,7 @@ export const SettingsModal: React.FC<SettingsModalProps> = ({ visible, onCancel,
<View style={styles.buttonContainer}> <View style={styles.buttonContainer}>
<Pressable <Pressable
style={({ focused }) => [styles.button, styles.buttonCancel, focused && styles.focusedButton]} style={({ focused }) => [styles.button, styles.buttonCancel, focused && styles.focusedButton]}
onPress={onCancel} onPress={hideModal}
> >
<Text style={styles.buttonText}></Text> <Text style={styles.buttonText}></Text>
</Pressable> </Pressable>

View File

@@ -50,7 +50,8 @@
"react-native-safe-area-context": "4.10.1", "react-native-safe-area-context": "4.10.1",
"react-native-screens": "3.31.1", "react-native-screens": "3.31.1",
"react-native-svg": "^15.12.0", "react-native-svg": "^15.12.0",
"react-native-web": "~0.19.10" "react-native-web": "~0.19.10",
"zustand": "^5.0.6"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.20.0", "@babel/core": "^7.20.0",

146
stores/homeStore.ts Normal file
View File

@@ -0,0 +1,146 @@
import { create } from 'zustand';
import { api, SearchResult, PlayRecord } from '@/services/api';
import { PlayRecordManager } from '@/services/storage';
export type RowItem = (SearchResult | PlayRecord) & {
id: string;
source: string;
title: string;
poster: string;
progress?: number;
lastPlayed?: number;
episodeIndex?: number;
sourceName?: string;
totalEpisodes?: number;
year?: string;
rate?: string;
};
export interface Category {
title: string;
type?: 'movie' | 'tv' | 'record';
tag?: string;
}
const initialCategories: Category[] = [
{ title: '最近播放', type: 'record' },
{ title: '热门剧集', type: 'tv', tag: '热门' },
{ title: '综艺', type: 'tv', tag: '综艺' },
{ title: '热门电影', type: 'movie', tag: '热门' },
{ title: '豆瓣 Top250', type: 'movie', tag: 'top250' },
{ title: '儿童', type: 'movie', tag: '少儿' },
{ title: '美剧', type: 'tv', tag: '美剧' },
{ title: '韩剧', type: 'tv', tag: '韩剧' },
{ title: '日剧', type: 'tv', tag: '日剧' },
{ title: '日漫', type: 'tv', tag: '日本动画' },
];
interface HomeState {
categories: Category[];
selectedCategory: Category;
contentData: RowItem[];
loading: boolean;
loadingMore: boolean;
pageStart: number;
hasMore: boolean;
error: string | null;
fetchInitialData: () => Promise<void>;
loadMoreData: () => Promise<void>;
selectCategory: (category: Category) => void;
refreshPlayRecords: () => Promise<void>;
}
const useHomeStore = create<HomeState>((set, get) => ({
categories: initialCategories,
selectedCategory: initialCategories[0],
contentData: [],
loading: true,
loadingMore: false,
pageStart: 0,
hasMore: true,
error: null,
fetchInitialData: async () => {
const { selectedCategory } = get();
set({ loading: true, contentData: [], pageStart: 0, hasMore: true, error: null });
await get().loadMoreData();
set({ loading: false });
},
loadMoreData: async () => {
const { selectedCategory, pageStart, loading, loadingMore, hasMore } = get();
if (loading || loadingMore || !hasMore) return;
if (selectedCategory.type === 'record') {
const records = await PlayRecordManager.getAll();
const rowItems = Object.entries(records)
.map(([key, record]) => {
const [source, id] = key.split('+');
return { ...record, id, source, progress: record.play_time / record.total_time, poster: record.cover, sourceName: record.source_name, episodeIndex: record.index, totalEpisodes: record.total_episodes, lastPlayed: record.save_time };
})
.filter(record => record.progress !== undefined && record.progress > 0 && record.progress < 1)
.sort((a, b) => (b.lastPlayed || 0) - (a.lastPlayed || 0));
set({ contentData: rowItems, hasMore: false });
return;
}
if (!selectedCategory.type || !selectedCategory.tag) return;
set({ loadingMore: true });
try {
const result = await api.getDoubanData(selectedCategory.type, selectedCategory.tag, 20, pageStart);
if (result.list.length === 0) {
set({ hasMore: false });
} else {
const newItems = result.list.map(item => ({
...item,
id: item.title,
source: 'douban',
})) as RowItem[];
set(state => ({
contentData: pageStart === 0 ? newItems : [...state.contentData, ...newItems],
pageStart: state.pageStart + result.list.length,
hasMore: true,
}));
}
} catch (err: any) {
if (err.message === 'API_URL_NOT_SET') {
set({ error: '请点击右上角设置按钮,配置您的 API 地址' });
} else {
set({ error: '加载失败,请重试' });
}
} finally {
set({ loadingMore: false });
}
},
selectCategory: (category: Category) => {
set({ selectedCategory: category });
get().fetchInitialData();
},
refreshPlayRecords: async () => {
const records = await PlayRecordManager.getAll();
const hasRecords = Object.keys(records).length > 0;
set(state => {
const recordCategoryExists = state.categories.some(c => c.type === 'record');
if (hasRecords && !recordCategoryExists) {
return { categories: [initialCategories[0], ...state.categories] };
}
if (!hasRecords && recordCategoryExists) {
const newCategories = state.categories.filter(c => c.type !== 'record');
if (state.selectedCategory.type === 'record') {
get().selectCategory(newCategories[0] || null);
}
return { categories: newCategories };
}
return {};
});
if (get().selectedCategory.type === 'record') {
get().fetchInitialData();
}
},
}));
export default useHomeStore;

163
stores/playerStore.ts Normal file
View File

@@ -0,0 +1,163 @@
import { create } from 'zustand';
import { AVPlaybackStatus, Video } from 'expo-av';
import { RefObject } from 'react';
import { api, VideoDetail as ApiVideoDetail } from '@/services/api';
import { PlayRecordManager } from '@/services/storage';
interface Episode {
url: string;
title: string;
}
interface VideoDetail {
videoInfo: ApiVideoDetail['videoInfo'];
episodes: Episode[];
}
interface PlayerState {
videoRef: RefObject<Video> | null;
detail: VideoDetail | null;
episodes: Episode[];
currentEpisodeIndex: number;
status: AVPlaybackStatus | null;
isLoading: boolean;
showControls: boolean;
showEpisodeModal: boolean;
showNextEpisodeOverlay: boolean;
isSeeking: boolean;
seekPosition: number;
progressPosition: number;
setVideoRef: (ref: RefObject<Video>) => void;
loadVideo: (source: string, id: string, episodeIndex: number) => Promise<void>;
playEpisode: (index: number) => void;
togglePlayPause: () => void;
seek: (forward: boolean) => void;
handlePlaybackStatusUpdate: (newStatus: AVPlaybackStatus) => void;
setLoading: (loading: boolean) => void;
setShowControls: (show: boolean) => void;
setShowEpisodeModal: (show: boolean) => void;
setShowNextEpisodeOverlay: (show: boolean) => void;
reset: () => void;
}
const usePlayerStore = create<PlayerState>((set, get) => ({
videoRef: null,
detail: null,
episodes: [],
currentEpisodeIndex: 0,
status: null,
isLoading: true,
showControls: true,
showEpisodeModal: false,
showNextEpisodeOverlay: false,
isSeeking: false,
seekPosition: 0,
progressPosition: 0,
setVideoRef: (ref) => set({ videoRef: ref }),
loadVideo: async (source, id, episodeIndex) => {
set({ isLoading: true, detail: null, episodes: [], currentEpisodeIndex: 0 });
try {
const videoDetail = await api.getVideoDetail(source, id);
const episodes = videoDetail.episodes.map((ep, index) => ({ url: ep, title: `${index + 1}` }));
set({
detail: { videoInfo: videoDetail.videoInfo, episodes },
episodes,
currentEpisodeIndex: episodeIndex,
isLoading: false,
});
} catch (error) {
console.error("Failed to load video details", error);
set({ isLoading: false });
}
},
playEpisode: (index) => {
const { episodes, videoRef } = get();
if (index >= 0 && index < episodes.length) {
set({ currentEpisodeIndex: index, showNextEpisodeOverlay: false });
videoRef?.current?.replayAsync();
}
},
togglePlayPause: () => {
const { status, videoRef } = get();
if (status?.isLoaded) {
if (status.isPlaying) {
videoRef?.current?.pauseAsync();
} else {
videoRef?.current?.playAsync();
}
}
},
seek: (forward) => {
const { status, videoRef } = get();
if (status?.isLoaded) {
const newPosition = status.positionMillis + (forward ? 15000 : -15000);
videoRef?.current?.setPositionAsync(Math.max(0, newPosition));
}
},
handlePlaybackStatusUpdate: (newStatus) => {
set({ status: newStatus });
if (!newStatus.isLoaded) {
if (newStatus.error) {
console.error(`Playback Error: ${newStatus.error}`);
}
return;
}
const { detail, currentEpisodeIndex, episodes } = get();
if (detail && newStatus.durationMillis) {
const { videoInfo } = detail;
PlayRecordManager.save(
videoInfo.source,
videoInfo.id,
{
title: videoInfo.title,
cover: videoInfo.cover || '',
index: currentEpisodeIndex,
total_episodes: episodes.length,
play_time: newStatus.positionMillis,
total_time: newStatus.durationMillis,
source_name: videoInfo.source_name,
}
);
const isNearEnd = newStatus.positionMillis / newStatus.durationMillis > 0.95;
if (isNearEnd && currentEpisodeIndex < episodes.length - 1) {
set({ showNextEpisodeOverlay: true });
} else {
set({ showNextEpisodeOverlay: false });
}
}
if (newStatus.didJustFinish) {
const { playEpisode, currentEpisodeIndex, episodes } = get();
if (currentEpisodeIndex < episodes.length - 1) {
playEpisode(currentEpisodeIndex + 1);
}
}
},
setLoading: (loading) => set({ isLoading: loading }),
setShowControls: (show) => set({ showControls: show }),
setShowEpisodeModal: (show) => set({ showEpisodeModal: show }),
setShowNextEpisodeOverlay: (show) => set({ showNextEpisodeOverlay: show }),
reset: () => {
set({
detail: null,
episodes: [],
currentEpisodeIndex: 0,
status: null,
isLoading: true,
showControls: true,
showEpisodeModal: false,
showNextEpisodeOverlay: false,
});
},
}));
export default usePlayerStore;

32
stores/settingsStore.ts Normal file
View File

@@ -0,0 +1,32 @@
import { create } from 'zustand';
import { SettingsManager } from '@/services/storage';
import { api } from '@/services/api';
interface SettingsState {
apiBaseUrl: string;
isModalVisible: boolean;
loadSettings: () => Promise<void>;
setApiBaseUrl: (url: string) => void;
saveSettings: () => Promise<void>;
showModal: () => void;
hideModal: () => void;
}
export const useSettingsStore = create<SettingsState>((set, get) => ({
apiBaseUrl: '',
isModalVisible: false,
loadSettings: async () => {
const settings = await SettingsManager.get();
set({ apiBaseUrl: settings.apiBaseUrl });
api.setBaseUrl(settings.apiBaseUrl);
},
setApiBaseUrl: (url) => set({ apiBaseUrl: url }),
saveSettings: async () => {
const { apiBaseUrl } = get();
await SettingsManager.save({ apiBaseUrl });
api.setBaseUrl(apiBaseUrl);
set({ isModalVisible: false });
},
showModal: () => set({ isModalVisible: true }),
hideModal: () => set({ isModalVisible: false }),
}));

View File

@@ -7909,16 +7909,7 @@ string-length@^5.0.1:
char-regex "^2.0.0" char-regex "^2.0.0"
strip-ansi "^7.0.1" strip-ansi "^7.0.1"
"string-width-cjs@npm:string-width@^4.2.0": "string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3" version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -7982,7 +7973,7 @@ string_decoder@~1.1.1:
dependencies: dependencies:
safe-buffer "~5.1.0" safe-buffer "~5.1.0"
"strip-ansi-cjs@npm:strip-ansi@^6.0.1": "strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1" version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -7996,13 +7987,6 @@ strip-ansi@^5.0.0, strip-ansi@^5.2.0:
dependencies: dependencies:
ansi-regex "^4.1.0" ansi-regex "^4.1.0"
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"
strip-ansi@^7.0.1: strip-ansi@^7.0.1:
version "7.1.0" version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
@@ -8750,7 +8734,7 @@ wonka@^6.3.2:
resolved "https://registry.yarnpkg.com/wonka/-/wonka-6.3.5.tgz#33fa54ea700ff3e87b56fe32202112a9e8fea1a2" resolved "https://registry.yarnpkg.com/wonka/-/wonka-6.3.5.tgz#33fa54ea700ff3e87b56fe32202112a9e8fea1a2"
integrity sha512-SSil+ecw6B4/Dm7Pf2sAshKQ5hWFvfyGlfPbEd6A14dOH6VDjrmbY86u6nZvy9omGwwIPFR8V41+of1EezgoUw== integrity sha512-SSil+ecw6B4/Dm7Pf2sAshKQ5hWFvfyGlfPbEd6A14dOH6VDjrmbY86u6nZvy9omGwwIPFR8V41+of1EezgoUw==
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": "wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0" version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -8768,15 +8752,6 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0" string-width "^4.1.0"
strip-ansi "^6.0.0" strip-ansi "^6.0.0"
wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"
wrap-ansi@^8.1.0: wrap-ansi@^8.1.0:
version "8.1.0" version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
@@ -8953,3 +8928,8 @@ zod@^3.22.4:
version "3.25.67" version "3.25.67"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.25.67.tgz#62987e4078e2ab0f63b491ef0c4f33df24236da8" resolved "https://registry.yarnpkg.com/zod/-/zod-3.25.67.tgz#62987e4078e2ab0f63b491ef0c4f33df24236da8"
integrity sha512-idA2YXwpCdqUSKRCACDE6ItZD9TZzy3OZMtpfLoh6oPR47lipysRrJfjzMqFxQ3uJuUPyUeWe1r9vLH33xO/Qw== integrity sha512-idA2YXwpCdqUSKRCACDE6ItZD9TZzy3OZMtpfLoh6oPR47lipysRrJfjzMqFxQ3uJuUPyUeWe1r9vLH33xO/Qw==
zustand@^5.0.6:
version "5.0.6"
resolved "https://registry.yarnpkg.com/zustand/-/zustand-5.0.6.tgz#a2da43d8dc3d31e314279e5baec06297bea70a5c"
integrity sha512-ihAqNeUVhe0MAD+X8M5UzqyZ9k3FFZLBTtqo6JLPwV53cbRB/mJwBI0PxcIgqhBBHlEs8G45OTDTMq3gNcLq3A==