1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
<script lang="ts">
/* eslint svelte/no-at-html-tags: "off" */
import settings from '$stores/settings';
import type { Media } from '$lib/Data/AniList/media';
import { cleanCache, incrementMediaProgress } from '$lib/Media/Anime/cache';
import { totalEpisodes } from '$lib/Media/Anime/episodes';
import type { AniListAuthorisation } from '$lib/Data/AniList/identity';
import ListTitle from '../ListTitle.svelte';
import { onDestroy, onMount } from 'svelte';
import AiringTime from '$lib/Media/Anime/Airing/AiringTime.svelte';
import { browser } from '$app/environment';
import identity from '$stores/identity';
import '../covers.css';
import revalidateAnime from '$stores/revalidateAnime';
import CleanGrid from '$lib/List/CleanGrid.svelte';
import CleanList from '../CleanList.svelte';
import stateBin from '$stores/stateBin';
import localforage from 'localforage';
export let media: Media[];
export let title: any;
export let animeLists: Promise<Media[]>;
export let user: AniListAuthorisation;
export let endTime: number;
export let lastUpdatedMedia: number;
export let completed = false;
export let previousAnimeList: Media[];
export let pendingUpdate: number | null;
export let upcoming = false;
export let notYetReleased = false;
export let dummy = false;
export let disableFilter = false;
let keyCacher: NodeJS.Timeout;
let totalEpisodeDueCount = media
.map((anime) => {
if ($settings.displayTotalEpisodes && !$settings.displayTotalDueEpisodes) return 1;
if ($settings.displayTotalDueEpisodes && completed && !$settings.displayTotalEpisodes)
return 1;
if ($settings.displayTotalEpisodes && anime.status === 'FINISHED')
return anime.episodes - (anime.mediaListEntry?.progress || 0);
if (anime.status === 'NOT_YET_RELEASED') return 1;
return (
(anime.nextAiringEpisode?.episode || 1) -
(anime.mediaListEntry?.progress || 0) -
(upcoming || notYetReleased ? 0 : 1)
);
})
.reduce((a, b) => a + b, 0);
const lists = Array.from(
new Set(
media
.flatMap((m) => Object.entries(m.mediaListEntry?.customLists ?? {}))
.filter(([_, value]) => value)
.map(([key]) => key)
)
);
let filterKind = upcoming
? 'Upcoming'
: notYetReleased
? 'NotYetReleased'
: completed
? 'Completed'
: 'Due';
const filterKey = `${filterKind}AnimeListFilter`;
$: selectedList = disableFilter ? 'All' : ($stateBin[filterKey] as string) || 'All';
$: filteredMedia =
selectedList === 'All' || !$settings.displayMediaListFilter
? media
: media.filter((m) => m.mediaListEntry?.customLists?.[selectedList]);
onMount(async () => {
if (dummy) return;
keyCacher = setInterval(
() => {
media = media;
if (
media.some(
(m) => m.nextAiringEpisode?.airingAt && m.nextAiringEpisode.airingAt < Date.now() / 1000
)
)
animeLists = cleanCache(user, $identity);
},
(() => {
const airingAt = media
.filter(
(m) =>
(m.status === 'RELEASING' || m.status === 'NOT_YET_RELEASED') &&
m.nextAiringEpisode?.airingAt
)
.find((m) => m.nextAiringEpisode?.airingAt)?.nextAiringEpisode?.airingAt;
const untilAiring = airingAt
? Math.round((airingAt - Date.now() / 1000) * 100) / 100
: undefined;
return untilAiring ? (untilAiring < 0 ? 1000 : untilAiring) : 1000;
})()
);
if (browser)
await localforage.setItem(
`last${
notYetReleased ? 'NotYetReleased' : upcoming ? 'Upcoming' : completed ? 'Completed' : ''
}AnimeListLength`,
media.length.toString()
);
});
onDestroy(() => clearInterval(keyCacher));
const increment = (anime: Media, progress: number) => {
if (!dummy && pendingUpdate !== anime.id) {
$revalidateAnime = true;
lastUpdatedMedia = anime.id;
pendingUpdate = anime.id;
incrementMediaProgress(anime.id, anime.mediaListEntry?.progress, user, () => {
const mediaListEntry = media.find((m) => m.id === anime.id)?.mediaListEntry;
if (mediaListEntry) mediaListEntry.progress = progress + 1;
previousAnimeList = media;
animeLists = cleanCache(user, $identity);
pendingUpdate = null;
});
}
};
</script>
<ListTitle
time={endTime / 1000}
count={$settings.displayTotalDueEpisodes || $settings.displayTotalEpisodes
? totalEpisodeDueCount
: filteredMedia.length}
{title}
hideTime={dummy}
hideCount={dummy}
/>
{#if media.length === 0}
No anime to display. <button on:click={() => (animeLists = cleanCache(user, $identity))}>
Force refresh
</button>
{:else if $settings.displayMediaListFilter && !disableFilter}
<select bind:value={$stateBin[filterKey]}>
<option value="All">All</option>
{#each lists as list}
<option value={list}>{list}</option>
{/each}
</select>
<p></p>
{/if}
{#if $settings.displayCoverModeAnime}
<CleanGrid
media={filteredMedia}
{dummy}
type="anime"
{upcoming}
{notYetReleased}
reverseSort={$settings.displayReverseSort}
>
<div slot="title" let:title={anime} let:progress>
{#if !upcoming && !notYetReleased}
{pendingUpdate === anime.id ? progress + 1 : progress}{@html totalEpisodes(anime)}
<button
class={`button-square button-action ${pendingUpdate === anime.id ? 'opaque' : ''}`}
style={pendingUpdate === anime.id ? 'pointer-events: none;' : ''}
on:click={() => increment(anime, progress)}>+</button
>
{#if !completed || dummy}
[{anime.nextAiringEpisode?.episode === -1
? '?'
: (anime.nextAiringEpisode?.episode || 1) - 1}]
<br />
<AiringTime originalAnime={anime} />
{/if}
{:else}
<AiringTime originalAnime={anime} upcoming={true} />
{/if}
</div>
</CleanGrid>
{:else}
<CleanList
media={filteredMedia}
type="anime"
{upcoming}
{notYetReleased}
{lastUpdatedMedia}
reverseSort={$settings.displayReverseSort}
>
<span slot="information" let:title={anime} let:progress>
{#if !upcoming || notYetReleased || !$settings.displayCountdownRightAligned}
<span class="opaque">|</span>
{/if}
{#if !upcoming || notYetReleased}
<!-- {anime.mediaListEntry?.progress || 0}{@html totalEpisodes(anime)} -->
{pendingUpdate === anime.id ? progress + 1 : progress}{@html totalEpisodes(anime)}
<button
class={`button-square button-action ${pendingUpdate === anime.id ? 'opaque' : ''}`}
style={pendingUpdate === anime.id ? 'pointer-events: none;' : ''}
on:click={() => increment(anime, progress)}>+</button
>
{#if !completed}
[{anime.nextAiringEpisode?.episode === -1
? '?'
: (anime.nextAiringEpisode?.episode || 1) - 1}]
<span class:countdown={$settings.displayCountdownRightAligned}>
<AiringTime originalAnime={anime} />
</span>
{/if}
{:else}
<span class:countdown={$settings.displayCountdownRightAligned}>
<AiringTime originalAnime={anime} upcoming={true} />
</span>
{/if}
</span>
</CleanList>
{/if}
|