Merge pull request #2765 from ecency/nt/latest-wave-check

Nt/latest wave check
This commit is contained in:
Feruz M 2023-09-28 06:59:43 +03:00 committed by GitHub
commit 4bf72abbdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 8 deletions

View File

@ -1,5 +1,4 @@
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
import { Alert } from 'react-native'
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
// Actions // Actions
@ -18,7 +17,6 @@ const SideMenuContainer = ({ navigation }) => {
const drawerStatus = useDrawerStatus(); const drawerStatus = useDrawerStatus();
const isLoggedIn = useSelector((state) => state.application.isLoggedIn); const isLoggedIn = useSelector((state) => state.application.isLoggedIn);
const currentAccount = useSelector((state) => state.account.currentAccount); const currentAccount = useSelector((state) => state.account.currentAccount);
const isVisibleAccountsBottomSheet = useSelector( const isVisibleAccountsBottomSheet = useSelector(

View File

@ -221,17 +221,55 @@ export const useWavesQuery = (host: string) => {
const _data = unionBy(...wavesQueries.map((query) => query.data), 'url'); const _data = unionBy(...wavesQueries.map((query) => query.data), 'url');
const _filteredData = useMemo(() => const _filteredData = useMemo(() =>
_data.filter(post => isArray(mutes) ? mutes.indexOf(post?.author) < 0 : true), _data.filter(post => isArray(mutes) ? mutes.indexOf(post?.author) < 0 : true),
[mutes, _data]) [mutes, _data])
const _lastestWavesFetch = async () => {
await _fetchPermlinks('', true);
const _prevLatestWave = _filteredData[0]
const _firstQuery = wavesQueries[0];
if(!_firstQuery){
return [];
}
const queryResponse = await _firstQuery.refetch();
const _newData:any[] = queryResponse.data || [];
//check if new waves are available
const _lastIndex = _newData?.findIndex(item =>
( item.author + item.permlink === _prevLatestWave.author + _prevLatestWave.permlink));
let _newWaves:any[] = []
if (_lastIndex && _lastIndex !== 0) {
if (_lastIndex < 0) {
_newWaves = _newData?.slice(0, 5) || [];
} else {
_newWaves = _newData?.slice(0, _lastIndex) || [];
}
}
return _newWaves
}
return { return {
data: _filteredData, data: _filteredData,
isRefreshing, isRefreshing,
isLoading: isLoading || wavesQueries.lastItem?.isLoading || wavesQueries.lastItem?.isFetching, isLoading: isLoading || wavesQueries.lastItem?.isLoading || wavesQueries.lastItem?.isFetching,
fetchNextPage: _fetchNextPage, fetchNextPage: _fetchNextPage,
latestWavesFetch: _lastestWavesFetch,
refresh: _refresh, refresh: _refresh,
}; };
}; };

View File

@ -1,5 +1,5 @@
import React, { useRef, useState } from 'react'; import React, { useEffect, useRef, useState } from 'react';
import { ActivityIndicator, NativeScrollEvent, NativeSyntheticEvent, RefreshControl, View, FlatList } from 'react-native'; import { ActivityIndicator, NativeScrollEvent, NativeSyntheticEvent, RefreshControl, View, FlatList, AppState, Alert } from 'react-native';
import { Comments, EmptyScreen, Header, PostOptionsModal } from '../../../components'; import { Comments, EmptyScreen, Header, PostOptionsModal } from '../../../components';
import styles from '../styles/wavesScreen.styles'; import styles from '../styles/wavesScreen.styles';
import { wavesQueries } from '../../../providers/queries'; import { wavesQueries } from '../../../providers/queries';
@ -8,6 +8,7 @@ import WavesHeader from '../children/wavesHeader';
import { PostTypes } from '../../../constants/postTypes'; import { PostTypes } from '../../../constants/postTypes';
import ScrollTopPopup from '../../../components/tabbedPosts/view/scrollTopPopup'; import ScrollTopPopup from '../../../components/tabbedPosts/view/scrollTopPopup';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import reactotron from 'reactotron-react-native';
const SCROLL_POPUP_THRESHOLD = 5000; const SCROLL_POPUP_THRESHOLD = 5000;
@ -19,14 +20,41 @@ const WavesScreen = () => {
const postsListRef = useRef<FlatList>(); const postsListRef = useRef<FlatList>();
const blockPopupRef = useRef(false); const blockPopupRef = useRef(false);
const scrollOffsetRef = useRef(0); const scrollOffsetRef = useRef(0);
const appState = useRef(AppState.currentState);
const wavesQuery = wavesQueries.useWavesQuery('ecency.waves'); const wavesQuery = wavesQueries.useWavesQuery('ecency.waves');
const isDarkTheme = useAppSelector(state => state.application.isDarkTheme) const isDarkTheme = useAppSelector(state => state.application.isDarkTheme)
const [enableScrollTop, setEnableScrollTop] = useState(false); const [enableScrollTop, setEnableScrollTop] = useState(false);
const [popupAvatars, setPopupAvatars] = useState<any[]>([])
useEffect(() => {
const _stateSub = AppState.addEventListener('change', _handleAppStateChange);
return () => {
_stateSub.remove();
}
}, [])
//actions
const _handleAppStateChange = async (nextAppState) => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
const latestWaves = await wavesQuery.latestWavesFetch()
if (latestWaves.length > 0) {
setPopupAvatars(latestWaves.map((item) => item.avatar))
setEnableScrollTop(true)
}
}
appState.current = nextAppState;
};
const _fetchData = ({ refresh }: { refresh?: boolean }) => { const _fetchData = ({ refresh }: { refresh?: boolean }) => {
if (refresh) { if (refresh) {
wavesQuery.refresh(); wavesQuery.refresh();
@ -38,8 +66,9 @@ const WavesScreen = () => {
//scrolls to top, blocks scroll popup for 2 seconds to reappear after scroll //scrolls to top, blocks scroll popup for 2 seconds to reappear after scroll
const _scrollTop = () => { const _scrollTop = () => {
if (postsListRef.current) { if (postsListRef.current) {
postsListRef.current.scrollToOffset({offset:0}); postsListRef.current.scrollToOffset({ offset: 0 });
setEnableScrollTop(false); setEnableScrollTop(false);
setPopupAvatars([])
scrollPopupDebouce.cancel(); scrollPopupDebouce.cancel();
blockPopupRef.current = true; blockPopupRef.current = true;
setTimeout(() => { setTimeout(() => {
@ -115,11 +144,12 @@ const WavesScreen = () => {
}} }}
/> />
<ScrollTopPopup <ScrollTopPopup
popupAvatars={[]} popupAvatars={popupAvatars}
enableScrollTop={enableScrollTop} enableScrollTop={enableScrollTop}
onPress={_scrollTop} onPress={_scrollTop}
onClose={() => { onClose={() => {
setEnableScrollTop(false); setEnableScrollTop(false);
setPopupAvatars([])
}} }}
/> />
</View> </View>