Skip to content

Commit

Permalink
Loading playlist library with new URI; Starting #600
Browse files Browse the repository at this point in the history
  • Loading branch information
jaedb committed Sep 3, 2020
1 parent 6020781 commit 6dca4ef
Show file tree
Hide file tree
Showing 13 changed files with 577 additions and 413 deletions.
612 changes: 352 additions & 260 deletions mopidy_iris/static/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion mopidy_iris/static/app.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion mopidy_iris/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@

// Release details
// These are automatically injected to built HTML
var build = "1598691847";
var build = "1599105011";
var version = "3.52.4";

// Construct the script tag
Expand Down
46 changes: 46 additions & 0 deletions src/js/components/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,39 @@ class ContextMenu extends React.Component {
hideContextMenu();
}

reload = () => {
const {
uiActions: {
hideContextMenu,
},
coreActions: {
loadArtist,
loadAlbum,
loadPlaylist,
},
menu: {
uris,
} = {},
} = this.props;

const uri = uris[0];

switch (uriType(uri)) {
case 'artist':
loadArtist(uri, true);
break;
case 'album':
loadAlbum(uri, true);
break;
case 'playlist':
loadPlaylist(uri, true);
break;
default:
break;
}
hideContextMenu();
}

renderTitle = () => {
const {
uiActions: {
Expand Down Expand Up @@ -1092,6 +1125,16 @@ class ContextMenu extends React.Component {
</div>
);

const reload = (
<div className="context-menu__item">
<a className="context-menu__item__link" onClick={this.reload}>
<span className="context-menu__item__label">
<I18n path="context_menu.reload" />
</span>
</a>
</div>
);

switch (context.name) {
case 'album':
return (
Expand All @@ -1104,6 +1147,7 @@ class ContextMenu extends React.Component {
<div className="context-menu__divider" />
{go_to_artist}
{copy_uris}
{reload}
</div>
);
case 'artist':
Expand All @@ -1116,6 +1160,7 @@ class ContextMenu extends React.Component {
<div className="context-menu__divider" />
{context.source === 'spotify' && go_to_recommendations}
{copy_uris}
{reload}
</div>
);
case 'playlist':
Expand All @@ -1138,6 +1183,7 @@ class ContextMenu extends React.Component {
{delete_playlist}
</div>
)}
{reload}
</div>
);
case 'current-track':
Expand Down
1 change: 1 addition & 0 deletions src/js/locale/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ context_menu:
discover_similar: Discover similar
start_radio: Start radio
copy_uri: Copy URI(s)
reload: Reload
dropzones:
dragging_things: 'Dragging %{count} things'
playback_controls:
Expand Down
8 changes: 8 additions & 0 deletions src/js/services/core/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ export function loadUserPlaylists(uri, force_reload = false) {
};
}

export function loadLibrary(uri, force_reload = false) {
return {
type: 'LOAD_LIBRARY',
uri,
force_reload,
};
}


/**
* Record loaders
Expand Down
99 changes: 71 additions & 28 deletions src/js/services/core/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ReactGA from 'react-ga';
import localForage from 'localforage';
import { arrayOf } from '../../util/arrays';
import URILink from '../../components/URILink';
import { uriSource, upgradeSpotifyPlaylistUris, uriType } from '../../util/helpers';
import { uriSource, upgradeSpotifyPlaylistUris, uriType, titleCase } from '../../util/helpers';
import {
formatTracks,
formatTrack,
Expand Down Expand Up @@ -348,20 +348,14 @@ const CoreMiddleware = (function () {
break;

case 'LOAD_ALBUM':

// Load from Redux store
if (
!action.force_reload
&& store.getState().core.albums[action.uri]
&& store.getState().core.albums[action.uri].tracks_uris) {
if (!action.force_reload && store.getState().core.items[action.uri]) {
console.info(`Loading "${action.uri}" from index`);
break;
}

// Try our cold storage
localForage.getItem(action.uri).then((result) => {

if (result) {
if (result && !action.force_reload) {
console.info(`Loading "${action.uri}" from database`);
store.dispatch(coreActions.restoreFromColdStore(result));
} else {
Expand Down Expand Up @@ -416,7 +410,7 @@ const CoreMiddleware = (function () {

// Try our cold storage
localForage.getItem(action.uri).then((result) => {
if (result) {
if (result && !action.force_reload) {
console.info(`Loading "${action.uri}" from database`);
store.dispatch(coreActions.restoreFromColdStore(result));
} else {
Expand All @@ -440,29 +434,40 @@ const CoreMiddleware = (function () {
break;

case 'LOAD_PLAYLIST':
if (
!action.force_reload
&& store.getState().core.playlists[action.uri]
&& store.getState().core.playlists[action.uri].tracks_uris !== undefined
) {
console.info(`Loading "${action.uri}" from index`);
break;
}
const fetchPlaylist = () => {
switch (uriSource(action.uri)) {
case 'spotify':
store.dispatch(spotifyActions.getPlaylist(action.uri));

switch (uriSource(action.uri)) {
case 'spotify':
store.dispatch(spotifyActions.getPlaylist(action.uri));
if (spotify.me) {
store.dispatch(spotifyActions.following(action.uri));
}
break;

if (spotify.me) {
store.dispatch(spotifyActions.following(action.uri));
}
break;
default:
store.dispatch(mopidyActions.getPlaylist(action.uri));
break;
}
};

default:
store.dispatch(mopidyActions.getPlaylist(action.uri));
break;
if (action.force_reload) {
fetchPlaylist();
break;
}
if (store.getState().core.items[action.uri]) {
console.info(`Using "${action.uri}" from index`);
break;
}

localForage.getItem(action.uri).then((result) => {
if (result) {
console.info(`Restoring "${action.uri}" from database`);
store.dispatch(coreActions.restoreFromColdStore(result));
} else {
fetchPlaylist();
}
});

next(action);
break;

Expand Down Expand Up @@ -514,6 +519,44 @@ const CoreMiddleware = (function () {
next(action);
break;

case 'LOAD_LIBRARY':
const fetchLibrary = () => {
switch (uriSource(action.uri)) {
case 'spotify':
store.dispatch(
spotifyActions[`getLibrary${titleCase(uriType(action.uri))}`](action.uri),
);
break;

default:
store.dispatch(
mopidyActions[`getLibrary${titleCase(uriType(action.uri))}`](action.uri),
);
break;
}
};

if (action.force_reload) {
fetchLibrary();
break;
}
if (store.getState().core.items[action.uri]) {
console.info(`Using "${action.uri}" from index`);
break;
}

localForage.getItem(action.uri).then((result) => {
if (result) {
console.info(`Restoring "${action.uri}" from database`);
store.dispatch(coreActions.restoreFromColdStore(result));
} else {
fetchLibrary();
}
});

next(action);
break;


/**
* Index actions
Expand Down
38 changes: 17 additions & 21 deletions src/js/services/mopidy/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -1600,39 +1600,35 @@ const MopidyMiddleware = (function () {
case 'MOPIDY_GET_LIBRARY_PLAYLISTS':
request(store, 'playlists.asList')
.then((response) => {
// drop in our URI list
const playlist_uris = arrayOf('uri', response);
const playlist_uris_filtered = [];

// Remove any Spotify playlists. These will be handled by our Spotify API
for (var i = 0; i < playlist_uris.length; i++) {
if (uriSource(playlist_uris[i]) != 'spotify') {
playlist_uris_filtered.push(playlist_uris[i]);
}
}

store.dispatch({ type: 'MOPIDY_LIBRARY_PLAYLISTS_LOADED', uris: playlist_uris_filtered });
store.dispatch({ type: 'MOPIDY_LIBRARY_PLAYLISTS_LOADED_ALL' });
const playlist_uris = arrayOf('uri', response).filter(
(uri) => uriSource(uri) !== 'spotify',
);
const libraryPlaylists = [];

// get the full playlist objects
for (var i = 0; i < playlist_uris_filtered.length; i++) {
request(store, 'playlists.lookup', { uri: playlist_uris_filtered[i] })
playlist_uris.forEach((uri, index) => {
request(store, 'playlists.lookup', { uri })
.then((response) => {
const source = uriSource(response.uri);
const playlist = {

libraryPlaylists.push({
type: 'playlist',
name: response.name,
uri: response.uri,
source,
source: uriSource(response.uri),
provider: 'mopidy',
last_modified: response.last_modified,
tracks_total: (response.tracks ? response.tracks.length : 0),
};
tracks: response.tracks,
});

store.dispatch(coreActions.playlistLoaded(playlist));
if (index === playlist_uris.length - 1) {
store.dispatch(coreActions.itemLoaded({
uri: 'mopidy:library:playlists',
items: libraryPlaylists,
}));
}
});
}
});
});
break;

Expand Down
Loading

0 comments on commit 6dca4ef

Please sign in to comment.