Loading library albums as process; Processes loader for less jammy animation

This commit is contained in:
James Barnsley
2017-08-10 12:00:08 +12:00
parent fcf44d0b28
commit fd46929ac3
11 changed files with 151 additions and 287 deletions

9
src/js/bootstrap.js vendored
View File

@ -30,12 +30,17 @@ let reducers = combineReducers({
var initialState = { var initialState = {
core: { core: {
current_tracklist: [], current_tracklist: [],
current_tltrack: false current_tltrack: false,
albums: {},
users: {},
playlists: {},
artists: {}
}, },
ui: { ui: {
slim_mode: false, slim_mode: false,
selected_tracks: [], selected_tracks: [],
notifications: [] notifications: [],
processes: {}
}, },
mopidy: { mopidy: {
connected: false, connected: false,

View File

@ -48,14 +48,16 @@ export default class Notifications extends React.Component{
if (processes[key].cancelling){ if (processes[key].cancelling){
items.push( items.push(
<div className="process notification cancelling" key={key}> <div className="process notification cancelling" key={key}>
<div className="loader"></div>
Cancelling Cancelling
</div> </div>
) )
} else { } else {
items.push( items.push(
<div className="process notification" key={key}> <div className="process notification" key={key}>
<FontAwesome name="close" className="close-button" onClick={ e => this.props.uiActions.cancelProcess(key) } /> <div className="loader"></div>
{ processes[key].content } { processes[key].content }
<FontAwesome name="close" className="close-button" onClick={ e => this.props.uiActions.cancelProcess(key) } />
</div> </div>
) )
} }

View File

@ -13,7 +13,7 @@ class Thumbnail extends React.Component{
constructor(props) { constructor(props) {
super(props); super(props);
} }
/*
// TODO: ascertain whether this is improving or hindering performance // TODO: ascertain whether this is improving or hindering performance
// The UI appears to work perfectly fine without this // The UI appears to work perfectly fine without this
shouldComponentUpdate(nextProps, nextState){ shouldComponentUpdate(nextProps, nextState){
@ -22,23 +22,24 @@ class Thumbnail extends React.Component{
if (!nextProps.image && !this.props.image && !nextProps.images && !this.props.images) return false if (!nextProps.image && !this.props.image && !nextProps.images && !this.props.images) return false
// image changed // image changed
if( !this.props.image && nextProps.image ) return true if (!this.props.image && nextProps.image) return true
if( this.props.image && nextProps.image ) return true if (this.props.image && nextProps.image) return true
if( this.props.image != nextProps.image ) return true if (this.props.image != nextProps.image) return true
// images array changed // images array changed
if( typeof(this.props.images) === 'undefined' && nextProps.images ) return true if (this.props.images === undefined && nextProps.images ) return true
if( this.props.images && typeof(nextProps.images) === 'undefined' ) return true if (this.props.images && nextProps.images === undefined) return true
if( this.props.images.length != nextProps.images.length ) return true if (this.props.images && !nextProps.images || this.props.images.length != nextProps.images.length ) return true
// image item changed // image item changed
var size = 'medium' var size = 'medium'
var images = helpers.sizedImages( nextProps.images ) var images = helpers.sizedImages( nextProps.images )
if( this.props.size ) size = this.props.size if (this.props.size ) size = this.props.size
if( this.props.images[size] != images[size] ) return true if (this.props.images[size] != images[size]) return true
return false return false
} }
*/
mapImageSizes( props = this.props ){ mapImageSizes( props = this.props ){
@ -79,7 +80,7 @@ class Thumbnail extends React.Component{
return ( return (
<div className={class_name}> <div className={class_name}>
<LazyLoad height={10} placeholder={<div className="image"></div>}> <LazyLoad height={600} placeholder={<div className="image"></div>}>
<div className="image loaded" style={{backgroundImage: 'url("'+image+'")'}}></div> <div className="image loaded" style={{backgroundImage: 'url("'+image+'")'}}></div>
</LazyLoad> </LazyLoad>

View File

@ -35,7 +35,6 @@ import LibraryTracks from './views/library/LibraryTracks'
import LibraryPlaylists from './views/library/LibraryPlaylists' import LibraryPlaylists from './views/library/LibraryPlaylists'
import LibraryBrowse from './views/library/LibraryBrowse' import LibraryBrowse from './views/library/LibraryBrowse'
import LibraryLocalArtists from './views/library/LibraryLocalArtists' import LibraryLocalArtists from './views/library/LibraryLocalArtists'
import LibraryLocalAlbums from './views/library/LibraryLocalAlbums'
// setup our analytics tracking // setup our analytics tracking
ReactGA.initialize('UA-64701652-3'); ReactGA.initialize('UA-64701652-3');
@ -76,7 +75,6 @@ ReactDOM.render(
<Route path="library/tracks" component={LibraryTracks} /> <Route path="library/tracks" component={LibraryTracks} />
<Route path="library/playlists" component={LibraryPlaylists} /> <Route path="library/playlists" component={LibraryPlaylists} />
<Route path="library/browse/local-artists" component={LibraryLocalArtists} /> <Route path="library/browse/local-artists" component={LibraryLocalArtists} />
<Route path="library/browse/local-albums" component={LibraryLocalAlbums} />
<Route path="library/browse(/:uri)" component={LibraryBrowse} /> <Route path="library/browse(/:uri)" component={LibraryBrowse} />
</Route> </Route>

View File

@ -217,10 +217,11 @@ export function getAlbum( uri ){
} }
} }
export function getAlbums( uris ){ export function getAlbums(uris, processor = null){
return { return {
type: 'MOPIDY_GET_ALBUMS', type: 'MOPIDY_GET_ALBUMS',
uris: uris uris: uris,
processor: processor
} }
} }
@ -230,6 +231,18 @@ export function getLibraryAlbums(){
} }
} }
export function runProcessor(processor){
return {
type: processor
}
}
export function cancelProcessor(processor){
return {
type: processor+'_CANCEL'
}
}
/** /**
* Searching * Searching

View File

@ -840,22 +840,47 @@ const MopidyMiddleware = (function(){
.then( response => { .then( response => {
if (response.length <= 0) return if (response.length <= 0) return
var uris = helpers.arrayOf('uri',response)
/*
store.dispatch({
type: 'MOPIDY_GET_ALBUMS',
uris: uris//.slice(0,50) THIS PAGINATES REQUESTS. Slow servers might need this...
});
*/
store.dispatch({ store.dispatch({
type: 'LIBRARY_ALBUMS_LOADED', type: 'LIBRARY_ALBUMS_LOADED',
uris: uris uris: helpers.arrayOf('uri',response)
}); });
// Start our process to load the full album objects
store.dispatch(mopidyActions.runProcessor('MOPIDY_LIBRARY_ALBUMS_PROCESSOR'));
}) })
break; break;
case 'MOPIDY_LIBRARY_ALBUMS_PROCESSOR':
if (store.getState().ui.processes['MOPIDY_LIBRARY_ALBUMS'] !== undefined){
var processor = store.getState().ui.processes['MOPIDY_LIBRARY_ALBUMS']
if (processor.cancelling){
store.dispatch(uiActions.stopProcess('MOPIDY_LIBRARY_ALBUMS'))
return false
}
}
store.dispatch(uiActions.startProcess('MOPIDY_LIBRARY_ALBUMS', 'Loading album library'))
// Figure out the remaining items
var library_uris = store.getState().core.library_albums
var to_load = []
for (var i = 0; i < library_uris.length && to_load.length < 50; i++){
var uri = library_uris[i]
if (helpers.uriSource(uri) == 'local' && store.getState().core.albums[uri] === undefined){
to_load.push(uri)
}
}
if (to_load.length > 0){
store.dispatch(mopidyActions.getAlbums(to_load, 'MOPIDY_LIBRARY_ALBUMS_PROCESSOR'))
} else {
store.dispatch(uiActions.stopProcess('MOPIDY_LIBRARY_ALBUMS'))
}
break
case 'MOPIDY_GET_ALBUMS': case 'MOPIDY_GET_ALBUMS':
instruct( socket, store, 'library.lookup', { uris: action.uris } ) instruct( socket, store, 'library.lookup', { uris: action.uris } )
.then( response => { .then( response => {
@ -883,7 +908,19 @@ const MopidyMiddleware = (function(){
store.dispatch({ store.dispatch({
type: 'ALBUMS_LOADED', type: 'ALBUMS_LOADED',
albums: albums albums: albums
}); })
// Re-run any consequential processes in 100ms. This allows a small window for other
// server requests before our next batch. It's a little crude but it means the server isn't
// locked until we're completely done.
if (action.processor){
setTimeout(
function(){
store.dispatch(mopidyActions.runProcessor(action.processor))
},
100
)
}
}) })
break; break;

View File

@ -126,6 +126,9 @@ const UIMiddleware = (function(){
if (action.key == 'MOPIDY_ENQUEUE_URIS'){ if (action.key == 'MOPIDY_ENQUEUE_URIS'){
store.dispatch(mopidyActions.enqueueURIsCancel()) store.dispatch(mopidyActions.enqueueURIsCancel())
} }
if (action.key == 'MOPIDY_LIBRARY_ALBUMS_PROCESSOR'){
store.dispatch(mopidyActions.cancelProcessor(action.key))
}
next(action) next(action)
break break

View File

@ -1,203 +0,0 @@
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import AlbumGrid from '../../components/AlbumGrid'
import Header from '../../components/Header'
import DropdownField from '../../components/DropdownField'
import List from '../../components/List'
import LazyLoadListener from '../../components/LazyLoadListener'
import * as helpers from '../../helpers'
import * as uiActions from '../../services/ui/actions'
import * as mopidyActions from '../../services/mopidy/actions'
import * as spotifyActions from '../../services/spotify/actions'
class LibraryLocalAlbums extends React.Component{
constructor(props) {
super(props);
}
componentDidMount(){
this.loadAlbums()
}
componentWillReceiveProps( nextProps ){
if (!this.props.mopidy_connected && nextProps.mopidy_connected){
this.loadAlbums(nextProps);
}
}
loadAlbums(props = this.props){
if (props.mopidy_connected && !this.props.local_albums){
this.props.mopidyActions.getLibraryAlbums();
}
}
moreURIsToLoad(){
var uris = []
if (this.props.albums && this.props.local_albums){
for (var i = 0; i < this.props.local_albums.length; i++){
var uri = this.props.local_albums[i]
if (!this.props.albums.hasOwnProperty(uri)){
uris.push(uri)
}
// limit each lookup to 50 URIs
if (uris.length >= 50) break
}
}
return uris
}
loadMore(){
var uris = this.moreURIsToLoad()
this.props.mopidyActions.getAlbums(uris)
}
setSort(value){
var reverse = false
if( this.props.sort == value ) reverse = !this.props.sort_reverse
var data = {
library_local_albums_sort_reverse: reverse,
library_local_albums_sort: value
}
this.props.uiActions.set(data)
}
renderView(albums){
if( this.props.view == 'list' ){
var columns = [
{
label: 'Name',
name: 'name',
width: 40
},
{
label: 'Artists',
name: 'artists',
width: 30
},
{
label: 'Tracks',
name: 'tracks_total',
width: 15
}
]
return (
<section className="content-wrapper">
<List
columns={columns}
rows={albums}
className="library-local-album-list"
link_prefix={global.baseURL+"album/"} />
</section>
)
}else{
return (
<section className="content-wrapper">
<AlbumGrid albums={albums} />
</section>
)
}
}
render(){
var albums = []
if (this.props.albums && this.props.local_albums){
for (var i = 0; i < this.props.local_albums.length; i++){
var uri = this.props.local_albums[i]
if (this.props.albums.hasOwnProperty(uri)){
albums.push(this.props.albums[uri])
}
}
if( this.props.sort ){
albums = helpers.sortItems(albums, this.props.sort, this.props.sort_reverse)
}
}
var view_options = [
{
value: 'thumbnails',
label: 'Thumbnails'
},
{
value: 'list',
label: 'List'
}
]
var sort_options = [
{
value: 'name',
label: 'Name'
},
{
value: 'tracks_total',
label: 'Tracks'
}
]
var options = (
<span>
<DropdownField icon="sort" name="Sort" value={this.props.sort} options={sort_options} reverse={this.props.sort_reverse} handleChange={val => {this.setSort(val); this.props.uiActions.hideContextMenu() }} />
<DropdownField icon="eye" name="View" value={this.props.view} options={view_options} handleChange={val => {this.props.uiActions.set({ library_local_albums_view: val }); this.props.uiActions.hideContextMenu() }} />
</span>
)
if (albums.length <= 0 && helpers.isLoading(this.props.load_queue,['mopidy_lookup','mopidy_browse'])){
return (
<div className="view library-local-view">
<Header icon="music" title="Local albums" options={options} uiActions={this.props.uiActions} />
<div className="body-loader loading">
<div className="loader"></div>
</div>
</div>
)
}
return (
<div className="view library-local-view">
<Header icon="music" title="Local albums" options={options} uiActions={this.props.uiActions} />
{this.renderView(albums)}
<LazyLoadListener enabled={(this.moreURIsToLoad().length > 0)} loadMore={ () => this.loadMore() }/>
</div>
)
}
}
/**
* Export our component
*
* We also integrate our global store, using connect()
**/
const mapStateToProps = (state, ownProps) => {
return {
load_queue: state.ui.load_queue,
mopidy_connected: state.mopidy.connected,
albums: state.core.albums,
local_albums: state.core.local_albums,
view: state.ui.library_local_albums_view,
sort: (state.ui.library_local_albums_sort ? state.ui.library_local_albums_sort : 'name'),
sort_reverse: (state.ui.library_local_albums_sort_reverse ? true : false)
}
}
const mapDispatchToProps = (dispatch) => {
return {
uiActions: bindActionCreators(uiActions, dispatch),
mopidyActions: bindActionCreators(mopidyActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(LibraryLocalAlbums)

View File

@ -1,14 +1,5 @@
.body-loader { .loader {
padding: 35vh 0;
&.loading {
.loader {
opacity: 1;
}
}
.loader {
height: 120px; height: 120px;
width: 120px; width: 120px;
margin: 0 auto; margin: 0 auto;
@ -46,6 +37,15 @@
border-bottom-color: transparent; border-bottom-color: transparent;
border-right-color: $blue; border-right-color: $blue;
} }
}
.body-loader {
padding: 35vh 0;
&.loading {
.loader {
opacity: 1;
}
} }
&.lazy-loader { &.lazy-loader {

View File

@ -64,25 +64,27 @@
&.process { &.process {
position: relative; position: relative;
padding-right: 18px; padding-right: 18px;
padding-left: 50px;
&.cancelling { &.cancelling {
color: rgba(255,255,255,0.5); color: rgba(255,255,255,0.5);
} }
&:after { .loader {
pointer-events: none; width: 20px;
height: 20px;
position: absolute; position: absolute;
display: block; top: 16px;
content: ''; left: 18px;
background: $white; opacity: 1;
opacity: 0.15;
animation: slideloader 1s infinite; &:before {
top: 0; border-color: transparent;
right: 0; }
bottom: 0;
left: 0; &:after {
z-index: 1; border-right-color: $white;
border-radius: 0 0 3px 3px; }
} }
} }
} }

View File

@ -116,9 +116,15 @@ $bp_shallow: 650px;
} }
@keyframes slideloader { @keyframes slideloader {
0% { left: 0%; right: 100%; } 0% {
50% { left: 0%; right: 0%; } transform: translateX(-100%);
100% { left: 100%; right: 0%; } }
50% {
transform: translateX(0);
}
100% {
transform: translateX(+100%);
}
} }