fixed order of library screen contents

This commit is contained in:
tapframe 2025-10-24 21:12:36 +05:30
parent a0e5332897
commit d6216d95db
2 changed files with 22 additions and 3 deletions

View file

@ -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,

View file

@ -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<void> {
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 {}