Skip to content

Commit

Permalink
Show toast on save success and error
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzalezg9 committed Oct 27, 2024
1 parent 23781a0 commit e33fa12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/hooks/useSaveImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const useSaveImage = (props: UseSaveImageProps) => {
// STATE
const [error, setError] = useState("");
const [isSaving, setIsSaving] = useState(false);
const [isSuccess, setIsSuccess] = useState(false);

// HOOKS
const [permission, requestPermission] = MediaLibrary.usePermissions();
Expand All @@ -23,6 +24,7 @@ export const useSaveImage = (props: UseSaveImageProps) => {
const save = async () => {
const fileUri = `${FileSystem.documentDirectory}${fileName}.jpg`;

setIsSuccess(false);
setIsSaving(true);

try {
Expand All @@ -41,11 +43,9 @@ export const useSaveImage = (props: UseSaveImageProps) => {
await MediaLibrary.addAssetsToAlbumAsync([asset], album, false);
}

console.log(`Saved image from "${imageURL}" in album "${albumName}`);
setIsSuccess(true);
} catch (err) {
setError(
`Error saving image from "${imageURL}" in album "${albumName}". ${err}`
);
setError(String(err));
} finally {
setIsSaving(false);
}
Expand All @@ -54,6 +54,7 @@ export const useSaveImage = (props: UseSaveImageProps) => {
return {
error,
isSaving,
isSuccess,
save,
};
};
13 changes: 11 additions & 2 deletions src/wallpaper/components/WallpaperAuthorInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Linking,
StyleProp,
StyleSheet,
ToastAndroid,
View,
ViewStyle,
} from "react-native";
Expand All @@ -32,17 +33,25 @@ export const WallpaperAuthorInfo = (props: WallpaperAuthorInfoProps) => {
}

// HOOKS
const { error, isSaving, save } = useSaveImage({
const { error, isSaving, isSuccess, save } = useSaveImage({
imageURL: uri,
fileName: details?.slug || "wallpaper",
albumName: "Sweather Wallpaper",
});

// EFFECTS
useEffect(() => {
if (error) console.log(error);
if (error) {
ToastAndroid.show(error, ToastAndroid.LONG);
}
}, [error]);

useEffect(() => {
if (isSuccess) {
ToastAndroid.show("Wallpaper saved successfully", ToastAndroid.SHORT);
}
}, [isSuccess]);

// VARS
const combinedStyle = StyleSheet.flatten([styles.default, style]);

Expand Down

0 comments on commit e33fa12

Please sign in to comment.