Averaging group volume for state

This commit is contained in:
James Barnsley
2019-11-08 16:39:25 +13:00
parent 0efa8a2277
commit 6cbb8823e5
11 changed files with 10063 additions and 5476 deletions

View File

@ -71,11 +71,12 @@ export function setClientMute(id, mute) {
};
}
export function setClientVolume(id, volume) {
export function setClientVolume(id, volume, group_id = null) {
return {
type: 'SNAPCAST_SET_CLIENT_VOLUME',
id,
volume,
group_id,
};
}
@ -135,6 +136,15 @@ export function setGroupVolume(id, percent, old_percent = 0) {
};
}
export function calculateGroupVolume(id, clients) {
return {
type: 'SNAPCAST_CALCULATE_GROUP_VOLUME',
id,
clients,
};
}
/**
* Record loaders
@ -167,7 +177,6 @@ export function groupsLoaded(groups, flush = false) {
return {
type: 'SNAPCAST_GROUPS_LOADED',
groups,
flush,
};
}

View File

@ -297,6 +297,7 @@ const SnapcastMiddleware = (function () {
if (raw_group.clients) {
group.clients_ids = helpers.arrayOf('id', raw_group.clients);
clients_loaded = [...clients_loaded, ...raw_group.clients];
store.dispatch(snapcastActions.calculateGroupVolume(group.id, raw_group.clients));
}
// Create a name (display only) based on it's ID
@ -316,6 +317,17 @@ const SnapcastMiddleware = (function () {
next(action);
break;
case 'SNAPCAST_CALCULATE_GROUP_VOLUME':
const totalVolume = action.clients.reduce((accumulator, client) => {
return accumulator += helpers.formatClient(client).volume;
}, 0);
store.dispatch(snapcastActions.groupLoaded({
id: action.id,
volume: totalVolume / action.clients.length,
}));
break;
case 'SNAPCAST_CLIENTS_LOADED':
var clients_index = { ...snapcast.clients };
var clients_loaded = [];
@ -405,6 +417,15 @@ const SnapcastMiddleware = (function () {
volume: response.volume.percent,
},
));
/*
// A group was referenced, so we should update the group's averaged volume
if (action.group_id) {
const group = snapcast.groups[action.group_id];
const clients = [];
snapcast.groups.filter
store.dispatch(snapcastActions.calculateGroupVolume(group.id, clients));
}
*/
},
(error) => {
store.dispatch(coreActions.handleException(

View File

@ -36,7 +36,10 @@ export default function reducer(snapcast = {}, action) {
}
for (const group of action.groups) {
groups[group.id] = group;
groups[group.id] = {
...(groups[group.id] ? groups[group.id] : {}),
...group,
}
}
return { ...snapcast, groups };