feat: add loading state and error handling
This commit is contained in:
@@ -4,9 +4,11 @@ import * as MediaLibrary from "expo-media-library";
|
|||||||
import { Ionicons } from "@expo/vector-icons";
|
import { Ionicons } from "@expo/vector-icons";
|
||||||
import { useRouter } from "expo-router";
|
import { useRouter } from "expo-router";
|
||||||
import { Image, Text, View, TouchableOpacity, Alert } from "react-native";
|
import { Image, Text, View, TouchableOpacity, Alert } from "react-native";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
export default function WallpaperPage() {
|
export default function WallpaperPage() {
|
||||||
const selectedWallpaper = useStore((store) => store.selectedWallpaper);
|
const selectedWallpaper = useStore((store) => store.selectedWallpaper);
|
||||||
|
const [isDownloading, setIsDownloading] = useState(false);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
if (!selectedWallpaper) {
|
if (!selectedWallpaper) {
|
||||||
@@ -20,28 +22,35 @@ export default function WallpaperPage() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const permStatus = await MediaLibrary.requestPermissionsAsync();
|
setIsDownloading(true);
|
||||||
if (permStatus.status != MediaLibrary.PermissionStatus.GRANTED) {
|
|
||||||
Alert.alert(
|
try {
|
||||||
"Media library access required",
|
const permStatus = await MediaLibrary.requestPermissionsAsync();
|
||||||
"Doors need access to your media library in order to save wallpapers.",
|
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}`,
|
||||||
);
|
);
|
||||||
return;
|
await MediaLibrary.createAssetAsync(result.uri);
|
||||||
|
Alert.alert(
|
||||||
|
"Wallpaper saved successfully",
|
||||||
|
"You can change the wallpaper in Settings.",
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
Alert.alert(
|
||||||
|
"Unable to download wallpaper",
|
||||||
|
"Check your internet connection. Otherwise, it's probably our fault.",
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
setIsDownloading(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
|
||||||
`${FileSystem.documentDirectory}${selectedWallpaper.asset_id}.${selectedWallpaper.format}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const result = await FileSystem.downloadAsync(
|
|
||||||
selectedWallpaper.secure_url,
|
|
||||||
`${FileSystem.documentDirectory}${selectedWallpaper.asset_id}.${selectedWallpaper.format}`,
|
|
||||||
);
|
|
||||||
await MediaLibrary.createAssetAsync(result.uri);
|
|
||||||
Alert.alert(
|
|
||||||
"Wallpaper saved successfully",
|
|
||||||
"You can change the wallpaper in Settings.",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -83,9 +92,12 @@ export default function WallpaperPage() {
|
|||||||
) : null}
|
) : null}
|
||||||
</View>
|
</View>
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
|
disabled={isDownloading}
|
||||||
className="w-full"
|
className="w-full"
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
downloadWallpaper();
|
if (!isDownloading) {
|
||||||
|
downloadWallpaper();
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<View
|
<View
|
||||||
@@ -94,7 +106,9 @@ export default function WallpaperPage() {
|
|||||||
backgroundColor: "#E5202B",
|
backgroundColor: "#E5202B",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Text className="text-white">Download</Text>
|
<Text className="text-white">
|
||||||
|
{isDownloading ? "Downloading…" : "Download"}
|
||||||
|
</Text>
|
||||||
<Text className="text-white opacity-80 text-xs">
|
<Text className="text-white opacity-80 text-xs">
|
||||||
{selectedWallpaper.width}x{selectedWallpaper.height}
|
{selectedWallpaper.width}x{selectedWallpaper.height}
|
||||||
</Text>
|
</Text>
|
||||||
|
Reference in New Issue
Block a user