From cd902c06365732508e60c26cdce4fbf485bdc6ff Mon Sep 17 00:00:00 2001 From: Greg Lorenzen Date: Thu, 11 Jul 2024 17:37:02 +0000 Subject: [PATCH] Add functionality for 'end of chapter' sleep timer --- .../components/app/MediaPlayerContainer.vue | 45 +++++++++++++++---- client/components/modals/SleepTimerModal.vue | 36 +++++++++------ client/components/player/PlayerUi.vue | 4 +- 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/client/components/app/MediaPlayerContainer.vue b/client/components/app/MediaPlayerContainer.vue index 3c99a6da69..1f6846541e 100644 --- a/client/components/app/MediaPlayerContainer.vue +++ b/client/components/app/MediaPlayerContainer.vue @@ -35,6 +35,7 @@ chapter.start <= this.currentTime && this.currentTime < chapter.end) + }, title() { if (this.playerHandler.displayTitle) return this.playerHandler.displayTitle return this.mediaMetadata.title || 'No Title' @@ -204,14 +216,19 @@ export default { this.$store.commit('setIsPlaying', isPlaying) this.updateMediaSessionPlaybackState() }, - setSleepTimer(seconds) { + setSleepTimer(time) { this.sleepTimerSet = true - this.sleepTimerTime = seconds - this.sleepTimerRemaining = seconds - this.runSleepTimer() this.showSleepTimerModal = false + + this.sleepTimerType = time.timerType + if (this.sleepTimerType === this.$constants.SleepTimerTypes.COUNTDOWN) { + this.runSleepTimer(time) + } }, - runSleepTimer() { + runSleepTimer(time) { + this.sleepTimerTime = time.seconds + this.sleepTimerRemaining = time.seconds + var lastTick = Date.now() clearInterval(this.sleepTimer) this.sleepTimer = setInterval(() => { @@ -220,12 +237,23 @@ export default { this.sleepTimerRemaining -= elapsed / 1000 if (this.sleepTimerRemaining <= 0) { - this.clearSleepTimer() - this.playerHandler.pause() - this.$toast.info('Sleep Timer Done.. zZzzZz') + this.sleepTimerEnd() } }, 1000) }, + checkChapterEnd(time) { + const chapterEndTime = this.currentChapter.end + const tolerance = 0.5 + if (time >= chapterEndTime - tolerance) { + console.log('Chapter end reached', time, chapterEndTime) + this.sleepTimerEnd() + } + }, + sleepTimerEnd() { + this.clearSleepTimer() + this.playerHandler.pause() + this.$toast.info('Sleep Timer Done.. zZzzZz') + }, cancelSleepTimer() { this.showSleepTimerModal = false this.clearSleepTimer() @@ -235,6 +263,7 @@ export default { this.sleepTimerRemaining = 0 this.sleepTimer = null this.sleepTimerSet = false + this.sleepTimerType = null }, incrementSleepTimer(amount) { if (!this.sleepTimerSet) return diff --git a/client/components/modals/SleepTimerModal.vue b/client/components/modals/SleepTimerModal.vue index 051c5d3d76..626883cae6 100644 --- a/client/components/modals/SleepTimerModal.vue +++ b/client/components/modals/SleepTimerModal.vue @@ -9,7 +9,7 @@
@@ -48,7 +48,8 @@ export default { value: Boolean, timerSet: Boolean, timerTime: Number, - remaining: Number + remaining: Number, + currentChapter: Object }, data() { return { @@ -56,36 +57,45 @@ export default { sleepTimes: [ { seconds: 60 * 5, - text: '5 minutes' + text: '5 minutes', + timerType: this.$constants.SleepTimerTypes.COUNTDOWN }, { seconds: 60 * 15, - text: '15 minutes' + text: '15 minutes', + timerType: this.$constants.SleepTimerTypes.COUNTDOWN }, { seconds: 60 * 20, - text: '20 minutes' + text: '20 minutes', + timerType: this.$constants.SleepTimerTypes.COUNTDOWN }, { seconds: 60 * 30, - text: '30 minutes' + text: '30 minutes', + timerType: this.$constants.SleepTimerTypes.COUNTDOWN }, { seconds: 60 * 45, - text: '45 minutes' + text: '45 minutes', + timerType: this.$constants.SleepTimerTypes.COUNTDOWN }, { seconds: 60 * 60, - text: '60 minutes' + text: '60 minutes', + timerType: this.$constants.SleepTimerTypes.COUNTDOWN }, { seconds: 60 * 90, - text: '90 minutes' + text: '90 minutes', + timerType: this.$constants.SleepTimerTypes.COUNTDOWN }, { seconds: 60 * 120, - text: '2 hours' - } + text: '2 hours', + timerType: this.$constants.SleepTimerTypes.COUNTDOWN + }, + { seconds: -1, text: 'End of chapter', timerType: this.$constants.SleepTimerTypes.CHAPTER } ] } }, @@ -115,8 +125,8 @@ export default { const timeInSeconds = Math.round(Number(this.customTime) * 60) this.setTime(timeInSeconds) }, - setTime(seconds) { - this.$emit('set', seconds) + setTime(time) { + this.$emit('set', time) }, increment(amount) { this.$emit('increment', amount) diff --git a/client/components/player/PlayerUi.vue b/client/components/player/PlayerUi.vue index 3093975f61..058c6e1162 100644 --- a/client/components/player/PlayerUi.vue +++ b/client/components/player/PlayerUi.vue @@ -72,6 +72,7 @@ export default { type: Array, default: () => [] }, + currentChapter: Object, bookmarks: { type: Array, default: () => [] @@ -135,9 +136,6 @@ export default { if (!duration) return 0 return Math.round((100 * time) / duration) }, - currentChapter() { - return this.chapters.find((chapter) => chapter.start <= this.currentTime && this.currentTime < chapter.end) - }, currentChapterName() { return this.currentChapter ? this.currentChapter.title : '' },