✨ (wake lock) ask for permission to lock the screen
This commit is contained in:
@@ -2,6 +2,7 @@ import { ref, computed, watch } from '@vue/composition-api'
|
|||||||
import { StopwatchState } from '@/types/StopwatchState'
|
import { StopwatchState } from '@/types/StopwatchState'
|
||||||
import { notify } from '@/utils/notification'
|
import { notify } from '@/utils/notification'
|
||||||
import { useInterval } from './useInterval'
|
import { useInterval } from './useInterval'
|
||||||
|
import { useWakeLock } from './useWakeLock'
|
||||||
|
|
||||||
const format = (num: number) => {
|
const format = (num: number) => {
|
||||||
return num.toString().padStart(2, '0')
|
return num.toString().padStart(2, '0')
|
||||||
@@ -15,6 +16,8 @@ const formatSeconds = (seconds: number) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const useTimer = () => {
|
export const useTimer = () => {
|
||||||
|
const { askWakeLockPermission, releaseWakeLock } = useWakeLock()
|
||||||
|
|
||||||
const timeState = ref<StopwatchState>('stopped')
|
const timeState = ref<StopwatchState>('stopped')
|
||||||
const seconds = ref(0)
|
const seconds = ref(0)
|
||||||
const { interval } = useInterval()
|
const { interval } = useInterval()
|
||||||
@@ -33,6 +36,7 @@ export const useTimer = () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const start = () => {
|
const start = () => {
|
||||||
|
askWakeLockPermission()
|
||||||
timeState.value = 'started'
|
timeState.value = 'started'
|
||||||
|
|
||||||
stopwatchId = setInterval(() => {
|
stopwatchId = setInterval(() => {
|
||||||
@@ -56,6 +60,7 @@ export const useTimer = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const pause = () => {
|
const pause = () => {
|
||||||
|
releaseWakeLock()
|
||||||
timeState.value = 'stopped'
|
timeState.value = 'stopped'
|
||||||
if (stopwatchId) {
|
if (stopwatchId) {
|
||||||
clearInterval(stopwatchId)
|
clearInterval(stopwatchId)
|
||||||
|
|||||||
40
src/hooks/useWakeLock.ts
Normal file
40
src/hooks/useWakeLock.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
interface WakeLock {
|
||||||
|
request(type: string): Promise<WakeLockSentinel>
|
||||||
|
}
|
||||||
|
|
||||||
|
type WakeLockType = 'screen' | null
|
||||||
|
|
||||||
|
interface WakeLockSentinel {
|
||||||
|
release(): Promise<void>
|
||||||
|
readonly WakeLocktype: WakeLockType
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NavigatorExtended extends Navigator {
|
||||||
|
readonly wakeLock: WakeLock
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useWakeLock = () => {
|
||||||
|
let wakeLock: WakeLockSentinel | null = null
|
||||||
|
|
||||||
|
const askWakeLockPermission = async () => {
|
||||||
|
if (!('wakeLock' in navigator)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
wakeLock = await (navigator as NavigatorExtended).wakeLock.request(
|
||||||
|
'screen'
|
||||||
|
)
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`${err.name}, ${err.message}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const releaseWakeLock = () => {
|
||||||
|
wakeLock?.release()
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
askWakeLockPermission,
|
||||||
|
releaseWakeLock
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user