From cb32ae7db3fd2b44b32627622adaa386d89d2fba Mon Sep 17 00:00:00 2001 From: PrestonN Date: Thu, 8 Nov 2018 14:21:04 -0500 Subject: [PATCH] [Fix] Put History in Chronological order again and better sub handling --- src/js/history.js | 3 ++ src/js/subscriptions.js | 63 ++++++++++++++++++++++++----------------- src/js/videos.js | 3 +- src/js/youtubeApi.js | 9 +++--- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/js/history.js b/src/js/history.js index f56353f8a..e8a59915d 100644 --- a/src/js/history.js +++ b/src/js/history.js @@ -58,9 +58,12 @@ function showHistory(){ historyDb.find({}).sort({ timeWatched: -1 }).exec((err, docs) => { + let position = 0; docs.forEach((video) => { invidiousAPI('videos', video.videoId, {}, (data) => { + data.position = position; displayVideo(data, 'history'); + position++; }); }); diff --git a/src/js/subscriptions.js b/src/js/subscriptions.js index 25851ef72..a3d0c5feb 100644 --- a/src/js/subscriptions.js +++ b/src/js/subscriptions.js @@ -108,33 +108,15 @@ function loadSubscriptions() { progressView.progressWidth = (counter / results.length) * 100; if (counter === results.length) { - videoList.sort((a, b) => { - return b.published - a.published; - }); - - subscriptionView.videoList = []; - console.log(videoList); - - if (videoList.length > 100) { - for (let i = 0; i < 100; i++) { - displayVideo(videoList[i], 'subscriptions'); - } - } else { - videoList.forEach((video) => { - displayVideo(video, 'subscriptions'); - }); - } - - loadingView.seen = false; - progressView.seen = false; - progressView.progressWidth = 0; - - subscriptionTimer = window.setTimeout(() => { - checkSubscriptions = true; - }, 60000); - - console.log('Done'); + addSubsToView(videoList); } + },(errorData) => { + showToast('Unable to load channel: ' + results[i]['channelName']); + counter = counter + 1; + progressView.progressWidth = (counter / results.length) * 100; + if (counter === results.length) { + addSubsToView(videoList); + } }); } } else { @@ -146,6 +128,35 @@ function loadSubscriptions() { }); } +function addSubsToView (videoList) { + videoList.sort((a, b) => { + return b.published - a.published; + }); + + subscriptionView.videoList = []; + console.log(videoList); + + if (videoList.length > 100) { + for (let i = 0; i < 100; i++) { + displayVideo(videoList[i], 'subscriptions'); + } + } else { + videoList.forEach((video) => { + displayVideo(video, 'subscriptions'); + }); + } + + loadingView.seen = false; + progressView.seen = false; + progressView.progressWidth = 0; + + subscriptionTimer = window.setTimeout(() => { + checkSubscriptions = true; + }, 60000); + + console.log('Done'); +} + /** * Get the list of subscriptions from the user's subscription database. * diff --git a/src/js/videos.js b/src/js/videos.js index 3e58173f3..74516f8c6 100644 --- a/src/js/videos.js +++ b/src/js/videos.js @@ -193,7 +193,8 @@ function displayVideo(videoData, listType = '') { video.removeFromSave = false; break; case 'history': - historyView.videoList = historyView.videoList.concat(video); + historyView.videoList.splice(videoData.position, 0, video); + console.log(video); video.removeFromSave = false; break; case 'channel': diff --git a/src/js/youtubeApi.js b/src/js/youtubeApi.js index 6b5d1bc07..37bb661bc 100644 --- a/src/js/youtubeApi.js +++ b/src/js/youtubeApi.js @@ -25,7 +25,10 @@ * @return {Void} */ -function invidiousAPI(resource, id, params, success) { +function invidiousAPI(resource, id, params, success, fail = function(){ + showToast('There was an error calling the Invidious API.'); + loadingView.seen = false; +}) { let requestUrl = 'https://www.invidio.us/api/v1/' + resource + '/' + id + '?' + $.param(params); if (useTor) { @@ -45,12 +48,10 @@ function invidiousAPI(resource, id, params, success) { requestUrl, success ).fail((xhr, textStatus, error) => { - showToast('There was an error calling the Invidious API.'); - console.log(error); + fail(xhr); console.log(xhr); console.log(textStatus); console.log(requestUrl); - loadingView.seen = false; }); } }