From 19748c34d4dbe0b6bf9861a5d9c5039471bd505d Mon Sep 17 00:00:00 2001 From: shvl Date: Mon, 8 Apr 2024 21:35:23 +0200 Subject: [PATCH] #237 save volume and rate to local storage --- hugo/src/js/controllers/player_controller.js | 40 ++++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/hugo/src/js/controllers/player_controller.js b/hugo/src/js/controllers/player_controller.js index f843b846..dfc76633 100644 --- a/hugo/src/js/controllers/player_controller.js +++ b/hugo/src/js/controllers/player_controller.js @@ -34,6 +34,28 @@ export default class extends Controller { return this.state; } + get storedVolumeLevel() { + return getLocalStorage('player-parameters', (player) => player['volume'] || 100); + } + + set storedVolumeLevel(volumeLevel) { + updateLocalStorage('player-parameters', (player) => { + player['volume'] = volumeLevel; + return player; + }); + } + + get rate() { + return getLocalStorage('player-parameters', (player) => player['rate'] || 1); + } + + set rate(rate) { + updateLocalStorage('player-parameters', (player) => { + player['rate'] = rate; + return player; + }); + } + updateState(state) { Object.assign(this.constructor.state, state); this.dispatchEvent( @@ -139,6 +161,8 @@ export default class extends Controller { this.numberTarget.textContent = detail.number; this.audioTarget.load(); this.rateTarget.textContent = this.audioTarget.playbackRate; + this.updateVolumeLevel(this.storedVolumeLevel); + this.updatePlaybackRate(this.rate); if (!detail.online) { if (!detail.timeLabel) { const podcast = getLocalStorage( @@ -222,6 +246,11 @@ export default class extends Controller { this.audioTarget.volume = volumeLevel / 100; } + updateVolumeLevel(volumeLevel) { + this.volumeLevelTarget.value = volumeLevel; + this.setVolume(volumeLevel); + } + close() { window.localStorage.removeItem('player'); this.element.classList.add('d-none'); @@ -276,12 +305,10 @@ export default class extends Controller { muteUnmute() { if (this.isMuted) { - this.setVolume(this.storedVolumeLevel); - this.volumeLevelTarget.value = this.storedVolumeLevel; + this.updateVolumeLevel(this.storedVolumeLevel); } else { - this.setVolume(0); this.storedVolumeLevel = this.volumeLevelTarget.value; - this.volumeLevelTarget.value = 0; + this.updateVolumeLevel(0); } } @@ -292,6 +319,11 @@ export default class extends Controller { rate = 0.5; } + this.updatePlaybackRate(rate); + } + + updatePlaybackRate(rate) { + this.rate = rate; this.audioTarget.playbackRate = rate; this.rateTarget.innerText = rate; }