From ff247eafb3e44949dc6129d5dc00e72d0c04b409 Mon Sep 17 00:00:00 2001 From: robbevp Date: Sat, 20 Nov 2021 12:20:30 +0100 Subject: [PATCH] Add recently played albums and artists on home (#647) --- src/comparators.js | 9 ++++ src/components/TracksTable.vue | 12 ++++-- src/locales/en.json | 2 + src/locales/nl.json | 2 + src/store/plays.js | 66 ++++++++++++++++++++++++++-- src/views/Home.vue | 78 +++++++++++++++++++++++++++++++++- 6 files changed, 161 insertions(+), 8 deletions(-) diff --git a/src/comparators.js b/src/comparators.js index 38ee1896..282690dc 100644 --- a/src/comparators.js +++ b/src/comparators.js @@ -137,3 +137,12 @@ export function compareAlbumsByReleaseFirst(newestFirst = false) { return (order = order === 0 ? compareStrings(a1.id, a2.id) : order); }; } + +export function compareByRecentlyPlayed(stats) { + return function (i1, i2) { + return ( + (stats[i2.id]?.last_played_at || new Date(0)) - + (stats[i1.id]?.last_played_at || new Date(0)) + ); + }; +} diff --git a/src/components/TracksTable.vue b/src/components/TracksTable.vue index a92c9dd0..76d6e69d 100644 --- a/src/components/TracksTable.vue +++ b/src/components/TracksTable.vue @@ -88,7 +88,11 @@ + + + + + + + + + + + + @@ -199,7 +263,11 @@ import { mapGetters } from "vuex"; import AlbumCard from "../components/AlbumCard"; import ArtistCard from "../components/ArtistCard"; -import { compareAlbumsByReleaseFirst, compareStrings } from "../comparators"; +import { + compareAlbumsByReleaseFirst, + compareByRecentlyPlayed, + compareStrings, +} from "../comparators"; export default { name: "Home", @@ -217,6 +285,12 @@ export default { }); return items; }, + recentlyPlayedAlbumsSort(albums) { + return albums.sort(compareByRecentlyPlayed(this.playStatsByAlbum)); + }, + recentlyPlayedArtistsSort(artists) { + return artists.sort(compareByRecentlyPlayed(this.playStatsByArtist)); + }, randomSort(items) { const newItems = [...items]; for (let i = newItems.length - 1; i > 0; i--) { @@ -231,6 +305,8 @@ export default { albums: "albums/albums", albumsOnThisDay: "albums/albumsOnThisDay", artists: "artists/artists", + playStatsByAlbum: "plays/playStatsByAlbum", + playStatsByArtist: "plays/playStatsByArtist", }), randomAlbums() { return this.randomSort(this.albums);