Skip to content

Commit ea222f4

Browse files
committed
Merge pull request #539 from Soundnode/charts-view
add charts (top 50) view. closes #522
2 parents d459ad1 + cb299ea commit ea222f4

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

app/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ <h4>Authenticating your Soundcloud account.</h4>
9696
<nav class="mainNav">
9797
<h2 class="ui_title">Main</h2>
9898
<ul class="mainNav_nav">
99+
<li class="mainNav_item" ui-sref-active="active">
100+
<a class="mainNav_button" ui-sref="charts">
101+
<i class="fa fa-trophy"></i>
102+
<span class="mainNav_tit">Top 50</span>
103+
</a>
104+
</li>
99105
<li class="mainNav_item" ui-sref-active="active">
100106
<a class="mainNav_button" ui-sref="stream">
101107
<i class="fa fa-cloud"></i>
@@ -221,6 +227,7 @@ <h4 id="playerUser" class="player_user" ng-click="goToUser($event)"></h4>
221227
<script src="public/js/search/searchInputCtrl.js"></script>
222228
<script src="public/js/search/searchCtrl.js"></script>
223229
<script src="public/js/stream/streamCtrl.js"></script>
230+
<script src="public/js/charts/chartsCtrl.js"></script>
224231
<script src="public/js/user/userCtrl.js"></script>
225232
<script src="public/js/player/playerCtrl.js"></script>
226233
<script src="public/js/favorites/favoritesCtrl.js"></script>

app/public/js/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ app.config(function (
2626
templateUrl: 'views/stream/stream.html',
2727
controller: 'StreamCtrl'
2828
})
29+
.state('charts', {
30+
url: '/',
31+
templateUrl: 'views/charts/charts.html',
32+
controller: 'ChartsCtrl'
33+
})
2934
.state('favorites', {
3035
url: '/favorites',
3136
templateUrl: 'views/favorites/favorites.html',

app/public/js/charts/chartsCtrl.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
'use strict';
2+
3+
app.controller('ChartsCtrl', function (
4+
$scope,
5+
$rootScope,
6+
SCapiService,
7+
SC2apiService,
8+
utilsService
9+
) {
10+
var tracksIds = [];
11+
12+
$scope.title = 'Top 50';
13+
$scope.originalData = '';
14+
$scope.data = '';
15+
$scope.busy = false;
16+
17+
SC2apiService.getCharts()
18+
.then(filterCollection)
19+
.then(function (collection) {
20+
$scope.originalData = collection;
21+
$scope.data = collection;
22+
23+
loadTracksInfo(collection);
24+
})
25+
.catch(function (error) {
26+
console.log('error', error);
27+
})
28+
.finally(function () {
29+
utilsService.updateTracksLikes($scope.data);
30+
utilsService.updateTracksReposts($scope.data);
31+
$rootScope.isLoading = false;
32+
});
33+
34+
$scope.loadMore = function() {
35+
if ( $scope.busy ) {
36+
return;
37+
}
38+
$scope.busy = true;
39+
40+
SC2apiService.getNextPage()
41+
.then(filterCollection)
42+
.then(function (collection) {
43+
$scope.originalData = $scope.originalData.concat(collection);
44+
$scope.data = $scope.data.concat(collection);
45+
utilsService.updateTracksLikes(collection, true);
46+
utilsService.updateTracksReposts(collection, true);
47+
loadTracksInfo(collection);
48+
}, function (error) {
49+
console.log('error', error);
50+
}).finally(function () {
51+
$scope.busy = false;
52+
$rootScope.isLoading = false;
53+
});
54+
};
55+
56+
function filterCollection(data) {
57+
return data.collection.filter(function (item) {
58+
// Keep only tracks (remove playlists, etc)
59+
var isTrackType = item.type === 'track' ||
60+
item.type === 'track-repost' ||
61+
!!(item.track && item.track.streamable);
62+
if (!isTrackType) {
63+
return false;
64+
}
65+
66+
// Filter reposts: display only first appearance of track in stream
67+
var exists = tracksIds.indexOf(item.track.id) > -1;
68+
if (exists) {
69+
return false;
70+
}
71+
72+
// "stream_url" property is missing in V2 API
73+
item.track.stream_url = item.track.uri + '/stream';
74+
75+
tracksIds.push(item.track.id);
76+
return true;
77+
});
78+
}
79+
80+
// Load extra information, because SoundCloud v2 API does not return
81+
// number of track likes
82+
function loadTracksInfo(collection) {
83+
var ids = collection.map(function (item) {
84+
return item.track.id;
85+
});
86+
87+
SC2apiService.getTracksByIds(ids)
88+
.then(function (tracks) {
89+
// Both collections are unordered
90+
collection.forEach(function (item) {
91+
tracks.forEach(function (track) {
92+
if (item.track.id === track.id) {
93+
item.track.favoritings_count = track.likes_count;
94+
return;
95+
}
96+
});
97+
});
98+
})
99+
.catch(function (error) {
100+
console.log('error', error);
101+
});
102+
}
103+
104+
});

app/public/js/common/SC2apiService.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ app.service('SC2apiService', function (
4141
.catch(onResponseError);
4242
};
4343

44+
/**
45+
* Get charts (top 50)
46+
* @returns {Promise.<T>}
47+
*/
48+
this.getCharts = function () {
49+
// kind=top&genre=soundcloud%3Agenres%3Aall-music&limit=50
50+
51+
var params = {
52+
kind: 'top',
53+
genre: 'soundcloud:genres:all-music',
54+
limit: 50
55+
};
56+
return sendRequest('charts', { params: params })
57+
.then(onResponseSuccess)
58+
.then(updateNextPageUrl)
59+
.catch(onResponseError);
60+
};
61+
4462
/**
4563
* Get next page for last requested resource
4664
* @return {promise}

app/views/charts/charts.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="streamView">
2+
<h1> {{ title }}</h1>
3+
4+
<!-- Song list wrapper -->
5+
<div class="streamView_inner">
6+
<ul class="songList"
7+
infinite-scroll='loadMore()'
8+
infinite-scroll-distance='0'
9+
infinite-scroll-container='".mainView"'
10+
infinite-scroll-immediate-check='false' >
11+
12+
<li class="songList_item" ng-repeat="data in data">
13+
14+
<tracks data="data.track" type="data.type" user="data.user"/>
15+
16+
</li>
17+
</ul>
18+
</div>
19+
<!-- Song list wrapper / end -->
20+
21+
<div ng-include="'views/common/loading.html'"></div>
22+
23+
</div>

0 commit comments

Comments
 (0)