2020-08-16 20:24:45 +02:00
|
|
|
import { playbackManager } from './playback/playbackmanager';
|
2020-08-14 08:46:34 +02:00
|
|
|
import * as userSettings from '../scripts/settings/userSettings';
|
2022-10-14 10:53:16 -04:00
|
|
|
import Events from '../utils/events.ts';
|
2020-10-17 19:08:56 +01:00
|
|
|
import ServerConnections from './ServerConnections';
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
let currentOwnerId;
|
|
|
|
let currentThemeIds = [];
|
2020-07-31 09:12:44 +01:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
function playThemeMedia(items, ownerId) {
|
|
|
|
const currentThemeItems = items.filter(function (i) {
|
|
|
|
return enabled(i.MediaType);
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
if (currentThemeItems.length) {
|
|
|
|
// Stop if a theme song from another ownerId
|
|
|
|
// Leave it alone if anything else (e.g user playing a movie)
|
|
|
|
if (!currentOwnerId && playbackManager.isPlaying()) {
|
|
|
|
return;
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
currentThemeIds = currentThemeItems.map(function (i) {
|
|
|
|
return i.Id;
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2022-03-12 22:34:22 +03:00
|
|
|
currentThemeItems.forEach((i) => {
|
|
|
|
i.playOptions = {
|
|
|
|
fullscreen: false,
|
|
|
|
enableRemotePlayers: false
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
playbackManager.play({
|
|
|
|
items: currentThemeItems,
|
|
|
|
fullscreen: false,
|
|
|
|
enableRemotePlayers: false
|
|
|
|
}).then(function () {
|
|
|
|
currentOwnerId = ownerId;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
stopIfPlaying();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2020-08-06 11:41:45 +01:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
function stopIfPlaying() {
|
|
|
|
if (currentOwnerId) {
|
|
|
|
playbackManager.stop();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
currentOwnerId = null;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
function enabled(mediaType) {
|
|
|
|
if (mediaType === 'Video') {
|
|
|
|
return userSettings.enableThemeVideos();
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
return userSettings.enableThemeSongs();
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
const excludeTypes = ['CollectionFolder', 'UserView', 'Program', 'SeriesTimer', 'Person', 'TvChannel', 'Channel'];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
function loadThemeMedia(item) {
|
|
|
|
if (item.CollectionType) {
|
|
|
|
stopIfPlaying();
|
|
|
|
return;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
if (excludeTypes.indexOf(item.Type) !== -1) {
|
|
|
|
stopIfPlaying();
|
|
|
|
return;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-10-17 19:08:56 +01:00
|
|
|
const apiClient = ServerConnections.getApiClient(item.ServerId);
|
2020-08-06 11:41:45 +01:00
|
|
|
apiClient.getThemeMedia(apiClient.getCurrentUserId(), item.Id, true).then(function (themeMediaResult) {
|
2022-03-12 22:48:00 +03:00
|
|
|
const result = userSettings.enableThemeVideos() && themeMediaResult.ThemeVideosResult.Items.length ? themeMediaResult.ThemeVideosResult : themeMediaResult.ThemeSongsResult;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2022-03-12 22:48:00 +03:00
|
|
|
const ownerId = result.OwnerId;
|
2020-08-06 11:41:45 +01:00
|
|
|
|
2022-03-12 22:48:00 +03:00
|
|
|
if (ownerId !== currentOwnerId) {
|
|
|
|
playThemeMedia(result.Items, ownerId);
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2020-08-06 11:41:45 +01:00
|
|
|
});
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
document.addEventListener('viewshow', function (e) {
|
|
|
|
const state = e.detail.state || {};
|
|
|
|
const item = state.item;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
if (item && item.ServerId) {
|
|
|
|
loadThemeMedia(item);
|
|
|
|
return;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-06 11:41:45 +01:00
|
|
|
const viewOptions = e.detail.options || {};
|
|
|
|
|
|
|
|
if (viewOptions.supportsThemeMedia) {
|
|
|
|
// Do nothing here, allow it to keep playing
|
|
|
|
} else {
|
|
|
|
playThemeMedia([], null);
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
Events.on(playbackManager, 'playbackstart', function (e, player) {
|
|
|
|
const item = playbackManager.currentItem(player);
|
|
|
|
// User played something manually
|
|
|
|
if (currentThemeIds.indexOf(item.Id) == -1) {
|
|
|
|
currentOwnerId = null;
|
|
|
|
}
|
2020-02-22 11:47:03 -05:00
|
|
|
});
|