Skip to content

Commit 095206f

Browse files
authored
Merge pull request #48 from jaedb/44-local-artwork
Local album art
2 parents e01e444 + 0d6a539 commit 095206f

File tree

5 files changed

+167
-23
lines changed

5 files changed

+167
-23
lines changed

src/js/helpers.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ export let sizedImages = function( images ){
1212
if( images.length <= 0 ) return sizes;
1313

1414
for( var i = 0; i < images.length; i++ ){
15+
16+
// Mopidy image object
17+
if (typeof(images[i].__model__) !== 'undefined' && images[i].__model__ == 'Image'){
18+
sizes.small = images[i].uri
19+
20+
// Mopidy image string
21+
} else if (typeof(images[i]) == 'string'){
22+
sizes.small = images[i]
1523

1624
// spotify-styled images
17-
if( typeof(images[i].width) !== 'undefined' ){
25+
} else if (typeof(images[i].width) !== 'undefined'){
1826
if( images[i].width < 400 ){
1927
sizes.small = images[i].url;
2028
}else if( images[i].width < 800 ){
@@ -26,7 +34,7 @@ export let sizedImages = function( images ){
2634
}
2735

2836
// lastfm-styled images
29-
}else if( typeof(images[i].size) !== 'undefined' ){
37+
} else if (typeof(images[i].size) !== 'undefined'){
3038
switch( images[i].size ){
3139
case 'mega':
3240
case 'extralarge':
@@ -42,10 +50,6 @@ export let sizedImages = function( images ){
4250
sizes.small = images[i]['#text']
4351
break
4452
}
45-
46-
// Mopidy-Images styled images
47-
}else if( typeof(images[i]) == 'string' ){
48-
sizes.small = images[i]
4953
}
5054
}
5155

src/js/services/mopidy/actions.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,16 @@ export function getAlbum( uri ){
199199
}
200200
}
201201

202-
export function getAlbums(){
202+
export function getAlbums( uris ){
203203
return {
204-
type: 'MOPIDY_GET_ALBUMS'
204+
type: 'MOPIDY_GET_ALBUMS',
205+
uris: uris
206+
}
207+
}
208+
209+
export function getLocalAlbums(){
210+
return {
211+
type: 'MOPIDY_GET_LOCAL_ALBUMS'
205212
}
206213
}
207214

src/js/services/mopidy/middleware.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -544,16 +544,49 @@ const MopidyMiddleware = (function(){
544544
* ======================================================================================
545545
**/
546546

547-
case 'MOPIDY_GET_ALBUMS':
547+
case 'MOPIDY_GET_LOCAL_ALBUMS':
548548
instruct( socket, store, 'library.browse', { uri: 'local:directory?type=album' } )
549549
.then( response => {
550+
551+
var uris = helpers.asURIs(response)
552+
550553
store.dispatch({
551-
type: 'ALBUMS_LOADED',
552-
albums: response
554+
type: 'MOPIDY_GET_ALBUMS',
555+
uris: uris.slice(0,50)
553556
});
557+
554558
store.dispatch({
555559
type: 'LOCAL_ALBUMS_LOADED',
556-
uris: helpers.asURIs(response)
560+
uris: uris
561+
});
562+
})
563+
break;
564+
565+
case 'MOPIDY_GET_ALBUMS':
566+
instruct( socket, store, 'library.lookup', { uris: action.uris } )
567+
.then( response => {
568+
569+
var albums = []
570+
571+
for (var uri in response){
572+
if (response.hasOwnProperty(uri) && response[uri].length > 0 && response[uri][0] && response[uri][0].album ){
573+
var album = Object.assign(
574+
{},
575+
{
576+
artists: response[uri][0].artists,
577+
tracks: response[uri],
578+
tracks_total: response[uri].length
579+
},
580+
response[uri][0].album
581+
)
582+
583+
albums.push(album)
584+
}
585+
}
586+
587+
store.dispatch({
588+
type: 'ALBUMS_LOADED',
589+
albums: albums
557590
});
558591
})
559592
break;
@@ -654,7 +687,7 @@ const MopidyMiddleware = (function(){
654687
var existingAlbum = albums.find(getByURI);
655688
if( !existingAlbum ){
656689
albums.push(album)
657-
}
690+
}
658691
}
659692
}
660693
if (albums){

src/js/services/ui/reducer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export default function reducer(ui = {}, action){
198198
for (var i = 0; i < action.albums.length; i++){
199199
var album = action.albums[i]
200200
if (albums[album.uri]){
201-
artist = Object.assign({}, albums[album.uri], album)
201+
album = Object.assign({}, albums[album.uri], album)
202202
}
203203
albums[album.uri] = album
204204
}

src/js/views/library/LibraryLocalAlbums.js

+109-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import React, { PropTypes } from 'react'
33
import { connect } from 'react-redux'
44
import { bindActionCreators } from 'redux'
55

6+
import AlbumGrid from '../../components/AlbumGrid'
67
import Header from '../../components/Header'
8+
import DropdownField from '../../components/DropdownField'
79
import List from '../../components/List'
10+
import LazyLoadListener from '../../components/LazyLoadListener'
811

12+
import * as helpers from '../../helpers'
13+
import * as uiActions from '../../services/ui/actions'
914
import * as mopidyActions from '../../services/mopidy/actions'
1015
import * as spotifyActions from '../../services/spotify/actions'
1116

@@ -20,14 +25,77 @@ class LibraryLocalAlbums extends React.Component{
2025
}
2126

2227
componentWillReceiveProps( nextProps ){
23-
if( !this.props.mopidy_connected && nextProps.mopidy_connected ){
28+
if (!this.props.mopidy_connected && nextProps.mopidy_connected){
2429
this.loadAlbums(nextProps);
2530
}
2631
}
2732

2833
loadAlbums(props = this.props){
29-
if( props.mopidy_connected ){
30-
this.props.mopidyActions.getAlbums();
34+
if (props.mopidy_connected && !this.props.local_albums){
35+
this.props.mopidyActions.getLocalAlbums();
36+
}
37+
}
38+
39+
loadMore(){
40+
var uris = []
41+
if (this.props.albums && this.props.local_albums){
42+
for (var i = 0; i < this.props.local_albums.length; i++){
43+
var uri = this.props.local_albums[i]
44+
if (!this.props.albums.hasOwnProperty(uri)){
45+
uris.push(uri)
46+
}
47+
48+
// limit each lookup to 50 URIs
49+
if (uris.length >= 50) break
50+
}
51+
}
52+
53+
if (uris && uris.length > 0) this.props.mopidyActions.getAlbums(uris)
54+
}
55+
56+
setSort(value){
57+
var reverse = false
58+
if( this.props.sort == value ) reverse = !this.props.sort_reverse
59+
60+
var data = {
61+
library_local_albums_sort_reverse: reverse,
62+
library_local_albums_sort: value
63+
}
64+
this.props.uiActions.set(data)
65+
}
66+
67+
renderView(albums){
68+
if( this.props.view == 'list' ){
69+
70+
var columns = [
71+
{
72+
label: 'Name',
73+
name: 'name',
74+
width: 40
75+
},
76+
{
77+
label: 'Artists',
78+
name: 'artists',
79+
width: 30
80+
},
81+
{
82+
label: 'Tracks',
83+
name: 'tracks_total',
84+
width: 15
85+
}
86+
]
87+
88+
return (
89+
<section className="list-wrapper">
90+
<List columns={columns} rows={albums} link_prefix={global.baseURL+"album/"} />
91+
</section>
92+
)
93+
}else{
94+
return (
95+
<section className="grid-wrapper">
96+
<AlbumGrid albums={albums} />
97+
</section>
98+
)
3199
}
32100
}
33101

@@ -40,16 +108,44 @@ class LibraryLocalAlbums extends React.Component{
40108
albums.push(this.props.albums[uri])
41109
}
42110
}
111+
112+
if( this.props.sort ){
113+
albums = helpers.sortItems(albums, this.props.sort, this.props.sort_reverse)
114+
}
43115
}
44116

117+
var view_options = [
118+
{
119+
value: 'thumbnails',
120+
label: 'Thumbnails'
121+
},
122+
{
123+
value: 'list',
124+
label: 'List'
125+
}
126+
]
127+
128+
var sort_options = [
129+
{
130+
value: 'name',
131+
label: 'Name'
132+
}
133+
]
134+
135+
var actions = (
136+
<span>
137+
<DropdownField icon="sort" name="Sort" value={this.props.sort} options={sort_options} reverse={this.props.sort_reverse} handleChange={val => this.setSort(val)} />
138+
<DropdownField icon="eye" name="View" value={this.props.view} options={view_options} handleChange={val => this.props.uiActions.set({ library_local_albums_view: val })} />
139+
</span>
140+
)
141+
45142
return (
46143
<div className="view library-local-view">
47-
<Header icon="music" title="Local albums" />
48-
<div>
49-
<List columns={[{ name: 'name', width: '100'}]} rows={albums} link_prefix={global.baseURL+"album/"} />
50-
</div>
144+
<Header icon="music" title="Local albums" actions={actions} />
145+
{this.renderView(albums)}
146+
<LazyLoadListener loadMore={ () => this.loadMore() }/>
51147
</div>
52-
);
148+
)
53149
}
54150
}
55151

@@ -63,13 +159,17 @@ class LibraryLocalAlbums extends React.Component{
63159
const mapStateToProps = (state, ownProps) => {
64160
return {
65161
mopidy_connected: state.mopidy.connected,
162+
albums: state.ui.albums,
66163
local_albums: state.ui.local_albums,
67-
albums: state.ui.albums
164+
view: state.ui.library_local_albums_view,
165+
sort: state.ui.library_local_albums_sort,
166+
sort_reverse: state.ui.library_local_albums_sort_reverse
68167
}
69168
}
70169

71170
const mapDispatchToProps = (dispatch) => {
72171
return {
172+
uiActions: bindActionCreators(uiActions, dispatch),
73173
mopidyActions: bindActionCreators(mopidyActions, dispatch),
74174
spotifyActions: bindActionCreators(spotifyActions, dispatch)
75175
}

0 commit comments

Comments
 (0)