Skip to content

Commit c41881d

Browse files
authored
Extract favorite click handling into handleFavoriteClick
Refactor favorite handling into a separate function for better code organization and reusability.
1 parent e19af60 commit c41881d

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

js/index.js

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ let device = {
4141
}
4242
};*/
4343
let LANGUAGE = undefined;
44-
const favAnimMS = 500;
4544

4645
/** Ensure we run transfers one after the other rather that potentially having them overlap if the user clicks around
4746
https://github.com/espruino/EspruinoAppLoaderCore/issues/67 */
@@ -669,6 +668,23 @@ function refreshSort(){
669668
if(activeSort) sortContainer.querySelector('.chip[sortid="'+activeSort+'"]').classList.add('active');
670669
else sortContainer.querySelector('.chip[sortid]').classList.add('active');
671670
}
671+
function handleFavoriteClick(icon,app,button){
672+
const favAnimMS = 500; // duration of favourite animation in ms
673+
// clicked: animate and toggle favourite state immediately for instant feedback
674+
let favourite = SETTINGS.favourites.find(e => e == app.id);
675+
changeAppFavourite(!favourite, app,false);
676+
if (icon) icon.classList.toggle("icon-favourite-active", !favourite);
677+
if (icon) icon.classList.add("favoriteAnim");
678+
// update visible count optimistically (always update, even if 0)
679+
let cnt = getAppFavorites(app);
680+
let txt = (cnt > 999) ? Math.round(cnt/100)/10+"k" : cnt;
681+
let countEl = button.querySelector('.fav-count');
682+
if (countEl) countEl.textContent = String(txt);
683+
// ensure animation class is removed after the duration so it can be re-triggered
684+
setTimeout(() => {
685+
try { if (icon) icon.classList.remove("favoriteAnim"); } catch (e) { console.error(e); }
686+
}, favAnimMS);
687+
}
672688
// Refill the library with apps
673689
function refreshLibrary(options) {
674690
options = options||{};
@@ -861,20 +877,7 @@ function refreshLibrary(options) {
861877
if (err != "") showToast("Failed, "+err, "error");
862878
});
863879
} else if ( button.classList.contains("btn-favourite")) {
864-
// clicked: animate and toggle favourite state immediately for instant feedback
865-
let favourite = SETTINGS.favourites.find(e => e == app.id);
866-
changeAppFavourite(!favourite, app,false);
867-
if (icon) icon.classList.toggle("icon-favourite-active", !favourite);
868-
if (icon) icon.classList.add("favoriteAnim");
869-
// update visible count optimistically (always update, even if 0)
870-
let cnt = getAppFavorites(app);
871-
let txt = (cnt > 999) ? Math.round(cnt/100)/10+"k" : cnt;
872-
let countEl = button.querySelector('.fav-count');
873-
if (countEl) countEl.textContent = String(txt);
874-
// ensure animation class is removed after the duration so it can be re-triggered
875-
setTimeout(() => {
876-
try { if (icon) icon.classList.remove("favoriteAnim"); } catch (e) { console.error(e); }
877-
}, favAnimMS);
880+
handleFavoriteClick(icon,app,button);
878881
}
879882
});
880883
});
@@ -1153,18 +1156,7 @@ function refreshMyApps() {
11531156
});
11541157
// handle favourites on My Apps page (button has class btn-favourite)
11551158
if (button.classList && button.classList.contains("btn-favourite")) {
1156-
let favourite = SETTINGS.favourites.find(e => e == app.id);
1157-
changeAppFavourite(!favourite, app, false);
1158-
if (icon) icon.classList.toggle("icon-favourite-active", !favourite);
1159-
if (icon) icon.classList.add("favoriteAnim");
1160-
// update visible count optimistically (always update, even if 0)
1161-
let cnt = getAppFavorites(app);
1162-
let txt = (cnt > 999) ? Math.round(cnt/100)/10+"k" : cnt;
1163-
let countEl = button.querySelector('.fav-count');
1164-
if (countEl) countEl.textContent = String(txt);
1165-
setTimeout(() => {
1166-
try { if (icon) icon.classList.remove("favoriteAnim"); } catch (e) {}
1167-
}, favAnimMS);
1159+
handleFavoriteClick(icon,app,button);
11681160
}
11691161
});
11701162
});

0 commit comments

Comments
 (0)