From 5b4ad037099762f9013211d22a6eb79c78e7723e Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Fri, 27 Dec 2024 02:21:01 -0800
Subject: feat(Wrapped): start and end data filters

---
 src/lib/Tools/Wrapped/Tool.svelte | 62 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 58 insertions(+), 4 deletions(-)

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

diff --git a/src/lib/Tools/Wrapped/Tool.svelte b/src/lib/Tools/Wrapped/Tool.svelte
index a68de8b9..a0fa9214 100644
--- a/src/lib/Tools/Wrapped/Tool.svelte
+++ b/src/lib/Tools/Wrapped/Tool.svelte
@@ -70,6 +70,9 @@
   let activityHistoryPosition: 'TOP' | 'BELOW_TOP' | 'ORIGINAL' = 'ORIGINAL';
   let includeOngoingMediaFromPreviousYears = false;
   let exlcludeUnratedUnwatched = true;
+  let startDateFilter: Date | null = null;
+  let endDateFilter: Date | null = null;
+  let dateTicked = false;
 
   $: {
     if (browser && mounted) {
@@ -284,7 +287,7 @@
       $anime,
       $lastPruneTimes.anime,
       {
-        forcePrune: true,
+        forcePrune: dateTicked ? false : true,
         includeCompleted: true,
         all: true
       }
@@ -310,7 +313,21 @@
           (includeSpecials ? true : item.format !== 'SPECIAL') &&
           (includeOVAs ? true : item.format !== 'OVA') &&
           (exlcludeUnratedUnwatched ? item.mediaListEntry?.score !== 0 : true) &&
-          (exlcludeUnratedUnwatched ? item.mediaListEntry?.progress !== 0 : true)
+          (exlcludeUnratedUnwatched ? item.mediaListEntry?.progress !== 0 : true) &&
+          (startDateFilter && item.mediaListEntry?.startedAt
+            ? new Date(
+                item.mediaListEntry.startedAt.year,
+                item.mediaListEntry?.startedAt.month - 1,
+                item.mediaListEntry.startedAt.day
+              ) >= new Date(startDateFilter)
+            : true) &&
+          (endDateFilter && item.mediaListEntry?.startedAt
+            ? new Date(
+                item.mediaListEntry.completedAt.year,
+                item.mediaListEntry?.completedAt.month - 1,
+                item.mediaListEntry.completedAt.day
+              ) <= new Date(endDateFilter)
+            : true)
       )
       .sort((a, b) => {
         switch (mediaSort) {
@@ -383,7 +400,7 @@
       $manga,
       $lastPruneTimes.manga,
       {
-        forcePrune: true,
+        forcePrune: dateTicked ? false : true,
         includeCompleted: true,
         all: true
       }
@@ -401,7 +418,21 @@
               ? item.mediaListEntry?.progress >= 1
               : false)) &&
           (exlcludeUnratedUnwatched ? item.mediaListEntry?.score !== 0 : true) &&
-          (exlcludeUnratedUnwatched ? item.mediaListEntry?.progress !== 0 : true)
+          (exlcludeUnratedUnwatched ? item.mediaListEntry?.progress !== 0 : true) &&
+          (startDateFilter && item.mediaListEntry?.startedAt
+            ? new Date(
+                item.mediaListEntry.startedAt.year,
+                item.mediaListEntry?.startedAt.month - 1,
+                item.mediaListEntry.startedAt.day
+              ) >= new Date(startDateFilter)
+            : true) &&
+          (endDateFilter && item.mediaListEntry?.startedAt
+            ? new Date(
+                item.mediaListEntry.completedAt.year,
+                item.mediaListEntry?.completedAt.month - 1,
+                item.mediaListEntry.completedAt.day
+              ) <= new Date(endDateFilter)
+            : true)
       )
       .sort((a, b) => {
         if (a.mediaListEntry?.score === undefined) return 1;
@@ -438,6 +469,7 @@
     episodes = 0;
     minutesWatched = 0;
     chapters = 0;
+    dateTicked = false;
 
     for (const media of calculatedAnimeList) {
       episodes += media.mediaListEntry?.progress || 0;
@@ -719,6 +751,28 @@
                 {/each}
               </select>
               Calculate for year<br />
+              <input
+                type="date"
+                bind:value={startDateFilter}
+                placeholder="Start date"
+                on:change={() => {
+                  dateTicked = true;
+
+                  update();
+                }}
+              />
+              Start date filter<br />
+              <input
+                type="date"
+                bind:value={endDateFilter}
+                placeholder="End date"
+                on:change={() => {
+                  dateTicked = true;
+
+                  update();
+                }}
+              />
+              End date filter<br />
               <select bind:value={mediaSort}>
                 <option value={SortOptions.SCORE}>Score</option>
                 <option value={SortOptions.MINUTES_WATCHED}>Minutes Watched/Read</option>
-- 
cgit v1.2.3

