-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hook to save the current wallpaper
- Loading branch information
1 parent
45e659a
commit 8e02163
Showing
5 changed files
with
85 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as FileSystem from "expo-file-system"; | ||
import * as MediaLibrary from "expo-media-library"; | ||
import { useState } from "react"; | ||
import { Uri } from "../interfaces"; | ||
|
||
const ALBUM_NAME = "Sweather Wallpaper"; | ||
const WALLPAPER_NAME = "wallpaper.jpg"; | ||
|
||
export const useWallpaperStorage = () => { | ||
const [error, setError] = useState(""); | ||
const [permissionResponse, requestPermission] = MediaLibrary.usePermissions({ | ||
writeOnly: true, | ||
}); | ||
|
||
const moveWallpaperToAlbum = async (wallpaper: string, albumName: string) => { | ||
try { | ||
const album = await MediaLibrary.getAlbumAsync(albumName); | ||
const asset = await MediaLibrary.createAssetAsync(wallpaper); | ||
|
||
if (album == null) { | ||
await MediaLibrary.createAlbumAsync(albumName, asset, false); | ||
} else { | ||
await MediaLibrary.addAssetsToAlbumAsync([asset], album, false); | ||
} | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
setError( | ||
`Error setting wallpaper in album "${albumName}": ` + err.message | ||
); | ||
} | ||
} | ||
}; | ||
|
||
const storeWallpaper = async (wallpaper: Uri) => { | ||
const fileUri = FileSystem.documentDirectory + WALLPAPER_NAME; | ||
|
||
try { | ||
const res = await FileSystem.downloadAsync(wallpaper.uri, fileUri); | ||
|
||
if (permissionResponse?.status !== "granted") { | ||
await requestPermission(); | ||
} | ||
|
||
moveWallpaperToAlbum(res.uri, ALBUM_NAME); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
setError("Error downloading wallpaper: " + err.message); | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
storeWallpaper, | ||
error, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5364,6 +5364,11 @@ expo-location@~17.0.1: | |
resolved "https://registry.yarnpkg.com/expo-location/-/expo-location-17.0.1.tgz#3bdbf91066f1a40ec3468dc1807f3e106473f74c" | ||
integrity sha512-m+OzotzlAXO3ZZ1uqW5GC25nXW868zN+ROyBA1V4VF6jGay1ZEs4URPglCVUDzZby2F5wt24cMzqDKw2IX6nRw== | ||
|
||
expo-media-library@~16.0.3: | ||
version "16.0.3" | ||
resolved "https://registry.yarnpkg.com/expo-media-library/-/expo-media-library-16.0.3.tgz#789277a827b9c04e59eef84aa11f4edcf5f1cfce" | ||
integrity sha512-E++h+ZyVOXhGOKddeg/wjoEFH+JAGazcAsMpjAOT+/F69nrIEl27Fymi9XVAzooazp38dKrDBNeOJuWoXUdUAw== | ||
|
||
[email protected]: | ||
version "1.11.1" | ||
resolved "https://registry.yarnpkg.com/expo-modules-autolinking/-/expo-modules-autolinking-1.11.1.tgz#4a867f727d9dfde07de8dde14b333a3cbf82ce3c" | ||
|
@@ -5382,6 +5387,11 @@ [email protected]: | |
dependencies: | ||
invariant "^2.2.4" | ||
|
||
expo-permissions@^14.4.0: | ||
version "14.4.0" | ||
resolved "https://registry.yarnpkg.com/expo-permissions/-/expo-permissions-14.4.0.tgz#d7bb2f0026b87e1294ee551969c35306193f4ad3" | ||
integrity sha512-oAcnJ7dlZhpBydK73cwomA2xofizayVUz+FW5REl7dMu7MYyeN/3aqhlpZ3mYddrxvG161bqu97MQr01UixUnw== | ||
|
||
[email protected]: | ||
version "0.0.127" | ||
resolved "https://registry.yarnpkg.com/expo-pwa/-/expo-pwa-0.0.127.tgz#b8d2fd28efff408a24e0f2539bfb47e09f8e4ebe" | ||
|