From b5b914878733d9c1a7f95f833dcda15cad9770d9 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 13 Sep 2018 14:11:49 +0300 Subject: add download complete notifications --- js/index.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'js') diff --git a/js/index.js b/js/index.js index c51515b..b16a742 100644 --- a/js/index.js +++ b/js/index.js @@ -1,5 +1,7 @@ 'use strict'; +var _dl_progress_timeout; + function cache_refresh(force) { if ('serviceWorker' in navigator) { localforage.getItem("epube.cache-timestamp").then(function(stamp) { @@ -94,7 +96,9 @@ function offline_cache(bookId, callback) { console.log(cacheId + ' got data'); - fetch('backend.php?op=download&id=' + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { + var promises = []; + + promises.push(fetch('backend.php?op=download&id=' + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { if (resp.status == 200) { console.log(cacheId + ' got book'); @@ -102,9 +106,9 @@ function offline_cache(bookId, callback) { localforage.setItem(cacheId + '.book', resp.blob()); } - }); + })); - fetch("backend.php?op=getpagination&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { + promises.push(fetch("backend.php?op=getpagination&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { if (resp.status == 200) { console.log(cacheId + ' got pagination'); @@ -112,30 +116,41 @@ function offline_cache(bookId, callback) { localforage.setItem(cacheId + '.locations', JSON.parse(text)); }); } - }); + })); - fetch("backend.php?op=getlastread&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { + promises.push(fetch("backend.php?op=getlastread&id=" + data.epub_id, {credentials: 'same-origin'}).then(function(resp) { if (resp.status == 200) { console.log(cacheId + ' got lastread'); resp.text().then(function(text) { localforage.setItem(cacheId + '.lastread', JSON.parse(text)); }); } - }); + })); if (data.has_cover) { - fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) { + promises.push(fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) { if (resp.status == 200) { console.log(cacheId + ' got cover'); localforage.setItem(cacheId + '.cover', resp.blob()); } - }); + })); } + Promise.all(promises).then(function() { + $(".dl-progress") + .show() + .html("Finished downloading " + data.title + ""); + + window.clearTimeout(_dl_progress_timeout); + + _dl_progress_timeout = window.setTimeout(function() { + $(".dl-progress").fadeOut(); + }, 5*1000); + }); }); } -- cgit v1.2.3