perf opt for homesreen
This commit is contained in:
parent
b43957e6f9
commit
fca3c46372
2 changed files with 65 additions and 11 deletions
|
|
@ -1834,8 +1834,10 @@ const AndroidVideoPlayer: React.FC = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Keep screen awake during active playback (Android)
|
// Enhanced screen lock prevention - keep screen awake as soon as player mounts
|
||||||
const keepAwakeModuleRef = useRef<any>(null);
|
const keepAwakeModuleRef = useRef<any>(null);
|
||||||
|
const keepAwakeActiveRef = useRef<boolean>(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
try {
|
try {
|
||||||
// Use require to avoid TS dynamic import constraints
|
// Use require to avoid TS dynamic import constraints
|
||||||
|
|
@ -1848,23 +1850,75 @@ const AndroidVideoPlayer: React.FC = () => {
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Toggle keep-awake based on playback state
|
// Activate keep-awake immediately when player mounts and keep it active
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const mod = keepAwakeModuleRef.current;
|
const mod = keepAwakeModuleRef.current;
|
||||||
if (!mod) return;
|
if (!mod) return;
|
||||||
|
|
||||||
const activate = mod.activateKeepAwakeAsync || mod.activateKeepAwake;
|
const activate = mod.activateKeepAwakeAsync || mod.activateKeepAwake;
|
||||||
const deactivate = mod.deactivateKeepAwakeAsync || mod.deactivateKeepAwake;
|
const deactivate = mod.deactivateKeepAwakeAsync || mod.deactivateKeepAwake;
|
||||||
|
|
||||||
|
// Activate immediately when component mounts
|
||||||
try {
|
try {
|
||||||
if (!paused && isPlayerReady && duration > 0) {
|
if (activate && !keepAwakeActiveRef.current) {
|
||||||
activate && activate();
|
activate();
|
||||||
} else {
|
keepAwakeActiveRef.current = true;
|
||||||
deactivate && deactivate();
|
logger.log('[AndroidVideoPlayer] Screen lock prevention activated on mount');
|
||||||
}
|
}
|
||||||
} catch (_e) {}
|
} catch (error) {
|
||||||
|
logger.warn('[AndroidVideoPlayer] Failed to activate keep-awake:', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep it active throughout the entire player session
|
||||||
|
const keepAliveInterval = setInterval(() => {
|
||||||
|
try {
|
||||||
|
if (activate && !keepAwakeActiveRef.current) {
|
||||||
|
activate();
|
||||||
|
keepAwakeActiveRef.current = true;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn('[AndroidVideoPlayer] Failed to maintain keep-awake:', error);
|
||||||
|
}
|
||||||
|
}, 5000); // Re-activate every 5 seconds to ensure it stays active
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
try { deactivate && deactivate(); } catch (_e) {}
|
clearInterval(keepAliveInterval);
|
||||||
|
try {
|
||||||
|
if (deactivate && keepAwakeActiveRef.current) {
|
||||||
|
deactivate();
|
||||||
|
keepAwakeActiveRef.current = false;
|
||||||
|
logger.log('[AndroidVideoPlayer] Screen lock prevention deactivated on unmount');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn('[AndroidVideoPlayer] Failed to deactivate keep-awake:', error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, [paused, isPlayerReady, duration]);
|
}, []); // Empty dependency array - only run on mount/unmount
|
||||||
|
|
||||||
|
// Additional keep-awake activation on app state changes
|
||||||
|
useEffect(() => {
|
||||||
|
const mod = keepAwakeModuleRef.current;
|
||||||
|
if (!mod) return;
|
||||||
|
|
||||||
|
const activate = mod.activateKeepAwakeAsync || mod.activateKeepAwake;
|
||||||
|
|
||||||
|
const handleAppStateChange = (nextAppState: string) => {
|
||||||
|
if (nextAppState === 'active') {
|
||||||
|
try {
|
||||||
|
if (activate && !keepAwakeActiveRef.current) {
|
||||||
|
activate();
|
||||||
|
keepAwakeActiveRef.current = true;
|
||||||
|
logger.log('[AndroidVideoPlayer] Screen lock prevention re-activated on app foreground');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn('[AndroidVideoPlayer] Failed to re-activate keep-awake on app foreground:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const subscription = AppState.addEventListener('change', handleAppStateChange);
|
||||||
|
return () => subscription?.remove();
|
||||||
|
}, []);
|
||||||
|
|
||||||
const handleErrorExit = () => {
|
const handleErrorExit = () => {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -503,7 +503,7 @@ const HomeScreen = () => {
|
||||||
await new Promise(resolve => setTimeout(resolve, 100));
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
navigation.navigate('Player', {
|
navigation.navigate(Platform.OS === 'ios' ? 'PlayerIOS' : 'PlayerAndroid', {
|
||||||
uri: stream.url,
|
uri: stream.url,
|
||||||
title: featuredContent.name,
|
title: featuredContent.name,
|
||||||
year: featuredContent.year,
|
year: featuredContent.year,
|
||||||
|
|
@ -516,7 +516,7 @@ const HomeScreen = () => {
|
||||||
logger.error('[HomeScreen] Error in handlePlayStream:', error);
|
logger.error('[HomeScreen] Error in handlePlayStream:', error);
|
||||||
|
|
||||||
// Fallback: navigate anyway
|
// Fallback: navigate anyway
|
||||||
navigation.navigate('Player', {
|
navigation.navigate(Platform.OS === 'ios' ? 'PlayerIOS' : 'PlayerAndroid', {
|
||||||
uri: stream.url,
|
uri: stream.url,
|
||||||
title: featuredContent.name,
|
title: featuredContent.name,
|
||||||
year: featuredContent.year,
|
year: featuredContent.year,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue