diff --git a/js/index.js b/js/index.js
index 259ee0d..00f2b66 100644
--- a/js/index.js
+++ b/js/index.js
@@ -1,3 +1,4 @@
+//FAV WITH PILL AND ANIM
let appJSON = []; // List of apps and info from apps.json
let appSortInfo = {}; // list of data to sort by, from appdates.csv { created, modified }
let appCounts = {};
@@ -501,7 +502,7 @@ function handleAppInterface(app) {
});
}
-function changeAppFavourite(favourite, app) {
+function changeAppFavourite(favourite, app,refresh=true) {
if (favourite) {
SETTINGS.appsFavoritedThisSession.push({"id":app.id,"favs":appSortInfo[app.id]&&appSortInfo[app.id].favourites?appSortInfo[app.id].favourites:0});
SETTINGS.favourites = SETTINGS.favourites.concat([app.id]);
@@ -510,10 +511,11 @@ function changeAppFavourite(favourite, app) {
SETTINGS.favourites = SETTINGS.favourites.filter(e => e != app.id);
}
saveSettings();
- refreshLibrary();
- refreshMyApps();
+ if(refresh) {
+ refreshLibrary();
+ refreshMyApps();
+ }
}
-
// =========================================== Top Navigation
function showTab(tabname) {
htmlToArray(document.querySelectorAll("#tab-navigate .tab-item")).forEach(tab => {
@@ -543,6 +545,29 @@ librarySearchInput.addEventListener('input', evt => {
// =========================================== App Info
+
+
+
+function getAppFavorites(app){
+ let info = appSortInfo[app.id] || {};
+ // start with whatever number we have in the database (may be undefined -> treat as 0)
+ let appFavourites = (typeof info.favourites === 'number') ? info.favourites : 0;
+ let favsThisSession = SETTINGS.appsFavoritedThisSession.find(obj => obj.id === app.id);
+ if (favsThisSession) {
+ // If the database count changed since we recorded the session-favourite, it means
+ // the server/db has been updated and our optimistic session entry is stale.
+ if (typeof info.favourites === 'number' && info.favourites !== favsThisSession.favs) {
+ // remove stale session entry
+ SETTINGS.appsFavoritedThisSession = SETTINGS.appsFavoritedThisSession.filter(obj => obj.id !== app.id);
+ } else {
+ // otherwise include our optimistic +1 so the UI updates immediately
+ appFavourites += 1;
+ }
+ }
+ return appFavourites;
+}
+
+
function getAppHTML(app, appInstalled, forInterface) {
let version = getVersionInfo(app, appInstalled);
let versionInfo = version.text;
@@ -559,21 +584,11 @@ function getAppHTML(app, appInstalled, forInterface) {
infoTxt.push(`${info.installs} reported installs (${percentText})`);
}
if (info.favourites) {
- let favsThisSession = SETTINGS.appsFavoritedThisSession.find(obj => obj.id === app.id);
- let percent=(info.favourites / info.installs * 100).toFixed(0);
+ appFavourites = getAppFavorites(app);
+ let percent=(appFavourites / info.installs * 100).toFixed(0);
let percentText=percent>100?"More than 100% of installs":percent+"% of installs";
- if(!info.installs||info.installs<1) {infoTxt.push(`${info.favourites} users favourited`)}
- else {infoTxt.push(`${info.favourites} users favourited (${percentText})`)}
- appFavourites = info.favourites;
- if(favsThisSession){
- if(info.favourites!=favsThisSession.favs){
- //database has been updated, remove app from favsThisSession
- SETTINGS.appsFavoritedThisSession = SETTINGS.appsFavoritedThisSession.filter(obj => obj.id !== app.id);
- }
- else{
- appFavourites += 1; //add one to give the illusion of immediate database changes
- }
- }
+ if(!info.installs||info.installs<1) {infoTxt.push(`${appFavourites} users favourited`)}
+ else {infoTxt.push(`${appFavourites} users favourited (${percentText})`)}
}
if (infoTxt.length)
versionTitle = `title="${infoTxt.join("\n")}"`;
@@ -585,9 +600,10 @@ function getAppHTML(app, appInstalled, forInterface) {
let githubLink = Const.APP_SOURCECODE_URL ?
`
` : "";
let getAppFavouritesHTML = cnt => {
- if (!cnt) return "";
- let txt = (cnt > 999) ? Math.round(cnt/1000)+"k" : cnt;
- return `${txt}`;
+ // Always show a count (0 if none) and format large numbers with 'k'
+ let n = (cnt && typeof cnt === 'number') ? cnt : 0;
+ let txt = (n > 999) ? Math.round(n/1000)+"k" : n;
+ return `${txt}`;
};
let html = `