Merge pull request #1469 from benscobie/issue/1434

Keep screen awake when viewing recipe
This commit is contained in:
vabene1111 2022-02-07 14:48:21 +01:00 committed by GitHub
commit 5f9820ed30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -208,6 +208,7 @@ export default {
servings_cache: {},
start_time: "",
share_uid: window.SHARE_UID,
wake_lock: null,
}
},
watch: {
@ -218,8 +219,37 @@ export default {
mounted() {
this.loadRecipe(window.RECIPE_ID)
this.$i18n.locale = window.CUSTOM_LOCALE
this.requestWakeLock()
},
beforeUnmount() {
this.destroyWakeLock()
},
methods: {
requestWakeLock: async function() {
if ('wakeLock' in navigator) {
try {
this.wake_lock = await navigator.wakeLock.request('screen')
document.addEventListener('visibilitychange', this.visibilityChange)
} catch (err) {
console.log(err)
}
}
},
destroyWakeLock: function() {
if (this.wake_lock != null) {
this.wake_lock.release()
.then(() => {
this.wake_lock = null
});
}
document.removeEventListener('visibilitychange', this.visibilityChange)
},
visibilityChange: async function() {
if (this.wake_lock != null && document.visibilityState === 'visible') {
await this.requestWakeLock()
}
},
loadRecipe: function (recipe_id) {
apiLoadRecipe(recipe_id).then((recipe) => {
if (window.USER_SERVINGS !== 0) {