Files
doors-wallpaper/app/(tabs)/index.tsx

115 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-09-24 23:40:27 +01:00
import {
View,
Image,
Text,
TouchableOpacity,
2024-09-25 19:13:53 +01:00
ActivityIndicator,
Alert,
2024-09-24 23:40:27 +01:00
} from "react-native";
2024-09-24 23:40:27 +01:00
import { useEffect, useState } from "react";
2024-09-28 13:16:55 +01:00
import { Ionicons } from "@expo/vector-icons";
2024-09-25 19:13:53 +01:00
import { type ImageAsset } from "@/cloudinary/cloudinary";
2024-09-24 23:40:27 +01:00
import { MasonryFlashList } from "@shopify/flash-list";
import { useRouter } from "expo-router";
import { useStore } from "@/store/store";
export default function HomeScreen() {
2024-09-24 23:40:27 +01:00
const router = useRouter();
const setSelectedWallpaper = useStore((store) => store.setSelectedWallpaper);
const [images, setImages] = useState<ImageAsset[] | null>(null);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
fetchWallpapers();
}, []);
2024-09-28 13:16:55 +01:00
async function fetchWallpapers() {
try {
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 (
2024-09-25 19:13:53 +01:00
<View className="flex-1 items-center justify-center">
<ActivityIndicator />
</View>
2024-09-28 13:16:55 +01:00
);
}
if (images) {
return (
2024-09-25 19:13:53 +01:00
<MasonryFlashList
data={images}
numColumns={2}
ListHeaderComponent={() => (
2024-09-26 23:17:04 +01:00
<Text className="px-4 pt-20 pb-4 text-xl font-bold text-center dark:text-white">
2024-09-25 19:13:53 +01:00
Wallpapers
</Text>
)}
renderItem={({ item }) => (
<View className="w-full p-2">
<View className="w-full relative rounded overflow-hidden">
<TouchableOpacity
onPress={() => {
setSelectedWallpaper(item);
router.push("/wallpaper");
}}
>
<Image
className="w-full h-60"
2024-09-25 19:33:31 +01:00
source={{ uri: item.thumbnail_url }}
2024-09-25 19:13:53 +01:00
/>
</TouchableOpacity>
<View
className="w-full absolute bottom-0 left-0 right-0 p-2"
style={{
backgroundColor: "rgba(0, 0, 0, 0.8)",
}}
>
<Text className="text-white opacity-80">
{item.display_name}
2024-09-25 19:13:53 +01:00
</Text>
</View>
2024-09-25 18:34:58 +01:00
</View>
2024-09-24 23:40:27 +01:00
</View>
2024-09-25 19:13:53 +01:00
)}
estimatedItemSize={200}
/>
2024-09-28 13:16:55 +01:00
);
}
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>;
}