Enable fetching public and federated timelines in Discover

This commit is contained in:
Nat 2021-05-18 17:33:40 -03:00
parent 3ef5d1124d
commit f0c422681e
3 changed files with 125 additions and 27 deletions

View File

@ -4,16 +4,28 @@ import { View, TextInput, Text, Dimensions } from "react-native";
import { TabView, TabBar, SceneMap } from "react-native-tab-view"; import { TabView, TabBar, SceneMap } from "react-native-tab-view";
import { Ionicons } from "@expo/vector-icons"; import { Ionicons } from "@expo/vector-icons";
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as requests from "src/requests";
import PagedGridJsx from "src/components/posts/paged-grid"; import PagedGridJsx from "src/components/posts/paged-grid";
import { ScreenWithTrayJsx } from "src/components/navigation/navigators"; import { ScreenWithTrayJsx } from "src/components/navigation/navigators";
import { TouchableWithoutFeedback } from "react-native-gesture-handler"; import { TouchableWithoutFeedback } from "react-native-gesture-handler";
const DiscoverJsx = (props) => { const DiscoverJsx = (props) => {
const POST_FETCH_PARAMS = {
only_media: true,
limit: 18,
};
const [ state, setState ] = useState({
loaded: false,
});
const [ index, setIndex ] = useState(0); const [ index, setIndex ] = useState(0);
const [ routes ] = useState([ const [ routes ] = useState([
{ {
key: "home", key: "local",
icon: "md-home", icon: "md-home",
}, },
{ {
@ -22,20 +34,92 @@ const DiscoverJsx = (props) => {
}, },
]); ]);
const HomeTimeline = () => ( useEffect(() => {
let instance, accessToken;
AsyncStorage.
multiGet([
"@user_instance",
"@user_token",
])
.then(([instancePair, tokenPair]) => {
instance = instancePair[1];
accessToken = JSON.parse(tokenPair[1]).access_token;
return Promise.all([
requests.fetchPublicTimeline(
instance,
accessToken,
{ ...POST_FETCH_PARAMS, local: true, }
),
requests.fetchPublicTimeline(
instance,
accessToken,
{ ...POST_FETCH_PARAMS, remote: true, }
)
]);
})
.then(([localPosts, federatedPosts]) => {
setState({...state,
localPosts,
federatedPosts,
instance,
accessToken,
loaded: true,
});
})
}, []);
const _handleLocalTabUpdate = async () => {
const newPosts = await requests.fetchPublicTimeline(
state.instance,
state.accessToken,
{
...POST_FETCH_PARAMS,
local: true,
max_id: state.localPosts[state.localPosts.length - 1].id
}
);
setState({...state,
localPosts: state.localPosts.concat(newPosts),
});
};
const _handleFederatedTabUpdate = async () => {
const lastId = state.federatedPosts[state.federatedPosts.length - 1].id
const newPosts = await requests.fetchPublicTimeline(
state.instance,
state.accessToken,
{
...POST_FETCH_PARAMS,
remote: true,
max_id: lastId,
}
);
setState({...state,
federatedPosts: state.federatedPosts.concat(newPosts),
});
};
const LocalTimeline = () => (
<PagedGridJsx <PagedGridJsx
navigation = { props.navigation } navigation = { props.navigation }
posts = { state.localPosts }
onShowMore = { _handleLocalTabUpdate }
originTab = "Discover" /> originTab = "Discover" />
); );
const FederatedTimeline = () => ( const FederatedTimeline = () => (
<PagedGridJsx <PagedGridJsx
navigation = { props.navigation } navigation = { props.navigation }
posts = { state.federatedPosts }
onShowMore = { _handleFederatedTabUpdate }
originTab = "Discover" /> originTab = "Discover" />
); );
const renderScene = SceneMap({ const renderScene = SceneMap({
home: HomeTimeline, local: LocalTimeline,
federated: FederatedTimeline, federated: FederatedTimeline,
}); });
@ -57,7 +141,9 @@ const DiscoverJsx = (props) => {
); );
return ( return (
<ScreenWithTrayJsx <>
{ state.loaded
? <ScreenWithTrayJsx
active = "Discover" active = "Discover"
navigation = { props.navigation } navigation = { props.navigation }
statusBarColor = "white"> statusBarColor = "white">
@ -78,6 +164,9 @@ const DiscoverJsx = (props) => {
onIndexChange = { setIndex } onIndexChange = { setIndex }
initialLayout = { { width: SCREEN_WIDTH } } /> initialLayout = { { width: SCREEN_WIDTH } } />
</ScreenWithTrayJsx> </ScreenWithTrayJsx>
: <></>
}
</>
); );
}; };

View File

@ -78,7 +78,7 @@ export const RawPostJsx = (props) => {
style = { styles.pfp } style = { styles.pfp }
source = { { uri: props.data.account.avatar } } /> source = { { uri: props.data.account.avatar } } />
<Text style = { styles.postHeaderName }> <Text style = { styles.postHeaderName }>
{ props.data.account.username } { props.data.account.acct }
</Text> </Text>
<ModerateMenuJsx <ModerateMenuJsx
containerStyle = { styles.menu } containerStyle = { styles.menu }

View File

@ -99,3 +99,12 @@ export async function fetchHomeTimeline(domain, token, params = false) {
); );
return resp.json(); return resp.json();
} }
export async function fetchPublicTimeline(domain, token, params = false) {
const resp = await get(
`https://${domain}/api/v1/timelines/public`,
token,
params
);
return resp.json();
}