feat: add retry button

This commit is contained in:
2024-09-28 13:16:55 +01:00
parent 8eb7a9c44f
commit 8b04723cce
2 changed files with 48 additions and 43 deletions

View File

@@ -8,6 +8,7 @@ import {
} from "react-native"; } from "react-native";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Ionicons } from "@expo/vector-icons";
import { type ImageAsset } from "@/cloudinary/cloudinary"; import { type ImageAsset } from "@/cloudinary/cloudinary";
import { MasonryFlashList } from "@shopify/flash-list"; import { MasonryFlashList } from "@shopify/flash-list";
import { useRouter } from "expo-router"; import { useRouter } from "expo-router";
@@ -20,33 +21,39 @@ export default function HomeScreen() {
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
useEffect(() => { useEffect(() => {
async function fetchWallpapers() {
try {
setIsLoading(true);
const res = await fetch(
`${process.env.EXPO_PUBLIC_API_URL}/wallpapers`,
);
const images: ImageAsset[] = await res.json();
setImages(images);
setIsLoading(false);
} catch (err) {
console.log(err);
Alert.alert(
"Unable to fetch wallpaper",
"Check your internet connection. Otherwise, it's probably our fault.",
);
}
}
fetchWallpapers(); fetchWallpapers();
}, []); }, []);
return ( async function fetchWallpapers() {
<View className="flex-1"> try {
{isLoading ? ( setIsLoading(true);
const res = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/wallpapers`);
if (res.status !== 200) {
throw new Error("server returned error response");
}
const images: ImageAsset[] = await res.json();
setImages(images);
} catch (err) {
Alert.alert(
"Unable to fetch wallpaper",
"Check your internet connection. Otherwise, it's probably our fault.",
);
} finally {
setIsLoading(false);
}
}
function content() {
if (isLoading) {
return (
<View className="flex-1 items-center justify-center"> <View className="flex-1 items-center justify-center">
<ActivityIndicator /> <ActivityIndicator />
</View> </View>
) : ( );
}
if (images) {
return (
<MasonryFlashList <MasonryFlashList
data={images} data={images}
numColumns={2} numColumns={2}
@@ -84,7 +91,24 @@ export default function HomeScreen() {
)} )}
estimatedItemSize={200} estimatedItemSize={200}
/> />
)} );
</View> }
);
return (
<View className="flex-1 items-center justify-center">
<TouchableOpacity
onPress={() => {
fetchWallpapers();
}}
>
<View className="flex-col items-center justify-center space-y-2">
<Ionicons name="refresh" size={24} color="white" />
<Text className="text-white">Reload</Text>
</View>
</TouchableOpacity>
</View>
);
}
return <View className="flex-1">{content()}</View>;
} }

View File

@@ -3,32 +3,13 @@ import {
DefaultTheme, DefaultTheme,
ThemeProvider, ThemeProvider,
} from "@react-navigation/native"; } from "@react-navigation/native";
import { useFonts } from "expo-font";
import { Stack } from "expo-router"; import { Stack } from "expo-router";
import * as SplashScreen from "expo-splash-screen";
import { useEffect } from "react";
import "react-native-reanimated"; import "react-native-reanimated";
import { useColorScheme } from "@/hooks/useColorScheme"; import { useColorScheme } from "@/hooks/useColorScheme";
// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync();
export default function RootLayout() { export default function RootLayout() {
const colorScheme = useColorScheme(); const colorScheme = useColorScheme();
const [loaded] = useFonts({
SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"),
});
useEffect(() => {
if (loaded) {
SplashScreen.hideAsync();
}
}, [loaded]);
if (!loaded) {
return null;
}
return ( return (
<ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}> <ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>