Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC - Split remotecontrol #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/components/remotecontrol/btnPlayPause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { playbackManager } from 'components/playback/playbackmanager';
import Events from '../../utils/events.ts';

function buttonVisible(btn, enabled) {
if (enabled) {
btn.classList.remove('hide');
} else {
btn.classList.add('hide');
}
}

export default function () {
function updatePlayerState(player, context, state) {
const item = state.NowPlayingItem;
const playState = state.PlayState || {};

updatePlayPauseState(playState.IsPaused, item != null);
}

function updatePlayPauseState(isPaused, isActive) {
const context = dlg;
const btnPlayPause = context.querySelector('.btnPlayPause');
const btnPlayPauseIcon = btnPlayPause.querySelector('.material-icons');

btnPlayPauseIcon.classList.remove('play_circle_filled', 'pause_circle_filled');
btnPlayPauseIcon.classList.add(isPaused ? 'play_circle_filled' : 'pause_circle_filled');

const playlistIndicator = context.querySelector('.playlistIndexIndicatorImage');
if (playlistIndicator) {
playlistIndicator.classList.toggle('playlistIndexIndicatorPausedImage', isPaused);
}

buttonVisible(btnPlayPause, isActive);
}

function onPlayPauseStateChanged() {
updatePlayPauseState(this.paused(), true);
}

function releaseCurrentPlayer() {
const player = currentPlayer;

if (player) {
Events.off(player, 'pause', onPlayPauseStateChanged);
Events.off(player, 'unpause', onPlayPauseStateChanged);
currentPlayer = null;
}
}

function bindToPlayer(context, player) {
releaseCurrentPlayer();
currentPlayer = player;

if (player) {
Events.on(player, 'pause', onPlayPauseStateChanged);
Events.on(player, 'unpause', onPlayPauseStateChanged);
}
}

function bindEvents(context) {
context.querySelector('.btnPlayPause').addEventListener('click', function () {
if (currentPlayer) {
playbackManager.playPause(currentPlayer);
}
});
}

function init(ownerView, context) {
bindEvents(context);
}

function onDialogClosed() {
releaseCurrentPlayer();
}

function onShow(context) {
bindToPlayer(context, playbackManager.getCurrentPlayer());
}

let dlg;
let currentPlayer;
const self = this;

self.onPlaybackStopped = function (e, state) {
const player = this;

if (!state.NextMediaType) {
updatePlayerState(player, dlg, {});
}
};

self.onStateChanged = function (event, state) {
const player = this;
updatePlayerState(player, dlg, state);
};

self.onPlayerChange = function (context, player) {
bindToPlayer(context, player);
};

self.init = function (ownerView, context) {
dlg = context;
init(ownerView, dlg);
};

self.onShow = function () {
onShow(dlg);
};

self.destroy = function () {
onDialogClosed();
};
}
41 changes: 10 additions & 31 deletions src/components/remotecontrol/remotecontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ServerConnections from '../ServerConnections';
import toast from '../toast/toast';
import { appRouter } from '../router/appRouter';
import { getDefaultBackgroundClass } from '../cardbuilder/cardBuilderUtils';
import BtnPlayPause from './btnPlayPause';

let showMuteButton = true;
let showVolumeSlider = true;
Expand Down Expand Up @@ -353,7 +354,6 @@ export default function () {
positionSlider.setIsClear(isProgressClear);
}

updatePlayPauseState(playState.IsPaused, item != null);
updateTimeDisplay(playState.PositionTicks, item ? item.RunTimeTicks : null);
updatePlayerVolumeState(context, playState.IsMuted, playState.VolumeLevel);

Expand Down Expand Up @@ -451,22 +451,6 @@ export default function () {
}
}

function updatePlayPauseState(isPaused, isActive) {
const context = dlg;
const btnPlayPause = context.querySelector('.btnPlayPause');
const btnPlayPauseIcon = btnPlayPause.querySelector('.material-icons');

btnPlayPauseIcon.classList.remove('play_circle_filled', 'pause_circle_filled');
btnPlayPauseIcon.classList.add(isPaused ? 'play_circle_filled' : 'pause_circle_filled');

const playlistIndicator = context.querySelector('.playlistIndexIndicatorImage');
if (playlistIndicator) {
playlistIndicator.classList.toggle('playlistIndexIndicatorPausedImage', isPaused);
}

buttonVisible(btnPlayPause, isActive);
}

function updateTimeDisplay(positionTicks, runtimeTicks) {
const context = dlg;
const positionSlider = context.querySelector('.nowPlayingPositionSlider');
Expand Down Expand Up @@ -600,20 +584,18 @@ export default function () {
console.debug('remotecontrol event: ' + e.type);
const player = this;

btnPlayPause.onPlaybackStopped(e, state);
if (!state.NextMediaType) {
updatePlayerState(player, dlg, {});
appRouter.back();
}
}

function onPlayPauseStateChanged() {
updatePlayPauseState(this.paused(), true);
}

function onStateChanged(event, state) {
const player = this;
updatePlayerState(player, dlg, state);
onPlaylistUpdate();
btnPlayPause.onStateChanged(event, state);
}

function onTimeUpdate() {
Expand Down Expand Up @@ -645,8 +627,6 @@ export default function () {
Events.off(player, 'playlistitemadd', onPlaylistUpdate);
Events.off(player, 'playbackstop', onPlaybackStopped);
Events.off(player, 'volumechange', onVolumeChanged);
Events.off(player, 'pause', onPlayPauseStateChanged);
Events.off(player, 'unpause', onPlayPauseStateChanged);
Events.off(player, 'timeupdate', onTimeUpdate);
currentPlayer = null;
}
Expand All @@ -670,8 +650,6 @@ export default function () {
Events.on(player, 'playlistitemadd', onPlaylistUpdate);
Events.on(player, 'playbackstop', onPlaybackStopped);
Events.on(player, 'volumechange', onVolumeChanged);
Events.on(player, 'pause', onPlayPauseStateChanged);
Events.on(player, 'unpause', onPlayPauseStateChanged);
Events.on(player, 'timeupdate', onTimeUpdate);
const playerInfo = playbackManager.getPlayerInfo();
const supportedCommands = playerInfo.supportedCommands;
Expand Down Expand Up @@ -744,11 +722,6 @@ export default function () {
playbackManager.stop(currentPlayer);
}
});
context.querySelector('.btnPlayPause').addEventListener('click', function () {
if (currentPlayer) {
playbackManager.playPause(currentPlayer);
}
});
context.querySelector('.btnNextTrack').addEventListener('click', function () {
if (currentPlayer) {
playbackManager.nextTrack(currentPlayer);
Expand Down Expand Up @@ -862,7 +835,9 @@ export default function () {
}

function onPlayerChange() {
bindToPlayer(dlg, playbackManager.getCurrentPlayer());
const player = playbackManager.getCurrentPlayer();
bindToPlayer(dlg, player);
btnPlayPause.onPlayerChange(dlg, player);
}

function onMessageSubmit(e) {
Expand Down Expand Up @@ -949,18 +924,22 @@ export default function () {
let currentPlayerSupportedCommands = [];
let lastUpdateTime = 0;
let currentRuntimeTicks = 0;
const btnPlayPause = new BtnPlayPause();
const self = this;

self.init = function (ownerView, context) {
dlg = context;
init(ownerView, dlg);
btnPlayPause.init(ownerView, context);
};

self.onShow = function () {
onShow(dlg);
btnPlayPause.onShow();
};

self.destroy = function () {
onDialogClosed();
btnPlayPause.destroy();
};
}