2016-08-10 23:50:23 +02:00
|
|
|
/**
|
|
|
|
* This implements the webplayer reducers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NPM imports
|
2016-08-07 00:58:36 +02:00
|
|
|
import Immutable from "immutable";
|
|
|
|
|
2016-08-10 23:50:23 +02:00
|
|
|
// Local imports
|
|
|
|
import { createReducer } from "../utils";
|
|
|
|
|
|
|
|
// Models
|
|
|
|
import { stateRecord } from "../models/webplayer";
|
|
|
|
|
|
|
|
// Actions
|
2016-08-07 00:58:36 +02:00
|
|
|
import {
|
|
|
|
PLAY_PAUSE,
|
2016-08-10 23:50:23 +02:00
|
|
|
STOP_PLAYBACK,
|
|
|
|
SET_PLAYLIST,
|
|
|
|
PUSH_SONG,
|
|
|
|
POP_SONG,
|
|
|
|
JUMP_TO_SONG,
|
2016-08-11 22:01:47 +02:00
|
|
|
PLAY_PREVIOUS_SONG,
|
|
|
|
PLAY_NEXT_SONG,
|
2016-08-07 00:58:36 +02:00
|
|
|
TOGGLE_RANDOM,
|
|
|
|
TOGGLE_REPEAT,
|
2016-08-08 12:22:35 +02:00
|
|
|
TOGGLE_MUTE,
|
2016-08-10 23:50:23 +02:00
|
|
|
SET_VOLUME,
|
2016-08-12 16:30:17 +02:00
|
|
|
SET_ERROR,
|
2016-08-08 12:22:35 +02:00
|
|
|
INVALIDATE_STORE } from "../actions";
|
2016-08-10 23:50:23 +02:00
|
|
|
|
2016-08-07 00:58:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initial state
|
|
|
|
*/
|
|
|
|
|
|
|
|
var initialState = new stateRecord();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reducers
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default createReducer(initialState, {
|
|
|
|
[PLAY_PAUSE]: (state, payload) => {
|
2016-08-10 23:50:23 +02:00
|
|
|
// Force play or pause
|
2016-08-12 16:30:17 +02:00
|
|
|
return (
|
|
|
|
state
|
|
|
|
.set("isPlaying", payload.isPlaying)
|
|
|
|
.set("error", null)
|
|
|
|
);
|
2016-08-07 00:58:36 +02:00
|
|
|
},
|
2016-08-10 23:50:23 +02:00
|
|
|
[STOP_PLAYBACK]: (state) => {
|
|
|
|
// Clear the playlist
|
2016-08-12 16:30:17 +02:00
|
|
|
return (
|
|
|
|
state
|
|
|
|
.set("isPlaying", false)
|
|
|
|
.set("currentIndex", 0)
|
|
|
|
.set("playlist", new Immutable.List())
|
|
|
|
.set("error", null)
|
|
|
|
);
|
2016-08-07 00:58:36 +02:00
|
|
|
},
|
2016-08-10 23:50:23 +02:00
|
|
|
[SET_PLAYLIST]: (state, payload) => {
|
|
|
|
// Set current playlist, reset playlist index
|
2016-08-07 00:58:36 +02:00
|
|
|
return (
|
|
|
|
state
|
|
|
|
.set("playlist", new Immutable.List(payload.playlist))
|
|
|
|
.set("currentIndex", 0)
|
2016-08-12 16:30:17 +02:00
|
|
|
.set("error", null)
|
2016-08-07 00:58:36 +02:00
|
|
|
);
|
|
|
|
},
|
2016-08-10 23:50:23 +02:00
|
|
|
[PUSH_SONG]: (state, payload) => {
|
|
|
|
// Push song to playlist
|
2016-08-11 22:01:47 +02:00
|
|
|
let newState = state;
|
|
|
|
if (payload.index) {
|
|
|
|
// If index is specified, insert it at this position
|
|
|
|
newState = newState.set(
|
|
|
|
"playlist",
|
|
|
|
newState.get("playlist").insert(payload.index, payload.song)
|
|
|
|
);
|
|
|
|
if (payload.index <= newState.get("currentIndex")) { // "<=" because insertion is made before
|
|
|
|
// If we insert before the current position in the playlist, we
|
|
|
|
// update the current position to keep the currently played
|
|
|
|
// music
|
|
|
|
newState = newState.set(
|
|
|
|
"currentIndex",
|
|
|
|
Math.min(newState.get("currentIndex") + 1, newState.get("playlist").size)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Else, push at the end of the playlist
|
|
|
|
newState = newState.set(
|
|
|
|
"playlist",
|
|
|
|
newState.get("playlist").push(payload.song)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return newState;
|
2016-08-10 23:50:23 +02:00
|
|
|
},
|
|
|
|
[POP_SONG]: (state, payload) => {
|
|
|
|
// Pop song from playlist
|
2016-08-11 22:01:47 +02:00
|
|
|
let newState = state.deleteIn(["playlist", payload.index]);
|
|
|
|
if (payload.index < state.get("currentIndex")) {
|
|
|
|
// If we delete before the current position in the playlist, we
|
|
|
|
// update the current position to keep the currently played
|
|
|
|
// music
|
|
|
|
newState = newState.set(
|
|
|
|
"currentIndex",
|
|
|
|
Math.max(newState.get("currentIndex") - 1, 0)
|
|
|
|
);
|
2016-08-12 16:30:17 +02:00
|
|
|
} else if (payload.index == state.get("currentIndex")) {
|
|
|
|
// If we remove current song, clear the error as well
|
|
|
|
newState = newState.set("error", null);
|
2016-08-11 22:01:47 +02:00
|
|
|
}
|
|
|
|
return newState;
|
2016-08-10 23:50:23 +02:00
|
|
|
},
|
|
|
|
[JUMP_TO_SONG]: (state, payload) => {
|
|
|
|
// Set current index
|
|
|
|
const newCurrentIndex = state.get("playlist").findKey(x => x == payload.song);
|
|
|
|
return state.set("currentIndex", newCurrentIndex);
|
|
|
|
},
|
2016-08-11 22:01:47 +02:00
|
|
|
[PLAY_PREVIOUS_SONG]: (state) => {
|
2016-08-10 23:50:23 +02:00
|
|
|
const newIndex = state.get("currentIndex") - 1;
|
|
|
|
if (newIndex < 0) {
|
2016-08-11 23:46:42 +02:00
|
|
|
// If there is an overlow on the left of the playlist, just play
|
|
|
|
// first music again
|
|
|
|
// TODO: Should seek to beginning of music
|
2016-08-12 16:30:17 +02:00
|
|
|
return state.set("error", null);
|
2016-08-10 23:50:23 +02:00
|
|
|
} else {
|
2016-08-12 16:30:17 +02:00
|
|
|
return (
|
|
|
|
state
|
|
|
|
.set("currentIndex", newIndex)
|
|
|
|
.set("error", null)
|
|
|
|
);
|
2016-08-10 23:50:23 +02:00
|
|
|
}
|
|
|
|
},
|
2016-08-11 22:01:47 +02:00
|
|
|
[PLAY_NEXT_SONG]: (state) => {
|
2016-09-18 20:57:51 +02:00
|
|
|
let newIndex = state.get("currentIndex") + 1;
|
2016-08-11 23:46:42 +02:00
|
|
|
if (newIndex >= state.get("playlist").size) {
|
|
|
|
// If there is an overflow
|
|
|
|
if (state.get("isRepeat")) {
|
2016-09-18 20:57:51 +02:00
|
|
|
// If we are at the end of the playlist and repeat mode is on,
|
|
|
|
// just play back first song.
|
|
|
|
newIndex = 0;
|
2016-08-11 23:46:42 +02:00
|
|
|
} else {
|
|
|
|
// Just stop playback
|
2016-08-12 16:30:17 +02:00
|
|
|
return (
|
|
|
|
state
|
|
|
|
.set("isPlaying", false)
|
|
|
|
.set("error", null)
|
|
|
|
);
|
2016-08-11 23:46:42 +02:00
|
|
|
}
|
2016-08-10 23:50:23 +02:00
|
|
|
} else {
|
|
|
|
// Else, play next item
|
2016-08-12 16:30:17 +02:00
|
|
|
return (
|
|
|
|
state
|
|
|
|
.set("currentIndex", newIndex)
|
|
|
|
.set("error", null)
|
|
|
|
);
|
2016-08-10 23:50:23 +02:00
|
|
|
}
|
|
|
|
},
|
2016-08-07 00:58:36 +02:00
|
|
|
[TOGGLE_RANDOM]: (state) => {
|
|
|
|
return state.set("isRandom", !state.get("isRandom"));
|
|
|
|
},
|
|
|
|
[TOGGLE_REPEAT]: (state) => {
|
|
|
|
return state.set("isRepeat", !state.get("isRepeat"));
|
|
|
|
},
|
|
|
|
[TOGGLE_MUTE]: (state) => {
|
|
|
|
return state.set("isMute", !state.get("isMute"));
|
|
|
|
},
|
2016-08-10 23:50:23 +02:00
|
|
|
[SET_VOLUME]: (state, payload) => {
|
|
|
|
return state.set("volume", payload.volume);
|
|
|
|
},
|
2016-08-12 16:30:17 +02:00
|
|
|
[SET_ERROR]: (state, payload) => {
|
|
|
|
return (
|
|
|
|
state
|
|
|
|
.set("isPlaying", false)
|
|
|
|
.set("error", payload.error)
|
|
|
|
);
|
|
|
|
},
|
2016-08-08 12:22:35 +02:00
|
|
|
[INVALIDATE_STORE]: () => {
|
|
|
|
return new stateRecord();
|
2016-08-10 23:50:23 +02:00
|
|
|
},
|
2016-08-07 00:58:36 +02:00
|
|
|
});
|