From a663f58502ceadfc43cc64c1447f41fc5e5d907f Mon Sep 17 00:00:00 2001 From: tapframe Date: Thu, 25 Sep 2025 23:13:09 +0530 Subject: [PATCH] account loading.. fix --- src/contexts/AccountContext.tsx | 22 +++++++++++++++------- src/screens/SettingsScreen.tsx | 23 +++++++++++++---------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/contexts/AccountContext.tsx b/src/contexts/AccountContext.tsx index d71676c..0359d3e 100644 --- a/src/contexts/AccountContext.tsx +++ b/src/contexts/AccountContext.tsx @@ -94,27 +94,35 @@ export const AccountProvider: React.FC<{ children: React.ReactNode }> = ({ child }, refreshCurrentUser: async () => { // Don't set loading if already loading to avoid conflicts - if (loading) return; - + if (loading) { + if (__DEV__) console.log('[AccountContext] Already loading, skipping refresh'); + return; + } + + if (__DEV__) console.log('[AccountContext] Starting refreshCurrentUser'); setLoading(true); - + // Set a timeout to prevent loading from getting stuck loadingTimeoutRef.current = setTimeout(() => { - console.warn('Account loading timeout, forcing loading to false'); + console.warn('[AccountContext] Account loading timeout, forcing loading to false'); setLoading(false); - }, 10000); // 10 second timeout - + }, 5000); // Reduced to 5 seconds for faster fallback + try { const u = await accountService.getCurrentUser(); + if (__DEV__) console.log('[AccountContext] refreshCurrentUser completed:', u ? 'user found' : 'no user'); setUser(u); } catch (error) { - console.error('Failed to refresh current user:', error); + console.error('[AccountContext] Failed to refresh current user:', error); + // Still set user to null on error to ensure we don't get stuck + setUser(null); } finally { if (loadingTimeoutRef.current) { clearTimeout(loadingTimeoutRef.current); loadingTimeoutRef.current = null; } setLoading(false); + if (__DEV__) console.log('[AccountContext] refreshCurrentUser finished'); } }, updateProfile: async (partial) => { diff --git a/src/screens/SettingsScreen.tsx b/src/screens/SettingsScreen.tsx index b5b0aed..506ca53 100644 --- a/src/screens/SettingsScreen.tsx +++ b/src/screens/SettingsScreen.tsx @@ -264,7 +264,14 @@ const SettingsScreen: React.FC = () => { // Tablet-specific state const [selectedCategory, setSelectedCategory] = useState('account'); - + + // States for dynamic content + const [addonCount, setAddonCount] = useState(0); + const [catalogCount, setCatalogCount] = useState(0); + const [mdblistKeySet, setMdblistKeySet] = useState(false); + const [openRouterKeySet, setOpenRouterKeySet] = useState(false); + const [initialLoadComplete, setInitialLoadComplete] = useState(false); + // Add a useEffect to check authentication status on focus useEffect(() => { // This will reload the Trakt auth status whenever the settings screen is focused @@ -277,26 +284,22 @@ const SettingsScreen: React.FC = () => { if (__DEV__) console.log('SettingsScreen focused, refreshing auth status. Current state:', { isAuthenticated, userProfile: userProfile?.username }); } refreshAuthStatus(); - // Only refresh account user if we don't have a user or if we're not already loading - if (!user && !accountLoading) { + // Don't refresh account user on every focus - the context handles auth state changes + // Only refresh if we have no user at all and aren't loading (rare edge case) + if (!user && !accountLoading && !initialLoadComplete) { refreshCurrentUser(); } }); return unsubscribe; - }, [navigation, isAuthenticated, userProfile, refreshAuthStatus, refreshCurrentUser, user, accountLoading]); - - // States for dynamic content - const [addonCount, setAddonCount] = useState(0); - const [catalogCount, setCatalogCount] = useState(0); - const [mdblistKeySet, setMdblistKeySet] = useState(false); - const [openRouterKeySet, setOpenRouterKeySet] = useState(false); + }, [navigation, isAuthenticated, userProfile, refreshAuthStatus, refreshCurrentUser, user, accountLoading, initialLoadComplete]); const loadData = useCallback(async () => { try { // Load addon count and get their catalogs const addons = await stremioService.getInstalledAddonsAsync(); setAddonCount(addons.length); + setInitialLoadComplete(true); // Count total available catalogs let totalCatalogs = 0;