diff --git a/src/screens/LibraryScreen.tsx b/src/screens/LibraryScreen.tsx index 0239c2de..341acd08 100644 --- a/src/screens/LibraryScreen.tsx +++ b/src/screens/LibraryScreen.tsx @@ -274,8 +274,16 @@ const LibraryScreen = () => { setLoading(true); try { const items = await catalogService.getLibraryItems(); + + // Sort by date added (most recent first) + const sortedItems = items.sort((a, b) => { + const timeA = (a as any).addedToLibraryAt || 0; + const timeB = (b as any).addedToLibraryAt || 0; + return timeB - timeA; // Descending order (newest first) + }); + // Load watched status for each item from AsyncStorage - const updatedItems = await Promise.all(items.map(async (item) => { + const updatedItems = await Promise.all(sortedItems.map(async (item) => { // Map StreamingContent to LibraryItem shape const libraryItem: LibraryItem = { ...item, @@ -301,8 +309,15 @@ const LibraryScreen = () => { // Subscribe to library updates const unsubscribe = catalogService.subscribeToLibraryUpdates(async (items) => { + // Sort by date added (most recent first) + const sortedItems = items.sort((a, b) => { + const timeA = (a as any).addedToLibraryAt || 0; + const timeB = (b as any).addedToLibraryAt || 0; + return timeB - timeA; // Descending order (newest first) + }); + // Sync watched status on update - const updatedItems = await Promise.all(items.map(async (item) => { + const updatedItems = await Promise.all(sortedItems.map(async (item) => { // Map StreamingContent to LibraryItem shape const libraryItem: LibraryItem = { ...item, diff --git a/src/services/catalogService.ts b/src/services/catalogService.ts index a86bb8ea..999a1e49 100644 --- a/src/services/catalogService.ts +++ b/src/services/catalogService.ts @@ -131,6 +131,7 @@ export interface StreamingContent { poster_path?: string; backdrop_path?: string; }; + addedToLibraryAt?: number; // Timestamp when added to library } export interface CatalogContent { @@ -853,7 +854,10 @@ class CatalogService { public async addToLibrary(content: StreamingContent): Promise { const key = `${content.type}:${content.id}`; - this.library[key] = content; + this.library[key] = { + ...content, + addedToLibraryAt: Date.now() // Add timestamp + }; this.saveLibrary(); this.notifyLibrarySubscribers(); try { this.libraryAddListeners.forEach(l => l(content)); } catch {}