2024-09-24 23:40:27 +01:00
|
|
|
import { useStore } from "@/store/store";
|
|
|
|
import * as FileSystem from "expo-file-system";
|
|
|
|
import * as MediaLibrary from "expo-media-library";
|
2024-09-25 19:33:45 +01:00
|
|
|
import * as Linking from "expo-linking";
|
2024-09-24 23:40:27 +01:00
|
|
|
import { Ionicons } from "@expo/vector-icons";
|
|
|
|
import { useRouter } from "expo-router";
|
|
|
|
import { Image, Text, View, TouchableOpacity, Alert } from "react-native";
|
2024-09-25 19:27:59 +01:00
|
|
|
import { useState } from "react";
|
2024-09-28 10:56:20 +01:00
|
|
|
import { SafeAreaView } from "react-native-safe-area-context";
|
2024-09-24 23:40:27 +01:00
|
|
|
|
|
|
|
export default function WallpaperPage() {
|
|
|
|
const selectedWallpaper = useStore((store) => store.selectedWallpaper);
|
2024-09-25 19:27:59 +01:00
|
|
|
const [isDownloading, setIsDownloading] = useState(false);
|
2024-09-24 23:40:27 +01:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
if (!selectedWallpaper) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-09-25 20:20:44 +01:00
|
|
|
const imageName = selectedWallpaper.display_name;
|
2024-09-24 23:40:27 +01:00
|
|
|
|
|
|
|
async function downloadWallpaper() {
|
|
|
|
if (!selectedWallpaper) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-25 19:27:59 +01:00
|
|
|
setIsDownloading(true);
|
|
|
|
|
|
|
|
try {
|
2024-09-29 12:02:41 +01:00
|
|
|
const permStatus = await MediaLibrary.requestPermissionsAsync(true, [
|
|
|
|
"photo",
|
|
|
|
]);
|
2024-09-25 19:27:59 +01:00
|
|
|
if (permStatus.status != MediaLibrary.PermissionStatus.GRANTED) {
|
|
|
|
Alert.alert(
|
|
|
|
"Media library access required",
|
|
|
|
"Doors need access to your media library in order to save wallpapers.",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await FileSystem.downloadAsync(
|
|
|
|
selectedWallpaper.secure_url,
|
|
|
|
`${FileSystem.documentDirectory}${selectedWallpaper.asset_id}.${selectedWallpaper.format}`,
|
|
|
|
);
|
|
|
|
await MediaLibrary.createAssetAsync(result.uri);
|
2024-09-24 23:40:27 +01:00
|
|
|
Alert.alert(
|
2024-09-25 19:27:59 +01:00
|
|
|
"Wallpaper saved successfully",
|
|
|
|
"You can change the wallpaper in Settings.",
|
2024-09-24 23:40:27 +01:00
|
|
|
);
|
2024-09-25 19:27:59 +01:00
|
|
|
} catch {
|
|
|
|
Alert.alert(
|
|
|
|
"Unable to download wallpaper",
|
|
|
|
"Check your internet connection. Otherwise, it's probably our fault.",
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
setIsDownloading(false);
|
2024-09-24 23:40:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-25 19:33:45 +01:00
|
|
|
async function openSourceUrl() {
|
|
|
|
if (selectedWallpaper?.source_url) {
|
|
|
|
Linking.openURL(selectedWallpaper.source_url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-24 23:40:27 +01:00
|
|
|
return (
|
2024-09-25 18:34:58 +01:00
|
|
|
<View className="flex-1 w-full h-full">
|
2024-09-24 23:40:27 +01:00
|
|
|
<Image
|
2024-09-25 18:34:58 +01:00
|
|
|
className="flex-1 w-full h-full"
|
2024-09-24 23:40:27 +01:00
|
|
|
source={{ uri: selectedWallpaper.secure_url }}
|
|
|
|
/>
|
2024-09-28 10:56:20 +01:00
|
|
|
<SafeAreaView className="absolute top-0 left-0 right-0 flex-row px-4">
|
2024-09-24 23:40:27 +01:00
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => {
|
|
|
|
router.dismiss();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<View
|
2024-09-25 18:34:58 +01:00
|
|
|
className="w-8 h-8 rounded-full items-center justify-center"
|
|
|
|
style={{ backgroundColor: "rgba(0, 0, 0, 0.8)", padding: 5 }}
|
2024-09-24 23:40:27 +01:00
|
|
|
>
|
|
|
|
<Ionicons size={24} name="close" color="white" />
|
|
|
|
</View>
|
|
|
|
</TouchableOpacity>
|
2024-09-28 10:56:20 +01:00
|
|
|
</SafeAreaView>
|
2024-09-25 18:34:58 +01:00
|
|
|
<View className="absolute bottom-0 left-0 right-0 h-1/3 w-full bg-neutral-800 justify-between items-center p-8">
|
2024-09-25 19:23:57 +01:00
|
|
|
<View className="flex-col justify-center items-center">
|
2024-10-01 17:40:47 +01:00
|
|
|
<Text
|
|
|
|
numberOfLines={1}
|
|
|
|
className="font-bold text-2xl text-white mb-2"
|
|
|
|
>
|
2024-09-25 19:23:57 +01:00
|
|
|
{imageName}
|
|
|
|
</Text>
|
|
|
|
{selectedWallpaper.creator_name ? (
|
|
|
|
<Text className="text-white text-center opacity-50 text-xs">
|
|
|
|
{selectedWallpaper.creator_name}
|
2024-09-25 21:40:59 +01:00
|
|
|
{selectedWallpaper.is_ai_generated ? " · AI Generated" : ""}
|
2024-09-25 19:23:57 +01:00
|
|
|
</Text>
|
|
|
|
) : null}
|
|
|
|
{selectedWallpaper.source_url ? (
|
2024-09-25 19:33:45 +01:00
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => {
|
|
|
|
openSourceUrl();
|
|
|
|
}}
|
|
|
|
>
|
2024-09-25 19:23:57 +01:00
|
|
|
<Text className="text-white underline text-center opacity-50 text-xs">
|
|
|
|
{selectedWallpaper.source_url}
|
|
|
|
</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
) : null}
|
|
|
|
</View>
|
2024-09-24 23:40:27 +01:00
|
|
|
<TouchableOpacity
|
2024-09-25 19:27:59 +01:00
|
|
|
disabled={isDownloading}
|
2024-09-25 18:34:58 +01:00
|
|
|
className="w-full"
|
2024-09-24 23:40:27 +01:00
|
|
|
onPress={() => {
|
2024-09-25 19:27:59 +01:00
|
|
|
if (!isDownloading) {
|
|
|
|
downloadWallpaper();
|
|
|
|
}
|
2024-09-24 23:40:27 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<View
|
2024-09-25 19:23:57 +01:00
|
|
|
className="w-full rounded justify-center items-center px-4 py-1"
|
2024-09-24 23:40:27 +01:00
|
|
|
style={{
|
|
|
|
backgroundColor: "#E5202B",
|
|
|
|
}}
|
|
|
|
>
|
2024-09-25 19:27:59 +01:00
|
|
|
<Text className="text-white">
|
|
|
|
{isDownloading ? "Downloading…" : "Download"}
|
|
|
|
</Text>
|
2024-09-25 19:23:57 +01:00
|
|
|
<Text className="text-white opacity-80 text-xs">
|
|
|
|
{selectedWallpaper.width}x{selectedWallpaper.height}
|
|
|
|
</Text>
|
2024-09-24 23:40:27 +01:00
|
|
|
</View>
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
2024-09-25 18:34:58 +01:00
|
|
|
</View>
|
2024-09-24 23:40:27 +01:00
|
|
|
);
|
|
|
|
}
|