From 45f9d63f4487e97a63608d001319afbd1909c8d3 Mon Sep 17 00:00:00 2001 From: natjms Date: Fri, 14 May 2021 15:23:42 -0300 Subject: [PATCH] Track which posts have already been seen and only display latest content --- src/components/pages/feed.js | 64 +++++++++++++++--------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/src/components/pages/feed.js b/src/components/pages/feed.js index 28dac04..76ab077 100644 --- a/src/components/pages/feed.js +++ b/src/components/pages/feed.js @@ -9,39 +9,6 @@ import AsyncStorage from "@react-native-async-storage/async-storage"; import * as requests from "src/requests"; -const TEST_IMAGE = "https://cache.desktopnexus.com/thumbseg/2255/2255124-bigthumbnail.jpg"; - -const TEST_POSTS = [ - { - id: 1, - avatar: TEST_IMAGE, - username: "njms", - replies_count: 3, - favourited: false, - reblogged: false, - content: "Also learning Claire de Lune feels a lot like reading the communist manifesto", - timestamp: 1596745156000, - media_attachments: [ - {url: TEST_IMAGE} - ] - }, - { - id: 2, - avatar: TEST_IMAGE, - username: "njms", - favourited: false, - reblogged: false, - replies_count: 0, - content: "Also learning Claire de Lune feels a lot like reading the communist manifesto", - timestamp: 1596745156000, - media_attachments: [ - { url: "https://college.mayo.edu/media/mccms/content-assets/campus-amp-community/arizona/mayo-clinic-phoenix-arizona-is453080663-hero-mobile.jpg" }, - { url: TEST_IMAGE }, - { url: TEST_IMAGE } - ] - } -]; - const FeedJsx = (props) => { const checkmark = require("assets/eva-icons/checkmark-circle-large.png"); const [state, setState] = useState({ @@ -49,24 +16,45 @@ const FeedJsx = (props) => { }); useEffect(() => { - let accessToken; - let instance; + let accessToken, instance, posts; AsyncStorage .multiGet([ "@user_token", "@user_instance", + "@user_latestPostId", ]) - .then(([tokenPair, instancePair]) => { + .then(([tokenPair, instancePair, latestPair]) => { accessToken = JSON.parse(tokenPair[1]).access_token; instance = instancePair[1]; + // NOTE: This is just a number, but the Pixelfed API will not + // accept query params like ?min_id="123" so it must be parsed + const latest = JSON.parse(latestPair[1]); + const params = { limit: 20 }; + + if (latest) { + // @user_latestPostId will be null the first time the feed + // is opened, so there's no need to specify it here. + params["min_id"] = latest; + } return requests.fetchHomeTimeline( instance, - accessToken + accessToken, + params ) }) - .then(posts => + .then(retrievedPosts => { + posts = retrievedPosts; + if(posts.length > 0) { + const latestId = posts[0].id; + return AsyncStorage.setItem( + "@user_latestPostId", + JSON.stringify(latestId) + ); + } + }) + .then(() => setState({...state, posts: posts, loaded: true,