Moving tracks into 'items' database; in_library now pre-populated; Next to add/remove from library

This commit is contained in:
James Barnsley
2020-09-11 22:13:06 +12:00
parent 5953fc84cf
commit f254170b2f
23 changed files with 297 additions and 318 deletions

View File

@ -315,6 +315,7 @@ const CoreMiddleware = (function () {
* */
case 'LOAD_ITEMS':
action.uris.forEach((uri) => {
store.dispatch(uiActions.startLoading(uri, `load_item_${uri}`));
store.dispatch({
type: `LOAD_${uriType(uri).toUpperCase()}`,
uri,
@ -574,11 +575,12 @@ const CoreMiddleware = (function () {
break;
case 'LOAD_LIBRARY':
store.dispatch(uiActions.startLoading(action.uri, `load_library_${action.uri}`));
const fetchLibrary = () => {
switch (uriSource(action.uri)) {
case 'spotify':
store.dispatch(
spotifyActions[`getLibrary${titleCase(uriType(action.uri))}`](action.forceRefetch),
spotifyActions[`getLibrary${titleCase(uriType(action.uri))}`](action.options.forceRefetch),
);
break;
case 'google':
@ -730,10 +732,14 @@ const CoreMiddleware = (function () {
break;
case 'ITEMS_LOADED':
const mergedItems = action.items.map((item) => ({
...core.items[item.uri] || {},
...item,
}));
const mergedItems = [];
action.items.forEach((item) => {
mergedItems.push({
...core.items[item.uri] || {},
...item,
});
store.dispatch(uiActions.stopLoading(item.uri));
});
store.dispatch(coreActions.updateColdStore(mergedItems));
next({
...action,
@ -742,10 +748,21 @@ const CoreMiddleware = (function () {
break;
case 'LIBRARY_LOADED':
store.dispatch(uiActions.stopLoading(action.library.uri));
store.dispatch(coreActions.updateColdStore([action.library]));
next(action);
break;
case 'RESTORE_ITEMS_FROM_COLD_STORE':
action.items.forEach((item) => store.dispatch(uiActions.stopLoading(item.uri)));
next(action);
break;
case 'RESTORE_LIBRARY_FROM_COLD_STORE':
store.dispatch(uiActions.stopLoading(action.library.uri));
next(action);
break;
case 'ARTISTS_LOADED':
var artists_index = { ...core.artists };
var artists_loaded = [];

View File

@ -147,7 +147,7 @@ export function getMe() {
* */
export function getTrackLyrics(uri, path) {
return (dispatch, getState) => {
dispatch(coreActions.trackLoaded({
dispatch(coreActions.itemLoaded({
uri,
lyrics: null,
lyrics_path: null,
@ -187,7 +187,7 @@ export function getTrackLyrics(uri, path) {
lyrics_html = lyrics_html.replace(/(\[)/g, '<span class="mid_grey-text">[');
lyrics_html = lyrics_html.replace(/(\])/g, ']</span>');
dispatch(coreActions.trackLoaded({
dispatch(coreActions.itemLoaded({
uri,
lyrics: lyrics_html,
lyrics_path: path,
@ -232,7 +232,7 @@ export function findTrackLyrics(track = null) {
path: response.hits[i].result.path,
});
}
dispatch(coreActions.trackLoaded({
dispatch(coreActions.itemLoaded({
uri: track.uri,
lyrics_results,
}));

View File

@ -1,5 +1,10 @@
import { formatImages, collate } from '../../util/format';
import {
collate,
formatImages,
formatTrack,
formatArtist,
formatAlbum,
} from '../../util/format';
import { generateGuid } from '../../util/helpers';
const coreActions = require('../core/actions');
@ -173,21 +178,12 @@ export function getMe() {
export function getTrack(uri) {
return (dispatch, getState) => {
if (getState().core.tracks[uri] !== undefined) {
var track = getState().core.tracks[uri];
if (!track.artists) {
dispatch(coreActions.handleException(
'Could not get LastFM track',
{},
'Track has no artists',
));
return;
}
} else {
const track = getState().core.items[uri];
if (!track || !track.artists) {
dispatch(coreActions.handleException(
'Could not get LastFM track',
{},
'Could not find track in index',
'Not in index or has no artists',
));
return;
}
@ -202,13 +198,15 @@ export function getTrack(uri) {
.then(
(response) => {
if (response.track) {
const merged_track = {
uri: track.uri,
...response.track,
...track,
};
dispatch(coreActions.trackLoaded(merged_track));
dispatch(
coreActions.itemLoaded(
formatTrack({
uri: track.uri,
...response.track,
...track,
}),
),
);
}
},
);
@ -228,14 +226,18 @@ export function getArtist(uri, artist, mbid = false) {
.then(
(response) => {
if (response.artist) {
dispatch(coreActions.itemLoaded({
uri,
mbid: response.artist.mbid,
biography: response.artist.bio.content,
biography_publish_date: response.artist.bio.published,
biography_link: response.artist.bio.links.link.href,
listeners: parseInt(response.artist.stats.listeners),
}));
dispatch(
coreActions.itemLoaded(
formatArtist({
uri,
mbid: response.artist.mbid,
biography: response.artist.bio.content,
biography_publish_date: response.artist.bio.published,
biography_link: response.artist.bio.links.link.href,
listeners: parseInt(response.artist.stats.listeners),
}),
),
);
}
},
);
@ -273,7 +275,7 @@ export function getAlbum(uri, artist, album, mbid = false) {
delete album.images;
}
dispatch(coreActions.itemLoaded(album));
dispatch(coreActions.itemLoaded(formatAlbum(album)));
}
},
);
@ -305,7 +307,7 @@ export function getImages(context, uri) {
(response) => {
if (response.album) {
const images = formatImages(response.album.image);
dispatch(coreActions.trackLoaded({ uri, images }));
dispatch(coreActions.itemLoaded(formatAlbum({ uri, images })));
}
},
);
@ -351,8 +353,8 @@ export function getImages(context, uri) {
export function loveTrack(uri) {
return (dispatch, getState) => {
if (getState().core.tracks[uri] !== undefined) {
var track = getState().core.tracks[uri];
if (getState().core.items[uri] !== undefined) {
var track = getState().core.items[uri];
if (!track.artists) {
dispatch(coreActions.handleException(
'Could not love LastFM track',
@ -375,15 +377,10 @@ export function loveTrack(uri) {
sendSignedRequest(dispatch, getState, params)
.then(
(response) => {
track = {
...track,
dispatch(coreActions.itemLoaded({
uri,
userloved: true,
};
dispatch({
type: 'TRACKS_LOADED',
tracks: [track],
});
}));
},
);
};
@ -391,8 +388,8 @@ export function loveTrack(uri) {
export function unloveTrack(uri) {
return (dispatch, getState) => {
if (getState().core.tracks[uri] !== undefined) {
var track = getState().core.tracks[uri];
if (getState().core.items[uri] !== undefined) {
var track = getState().core.items[uri];
if (!track.artists) {
dispatch(coreActions.handleException(
'Could not unlove LastFM track',
@ -415,15 +412,10 @@ export function unloveTrack(uri) {
sendSignedRequest(dispatch, getState, params)
.then(
(response) => {
track = {
...track,
dispatch(coreActions.itemLoaded({
uri,
userloved: false,
};
dispatch({
type: 'TRACKS_LOADED',
tracks: [track],
});
}));
},
);
};

View File

@ -57,6 +57,7 @@ const MopidyMiddleware = (function () {
case 'state:online':
store.dispatch({ type: 'MOPIDY_CONNECTED' });
store.dispatch(mopidyActions.getCurrentTrack());
store.dispatch(mopidyActions.getPlayState());
store.dispatch(mopidyActions.getVolume());
store.dispatch(mopidyActions.getMute());
@ -64,15 +65,14 @@ const MopidyMiddleware = (function () {
store.dispatch(mopidyActions.getRandom());
store.dispatch(mopidyActions.getRepeat());
store.dispatch(mopidyActions.getQueue());
store.dispatch(mopidyActions.getCurrentTrack());
store.dispatch(mopidyActions.getTimePosition());
store.dispatch(mopidyActions.getUriSchemes());
// Every 1s update our play position (when playing)
progress_interval = setInterval(() => {
if (store.getState().mopidy.play_state == 'playing') {
if (store.getState().mopidy.play_state === 'playing') {
// Every 10s get real position from server, provided we're in-focus
if (progress_interval_counter % 5 == 0 && store.getState().ui.window_focus === true) {
if (progress_interval_counter % 5 === 0 && store.getState().ui.window_focus === true) {
store.dispatch(mopidyActions.getTimePosition());
// Otherwise we just assume to add 1000ms every 1000ms of play time
@ -80,7 +80,7 @@ const MopidyMiddleware = (function () {
store.dispatch(mopidyActions.timePosition(store.getState().mopidy.time_position + 1000));
}
progress_interval_counter++;
progress_interval_counter += 1;
}
}, 1000);
@ -326,13 +326,13 @@ const MopidyMiddleware = (function () {
// Focus has just been regained
if (action.window_focus === true) {
store.dispatch(mopidyActions.getCurrentTrack());
store.dispatch(mopidyActions.getPlayState());
store.dispatch(mopidyActions.getVolume());
store.dispatch(mopidyActions.getMute());
store.dispatch(mopidyActions.getConsume());
store.dispatch(mopidyActions.getRandom());
store.dispatch(mopidyActions.getRepeat());
store.dispatch(mopidyActions.getCurrentTrack());
store.dispatch(mopidyActions.getTimePosition());
}
break;
@ -1980,7 +1980,7 @@ const MopidyMiddleware = (function () {
const tracks = indexToArray(_response);
store.dispatch(coreActions.tracksLoaded(tracks));
store.dispatch(coreActions.itemsLoaded(formatTracks(tracks)));
if (action.get_images) {
store.dispatch(mopidyActions.getImages(arrayOf('uri', tracks)));

View File

@ -1725,14 +1725,14 @@ export function getLibraryPlaylists(forceRefetch) {
.then((response) => {
const items = response.items.map(
(item) => ({
...item,
...formatPlaylist(item),
in_library: true,
can_edit: (getState().spotify.me && item.owner.id === getState().spotify.me.id),
}),
);
libraryItems = [...libraryItems, ...formatPlaylists(items)];
libraryItems = [...libraryItems, ...items];
if (response.next) {
fetchLibraryPlaylists(response.next);
fetchLibraryPlaylists(`${response.next}${forceRefetch ? `&refetch=${Date.now()}` : ''}`);
} else {
dispatch(coreActions.itemsLoaded(libraryItems));
dispatch(coreActions.libraryLoaded({
@ -1752,11 +1752,14 @@ export function getLibraryAlbums(forceRefetch) {
const fetchLibraryAlbums = (endpoint) => request(dispatch, getState, endpoint)
.then((response) => {
const items = response.items.map(
(item) => ({ ...item, in_library: true }),
(item) => ({
...formatAlbum(item),
in_library: true,
}),
);
libraryItems = [...libraryItems, ...formatAlbums(items)];
libraryItems = [...libraryItems, ...items];
if (response.next) {
fetchLibraryAlbums(response.next);
fetchLibraryAlbums(`${response.next}${forceRefetch ? `&refetch=${Date.now()}` : ''}`);
} else {
dispatch(coreActions.itemsLoaded(libraryItems));
dispatch(coreActions.libraryLoaded({
@ -1776,11 +1779,14 @@ export function getLibraryArtists(forceRefetch) {
const fetchLibraryArtists = (endpoint) => request(dispatch, getState, endpoint)
.then((response) => {
const items = response.artists.items.map(
(item) => ({ ...item, in_library: true }),
(item) => ({
...formatArtist(item),
in_library: true,
}),
);
libraryItems = [...libraryItems, ...formatArtists(items)];
libraryItems = [...libraryItems, ...items];
if (response.next) {
fetchLibraryArtists(response.next);
fetchLibraryArtists(`${response.next}${forceRefetch ? `&refetch=${Date.now()}` : ''}`);
} else {
dispatch(coreActions.itemsLoaded(libraryItems));
dispatch(coreActions.libraryLoaded({

View File

@ -263,8 +263,8 @@ export function removeNotification(key, manual = false) {
export function startLoading(key, source) {
return {
type: 'START_LOADING',
source,
key,
source,
};
}