channel groups
This commit is contained in:
@@ -82,6 +82,38 @@ App.session = App.session || {};
|
||||
return hydrated;
|
||||
};
|
||||
|
||||
// Builds a pseudo-channel representing a whole channel group, used so the
|
||||
// rest of the app (session, filters, video loading) can treat a selected
|
||||
// group the same way it treats a single channel.
|
||||
App.session.buildGroupChannel = function(group, channels) {
|
||||
if (!group) return null;
|
||||
const knownIds = new Set((channels || []).map((channel) => channel.id));
|
||||
const channelIds = Array.isArray(group.channelIds) ?
|
||||
group.channelIds.filter((id) => knownIds.has(id)) :
|
||||
[];
|
||||
return {
|
||||
id: `group:${group.id}`,
|
||||
name: group.title || group.id,
|
||||
isGroup: true,
|
||||
groupId: group.id,
|
||||
channelIds: channelIds
|
||||
};
|
||||
};
|
||||
|
||||
// Resolves a stored channel id (which may reference a single channel or a
|
||||
// "group:<id>" pseudo-channel) against a server's status payload.
|
||||
App.session.resolveChannelById = function(serverData, channelId) {
|
||||
if (!serverData || !channelId) return null;
|
||||
const channels = Array.isArray(serverData.channels) ? serverData.channels : [];
|
||||
if (channelId.startsWith('group:')) {
|
||||
const groupId = channelId.slice('group:'.length);
|
||||
const groups = Array.isArray(serverData.channelGroups) ? serverData.channelGroups : [];
|
||||
const group = groups.find((g) => g.id === groupId);
|
||||
return App.session.buildGroupChannel(group, channels);
|
||||
}
|
||||
return channels.find((channel) => channel.id === channelId) || null;
|
||||
};
|
||||
|
||||
App.session.savePreference = function(session) {
|
||||
if (!session || !session.server || !session.channel) return;
|
||||
const prefs = App.storage.getPreferences();
|
||||
@@ -176,7 +208,7 @@ App.session = App.session || {};
|
||||
const prefs = App.storage.getPreferences();
|
||||
const serverPrefs = prefs[selectedServerKey] || {};
|
||||
const preferredChannelId = serverPrefs.channelId;
|
||||
const channel = serverData.channels.find((ch) => ch.id === preferredChannelId) || serverData.channels[0];
|
||||
const channel = App.session.resolveChannelById(serverData, preferredChannelId) || serverData.channels[0];
|
||||
const savedOptions = serverPrefs.optionsByChannel ? serverPrefs.optionsByChannel[channel.id] : null;
|
||||
const options = savedOptions ? App.session.hydrateOptions(channel, savedOptions) : App.session.buildDefaultOptions(channel);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user