From bbc36ebc2c7b74867e6079cb5a3b4b48b8f5481d Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Sat, 10 Apr 2021 00:04:03 +0500 Subject: [PATCH] minor fixes --- .../services/tabbedPostsReducer.ts | 2 +- .../tabbedPosts/view/tabContent.tsx | 51 ++++++++++++++----- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/components/tabbedPosts/services/tabbedPostsReducer.ts b/src/components/tabbedPosts/services/tabbedPostsReducer.ts index dc9f445cd..b066c5959 100644 --- a/src/components/tabbedPosts/services/tabbedPostsReducer.ts +++ b/src/components/tabbedPosts/services/tabbedPostsReducer.ts @@ -55,7 +55,7 @@ export const onLoadComplete = (filter:string) => ({ type:CacheActions.ON_LOAD_COMPLETE }) -export const calculateTimeLeftForPostCheck = (firstPost) => { +export const calculateTimeLeftForPostCheck = (firstPost:any) => { const refetchTime = 600000; //schedules refresh 30 minutes after last post creation time diff --git a/src/components/tabbedPosts/view/tabContent.tsx b/src/components/tabbedPosts/view/tabContent.tsx index e93fe12ae..921087e3e 100644 --- a/src/components/tabbedPosts/view/tabContent.tsx +++ b/src/components/tabbedPosts/view/tabContent.tsx @@ -7,6 +7,7 @@ import TabEmptyView from './listEmptyView'; import { setInitPosts } from '../../../redux/actions/postsAction'; import NewPostsPopup from './newPostsPopup'; import { calculateTimeLeftForPostCheck } from '../services/tabbedPostsReducer'; +import { AppState } from 'react-native'; const TabContent = ({ @@ -49,17 +50,23 @@ const TabContent = ({ //refs let postsListRef = useRef() + const appState = useRef(AppState.currentState); const postsRef = useRef(posts); postsRef.current = posts; + //side effects useEffect(() => { - _initContent(initPosts); - return () => { - _isMounted = false; + + if (isFeedScreen) { + AppState.addEventListener('change', _handleAppStateChange); + _initContent(true); } + + return _cleanup; }, []) + useEffect(()=>{ if(isConnected && (username !== sessionUser || forceLoadPosts)){ if(filterKey !== 'friends'){ @@ -68,14 +75,34 @@ const TabContent = ({ setPosts([]) } } - },[username, forceLoadPosts]) + }, [username, forceLoadPosts]) + const _cleanup = () => { + _isMounted = false; + if(_postFetchTimer){ + clearTimeout(_postFetchTimer); + } + if (isFeedScreen) { + AppState.removeEventListener('change', _handleAppStateChange); + } + } + //actions - const _initContent = (_initPosts:any[] = []) => { - setPosts(_initPosts); + const _handleAppStateChange = (nextAppState) => { + if (appState.current.match(/inactive|background/) && nextAppState === 'active') { + const isLatestPostsCheck = true; + _loadPosts(false, isLatestPostsCheck); + } + + appState.current = nextAppState; + }; + + + const _initContent = (isFirstCall = false) => { + setPosts(initPosts || []); setTabMeta({ startAuthor:'', startPermlink:'', @@ -85,7 +112,7 @@ const TabContent = ({ setSessionUser(username); if(username || (filterKey !== 'friends' && filterKey !== 'communities')){ - _loadPosts(true); + _loadPosts(!isFirstCall); _getPromotedPosts(); } } @@ -133,12 +160,12 @@ const TabContent = ({ //schedules post fetch const _scheduleLatestPostsCheck = (firstPost:any) => { - if (_postFetchTimer) { - clearTimeout(_postFetchTimer); - } - - const timeLeft = calculateTimeLeftForPostCheck(firstPost) if (firstPost) { + if (_postFetchTimer) { + clearTimeout(_postFetchTimer); + } + + const timeLeft = calculateTimeLeftForPostCheck(firstPost) _postFetchTimer = setTimeout(() => { const isLatestPostsCheck = true; _loadPosts(false, isLatestPostsCheck);