memorizing scrolls

This commit is contained in:
Nouman Tahir 2021-03-08 18:57:58 +05:00
parent b9384eb829
commit 1e545d7545
5 changed files with 79 additions and 19 deletions

View File

@ -86,11 +86,11 @@ const PostsContainer = ({
const [recommendedUsers, setRecommendedUsers] = useState([]);
const [recommendedCommunities, setRecommendedCommunities] = useState([]);
const _setFeedPosts = (_posts) => {
const _setFeedPosts = (_posts, scrollPos = 0) => {
if (isFeedScreen) {
dispatch(setFeedPosts(_posts));
dispatch(setFeedPosts(_posts, scrollPos));
} else {
dispatch(setOtherPosts(_posts));
dispatch(setOtherPosts(_posts, scrollPos));
}
};
@ -110,6 +110,7 @@ const PostsContainer = ({
startAuthor: '',
startPermlink: '',
isLoading: false,
scrollPosition: 0,
};
}
});
@ -213,7 +214,8 @@ const PostsContainer = ({
state.currentFilter = filter;
console.log('New state:', state);
_setFeedPosts(state.cachedData[filter !== 'feed' ? filter : state.currentSubFilter].posts);
const data = state.cachedData[filter !== 'feed' ? filter : state.currentSubFilter];
_setFeedPosts(data.posts, data.scrollPosition);
return state;
}
@ -223,7 +225,19 @@ const PostsContainer = ({
state.currentSubFilter = filter;
console.log('New state:', state);
//dispatch to redux;
_setFeedPosts(state.cachedData[filter].posts);
const data = state.cachedData[filter];
_setFeedPosts(data.posts, data.scrollPosition);
return state;
}
case 'scroll-position-change': {
const scrollPosition = action.payload.scrollPosition || 0;
const filter = state.currentFilter;
const subFilter = state.currentSubFilter;
const cacheFilter = filter !== 'feed' ? filter : subFilter;
state.cachedData[cacheFilter].scrollPosition = scrollPosition;
return state;
}
@ -697,12 +711,30 @@ const PostsContainer = ({
);
};
const _handleOnScroll = (event) => {
if (handleOnScroll) {
handleOnScroll();
}
//memorize filter position
const scrollPosition = event.nativeEvent.contentOffset.y;
if (scrollPosition == 0) {
Alert.alert('Scroll positionsing failed', JSON.stringify(event));
}
cacheDispatch({
type: 'scroll-position-change',
payload: {
scrollPosition,
},
});
};
return (
<PostsView
ref={elem}
filterOptions={filterOptions}
handleImagesHide={_handleImagesHide}
handleOnScroll={handleOnScroll}
handleOnScroll={_handleOnScroll}
isHideImage={isHideImages}
isLoggedIn={isLoggedIn}
isAnalytics={isAnalytics}

View File

@ -238,12 +238,6 @@ const PostsView = ({
);
};
const _handleOnScroll = () => {
if (handleOnScroll) {
handleOnScroll();
}
};
const _scrollTop = () => {
postsList.current.scrollToTop();
};
@ -330,7 +324,7 @@ const PostsView = ({
onRefresh={handleOnRefreshPosts}
onEndReachedThreshold={1}
ListFooterComponent={_renderFooter}
onScrollEndDrag={_handleOnScroll}
onScrollEndDrag={handleOnScroll}
ListEmptyComponent={_renderEmptyContent}
refreshControl={
<RefreshControl

View File

@ -24,12 +24,38 @@ const postsListContainer = ({
: state.posts.otherPosts
});
const scrollPosition = useSelector((state) => {
return isFeedScreen
? state.posts.feedScrollPosition
: state.posts.otherScrollPosition
});
useImperativeHandle(ref, () => ({
scrollToTop() {
flatListRef.current?.scrollToOffset({ x: 0, y: 0, animated: true });
},
}));
useEffect(() => {
console.log("Scroll Position: ", scrollPosition)
if(posts.length == 0){
flatListRef.current?.scrollToOffset({
offset: 0,
animated: false
});
}
}, [posts])
useEffect(() => {
console.log("Scroll Position: ", scrollPosition)
flatListRef.current?.scrollToOffset({
offset: posts.length == 0?0:scrollPosition,
animated: false
});
}, [scrollPosition])
const _renderItem = ({ item, index }:{item:any, index:number}) => {
const e = [];
if (index % 3 === 0) {

View File

@ -8,16 +8,22 @@ import {
SET_INIT_POSTS,
} from '../constants/constants';
export const setFeedPosts = (payload) => ({
payload,
export const setFeedPosts = (posts, scrollPosition = 0) => ({
payload: {
posts,
scrollPosition,
},
type: SET_FEED_POSTS,
});
export const setInitPosts = (payload) => ({
payload,
type: SET_INIT_POSTS,
});
export const setOtherPosts = (payload) => ({
payload,
export const setOtherPosts = (posts, scrollPosition = 0) => ({
payload: {
posts,
scrollPosition,
},
type: SET_OTHER_POSTS,
});
export const fetchPosts = (payload) => ({

View File

@ -22,7 +22,8 @@ export default function (state = initialState, action) {
case SET_FEED_POSTS:
return {
...state,
feedPosts: action.payload,
feedPosts: action.payload.posts,
feedScrollPosition: action.payload.scrollPosition,
posts: action.payload,
};
case SET_INIT_POSTS:
@ -33,7 +34,8 @@ export default function (state = initialState, action) {
case SET_OTHER_POSTS:
return {
...state,
otherPosts: action.payload,
otherPosts: action.payload.posts,
otherScrollPosition: action.payload.scrollPosition,
posts: action.payload,
};
case FILTER_SELECTED: {