2022-03-06 12:56:22 +01:00
|
|
|
import { MWMediaMeta, getProviderMetadata } from "providers";
|
2022-02-18 21:11:23 +01:00
|
|
|
import React, { createContext, ReactNode, useContext, useState } from "react";
|
|
|
|
import { VideoProgressStore } from "./store";
|
|
|
|
|
2022-02-28 00:08:20 +01:00
|
|
|
interface WatchedStoreItem extends MWMediaMeta {
|
2022-02-18 21:11:23 +01:00
|
|
|
progress: number;
|
|
|
|
percentage: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface WatchedStoreData {
|
|
|
|
items: WatchedStoreItem[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface WatchedStoreDataWrapper {
|
2022-02-28 00:08:20 +01:00
|
|
|
updateProgress(media: MWMediaMeta, progress: number, total: number): void;
|
2022-03-06 12:56:22 +01:00
|
|
|
getFilteredWatched(): WatchedStoreItem[];
|
2022-02-18 21:11:23 +01:00
|
|
|
watched: WatchedStoreData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WatchedContext = createContext<WatchedStoreDataWrapper>({
|
2022-02-20 16:45:46 +01:00
|
|
|
updateProgress: () => {},
|
2022-03-06 12:56:22 +01:00
|
|
|
getFilteredWatched: () => [],
|
2022-02-18 21:11:23 +01:00
|
|
|
watched: {
|
|
|
|
items: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
WatchedContext.displayName = "WatchedContext";
|
|
|
|
|
|
|
|
export function WatchedContextProvider(props: { children: ReactNode }) {
|
|
|
|
const watchedLocalstorage = VideoProgressStore.get();
|
2022-02-20 16:45:46 +01:00
|
|
|
const [watched, setWatchedReal] = useState<WatchedStoreData>(
|
2022-02-18 21:11:23 +01:00
|
|
|
watchedLocalstorage as WatchedStoreData
|
|
|
|
);
|
2022-02-20 16:45:46 +01:00
|
|
|
|
|
|
|
function setWatched(data: any) {
|
|
|
|
setWatchedReal((old) => {
|
|
|
|
let newData = data;
|
|
|
|
if (data.constructor === Function) {
|
|
|
|
newData = data(old);
|
|
|
|
}
|
|
|
|
watchedLocalstorage.save(newData);
|
|
|
|
return newData;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-18 21:11:23 +01:00
|
|
|
const contextValue = {
|
2022-03-06 12:56:22 +01:00
|
|
|
updateProgress(media: MWMediaMeta, progress: number, total: number): void {
|
2022-02-20 16:45:46 +01:00
|
|
|
setWatched((data: WatchedStoreData) => {
|
2022-03-06 12:56:22 +01:00
|
|
|
let item = getWatchedFromPortable(data.items, media);
|
2022-02-20 16:45:46 +01:00
|
|
|
if (!item) {
|
|
|
|
item = {
|
|
|
|
mediaId: media.mediaId,
|
|
|
|
mediaType: media.mediaType,
|
|
|
|
providerId: media.providerId,
|
2022-02-28 00:08:20 +01:00
|
|
|
title: media.title,
|
|
|
|
year: media.year,
|
2022-02-20 16:45:46 +01:00
|
|
|
percentage: 0,
|
|
|
|
progress: 0,
|
|
|
|
episode: media.episode,
|
|
|
|
season: media.season,
|
|
|
|
};
|
|
|
|
data.items.push(item);
|
2022-02-18 21:11:23 +01:00
|
|
|
}
|
2022-02-20 16:45:46 +01:00
|
|
|
|
|
|
|
// update actual item
|
|
|
|
item.progress = progress;
|
|
|
|
item.percentage = Math.round((progress / total) * 100);
|
|
|
|
|
|
|
|
return data;
|
2022-02-18 21:11:23 +01:00
|
|
|
});
|
|
|
|
},
|
2022-03-06 12:56:22 +01:00
|
|
|
getFilteredWatched() {
|
|
|
|
return watched.items.filter((item) => {
|
|
|
|
return getProviderMetadata(item.providerId)?.enabled;
|
|
|
|
});
|
|
|
|
},
|
2022-02-18 21:11:23 +01:00
|
|
|
watched,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<WatchedContext.Provider value={contextValue}>
|
|
|
|
{props.children}
|
|
|
|
</WatchedContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useWatchedContext() {
|
|
|
|
return useContext(WatchedContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getWatchedFromPortable(
|
2022-03-06 12:56:22 +01:00
|
|
|
items: WatchedStoreItem[],
|
2022-02-28 00:08:20 +01:00
|
|
|
media: MWMediaMeta
|
2022-02-18 21:11:23 +01:00
|
|
|
): WatchedStoreItem | undefined {
|
2022-03-06 12:56:22 +01:00
|
|
|
return items.find((v) => {
|
2022-02-18 21:11:23 +01:00
|
|
|
return (
|
|
|
|
v.mediaId === media.mediaId &&
|
|
|
|
v.providerId === media.providerId &&
|
|
|
|
v.episode === media.episode &&
|
|
|
|
v.season === media.season
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|