Skip to content

Commit

Permalink
Add functionality for 'end of chapter' sleep timer
Browse files Browse the repository at this point in the history
  • Loading branch information
glorenzen committed Jul 11, 2024
1 parent 41e3965 commit cd902c0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
45 changes: 37 additions & 8 deletions client/components/app/MediaPlayerContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<player-ui
ref="audioPlayer"
:chapters="chapters"
:current-chapter="currentChapter"
:paused="!isPlaying"
:loading="playerLoading"
:bookmarks="bookmarks"
Expand Down Expand Up @@ -79,13 +80,21 @@ export default {
sleepTimerSet: false,
sleepTimerTime: 0,
sleepTimerRemaining: 0,
sleepTimerType: null,
sleepTimer: null,
displayTitle: null,
currentPlaybackRate: 1,
syncFailedToast: null,
coverAspectRatio: 1
}
},
watch: {
currentTime(newTime) {
if (this.sleepTimerType === this.$constants.SleepTimerTypes.CHAPTER && this.sleepTimerSet) {
this.checkChapterEnd(newTime)
}
}
},
computed: {
isSquareCover() {
return this.coverAspectRatio === 1
Expand Down Expand Up @@ -145,6 +154,9 @@ export default {
if (this.streamEpisode) return this.streamEpisode.chapters || []
return this.media.chapters || []
},
currentChapter() {
return this.chapters.find((chapter) => chapter.start <= this.currentTime && this.currentTime < chapter.end)
},
title() {
if (this.playerHandler.displayTitle) return this.playerHandler.displayTitle
return this.mediaMetadata.title || 'No Title'
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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()
Expand All @@ -235,6 +263,7 @@ export default {
this.sleepTimerRemaining = 0
this.sleepTimer = null
this.sleepTimerSet = false
this.sleepTimerType = null
},
incrementSleepTimer(amount) {
if (!this.sleepTimerSet) return
Expand Down
36 changes: 23 additions & 13 deletions client/components/modals/SleepTimerModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div ref="container" class="w-full rounded-lg bg-primary box-shadow-md overflow-y-auto overflow-x-hidden" style="max-height: 80vh">
<div v-if="!timerSet" class="w-full">
<template v-for="time in sleepTimes">
<div :key="time.text" class="flex items-center px-6 py-3 justify-center cursor-pointer hover:bg-bg relative" @click="setTime(time.seconds)">
<div :key="time.text" class="flex items-center px-6 py-3 justify-center cursor-pointer hover:bg-bg relative" @click="setTime(time)">
<p class="text-xl text-center">{{ time.text }}</p>
</div>
</template>
Expand Down Expand Up @@ -48,44 +48,54 @@ export default {
value: Boolean,
timerSet: Boolean,
timerTime: Number,
remaining: Number
remaining: Number,
currentChapter: Object
},
data() {
return {
customTime: null,
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 }
]
}
},
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions client/components/player/PlayerUi.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default {
type: Array,
default: () => []
},
currentChapter: Object,
bookmarks: {
type: Array,
default: () => []
Expand Down Expand Up @@ -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 : ''
},
Expand Down

0 comments on commit cd902c0

Please sign in to comment.