From d1410262a47aa4a0d2716ada0314ed972f3edf0e Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Wed, 22 Nov 2023 17:50:21 -0800
Subject: feat(wrapped): move calculations to media list

---
 src/lib/Tools/Wrapped.svelte | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)

(limited to 'src/lib/Tools/Wrapped.svelte')

diff --git a/src/lib/Tools/Wrapped.svelte b/src/lib/Tools/Wrapped.svelte
index 56099af9..3eb0a373 100644
--- a/src/lib/Tools/Wrapped.svelte
+++ b/src/lib/Tools/Wrapped.svelte
@@ -18,6 +18,8 @@
 
 	let currentUserIdentity = { name: '', id: -1 };
 	let episodes = 0;
+	let chapters = 0;
+	let minutesWatched = 0;
 	let animeList: Media[] | undefined = undefined;
 	let mangaList: Media[] | undefined = undefined;
 	let abbreviateTitles = true;
@@ -69,9 +71,10 @@
 			.filter(
 				(item, index, self) =>
 					self.findIndex((itemToCompare) => itemToCompare.id === item.id) === index &&
-					item.mediaListEntry?.startedAt.year === currentYear &&
 					(includeMusic ? true : item.format !== 'MUSIC') &&
-					(includeRepeats ? true : item.mediaListEntry?.repeat === 0)
+					(includeRepeats ? true : item.mediaListEntry?.repeat === 0) &&
+					(item.mediaListEntry?.startedAt.year === currentYear ||
+						item.mediaListEntry?.completedAt.year === currentYear)
 			)
 			.sort((a, b) => {
 				if (a.mediaListEntry?.score === undefined) {
@@ -96,8 +99,9 @@
 			.filter(
 				(item, index, self) =>
 					self.findIndex((itemToCompare) => itemToCompare.id === item.id) === index &&
-					item.mediaListEntry?.startedAt.year === currentYear &&
-					(includeRepeats ? true : item.mediaListEntry?.repeat === 0)
+					(includeRepeats ? true : item.mediaListEntry?.repeat === 0) &&
+					(item.mediaListEntry?.startedAt.year === currentYear ||
+						item.mediaListEntry?.completedAt.year === currentYear)
 			)
 			.sort((a, b) => {
 				if (a.mediaListEntry?.score === undefined) {
@@ -110,9 +114,16 @@
 			});
 
 		episodes = 0;
+		minutesWatched = 0;
+		chapters = 0;
 
 		for (const media of animeList) {
 			episodes += media.mediaListEntry?.progress || 0;
+			minutesWatched += (media.mediaListEntry?.progress || 0) * media.duration || 0;
+		}
+
+		for (const media of mangaList) {
+			chapters += media.mediaListEntry?.progress || 0;
 		}
 	};
 
@@ -199,9 +210,6 @@
 	{#await wrapped(user, currentUserIdentity)}
 		Loading ...
 	{:then wrapped}
-		{@const anime = year(wrapped.statistics.anime)}
-		{@const manga = year(wrapped.statistics.manga)}
-
 		<div class:light-theme={darkTheme} class="categories-grid">
 			<div class="grid-item image-grid avatar-grid">
 				<a href={`https://anilist.co/user/${currentUserIdentity.name}`} target="_blank">
@@ -239,10 +247,10 @@
 					<b>Anime</b>
 				</div>
 				<div class="grid-item">
-					Time Watched: {((anime.minutesWatched || 0) / 60 / 24).toFixed(2)} days
+					Time Watched: {((minutesWatched || 0) / 60 / 24).toFixed(2)} days
 				</div>
 				<div class="grid-item">
-					Completed: {anime.count}
+					Completed: {animeList?.length}
 				</div>
 				<div class="grid-item">Episodes: {episodes}</div>
 			</div>
@@ -251,13 +259,13 @@
 					<b>Manga</b>
 				</div>
 				<div class="grid-item">
-					Time Read: {(((manga.chaptersRead || 0) * 8.58) / 60 / 24).toFixed(2)} days
+					Time Read: {((chapters * 8.58) / 60 / 24).toFixed(2)} days
 				</div>
 				<div class="grid-item">
-					Completed: {manga.count}
+					Completed: {mangaList?.length}
 				</div>
 				<div class="grid-item">
-					Chapters: {manga.chaptersRead || 0}
+					Chapters: {chapters}
 				</div>
 			</div>
 			<div class="category-grid pure-category">
-- 
cgit v1.2.3

