Refactor ThisWeekSection and useLibrary hooks for improved episode loading and library updates

This update modifies the ThisWeekSection component to refresh episodes based on changes in library items instead of relying on catalog service subscriptions. The useLibrary hook is enhanced to subscribe to library updates from the catalog service, ensuring that library items are updated correctly and efficiently. Logging has been improved for better tracking of library changes.
This commit is contained in:
tapframe 2025-06-21 15:54:11 +05:30
parent 1605d5251e
commit 14d8f92b8e
2 changed files with 15 additions and 13 deletions

View file

@ -20,7 +20,6 @@ import { useLibrary } from '../../hooks/useLibrary';
import { RootStackParamList } from '../../navigation/AppNavigator';
import { parseISO, isThisWeek, format, isAfter, isBefore } from 'date-fns';
import Animated, { FadeIn, FadeInRight } from 'react-native-reanimated';
import { catalogService } from '../../services/catalogService';
const { width } = Dimensions.get('window');
const ITEM_WIDTH = width * 0.85;
@ -128,22 +127,13 @@ export const ThisWeekSection = () => {
}
}, [libraryItems]);
// Subscribe to library updates
useEffect(() => {
const unsubscribe = catalogService.subscribeToLibraryUpdates(() => {
console.log('[ThisWeekSection] Library updated, refreshing episodes');
fetchThisWeekEpisodes();
});
return () => unsubscribe();
}, [fetchThisWeekEpisodes]);
// Initial load
// Load episodes when library items change
useEffect(() => {
if (!libraryLoading) {
console.log('[ThisWeekSection] Library items changed, refreshing episodes. Items count:', libraryItems.length);
fetchThisWeekEpisodes();
}
}, [libraryLoading, fetchThisWeekEpisodes]);
}, [libraryLoading, libraryItems, fetchThisWeekEpisodes]);
const handleEpisodePress = (episode: ThisWeekEpisode) => {
// For upcoming episodes, go to the metadata screen

View file

@ -1,6 +1,7 @@
import { useState, useEffect, useCallback } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { StreamingContent } from '../types/metadata';
import { catalogService } from '../services/catalogService';
const LIBRARY_STORAGE_KEY = 'stremio-library';
@ -83,6 +84,17 @@ export const useLibrary = () => {
loadLibraryItems();
}, [loadLibraryItems]);
// Subscribe to catalogService library updates
useEffect(() => {
const unsubscribe = catalogService.subscribeToLibraryUpdates((items) => {
console.log('[useLibrary] Received library update from catalogService:', items.length, 'items');
setLibraryItems(items);
setLoading(false);
});
return () => unsubscribe();
}, []);
return {
libraryItems,
loading,