URI format for libraries, but own core.libraries for better control

This commit is contained in:
James Barnsley
2020-09-06 13:53:22 +12:00
parent f6f5e37aa9
commit 1bc7f2b372
21 changed files with 1231 additions and 1346 deletions

View File

@ -10,14 +10,13 @@ import FilterField from '../../components/Fields/FilterField';
import LazyLoadListener from '../../components/LazyLoadListener';
import Icon from '../../components/Icon';
import * as uiActions from '../../services/ui/actions';
import * as mopidyActions from '../../services/mopidy/actions';
import * as spotifyActions from '../../services/spotify/actions';
import * as googleActions from '../../services/google/actions';
import * as coreActions from '../../services/core/actions';
import {
uriSource,
} from '../../util/helpers';
import { sortItems, applyFilter } from '../../util/arrays';
import { I18n, i18n } from '../../locale';
import { collate, collateLibrary } from '../../util/format';
class LibraryArtists extends React.Component {
constructor(props) {
@ -65,51 +64,44 @@ class LibraryArtists extends React.Component {
getMopidyLibrary = () => {
const {
source,
mopidy_library_artists,
mopidyActions: {
getLibraryArtists,
coreActions: {
loadLibrary,
},
} = this.props;
if (source !== 'local' && source !== 'all') return;
if (mopidy_library_artists) return;
getLibraryArtists();
loadLibrary('mopidy:library:artists');
};
getGoogleLibrary = () => {
const {
source,
google_available,
google_library_artists,
googleActions: {
getLibraryArtists,
coreActions: {
loadLibrary,
},
} = this.props;
if (!google_available) return;
if (source !== 'google' && source !== 'all') return;
if (google_library_artists) return;
getLibraryArtists();
loadLibrary('google:library:artists');
};
getSpotifyLibrary = () => {
const {
source,
spotify_available,
spotify_library_artists_status,
spotifyActions: {
getLibraryArtists,
coreActions: {
loadLibrary,
},
} = this.props;
if (!spotify_available) return;
if (source !== 'spotify' && source !== 'all') return;
if (spotify_library_artists_status === 'finished') return;
if (spotify_library_artists_status === 'started') return;
getLibraryArtists();
loadLibrary('spotify:library:artists');
};
handleContextMenu(e, item) {
@ -144,70 +136,41 @@ class LibraryArtists extends React.Component {
this.props.uiActions.set(data);
}
renderView() {
let artists = [];
renderView = () => {
const {
spotify_library,
google_library,
mopidy_library,
items,
source,
sort,
sort_reverse,
view,
} = this.props;
const {
limit,
filter,
} = this.state;
// Mopidy library items
if (this.props.mopidy_library_artists && (this.props.source == 'all' || this.props.source == 'local')) {
for (uri of this.props.mopidy_library_artists) {
// Construct item placeholder. This is used as Mopidy needs to
// lookup ref objects to get the full object which can take some time
var source = uriSource(uri);
var artist = {
uri,
source,
};
let artists = [
...(source === 'all' || source === 'spotify' ? collate(spotify_library, { items }).items : []),
...(source === 'all' || source === 'google' ? collate(google_library, { items }).items : []),
...(source === 'all' || source === 'local' ? collate(mopidy_library, { items }).items : []),
];
if (this.props.artists.hasOwnProperty(uri)) {
artist = this.props.artists[uri];
}
artists.push(artist);
}
if (sort) {
artists = sortItems(artists, sort, sort_reverse);
}
// Google library items
if (this.props.google_library_artists && (this.props.source == 'all' || this.props.source == 'google')) {
for (uri of this.props.google_library_artists) {
// Construct item placeholder. This is used as Mopidy needs to
// lookup ref objects to get the full object which can take some time
var source = uriSource(uri);
var artist = {
uri,
source,
};
if (this.props.artists.hasOwnProperty(uri)) {
artist = this.props.artists[uri];
}
artists.push(artist);
}
}
// Spotify library items
if (this.props.spotify_library_artists && (this.props.source == 'all' || this.props.source == 'spotify')) {
for (let i = 0; i < this.props.spotify_library_artists.length; i++) {
var uri = this.props.spotify_library_artists[i];
if (this.props.artists.hasOwnProperty(uri)) {
artists.push(this.props.artists[uri]);
}
}
}
if (this.props.sort) {
artists = sortItems(artists, this.props.sort, this.props.sort_reverse);
}
if (this.state.filter !== '') {
artists = applyFilter('name', this.state.filter, artists);
if (filter !== '') {
artists = applyFilter('name', filter, artists);
}
// Apply our lazy-load-rendering
const total_artists = artists.length;
artists = artists.slice(0, this.state.limit);
artists = artists.slice(0, limit);
if (this.props.view == 'list') {
if (view === 'list') {
return (
<section className="content-wrapper">
<List
@ -220,8 +183,8 @@ class LibraryArtists extends React.Component {
link_prefix="/artist/"
/>
<LazyLoadListener
loadKey={total_artists > this.state.limit ? this.state.limit : total_artists}
showLoader={this.state.limit < total_artists}
loadKey={total_artists > limit ? limit : total_artists}
showLoader={limit < total_artists}
loadMore={() => this.loadMore()}
/>
</section>
@ -234,15 +197,20 @@ class LibraryArtists extends React.Component {
artists={artists}
/>
<LazyLoadListener
loadKey={total_artists > this.state.limit ? this.state.limit : total_artists}
showLoader={this.state.limit < total_artists}
loadKey={total_artists > limit ? limit : total_artists}
showLoader={limit < total_artists}
loadMore={() => this.loadMore()}
/>
</section>
);
}
render() {
render = () => {
const {
spotify_available,
google_available,
} = this.props;
const source_options = [
{
value: 'all',
@ -254,14 +222,14 @@ class LibraryArtists extends React.Component {
},
];
if (this.props.spotify_available) {
if (spotify_available) {
source_options.push({
value: 'spotify',
label: i18n('services.spotify.title'),
});
}
if (this.props.google_available) {
if (google_available) {
source_options.push({
value: 'google',
label: i18n('services.google.title'),
@ -347,13 +315,12 @@ class LibraryArtists extends React.Component {
const mapStateToProps = (state) => ({
mopidy_uri_schemes: state.mopidy.uri_schemes,
mopidy_library_artists: state.mopidy.library_artists,
google_available: (state.mopidy.uri_schemes && state.mopidy.uri_schemes.includes('gmusic:')),
google_library_artists: state.google.library_artists,
spotify_available: state.spotify.access_token,
spotify_library_artists: state.spotify.library_artists,
spotify_library_artists_status: (state.ui.processes.SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR !== undefined ? state.ui.processes.SPOTIFY_GET_LIBRARY_ARTISTS_PROCESSOR.status : null),
artists: state.core.artists,
items: state.core.items,
mopidy_library: state.core.libraries['mopidy:library:artists'] || { items_uris: [] },
spotify_library: state.core.libraries['spotify:library:artists'] || { items_uris: [] },
google_library: state.core.libraries['google:library:artists'] || { items_uris: [] },
source: (state.ui.library_artists_source ? state.ui.library_artists_source : 'all'),
sort: (state.ui.library_artists_sort ? state.ui.library_artists_sort : null),
sort_reverse: (state.ui.library_artists_sort_reverse ? state.ui.library_artists_sort_reverse : false),
@ -362,9 +329,7 @@ const mapStateToProps = (state) => ({
const mapDispatchToProps = (dispatch) => ({
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch),
googleActions: bindActionCreators(googleActions, dispatch),
coreActions: bindActionCreators(coreActions, dispatch),
});
export default connect(mapStateToProps, mapDispatchToProps)(LibraryArtists);