Files
OrionTV/hooks/useTVRemoteHandler.ts
zimplexing 5b4c8db317 Add Prettier configuration and refactor code for consistent formatting
- Introduced a .prettierrc file to standardize code formatting.
- Updated import statements and JSX attributes in NotFoundScreen, HomeScreen, PlayScreen, and PlayerControls for consistent use of double quotes.
- Refactored styles in various components to use double quotes for string values.
- Added SeekingBar component to enhance video playback experience.
2025-07-08 16:58:06 +08:00

98 lines
2.6 KiB
TypeScript

import { useEffect, useRef, useCallback } from "react";
import { useTVEventHandler, HWEvent } from "react-native";
import usePlayerStore from "@/stores/playerStore";
// 定时器延迟时间(毫秒)
const CONTROLS_TIMEOUT = 5000;
/**
* 管理播放器控件的显示/隐藏、遥控器事件和自动隐藏定时器。
* @returns onScreenPress - 一个函数,用于处理屏幕点击事件,以显示控件并重置定时器。
*/
export const useTVRemoteHandler = () => {
const { showControls, setShowControls, showEpisodeModal, togglePlayPause, seek } = usePlayerStore();
const controlsTimer = useRef<NodeJS.Timeout | null>(null);
// 重置或启动隐藏控件的定时器
const resetTimer = useCallback(() => {
// 清除之前的定时器
if (controlsTimer.current) {
clearTimeout(controlsTimer.current);
}
// 设置新的定时器
controlsTimer.current = setTimeout(() => {
setShowControls(false);
}, CONTROLS_TIMEOUT);
}, [setShowControls]);
// 当控件显示时,启动定时器
useEffect(() => {
if (showControls) {
resetTimer();
} else {
// 如果控件被隐藏,清除定时器
if (controlsTimer.current) {
clearTimeout(controlsTimer.current);
}
}
// 组件卸载时清除定时器
return () => {
if (controlsTimer.current) {
clearTimeout(controlsTimer.current);
}
};
}, [showControls, resetTimer]);
// 处理遥控器事件
const handleTVEvent = useCallback(
(event: HWEvent) => {
// 如果剧集选择模态框显示,则不处理任何事件
if (showEpisodeModal) {
return;
}
resetTimer();
if (!showControls) {
switch (event.eventType) {
case "select":
togglePlayPause();
setShowControls(true);
break;
case "left":
seek(-15000); // 快退15秒
break;
case "right":
seek(15000); // 快进15秒
break;
case "longLeft":
seek(-60000); // 快退60秒
break;
case "longRight":
seek(60000); // 快进60秒
break;
}
}
},
[showControls, showEpisodeModal, setShowControls, resetTimer, togglePlayPause, seek]
);
useTVEventHandler(handleTVEvent);
// 处理屏幕点击事件
const onScreenPress = () => {
// 切换控件的显示状态
const newShowControls = !showControls;
setShowControls(newShowControls);
// 如果控件变为显示状态,则重置定时器
if (newShowControls) {
resetTimer();
}
};
return { onScreenPress };
};