2020-04-03 18:49:19 +02:00
|
|
|
import events from 'events';
|
|
|
|
import connectionManager from 'connectionManager';
|
|
|
|
import playbackManager from 'playbackManager';
|
2020-05-06 23:41:54 +02:00
|
|
|
import syncPlayManager from 'syncPlayManager';
|
2020-04-03 18:49:19 +02:00
|
|
|
import loading from 'loading';
|
|
|
|
import toast from 'toast';
|
|
|
|
import actionsheet from 'actionsheet';
|
|
|
|
import globalize from 'globalize';
|
2020-04-15 18:09:34 +02:00
|
|
|
import playbackPermissionManager from 'playbackPermissionManager';
|
2020-04-03 18:49:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets active player id.
|
|
|
|
* @returns {string} The player's id.
|
|
|
|
*/
|
2020-04-15 18:15:28 +02:00
|
|
|
function getActivePlayerId () {
|
2020-04-03 18:49:19 +02:00
|
|
|
var info = playbackManager.getPlayerInfo();
|
|
|
|
return info ? info.id : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used when user needs to join a group.
|
|
|
|
* @param {HTMLElement} button - Element where to place the menu.
|
2020-04-04 18:20:39 +02:00
|
|
|
* @param {Object} user - Current user.
|
|
|
|
* @param {Object} apiClient - ApiClient.
|
2020-04-03 18:49:19 +02:00
|
|
|
*/
|
2020-04-15 18:15:28 +02:00
|
|
|
function showNewJoinGroupSelection (button, user, apiClient) {
|
2020-05-05 12:01:43 +02:00
|
|
|
const sessionId = getActivePlayerId() || 'none';
|
|
|
|
const inSession = sessionId !== 'none';
|
2020-04-04 18:20:39 +02:00
|
|
|
const policy = user.localUser ? user.localUser.Policy : {};
|
2020-04-15 18:15:28 +02:00
|
|
|
let playingItemId;
|
|
|
|
try {
|
|
|
|
const playState = playbackManager.getPlayerState();
|
|
|
|
playingItemId = playState.NowPlayingItem.Id;
|
2020-05-05 12:01:43 +02:00
|
|
|
console.debug('Item', playingItemId, 'is currently playing.');
|
2020-04-15 18:15:28 +02:00
|
|
|
} catch (error) {
|
2020-05-05 12:01:43 +02:00
|
|
|
playingItemId = '';
|
|
|
|
console.debug('No item is currently playing.');
|
2020-04-15 18:15:28 +02:00
|
|
|
}
|
2020-04-03 18:49:19 +02:00
|
|
|
|
2020-05-06 23:41:54 +02:00
|
|
|
apiClient.sendSyncPlayCommand(sessionId, 'ListGroups').then(function (response) {
|
2020-05-05 12:01:43 +02:00
|
|
|
response.json().then(function (groups) {
|
2020-04-03 18:49:19 +02:00
|
|
|
var menuItems = groups.map(function (group) {
|
|
|
|
return {
|
2020-05-05 12:01:43 +02:00
|
|
|
name: group.PlayingItemName,
|
|
|
|
icon: 'group',
|
2020-04-03 18:49:19 +02:00
|
|
|
id: group.GroupId,
|
|
|
|
selected: false,
|
2020-05-05 12:01:43 +02:00
|
|
|
secondaryText: group.Participants.join(', ')
|
2020-04-03 18:49:19 +02:00
|
|
|
};
|
2020-04-01 17:53:14 +02:00
|
|
|
});
|
|
|
|
|
2020-05-06 23:41:54 +02:00
|
|
|
if (inSession && policy.SyncPlayAccess === 'CreateAndJoinGroups') {
|
2020-04-03 18:49:19 +02:00
|
|
|
menuItems.push({
|
2020-05-06 23:41:54 +02:00
|
|
|
name: globalize.translate('LabelSyncPlayNewGroup'),
|
2020-05-05 12:01:43 +02:00
|
|
|
icon: 'add',
|
|
|
|
id: 'new-group',
|
2020-04-03 18:49:19 +02:00
|
|
|
selected: true,
|
2020-05-06 23:41:54 +02:00
|
|
|
secondaryText: globalize.translate('LabelSyncPlayNewGroupDescription')
|
2020-04-03 18:49:19 +02:00
|
|
|
});
|
|
|
|
}
|
2020-04-01 17:53:14 +02:00
|
|
|
|
2020-04-03 18:49:19 +02:00
|
|
|
if (menuItems.length === 0) {
|
2020-05-06 23:41:54 +02:00
|
|
|
if (inSession && policy.SyncPlayAccess === 'JoinGroups') {
|
2020-04-04 18:20:39 +02:00
|
|
|
toast({
|
2020-05-06 23:41:54 +02:00
|
|
|
text: globalize.translate('MessageSyncPlayCreateGroupDenied')
|
2020-04-04 18:20:39 +02:00
|
|
|
});
|
|
|
|
} else {
|
2020-04-03 18:49:19 +02:00
|
|
|
toast({
|
2020-05-06 23:41:54 +02:00
|
|
|
text: globalize.translate('MessageSyncPlayNoGroupsAvailable')
|
2020-04-03 18:49:19 +02:00
|
|
|
});
|
2020-04-04 18:20:39 +02:00
|
|
|
}
|
2020-04-03 18:49:19 +02:00
|
|
|
loading.hide();
|
|
|
|
return;
|
|
|
|
}
|
2020-04-01 17:53:14 +02:00
|
|
|
|
|
|
|
var menuOptions = {
|
2020-05-06 23:41:54 +02:00
|
|
|
title: globalize.translate('HeaderSyncPlaySelectGroup'),
|
2020-04-01 17:53:14 +02:00
|
|
|
items: menuItems,
|
|
|
|
positionTo: button,
|
|
|
|
resolveOnClick: true,
|
|
|
|
border: true
|
|
|
|
};
|
|
|
|
|
|
|
|
actionsheet.show(menuOptions).then(function (id) {
|
2020-05-05 12:01:43 +02:00
|
|
|
if (id == 'new-group') {
|
2020-05-06 23:41:54 +02:00
|
|
|
apiClient.sendSyncPlayCommand(sessionId, 'NewGroup');
|
2020-04-03 18:49:19 +02:00
|
|
|
} else {
|
2020-05-06 23:41:54 +02:00
|
|
|
apiClient.sendSyncPlayCommand(sessionId, 'JoinGroup', {
|
2020-04-15 18:15:28 +02:00
|
|
|
GroupId: id,
|
|
|
|
PlayingItemId: playingItemId
|
2020-04-03 18:49:19 +02:00
|
|
|
});
|
2020-04-01 17:53:14 +02:00
|
|
|
}
|
2020-05-05 12:01:43 +02:00
|
|
|
}).catch((error) => {
|
2020-05-06 23:41:54 +02:00
|
|
|
console.error('SyncPlay: unexpected error listing groups:', error);
|
2020-05-05 12:01:43 +02:00
|
|
|
});
|
2020-04-03 18:49:19 +02:00
|
|
|
|
|
|
|
loading.hide();
|
2020-04-01 17:53:14 +02:00
|
|
|
});
|
2020-04-03 18:49:19 +02:00
|
|
|
}).catch(function (error) {
|
|
|
|
console.error(error);
|
2020-04-04 18:20:39 +02:00
|
|
|
loading.hide();
|
|
|
|
toast({
|
2020-05-06 23:41:54 +02:00
|
|
|
text: globalize.translate('MessageSyncPlayErrorAccessingGroups')
|
2020-04-04 18:20:39 +02:00
|
|
|
});
|
2020-04-03 18:49:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used when user has joined a group.
|
|
|
|
* @param {HTMLElement} button - Element where to place the menu.
|
2020-04-04 18:20:39 +02:00
|
|
|
* @param {Object} user - Current user.
|
|
|
|
* @param {Object} apiClient - ApiClient.
|
2020-04-03 18:49:19 +02:00
|
|
|
*/
|
2020-04-15 18:15:28 +02:00
|
|
|
function showLeaveGroupSelection (button, user, apiClient) {
|
2020-04-03 18:49:19 +02:00
|
|
|
const sessionId = getActivePlayerId();
|
2020-04-15 18:15:28 +02:00
|
|
|
if (!sessionId) {
|
2020-05-06 23:41:54 +02:00
|
|
|
syncPlayManager.signalError();
|
2020-04-15 18:15:28 +02:00
|
|
|
toast({
|
2020-05-06 23:41:54 +02:00
|
|
|
text: globalize.translate('MessageSyncPlayErrorNoActivePlayer')
|
2020-04-15 18:15:28 +02:00
|
|
|
});
|
2020-05-05 12:01:43 +02:00
|
|
|
showNewJoinGroupSelection(button, user, apiClient);
|
2020-04-15 18:15:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-03 18:49:19 +02:00
|
|
|
|
|
|
|
const menuItems = [{
|
2020-05-06 23:41:54 +02:00
|
|
|
name: globalize.translate('LabelSyncPlayLeaveGroup'),
|
2020-05-05 12:01:43 +02:00
|
|
|
icon: 'meeting_room',
|
|
|
|
id: 'leave-group',
|
2020-04-03 18:49:19 +02:00
|
|
|
selected: true,
|
2020-05-06 23:41:54 +02:00
|
|
|
secondaryText: globalize.translate('LabelSyncPlayLeaveGroupDescription')
|
2020-04-03 18:49:19 +02:00
|
|
|
}];
|
|
|
|
|
|
|
|
var menuOptions = {
|
2020-05-06 23:41:54 +02:00
|
|
|
title: globalize.translate('HeaderSyncPlayEnabled'),
|
2020-04-03 18:49:19 +02:00
|
|
|
items: menuItems,
|
|
|
|
positionTo: button,
|
|
|
|
resolveOnClick: true,
|
|
|
|
border: true
|
|
|
|
};
|
2020-04-01 17:53:14 +02:00
|
|
|
|
2020-04-03 18:49:19 +02:00
|
|
|
actionsheet.show(menuOptions).then(function (id) {
|
2020-05-05 12:01:43 +02:00
|
|
|
if (id == 'leave-group') {
|
2020-05-06 23:41:54 +02:00
|
|
|
apiClient.sendSyncPlayCommand(sessionId, 'LeaveGroup');
|
2020-04-01 17:53:14 +02:00
|
|
|
}
|
2020-05-05 12:01:43 +02:00
|
|
|
}).catch((error) => {
|
2020-05-06 23:41:54 +02:00
|
|
|
console.error('SyncPlay: unexpected error showing group menu:', error);
|
2020-05-05 12:01:43 +02:00
|
|
|
});
|
2020-04-01 17:53:14 +02:00
|
|
|
|
2020-04-03 18:49:19 +02:00
|
|
|
loading.hide();
|
|
|
|
}
|
2020-04-01 17:53:14 +02:00
|
|
|
|
2020-05-06 23:41:54 +02:00
|
|
|
// Register to SyncPlay events
|
|
|
|
let syncPlayEnabled = false;
|
|
|
|
events.on(syncPlayManager, 'enabled', function (e, enabled) {
|
|
|
|
syncPlayEnabled = enabled;
|
2020-04-01 17:53:14 +02:00
|
|
|
});
|
2020-04-03 18:49:19 +02:00
|
|
|
|
|
|
|
/**
|
2020-05-06 23:41:54 +02:00
|
|
|
* Shows a menu to handle SyncPlay groups.
|
2020-04-03 18:49:19 +02:00
|
|
|
* @param {HTMLElement} button - Element where to place the menu.
|
|
|
|
*/
|
2020-04-15 18:15:28 +02:00
|
|
|
export function show (button) {
|
2020-04-04 18:20:39 +02:00
|
|
|
loading.show();
|
|
|
|
|
2020-04-15 18:09:34 +02:00
|
|
|
// TODO: should feature be disabled if playback permission is missing?
|
|
|
|
playbackPermissionManager.check().then(() => {
|
2020-05-05 12:01:43 +02:00
|
|
|
console.debug('Playback is allowed.');
|
2020-04-15 18:09:34 +02:00
|
|
|
}).catch((error) => {
|
2020-05-05 12:01:43 +02:00
|
|
|
console.error('Playback not allowed!', error);
|
2020-04-15 18:09:34 +02:00
|
|
|
toast({
|
2020-05-06 23:41:54 +02:00
|
|
|
text: globalize.translate('MessageSyncPlayPlaybackPermissionRequired')
|
2020-04-15 18:09:34 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-04 18:20:39 +02:00
|
|
|
const apiClient = connectionManager.currentApiClient();
|
|
|
|
connectionManager.user(apiClient).then((user) => {
|
2020-05-06 23:41:54 +02:00
|
|
|
if (syncPlayEnabled) {
|
2020-04-04 18:20:39 +02:00
|
|
|
showLeaveGroupSelection(button, user, apiClient);
|
|
|
|
} else {
|
|
|
|
showNewJoinGroupSelection(button, user, apiClient);
|
|
|
|
}
|
|
|
|
}).catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
loading.hide();
|
|
|
|
toast({
|
2020-05-06 23:41:54 +02:00
|
|
|
text: globalize.translate('MessageSyncPlayNoGroupsAvailable')
|
2020-04-04 18:20:39 +02:00
|
|
|
});
|
|
|
|
});
|
2020-04-03 18:49:19 +02:00
|
|
|
}
|