2016-08-10 23:50:23 +02:00
|
|
|
/**
|
|
|
|
* These actions are actions acting on the webplayer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Other actions
|
|
|
|
import { decrementRefCount, incrementRefCount } from "./entities";
|
2016-08-12 16:30:17 +02:00
|
|
|
import { i18nRecord } from "../models/i18n";
|
2016-08-10 23:50:23 +02:00
|
|
|
|
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
export const PLAY_PAUSE = "PLAY_PAUSE";
|
|
|
|
/**
|
2016-08-10 23:50:23 +02:00
|
|
|
* Toggle play / pause for the webplayer.
|
|
|
|
*
|
|
|
|
* @param playPause [Optional] True to play, false to pause. If not given,
|
|
|
|
* toggle the current state.
|
|
|
|
*
|
|
|
|
* @return Dispatch a PLAY_PAUSE action.
|
2016-08-07 00:58:36 +02:00
|
|
|
*/
|
|
|
|
export function togglePlaying(playPause) {
|
|
|
|
return (dispatch, getState) => {
|
2016-08-10 23:50:23 +02:00
|
|
|
let newIsPlaying = false;
|
2016-08-07 00:58:36 +02:00
|
|
|
if (typeof playPause !== "undefined") {
|
2016-08-10 23:50:23 +02:00
|
|
|
// If we want to force a mode
|
|
|
|
newIsPlaying = playPause;
|
2016-08-07 00:58:36 +02:00
|
|
|
} else {
|
2016-08-10 23:50:23 +02:00
|
|
|
// Else, just toggle
|
|
|
|
newIsPlaying = !(getState().webplayer.isPlaying);
|
2016-08-07 00:58:36 +02:00
|
|
|
}
|
2016-08-10 23:50:23 +02:00
|
|
|
// Dispatch action
|
2016-08-07 00:58:36 +02:00
|
|
|
dispatch({
|
|
|
|
type: PLAY_PAUSE,
|
|
|
|
payload: {
|
2016-08-10 23:50:23 +02:00
|
|
|
isPlaying: newIsPlaying,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const STOP_PLAYBACK = "STOP_PLAYBACK";
|
|
|
|
/**
|
|
|
|
* Stop the webplayer, clearing the playlist.
|
|
|
|
*
|
|
|
|
* Handle the entities store reference counting.
|
|
|
|
*
|
|
|
|
* @return Dispatch a STOP_PLAYBACK action.
|
|
|
|
*/
|
|
|
|
export function stopPlayback() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
// Stop playback
|
|
|
|
dispatch ({
|
|
|
|
type: STOP_PLAYBACK,
|
|
|
|
});
|
2016-09-18 22:19:50 +02:00
|
|
|
// Handle reference counting
|
|
|
|
dispatch(decrementRefCount({
|
|
|
|
song: getState().webplayer.get("playlist").toArray(),
|
|
|
|
}));
|
2016-08-10 23:50:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const SET_PLAYLIST = "SET_PLAYLIST";
|
|
|
|
/**
|
|
|
|
* Set a given playlist.
|
|
|
|
*
|
|
|
|
* Handle the entities store reference counting.
|
|
|
|
*
|
|
|
|
* @param playlist A list of song IDs.
|
|
|
|
*
|
|
|
|
* @return Dispatch a SET_PLAYLIST action.
|
|
|
|
*/
|
|
|
|
export function setPlaylist(playlist) {
|
2016-08-12 00:57:12 +02:00
|
|
|
// Attention, order of actions *do* matter in this method. We should first
|
|
|
|
// set the playlist and then handle the reference counting.
|
|
|
|
// We should first increment and then increment to avoid garbage collecting
|
|
|
|
// items that would stay in the playlist.
|
2016-08-10 23:50:23 +02:00
|
|
|
return (dispatch, getState) => {
|
2016-08-12 00:57:12 +02:00
|
|
|
const oldPlaylist = getState().webplayer.get("playlist").toArray();
|
|
|
|
|
|
|
|
// Set new playlist
|
2016-08-10 23:50:23 +02:00
|
|
|
dispatch ({
|
|
|
|
type: SET_PLAYLIST,
|
|
|
|
payload: {
|
|
|
|
playlist: playlist,
|
|
|
|
},
|
2016-08-07 00:58:36 +02:00
|
|
|
});
|
2016-08-12 00:57:12 +02:00
|
|
|
|
|
|
|
// Handle reference counting
|
|
|
|
dispatch(incrementRefCount({
|
|
|
|
song: playlist,
|
|
|
|
}));
|
|
|
|
dispatch(decrementRefCount({
|
|
|
|
song: oldPlaylist,
|
|
|
|
}));
|
2016-08-07 00:58:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Play a given song, emptying the current playlist.
|
|
|
|
*
|
|
|
|
* Handle the entities store reference counting.
|
|
|
|
*
|
|
|
|
* @param songID The id of the song to play.
|
|
|
|
*
|
|
|
|
* @return Dispatch a SET_PLAYLIST action to play this song and start playing.
|
|
|
|
*/
|
|
|
|
export function playSong(songID) {
|
2016-08-12 00:57:12 +02:00
|
|
|
// Attention, order of actions *do* matter in this method. We should first
|
|
|
|
// set the playlist and then handle the reference counting.
|
|
|
|
// We should first increment and then increment to avoid garbage collecting
|
|
|
|
// items that would stay in the playlist.
|
2016-08-07 00:58:36 +02:00
|
|
|
return (dispatch, getState) => {
|
2016-08-12 00:57:12 +02:00
|
|
|
// Get old and new playlists
|
|
|
|
const oldPlaylist = getState().webplayer.get("playlist").toArray();
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
// Set new playlist
|
2016-08-07 00:58:36 +02:00
|
|
|
dispatch({
|
2016-08-10 23:50:23 +02:00
|
|
|
type: SET_PLAYLIST,
|
2016-08-07 00:58:36 +02:00
|
|
|
payload: {
|
2016-08-10 23:50:23 +02:00
|
|
|
playlist: [songID],
|
|
|
|
},
|
2016-08-07 00:58:36 +02:00
|
|
|
});
|
2016-08-12 00:57:12 +02:00
|
|
|
|
|
|
|
// Handle reference counting
|
|
|
|
dispatch(incrementRefCount({
|
|
|
|
song: [songID],
|
|
|
|
}));
|
|
|
|
dispatch(decrementRefCount({
|
|
|
|
song: oldPlaylist,
|
|
|
|
}));
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
// Force playing
|
2016-08-07 00:58:36 +02:00
|
|
|
dispatch(togglePlaying(true));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
|
|
|
|
export const PUSH_SONG = "PUSH_SONG";
|
|
|
|
/**
|
|
|
|
* Push a given song in the playlist.
|
|
|
|
*
|
|
|
|
* Handle the entities store reference counting.
|
|
|
|
*
|
|
|
|
* @param songID The id of the song to push.
|
|
|
|
* @param index [Optional] The position to insert at in the playlist.
|
2016-08-11 22:01:47 +02:00
|
|
|
* If negative, counts from the end. Undefined (default)
|
|
|
|
* is last position.
|
2016-08-10 23:50:23 +02:00
|
|
|
*
|
|
|
|
* @return Dispatch a PUSH_SONG action.
|
|
|
|
*/
|
2016-08-11 22:01:47 +02:00
|
|
|
export function pushSong(songID, index) {
|
2016-08-10 23:50:23 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
// Handle reference counting
|
|
|
|
dispatch(incrementRefCount({
|
|
|
|
song: [songID],
|
|
|
|
}));
|
|
|
|
// Push song
|
2016-08-07 00:58:36 +02:00
|
|
|
dispatch({
|
2016-08-10 23:50:23 +02:00
|
|
|
type: PUSH_SONG,
|
2016-08-07 00:58:36 +02:00
|
|
|
payload: {
|
2016-08-10 23:50:23 +02:00
|
|
|
song: songID,
|
|
|
|
index: index,
|
|
|
|
},
|
2016-08-07 00:58:36 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2016-08-10 23:50:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
export const POP_SONG = "POP_SONG";
|
|
|
|
/**
|
|
|
|
* Pop a given song from the playlist.
|
|
|
|
*
|
|
|
|
* Handle the entities store reference counting.
|
|
|
|
*
|
|
|
|
* @param songID The id of the song to pop.
|
|
|
|
*
|
|
|
|
* @return Dispatch a POP_SONG action.
|
|
|
|
*/
|
|
|
|
export function popSong(songID) {
|
|
|
|
return (dispatch) => {
|
|
|
|
// Handle reference counting
|
|
|
|
dispatch(decrementRefCount({
|
|
|
|
song: [songID],
|
|
|
|
}));
|
|
|
|
// Pop song
|
2016-08-07 00:58:36 +02:00
|
|
|
dispatch({
|
2016-08-10 23:50:23 +02:00
|
|
|
type: POP_SONG,
|
2016-08-07 00:58:36 +02:00
|
|
|
payload: {
|
2016-08-10 23:50:23 +02:00
|
|
|
song: songID,
|
|
|
|
},
|
2016-08-07 00:58:36 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
|
|
|
|
export const JUMP_TO_SONG = "JUMP_TO_SONG";
|
|
|
|
/**
|
|
|
|
* Set current playlist index to specific song.
|
|
|
|
*
|
|
|
|
* @param songID The id of the song to play.
|
|
|
|
*
|
|
|
|
* @return Dispatch a JUMP_TO_SONG action.
|
|
|
|
*/
|
|
|
|
export function jumpToSong(songID) {
|
|
|
|
return (dispatch) => {
|
|
|
|
// Push song
|
|
|
|
dispatch({
|
|
|
|
type: JUMP_TO_SONG,
|
|
|
|
payload: {
|
|
|
|
song: songID,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 22:01:47 +02:00
|
|
|
export const PLAY_PREVIOUS_SONG = "PLAY_PREVIOUS_SONG";
|
2016-08-10 23:50:23 +02:00
|
|
|
/**
|
|
|
|
* Move one song backwards in the playlist.
|
|
|
|
*
|
2016-08-11 22:01:47 +02:00
|
|
|
* @return Dispatch a PLAY_PREVIOUS_SONG action.
|
2016-08-10 23:50:23 +02:00
|
|
|
*/
|
2016-08-11 22:01:47 +02:00
|
|
|
export function playPreviousSong() {
|
2016-08-10 23:50:23 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
dispatch({
|
2016-08-11 22:01:47 +02:00
|
|
|
type: PLAY_PREVIOUS_SONG,
|
2016-08-10 23:50:23 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 22:01:47 +02:00
|
|
|
export const PLAY_NEXT_SONG = "PLAY_NEXT_SONG";
|
2016-08-10 23:50:23 +02:00
|
|
|
/**
|
|
|
|
* Move one song forward in the playlist.
|
|
|
|
*
|
2016-08-11 22:01:47 +02:00
|
|
|
* @return Dispatch a PLAY_NEXT_SONG action.
|
2016-08-10 23:50:23 +02:00
|
|
|
*/
|
2016-08-11 22:01:47 +02:00
|
|
|
export function playNextSong() {
|
2016-08-10 23:50:23 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
dispatch({
|
2016-08-11 22:01:47 +02:00
|
|
|
type: PLAY_NEXT_SONG,
|
2016-08-10 23:50:23 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
export const TOGGLE_RANDOM = "TOGGLE_RANDOM";
|
2016-08-10 23:50:23 +02:00
|
|
|
/**
|
|
|
|
* Toggle random mode.
|
|
|
|
*
|
|
|
|
* @return Dispatch a TOGGLE_RANDOM action.
|
|
|
|
*/
|
2016-08-07 00:58:36 +02:00
|
|
|
export function toggleRandom() {
|
2016-08-12 16:30:17 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: TOGGLE_RANDOM,
|
|
|
|
});
|
2016-08-07 00:58:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
export const TOGGLE_REPEAT = "TOGGLE_REPEAT";
|
2016-08-10 23:50:23 +02:00
|
|
|
/**
|
|
|
|
* Toggle repeat mode.
|
|
|
|
*
|
|
|
|
* @return Dispatch a TOGGLE_REPEAT action.
|
|
|
|
*/
|
2016-08-07 00:58:36 +02:00
|
|
|
export function toggleRepeat() {
|
2016-08-12 16:30:17 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: TOGGLE_REPEAT,
|
|
|
|
});
|
2016-08-07 00:58:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
export const TOGGLE_MUTE = "TOGGLE_MUTE";
|
2016-08-10 23:50:23 +02:00
|
|
|
/**
|
|
|
|
* Toggle mute mode.
|
|
|
|
*
|
|
|
|
* @return Dispatch a TOGGLE_MUTE action.
|
|
|
|
*/
|
2016-08-07 00:58:36 +02:00
|
|
|
export function toggleMute() {
|
2016-08-12 16:30:17 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: TOGGLE_MUTE,
|
|
|
|
});
|
2016-08-10 23:50:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const SET_VOLUME = "SET_VOLUME";
|
|
|
|
/**
|
|
|
|
* Set the volume.
|
|
|
|
*
|
|
|
|
* @param volume Volume to set (between 0 and 100)
|
|
|
|
*
|
|
|
|
* @return Dispatch a SET_VOLUME action.
|
|
|
|
*/
|
|
|
|
export function setVolume(volume) {
|
2016-08-12 16:30:17 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: SET_VOLUME,
|
|
|
|
payload: {
|
|
|
|
volume: volume,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const SET_ERROR = "SET_ERROR";
|
2016-09-18 21:23:28 +02:00
|
|
|
export const UNSUPPORTED_MEDIA_TYPE = "UNSUPPORTED_MEDIA_TYPE";
|
|
|
|
export const ONLOAD_ERROR = "ONLOAD_ERROR";
|
2016-08-12 16:30:17 +02:00
|
|
|
/**
|
2016-09-18 21:23:28 +02:00
|
|
|
* Set an error appearing in the webplayer component.
|
2016-08-12 16:30:17 +02:00
|
|
|
*
|
2016-09-18 21:23:28 +02:00
|
|
|
* @note Does not do anything in case an invalid error identifier is passed.
|
|
|
|
*
|
|
|
|
* @return Dispatch the matching SET_ERROR action.
|
2016-08-12 16:30:17 +02:00
|
|
|
*/
|
2016-09-18 21:23:28 +02:00
|
|
|
export function setError(errorType) {
|
|
|
|
let dispatchedError;
|
|
|
|
switch (errorType) {
|
|
|
|
case UNSUPPORTED_MEDIA_TYPE:
|
|
|
|
dispatchedError = new i18nRecord({
|
|
|
|
id: "app.webplayer.unsupported",
|
|
|
|
values: {},
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case ONLOAD_ERROR:
|
|
|
|
dispatchedError = new i18nRecord({
|
|
|
|
id: "app.webplayer.onLoadError",
|
|
|
|
values: {},
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2016-08-12 16:30:17 +02:00
|
|
|
return (dispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: SET_ERROR,
|
|
|
|
payload: {
|
2016-09-18 21:23:28 +02:00
|
|
|
error: dispatchedError,
|
2016-08-12 16:30:17 +02:00
|
|
|
},
|
|
|
|
});
|
2016-08-07 00:58:36 +02:00
|
|
|
};
|
|
|
|
}
|