From 70bc44e695e64f04366c24b2cf7ec35604b54c0f Mon Sep 17 00:00:00 2001 From: Francisco Fernandes <1180964@isep.ipp.pt> Date: Mon, 27 Jul 2020 23:57:45 +0100 Subject: [PATCH] Add previous button to history sync (#150) --- src/_locales/en/messages.json | 3 + src/_locales/pt_BR/messages.json | 3 + src/class/NetflixApiUtils.js | 66 +++++++++++++------- src/class/history-sync/ViewingActivityApp.js | 40 +++++++++--- 4 files changed, 81 insertions(+), 31 deletions(-) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index b3c60db..fd35ea6 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -80,6 +80,9 @@ "nextPage": { "message": "Next page" }, + "previousPage": { + "message": "Previous page" + }, "pagesToLoad": { "message": "Pages to load" }, diff --git a/src/_locales/pt_BR/messages.json b/src/_locales/pt_BR/messages.json index c893a25..e315c4b 100644 --- a/src/_locales/pt_BR/messages.json +++ b/src/_locales/pt_BR/messages.json @@ -80,6 +80,9 @@ "nextPage": { "message": "Próxima página" }, + "previousPage": { + "message": "Página anterior" + }, "pagesToLoad": { "message": "Páginas para carregar" }, diff --git a/src/class/NetflixApiUtils.js b/src/class/NetflixApiUtils.js index 64866b1..db3ef6e 100644 --- a/src/class/NetflixApiUtils.js +++ b/src/class/NetflixApiUtils.js @@ -55,40 +55,47 @@ const netflixApiUtils = { }, /** - * @param {number} page The first page to load. - * @param {number} [lastPage] The last page to load. Defaults to the first page. If -1, all pages are loaded. + * @param {number} currentPage The current page. + * @param {number} [desiredPage] The desired page to load. Defaults to the first page. If -1, all pages are loaded. */ - async listActivities(page, lastPage) { - if (!lastPage) { - lastPage = page; + async listActivities(currentPage, desiredPage) { + if (!desiredPage) { + desiredPage = currentPage; } - let loadAll = lastPage === -1; + let loadAll = desiredPage === Number.MAX_VALUE; let isLastPage = false; const activities = []; - while (!isLastPage && (loadAll || page <= lastPage)) { - ActivityActionCreators.updatePage(page); - try { - const response = await Request.sendAndWait({ - method: `GET`, - url: `${NETFLIX_API_HOST}/${this.buildIdentifier}/viewingactivity?languages=en-US&authURL=${this.authUrl}&pg=${page}`, - }); - const _activities = JSON.parse(response).viewedItems; + + if(currentPage < desiredPage){ + while (!isLastPage && (loadAll || currentPage < desiredPage)) { + ActivityActionCreators.updatePage(currentPage); + const _activities = await this.getPageInformation(currentPage); + if (_activities.length) { activities.push(...(_activities.filter(a => (a.bookmark/a.duration)>0.8))); } else { isLastPage = true; } - page += 1; - } catch (error) { - ActivityActionCreators.receiveActivitiesFailed(error.status, error.response); - console.log(error.status, error.response); - return; + currentPage += 1; + } + } else if(currentPage > desiredPage) { + while (!isLastPage && (currentPage > desiredPage)) { + const possibleCurrentPage = currentPage - 2; + ActivityActionCreators.updatePage(currentPage); + const _activities = await this.getPageInformation(possibleCurrentPage); + + if (_activities.length) { + activities.push(...(_activities.filter(a => (a.bookmark/a.duration)>0.8))); + currentPage -= 1; + } else { + isLastPage = true; + } } } const result = (await this.getActivitiesMetadata(activities)).map(this.parseActivity.bind(this)); const parsedActivities = result.map(item => item.parsedItem); const promises = result.map(item => item.promise); - ActivityActionCreators.receiveActivities(parsedActivities, page); + ActivityActionCreators.receiveActivities(parsedActivities, currentPage); const storage = await BrowserStorage.get(`options`); if (storage.options && storage.options.sendReceiveSuggestions && (await browser.permissions.contains({ origins: [`*://script.google.com/*`, `*://script.googleusercontent.com/*`] }))) { Request.send({ @@ -124,11 +131,26 @@ const netflixApiUtils = { .then(ActivityActionCreators.finishLoadingTraktData.bind(ActivityActionCreators)); }, - getActivities(page = 0, lastPage = 0) { + async getPageInformation(currentPage){ + try { + const response = await Request.sendAndWait({ + method: `GET`, + url: `${NETFLIX_API_HOST}/${this.buildIdentifier}/viewingactivity?languages=en-US&authURL=${this.authUrl}&pg=${currentPage}`, + }); + const _activities = JSON.parse(response).viewedItems; + return _activities; + } catch (error) { + ActivityActionCreators.receiveActivitiesFailed(error.status, error.response); + console.log(error.status, error.response); + return; + } + }, + + getActivities(currentPage = 0, desiredPage = 1) { ActivityActionCreators.resetActivities(); ActivityActionCreators.startLoadingTraktData(); this.activateAPI() - .then(() => this.listActivities(page, lastPage)); + .then(() => this.listActivities(currentPage, desiredPage)); }, /** diff --git a/src/class/history-sync/ViewingActivityApp.js b/src/class/history-sync/ViewingActivityApp.js index 50725c5..6aeaee1 100644 --- a/src/class/history-sync/ViewingActivityApp.js +++ b/src/class/history-sync/ViewingActivityApp.js @@ -16,7 +16,8 @@ export default class ViewingActivityApp extends React.Component { return Object.assign(this.getStateFromStores(), { hideSynced: this.props.hideSynced, use24Clock: this.props.use24Clock, - pagesToLoad: this.props.pagesToLoad + pagesToLoad: this.props.pagesToLoad, + loadedPages: this.props.loadedPages }); } @@ -76,13 +77,28 @@ export default class ViewingActivityApp extends React.Component { this.setState({pagesToLoad}); } - _onNextPageClick() { + _loadedPagesChange(loadedPages){ + this.setState({loadedPages}); + } + + async _onNextPageClick() { this.setState({loading: true}); if (this.state.pagesToLoad === `All`) { - NetflixApiUtils.getActivities(this.state.page, -1); + await NetflixApiUtils.getActivities(this.state.page, Number.MAX_VALUE); } else { - NetflixApiUtils.getActivities(this.state.page, this.state.page + parseInt(this.state.pagesToLoad)); + await NetflixApiUtils.getActivities(this.state.page, this.state.page + parseInt(this.state.pagesToLoad)); } + this._loadedPagesChange(this.state.pagesToLoad); + } + + async _onPreviousPageClick(){ + this.setState({loading: true}); + + const currentPage = this.state.page - this.state.loadedPages - parseInt(this.state.pagesToLoad) + const desiredPage = this.state.page - this.state.loadedPages; + + await NetflixApiUtils.getActivities(currentPage, desiredPage); + this._loadedPagesChange(this.state.pagesToLoad); } _onToggleAll(event) { @@ -133,6 +149,7 @@ export default class ViewingActivityApp extends React.Component { render() { let content; + if (this.state.loading) { content = ( @@ -194,15 +211,20 @@ export default class ViewingActivityApp extends React.Component { {browser.i18n.getMessage(`nextPage`)} + +