Allows sorting items by last used

This commit is contained in:
Alicia Sykes 2021-09-05 01:00:44 +01:00
parent 955985071f
commit 11f5f8c9df
3 changed files with 20 additions and 1 deletions

View File

@ -107,7 +107,9 @@ export default {
} else { } else {
this.$emit('itemClicked'); this.$emit('itemClicked');
} }
// Update the most/ last used ledger, for smart-sorting
this.incrementMostUsedCount(this.id); this.incrementMostUsedCount(this.id);
this.incrementLastUsedCount(this.id);
}, },
/* Open custom context menu, and set position */ /* Open custom context menu, and set position */
openContextMenu(e) { openContextMenu(e) {
@ -200,7 +202,7 @@ export default {
default: window.open(url, '_blank'); default: window.open(url, '_blank');
} }
}, },
/* Used for smart-sort when sorting items by most/ last used */ /* Used for smart-sort when sorting items by most used apps */
incrementMostUsedCount(itemId) { incrementMostUsedCount(itemId) {
const mostUsed = JSON.parse(localStorage.getItem(localStorageKeys.MOST_USED) || '{}'); const mostUsed = JSON.parse(localStorage.getItem(localStorageKeys.MOST_USED) || '{}');
let counter = mostUsed[itemId] || 0; let counter = mostUsed[itemId] || 0;
@ -208,6 +210,12 @@ export default {
mostUsed[itemId] = counter; mostUsed[itemId] = counter;
localStorage.setItem(localStorageKeys.MOST_USED, JSON.stringify(mostUsed)); localStorage.setItem(localStorageKeys.MOST_USED, JSON.stringify(mostUsed));
}, },
/* Used for smart-sort when sorting by last used apps */
incrementLastUsedCount(itemId) {
const lastUsed = JSON.parse(localStorage.getItem(localStorageKeys.LAST_USED) || '{}');
lastUsed[itemId] = new Date().getTime();
localStorage.setItem(localStorageKeys.LAST_USED, JSON.stringify(lastUsed));
},
}, },
mounted() { mounted() {
// If ststus checking is enabled, then check service status // If ststus checking is enabled, then check service status

View File

@ -75,6 +75,7 @@ export default {
sortOrder() { sortOrder() {
return this.displayData.sortBy || defaultSortOrder; return this.displayData.sortBy || defaultSortOrder;
}, },
/* If the sortBy attribute is specified, then return sorted data */
sortedItems() { sortedItems() {
let { items } = this; let { items } = this;
if (this.sortOrder === 'alphabetical') { if (this.sortOrder === 'alphabetical') {
@ -83,6 +84,8 @@ export default {
items.sort((a, b) => (a.title < b.title ? 1 : -1)); items.sort((a, b) => (a.title < b.title ? 1 : -1));
} else if (this.sortOrder === 'most-used') { } else if (this.sortOrder === 'most-used') {
items = this.sortByMostUsed(items); items = this.sortByMostUsed(items);
} else if (this.sortOrder === 'last-used') {
items = this.sortBLastUsed(items);
} }
return items; return items;
}, },
@ -134,6 +137,13 @@ export default {
items.reverse().sort((a, b) => (gmu(a) < gmu(b) ? 1 : -1)); items.reverse().sort((a, b) => (gmu(a) < gmu(b) ? 1 : -1));
return items; return items;
}, },
/* Sorts items by most recently used */
sortBLastUsed(items) {
const usageCount = JSON.parse(localStorage.getItem(localStorageKeys.LAST_USED) || '{}');
const glu = (item) => usageCount[this.makeId(item.title)] || 0;
items.reverse().sort((a, b) => (glu(a) < glu(b) ? 1 : -1));
return items;
},
}, },
}; };
</script> </script>

View File

@ -94,6 +94,7 @@ module.exports = {
HIDE_SETTINGS: 'hideSettings', HIDE_SETTINGS: 'hideSettings',
USERNAME: 'username', USERNAME: 'username',
MOST_USED: 'mostUsed', MOST_USED: 'mostUsed',
LAST_USED: 'lastUsed',
}, },
/* Key names for cookie identifiers */ /* Key names for cookie identifiers */
cookieKeys: { cookieKeys: {