mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-12-19 19:31:54 +03:00
added basic posts refresh model
This commit is contained in:
parent
26cac88539
commit
59d768dade
@ -53,6 +53,7 @@ const PostsContainer = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
let _postFetchTimer = null;
|
||||||
|
|
||||||
const nsfw = useSelector((state) => state.application.nsfw);
|
const nsfw = useSelector((state) => state.application.nsfw);
|
||||||
const initPosts = useSelector((state) => state.posts.initPosts);
|
const initPosts = useSelector((state) => state.posts.initPosts);
|
||||||
@ -85,6 +86,7 @@ const PostsContainer = ({
|
|||||||
);
|
);
|
||||||
const [recommendedUsers, setRecommendedUsers] = useState([]);
|
const [recommendedUsers, setRecommendedUsers] = useState([]);
|
||||||
const [recommendedCommunities, setRecommendedCommunities] = useState([]);
|
const [recommendedCommunities, setRecommendedCommunities] = useState([]);
|
||||||
|
const [showNewPostsPopup, setShowNewPostsPopup] = useState(false);
|
||||||
|
|
||||||
const _resetLocalVoteMap = () => {
|
const _resetLocalVoteMap = () => {
|
||||||
dispatch(resetLocalVoteMap());
|
dispatch(resetLocalVoteMap());
|
||||||
@ -104,6 +106,30 @@ const PostsContainer = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const _scheduleLatestPostsCheck = (firstPost) => {
|
||||||
|
if (_postFetchTimer) {
|
||||||
|
clearTimeout(_postFetchTimer);
|
||||||
|
}
|
||||||
|
if (!firstPost) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentTime = new Date().getTime();
|
||||||
|
const createdAt = new Date(get(firstPost, 'created')).getTime();
|
||||||
|
|
||||||
|
const timeSpent = currentTime - createdAt;
|
||||||
|
// let timeLeft = 3600000 - timeSpent;
|
||||||
|
let timeLeft = 30000 - timeSpent;
|
||||||
|
if (timeLeft < 0) {
|
||||||
|
timeLeft = 30000;
|
||||||
|
}
|
||||||
|
|
||||||
|
_postFetchTimer = setTimeout(() => {
|
||||||
|
const isLatestPostsCheck = true;
|
||||||
|
_loadPosts(null, isLatestPostsCheck);
|
||||||
|
}, timeLeft);
|
||||||
|
};
|
||||||
|
|
||||||
const initCacheState = () => {
|
const initCacheState = () => {
|
||||||
const cachedData = {};
|
const cachedData = {};
|
||||||
|
|
||||||
@ -170,14 +196,15 @@ const PostsContainer = ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//cache latest posts for main tab for returning user
|
//cache latest posts for main tab for returning user
|
||||||
else if (
|
else if (isFeedScreen) {
|
||||||
isFeedScreen &&
|
//schedule refetch of new posts by checking time of current post
|
||||||
filter == (get(currentAccount, 'name', null) == null ? 'hot' : 'friends')
|
_scheduleLatestPostsCheck(nextPosts[0]);
|
||||||
) {
|
|
||||||
_setInitPosts(nextPosts);
|
if (filter == (get(currentAccount, 'name', null) == null ? 'hot' : 'friends')) {
|
||||||
|
_setInitPosts(nextPosts);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//if (!refreshing) {
|
|
||||||
cachedEntry.startAuthor = _posts[_posts.length - 1] && _posts[_posts.length - 1].author;
|
cachedEntry.startAuthor = _posts[_posts.length - 1] && _posts[_posts.length - 1].author;
|
||||||
cachedEntry.startPermlink = _posts[_posts.length - 1] && _posts[_posts.length - 1].permlink;
|
cachedEntry.startPermlink = _posts[_posts.length - 1] && _posts[_posts.length - 1].permlink;
|
||||||
cachedEntry.posts = _posts;
|
cachedEntry.posts = _posts;
|
||||||
@ -221,6 +248,10 @@ const PostsContainer = ({
|
|||||||
const data = state.cachedData[filter !== 'feed' ? filter : state.currentSubFilter];
|
const data = state.cachedData[filter !== 'feed' ? filter : state.currentSubFilter];
|
||||||
_setFeedPosts(data.posts, data.scrollPosition);
|
_setFeedPosts(data.posts, data.scrollPosition);
|
||||||
|
|
||||||
|
if (filter !== 'feed' && isFeedScreen) {
|
||||||
|
_scheduleLatestPostsCheck(data.posts[0]);
|
||||||
|
}
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,6 +262,9 @@ const PostsContainer = ({
|
|||||||
//dispatch to redux;
|
//dispatch to redux;
|
||||||
const data = state.cachedData[filter];
|
const data = state.cachedData[filter];
|
||||||
_setFeedPosts(data.posts, data.scrollPosition);
|
_setFeedPosts(data.posts, data.scrollPosition);
|
||||||
|
if (isFeedScreen) {
|
||||||
|
_scheduleLatestPostsCheck(data.posts[0]);
|
||||||
|
}
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,6 +304,12 @@ const PostsContainer = ({
|
|||||||
} else {
|
} else {
|
||||||
_setFeedPosts([]);
|
_setFeedPosts([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (_postFetchTimer) {
|
||||||
|
clearTimeout(_postFetchTimer);
|
||||||
|
}
|
||||||
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -436,7 +476,18 @@ const PostsContainer = ({
|
|||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
};
|
};
|
||||||
|
|
||||||
const _loadPosts = async (type) => {
|
const _matchFreshPosts = (posts, reducerFilter) => {
|
||||||
|
const cachedPosts = cache.cachedData[reducerFilter].posts;
|
||||||
|
|
||||||
|
const cachedPostId = get(cachedPosts[0], 'post_id');
|
||||||
|
const newPostId = get(posts[0], 'post_id');
|
||||||
|
|
||||||
|
if (cachedPostId !== newPostId) {
|
||||||
|
setShowNewPostsPopup(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const _loadPosts = async (type, isLatestPostCheck = false) => {
|
||||||
const filter = type || selectedFilterValue;
|
const filter = type || selectedFilterValue;
|
||||||
const reducerFilter = filter !== 'feed' ? filter : selectedFeedSubfilterValue;
|
const reducerFilter = filter !== 'feed' ? filter : selectedFeedSubfilterValue;
|
||||||
|
|
||||||
@ -510,7 +561,7 @@ const PostsContainer = ({
|
|||||||
|
|
||||||
const sAuthor = cache.cachedData[reducerFilter].startAuthor;
|
const sAuthor = cache.cachedData[reducerFilter].startAuthor;
|
||||||
const sPermlink = cache.cachedData[reducerFilter].startPermlink;
|
const sPermlink = cache.cachedData[reducerFilter].startPermlink;
|
||||||
if (sAuthor && sPermlink && !refreshing) {
|
if (sAuthor && sPermlink && !refreshing && !isLatestPostCheck) {
|
||||||
options.start_author = sAuthor;
|
options.start_author = sAuthor;
|
||||||
options.start_permlink = sPermlink;
|
options.start_permlink = sPermlink;
|
||||||
}
|
}
|
||||||
@ -531,13 +582,17 @@ const PostsContainer = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (_posts.length > 0) {
|
if (_posts.length > 0) {
|
||||||
cacheDispatch({
|
if (isLatestPostCheck) {
|
||||||
type: 'update-filter-cache',
|
_matchFreshPosts(_posts, reducerFilter);
|
||||||
payload: {
|
} else {
|
||||||
filter: reducerFilter,
|
cacheDispatch({
|
||||||
posts: _posts,
|
type: 'update-filter-cache',
|
||||||
},
|
payload: {
|
||||||
});
|
filter: reducerFilter,
|
||||||
|
posts: _posts,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (result.length === 0) {
|
} else if (result.length === 0) {
|
||||||
setIsNoPost(true);
|
setIsNoPost(true);
|
||||||
@ -758,6 +813,8 @@ const PostsContainer = ({
|
|||||||
followingUsers={followingUsers}
|
followingUsers={followingUsers}
|
||||||
subscribingCommunities={subscribingCommunities}
|
subscribingCommunities={subscribingCommunities}
|
||||||
isFeedScreen={isFeedScreen}
|
isFeedScreen={isFeedScreen}
|
||||||
|
showNewPostsPopup={showNewPostsPopup}
|
||||||
|
setShowNewPostsPopup={setShowNewPostsPopup}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -40,4 +40,28 @@ export default EStyleSheet.create({
|
|||||||
marginVertical: 16,
|
marginVertical: 16,
|
||||||
color: '$primaryBlack',
|
color: '$primaryBlack',
|
||||||
},
|
},
|
||||||
|
popupContainer: {
|
||||||
|
position: 'absolute',
|
||||||
|
top: 80,
|
||||||
|
left: 0,
|
||||||
|
right: 0,
|
||||||
|
alignItems: 'center',
|
||||||
|
},
|
||||||
|
popupContentContainer: {
|
||||||
|
flexDirection: 'row',
|
||||||
|
alignItems: 'center',
|
||||||
|
backgroundColor: '$primaryBlue',
|
||||||
|
paddingHorizontal: 16,
|
||||||
|
paddingVertical: 4,
|
||||||
|
borderRadius: 32,
|
||||||
|
},
|
||||||
|
popupText: {
|
||||||
|
fontWeight: '700',
|
||||||
|
color: '$white',
|
||||||
|
},
|
||||||
|
closeIcon: {
|
||||||
|
color: '$white',
|
||||||
|
margin: 0,
|
||||||
|
marginLeft: 8,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
/* eslint-disable react/jsx-wrap-multilines */
|
/* eslint-disable react/jsx-wrap-multilines */
|
||||||
import React, { useRef, useEffect } from 'react';
|
import React, { useRef, useEffect } from 'react';
|
||||||
import { FlatList, View, ActivityIndicator, RefreshControl, Text } from 'react-native';
|
import {
|
||||||
|
FlatList,
|
||||||
|
View,
|
||||||
|
ActivityIndicator,
|
||||||
|
RefreshControl,
|
||||||
|
Text,
|
||||||
|
TouchableOpacity,
|
||||||
|
Button,
|
||||||
|
} from 'react-native';
|
||||||
import { useIntl } from 'react-intl';
|
import { useIntl } from 'react-intl';
|
||||||
import { withNavigation } from 'react-navigation';
|
import { withNavigation } from 'react-navigation';
|
||||||
import { get } from 'lodash';
|
import { get } from 'lodash';
|
||||||
@ -13,8 +21,10 @@ import {
|
|||||||
NoPost,
|
NoPost,
|
||||||
UserListItem,
|
UserListItem,
|
||||||
CommunityListItem,
|
CommunityListItem,
|
||||||
|
TextWithIcon,
|
||||||
} from '../../basicUIElements';
|
} from '../../basicUIElements';
|
||||||
import { ThemeContainer } from '../../../containers';
|
import { ThemeContainer } from '../../../containers';
|
||||||
|
import { IconButton } from '../../iconButton';
|
||||||
|
|
||||||
// Styles
|
// Styles
|
||||||
import styles from './postsStyles';
|
import styles from './postsStyles';
|
||||||
@ -58,6 +68,8 @@ const PostsView = ({
|
|||||||
followingUsers,
|
followingUsers,
|
||||||
subscribingCommunities,
|
subscribingCommunities,
|
||||||
isFeedScreen,
|
isFeedScreen,
|
||||||
|
showNewPostsPopup,
|
||||||
|
setShowNewPostsPopup,
|
||||||
}) => {
|
}) => {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const postsList = useRef(null);
|
const postsList = useRef(null);
|
||||||
@ -298,6 +310,31 @@ const PostsView = ({
|
|||||||
isFeedScreen={isFeedScreen}
|
isFeedScreen={isFeedScreen}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{showNewPostsPopup && (
|
||||||
|
<View style={styles.popupContainer}>
|
||||||
|
<TouchableOpacity
|
||||||
|
onPress={() => {
|
||||||
|
handleOnRefreshPosts();
|
||||||
|
setShowNewPostsPopup(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<View style={styles.popupContentContainer}>
|
||||||
|
<Text style={styles.popupText}>NEW CONTENT AVAILABLE</Text>
|
||||||
|
|
||||||
|
<IconButton
|
||||||
|
iconStyle={styles.closeIcon}
|
||||||
|
iconType="MaterialCommunityIcons"
|
||||||
|
name="close"
|
||||||
|
onPress={() => {
|
||||||
|
setShowNewPostsPopup(false);
|
||||||
|
}}
|
||||||
|
size={20}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</TouchableOpacity>
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* <FlatList
|
{/* <FlatList
|
||||||
ref={postsList}
|
ref={postsList}
|
||||||
data={posts}
|
data={posts}
|
||||||
|
Loading…
Reference in New Issue
Block a user