From 14d8f92b8e1b68322784b95cde73d24063eae750 Mon Sep 17 00:00:00 2001 From: tapframe Date: Sat, 21 Jun 2025 15:54:11 +0530 Subject: [PATCH] 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. --- src/components/home/ThisWeekSection.tsx | 16 +++------------- src/hooks/useLibrary.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/components/home/ThisWeekSection.tsx b/src/components/home/ThisWeekSection.tsx index 30ebfe4c..d83f5cf1 100644 --- a/src/components/home/ThisWeekSection.tsx +++ b/src/components/home/ThisWeekSection.tsx @@ -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 diff --git a/src/hooks/useLibrary.ts b/src/hooks/useLibrary.ts index 0f8e656e..51643591 100644 --- a/src/hooks/useLibrary.ts +++ b/src/hooks/useLibrary.ts @@ -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,