|
| 1 | +//FAV WITH PILL AND ANIM |
1 | 2 | let appJSON = []; // List of apps and info from apps.json |
2 | 3 | let appSortInfo = {}; // list of data to sort by, from appdates.csv { created, modified } |
3 | 4 | let appCounts = {}; |
@@ -548,19 +549,19 @@ librarySearchInput.addEventListener('input', evt => { |
548 | 549 |
|
549 | 550 |
|
550 | 551 | function getAppFavorites(app){ |
551 | | - let info = appSortInfo[app.id]; |
552 | | - let appFavourites = 0; |
553 | | - if (info.favourites) { |
554 | | - let favsThisSession = SETTINGS.appsFavoritedThisSession.find(obj => obj.id === app.id); |
555 | | - appFavourites = info.favourites; |
556 | | - if(favsThisSession){ |
557 | | - if(info.favourites!=favsThisSession.favs){ |
558 | | - //database has been updated, remove app from favsThisSession |
559 | | - SETTINGS.appsFavoritedThisSession = SETTINGS.appsFavoritedThisSession.filter(obj => obj.id !== app.id); |
560 | | - } |
561 | | - else{ |
562 | | - appFavourites += 1; //add one to give the illusion of immediate database changes |
563 | | - } |
| 552 | + let info = appSortInfo[app.id] || {}; |
| 553 | + // start with whatever number we have in the database (may be undefined -> treat as 0) |
| 554 | + let appFavourites = (typeof info.favourites === 'number') ? info.favourites : 0; |
| 555 | + let favsThisSession = SETTINGS.appsFavoritedThisSession.find(obj => obj.id === app.id); |
| 556 | + if (favsThisSession) { |
| 557 | + // If the database count changed since we recorded the session-favourite, it means |
| 558 | + // the server/db has been updated and our optimistic session entry is stale. |
| 559 | + if (typeof info.favourites === 'number' && info.favourites !== favsThisSession.favs) { |
| 560 | + // remove stale session entry |
| 561 | + SETTINGS.appsFavoritedThisSession = SETTINGS.appsFavoritedThisSession.filter(obj => obj.id !== app.id); |
| 562 | + } else { |
| 563 | + // otherwise include our optimistic +1 so the UI updates immediately |
| 564 | + appFavourites += 1; |
564 | 565 | } |
565 | 566 | } |
566 | 567 | return appFavourites; |
@@ -862,13 +863,13 @@ function refreshLibrary(options) { |
862 | 863 | changeAppFavourite(!favourite, app,false); |
863 | 864 | if (icon) icon.classList.toggle("icon-favourite-active", !favourite); |
864 | 865 | if (icon) icon.classList.add("favoriteAnim"); |
865 | | - // update visible count optimistically |
| 866 | + // update visible count optimistically (always update, even if 0) |
866 | 867 | let cnt = getAppFavorites(app); |
867 | | - if (!cnt) return ""; |
868 | | - let txt = (cnt > 999) ? Math.round(cnt/1000)+"k" : cnt; |
| 868 | + let txt = (cnt > 999) ? Math.round(cnt/1000)+"k" : cnt; |
869 | 869 | let countEl = button.querySelector('.fav-count'); |
870 | | - countEl.textContent = String(txt); |
| 870 | + if (countEl) countEl.textContent = String(txt); |
871 | 871 | const ANIM_MS = 500; |
| 872 | + // ensure animation class is removed after the duration so it can be re-triggered |
872 | 873 | setTimeout(() => { |
873 | 874 | try { if (icon) icon.classList.remove("favoriteAnim"); } catch (e) {} |
874 | 875 | }, ANIM_MS); |
|
0 commit comments