Cleanup wake lock

This commit is contained in:
schroda
2026-07-16 11:37:18 +02:00
parent e53d064c92
commit 98ee3d46c8
3 changed files with 13 additions and 11 deletions

View File

@@ -15,7 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- (**Migration**) Add "in library" indicator
- (**Settings/WebView**) Add setting to enable/disable WebView
- (**Settings/Sync**) Add sync settings
- (**Reader**) Add Wake lock screen while reading
- (**Reader**) Add setting to keep the screen on while reading
- (**Extension**) Support installing external JARs
- (**Reader**) Add auto background color setting

View File

@@ -8,12 +8,18 @@
import { useEffect, useRef } from 'react';
export function useReaderWakeLock(shouldLock: boolean) {
export const useWakeLock = (shouldLock: boolean) => {
const wakeLockSentinelRef = useRef<WakeLockSentinel | null>(null);
useEffect(() => {
if (!('wakeLock' in navigator)) {
return;
}
if (!shouldLock) {
return;
}
const requestWakeLock = async () => {
try {
if (wakeLockSentinelRef.current) {
@@ -41,16 +47,12 @@ export function useReaderWakeLock(shouldLock: boolean) {
}
};
if (shouldLock) {
requestWakeLock();
document.addEventListener('visibilitychange', handleVisibilityChange);
} else {
releaseWakeLock();
}
return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange);
releaseWakeLock();
};
}, [shouldLock]);
}
};

View File

@@ -28,7 +28,7 @@ import { getErrorMessage } from '@/lib/HelperFunctions.ts';
import type { NavbarContextType } from '@/features/navigation-bar/NavigationBar.types.ts';
import { withPropsFrom } from '@/base/hoc/withPropsFrom.tsx';
import { useReaderResetStates } from '@/features/reader/hooks/useReaderResetStates.ts';
import { useReaderWakeLock } from '@/features/reader/wake-lock/useReaderWakeLock.ts';
import { useWakeLock } from '@/base/hooks/useWakeLock.ts';
import { useReaderSetSettingsState } from '@/features/reader/hooks/useReaderSetSettingsState.ts';
import { useReaderShowSettingPreviewOnChange } from '@/features/reader/hooks/useReaderShowSettingPreviewOnChange.ts';
import { useReaderSetChaptersState } from '@/features/reader/hooks/useReaderSetChaptersState.ts';
@@ -160,7 +160,7 @@ const BaseReader = ({
}, [mangaResponse.data?.manga]);
useReaderResetStates();
useReaderWakeLock(shouldWakeLockScreen);
useWakeLock(shouldWakeLockScreen);
useReaderSetSettingsState(
mangaResponse,
defaultSettingsResponse,