From 72b2ffefc68e7e0a64c5727c215cb64d2c370234 Mon Sep 17 00:00:00 2001 From: Adrian Castro <22133246+castdrian@users.noreply.github.com> Date: Wed, 27 Mar 2024 12:52:21 +0100 Subject: [PATCH] feat: jump to last watched position --- .../expo/src/components/player/VideoPlayer.tsx | 17 +++++++++-------- apps/expo/src/lib/meta.ts | 6 +++++- apps/expo/src/stores/settings/index.ts | 18 +++++++++++++++++- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/apps/expo/src/components/player/VideoPlayer.tsx b/apps/expo/src/components/player/VideoPlayer.tsx index 0a093ae..63f2d54 100644 --- a/apps/expo/src/components/player/VideoPlayer.tsx +++ b/apps/expo/src/components/player/VideoPlayer.tsx @@ -75,7 +75,8 @@ export const VideoPlayer = () => { const setMeta = usePlayerStore((state) => state.setMeta); const { gestureControls, autoPlay } = usePlayerSettingsStore(); - const { updateWatchHistory, removeFromWatchHistory } = useWatchHistoryStore(); + const { updateWatchHistory, removeFromWatchHistory, getWatchHistorItem } = + useWatchHistoryStore(); const updateResizeMode = (newMode: ResizeMode) => { setResizeMode(newMode); @@ -238,14 +239,14 @@ export const VideoPlayer = () => { setHasStartedPlaying(true); if (videoRef) { void videoRef.setRateAsync(currentSpeed, true); + if (meta) { - const item = convertMetaToItemData(meta); - const scrapeMedia = convertMetaToScrapeMedia(meta); - updateWatchHistory( - item, - scrapeMedia, - videoRef.props.positionMillis ?? 0, - ); + const media = convertMetaToScrapeMedia(meta); + const watchHistoryItem = getWatchHistorItem(media); + + if (watchHistoryItem) { + void videoRef.setPositionAsync(watchHistoryItem.positionMillis); + } } } }; diff --git a/apps/expo/src/lib/meta.ts b/apps/expo/src/lib/meta.ts index bb19995..0cb0457 100644 --- a/apps/expo/src/lib/meta.ts +++ b/apps/expo/src/lib/meta.ts @@ -1,5 +1,9 @@ import type { ScrapeMedia } from "@movie-web/provider-utils"; -import { fetchMediaDetails, fetchSeasonDetails, getMediaPoster } from "@movie-web/tmdb"; +import { + fetchMediaDetails, + fetchSeasonDetails, + getMediaPoster, +} from "@movie-web/tmdb"; import type { ItemData } from "~/components/item/item"; import type { PlayerMeta } from "~/stores/player/slices/video"; diff --git a/apps/expo/src/stores/settings/index.ts b/apps/expo/src/stores/settings/index.ts index 6056a07..e23e9f3 100644 --- a/apps/expo/src/stores/settings/index.ts +++ b/apps/expo/src/stores/settings/index.ts @@ -141,6 +141,7 @@ interface WatchHistoryItem { interface WatchHistoryStoreState { watchHistory: WatchHistoryItem[]; + getWatchHistorItem: (media: ScrapeMedia) => WatchHistoryItem | undefined; setWatchHistory: (watchHistory: WatchHistoryItem[]) => void; updateWatchHistory: ( item: ItemData, @@ -155,8 +156,23 @@ export const useWatchHistoryStore = create< [["zustand/persist", WatchHistoryStoreState]] >( persist( - (set) => ({ + (set, get) => ({ watchHistory: [], + getWatchHistorItem: (media: ScrapeMedia) => + get().watchHistory.find((historyItem) => { + if (historyItem.media.type === "movie" && media.type === "movie") { + return historyItem.media.tmdbId === media.tmdbId; + } else if ( + historyItem.media.type === "show" && + media.type === "show" + ) { + return ( + historyItem.media.tmdbId === media.tmdbId && + historyItem.media.season === media.season && + historyItem.media.episode === media.episode + ); + } + }), setWatchHistory: (watchHistory: WatchHistoryItem[]) => set({ watchHistory }), updateWatchHistory: (