UNPKG

@kietpt2003/react-native-core-ui

Version:
257 lines (254 loc) 10 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/hooks/index.ts var hooks_exports = {}; __export(hooks_exports, { useGalleryAssets: () => useGalleryAssets }); module.exports = __toCommonJS(hooks_exports); // src/hooks/useGalleryAssets.ts var import_react = __toESM(require("react")); var import_react_native = require("react-native"); var import_camera_roll = require("@react-native-camera-roll/camera-roll"); var import_react_native_permissions = require("react-native-permissions"); var numberAssetsToLoad = 15; function useGalleryAssets(defaultAssetType = "All") { const [assets, setAssets] = import_react.default.useState({}); const [albums, setAlbums] = import_react.default.useState([]); const [pagination, setPagination] = import_react.default.useState({}); const [fullAssets, setFullAssets] = import_react.default.useState([]); const [fullAssetsPagination, setFullAssetsPagination] = import_react.default.useState(void 0); const [totalAssets, setTotalAssets] = import_react.default.useState(0); const [loading, setLoading] = import_react.default.useState(true); const [error, setError] = import_react.default.useState(null); const [hasPermission, setHasPermission] = import_react.default.useState(false); const [assetType, setAssetType] = import_react.default.useState(defaultAssetType); const changeAssetType = (type) => { setAlbums([]); setAssets({}); setPagination({}); setFullAssets([]); setFullAssetsPagination(void 0); setTotalAssets(0); if (hasPermission && type !== assetType) { setAssetType(type); loadFullAssets(numberAssetsToLoad, void 0, type, true); loadAlbums(numberAssetsToLoad, type, true); } }; const checkPermission = async () => { if (import_react_native.Platform.OS === "android") { if (import_react_native.Platform.Version >= 33) { const result = await Promise.all([ import_react_native.PermissionsAndroid.check(import_react_native.PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES), import_react_native.PermissionsAndroid.check(import_react_native.PermissionsAndroid.PERMISSIONS.READ_MEDIA_VIDEO) ]); const hasPermission2 = result[0] && result[1]; setHasPermission(hasPermission2); return hasPermission2; } else { const result = await import_react_native.PermissionsAndroid.check(import_react_native.PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE); setHasPermission(result); return result; } } else { const checkResult = await (0, import_react_native_permissions.check)(import_react_native_permissions.PERMISSIONS.IOS.PHOTO_LIBRARY); if (checkResult === import_react_native_permissions.RESULTS.GRANTED) { setHasPermission(true); return true; } else { setHasPermission(false); return false; } } }; const requestPermission = async () => { if (import_react_native.Platform.OS === "android") { if (import_react_native.Platform.Version >= 33) { const requestResults = await import_react_native.PermissionsAndroid.requestMultiple([ import_react_native.PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES, import_react_native.PermissionsAndroid.PERMISSIONS.READ_MEDIA_VIDEO ]); const result = requestResults[import_react_native.PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES] === import_react_native.PermissionsAndroid.RESULTS.GRANTED && requestResults[import_react_native.PermissionsAndroid.PERMISSIONS.READ_MEDIA_VIDEO] === import_react_native.PermissionsAndroid.RESULTS.GRANTED; setHasPermission(result); return result; } else { const requestResults = await import_react_native.PermissionsAndroid.request(import_react_native.PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE); const result = requestResults === import_react_native.PermissionsAndroid.RESULTS.GRANTED; setHasPermission(result); return result; } } else { const requestResult = await (0, import_react_native_permissions.request)(import_react_native_permissions.PERMISSIONS.IOS.PHOTO_LIBRARY); const result = requestResult === import_react_native_permissions.RESULTS.GRANTED; setHasPermission(result); return result; } }; const loadFullAssets = async (numberAssetsToLoad2, after, type = "All", isReset = false) => { setLoading(true); if (hasPermission) { const res = await import_camera_roll.CameraRoll.getPhotos({ first: numberAssetsToLoad2, assetType: type, after }); if (isReset) { setFullAssets(res.edges); } else { const existingAssets = new Set(fullAssets.map((p) => p.node.image.uri)); const uniqueNewAssets = res.edges.filter( (p) => !existingAssets.has(p.node.image.uri) ); const newAssets = fullAssets.concat(uniqueNewAssets); setFullAssets(newAssets); } setFullAssetsPagination({ hasNextPage: res.page_info.has_next_page, endCursor: res.page_info.end_cursor }); setLoading(false); return res.page_info.has_next_page; } else { setAlbums([]); setAssets({}); setPagination({}); setFullAssets([]); setFullAssetsPagination(void 0); setLoading(false); return false; } }; const loadAlbums = async (numberAssetsToLoad2, type = "All", isReset = false) => { try { setLoading(true); if (!hasPermission) { setAlbums([]); setAssets({}); setPagination({}); setFullAssets([]); setFullAssetsPagination(void 0); setLoading(false); return; } const albumList = await import_camera_roll.CameraRoll.getAlbums({ assetType: type }); if (albumList.length > 0) { let total = 0; albumList.forEach(async (album) => { total += album.count; await loadAssets(album, numberAssetsToLoad2, isReset ? void 0 : pagination[album?.id ?? album.title]?.endCursor, type); }); setTotalAssets(total); } setAlbums(albumList); } catch (err) { setError("Failed to load albums"); console.error("Error loading albums:", err); } finally { setLoading(false); } }; const loadAssets = async (album, numberAssetsToLoad2, after, type = "All") => { try { setLoading(true); if (!assets[album?.id ?? album.title]) assets[album?.id ?? album.title] = []; if (hasPermission) { const albumAssets = await import_camera_roll.CameraRoll.getPhotos({ first: numberAssetsToLoad2, groupName: album.title, assetType: type, after }); setAssets((prevAssets) => { const existing = prevAssets[album?.id ?? album.title] || []; const existingAssets = new Set(existing.map((a) => a.node.image.uri)); const uniqueNewAssets = albumAssets.edges.filter( (a) => !existingAssets.has(a.node.image.uri) ); return { ...prevAssets, [album?.id ?? album.title]: [...existing, ...uniqueNewAssets] }; }); setPagination((prevPagination) => ({ ...prevPagination, [album?.id ?? album.title]: { hasNextPage: albumAssets.page_info.has_next_page, endCursor: albumAssets.page_info.end_cursor } })); setLoading(false); return albumAssets.page_info.has_next_page; } else { setAlbums([]); setAssets({}); setPagination({}); setFullAssets([]); setFullAssetsPagination(void 0); setLoading(false); return false; } } catch (error2) { setError("Failed to load assets from album"); console.error("Error loading assets from album:", error2); setLoading(false); return false; } }; import_react.default.useEffect(() => { if (hasPermission) { loadFullAssets(numberAssetsToLoad, fullAssetsPagination?.endCursor, assetType); loadAlbums(numberAssetsToLoad, assetType); } }, [hasPermission]); import_react.default.useEffect(() => { checkPermission(); }, []); return { albums, assets, loading, error, fullAssets, totalAssets, fullAssetsPagination, loadFullAssets, loadAssets, pagination, requestPermission, checkPermission, hasPermission, changeAssetType }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useGalleryAssets }); //# sourceMappingURL=index.js.map