@onekeyfe/react-native-photo-library 0.0.0-stage → 3.0.166

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +27 -2
  3. package/ReactNativePhotoLibrary.podspec +36 -0
  4. package/android/CMakeLists.txt +24 -0
  5. package/android/build.gradle +132 -0
  6. package/android/gradle.properties +4 -0
  7. package/android/src/main/AndroidManifest.xml +1 -0
  8. package/android/src/main/cpp/cpp-adapter.cpp +6 -0
  9. package/android/src/main/java/com/margelo/nitro/reactnativephotolibrary/ImagePhotoLibrary.kt +207 -0
  10. package/android/src/main/java/com/margelo/nitro/reactnativephotolibrary/PhotoLibraryException.kt +12 -0
  11. package/android/src/main/java/com/margelo/nitro/reactnativephotolibrary/ReactNativePhotoLibrary.kt +11 -0
  12. package/android/src/main/java/com/margelo/nitro/reactnativephotolibrary/ReactNativePhotoLibraryPackage.kt +22 -0
  13. package/docs/SPEC.md +108 -0
  14. package/ios/ImagePhotoLibrary.swift +80 -0
  15. package/ios/PhotoLibraryError.swift +11 -0
  16. package/ios/ReactNativePhotoLibrary.swift +15 -0
  17. package/ios/tests/ImagePhotoLibraryTests.swift +44 -0
  18. package/lib/module/ReactNativePhotoLibrary.nitro.js +4 -0
  19. package/lib/module/ReactNativePhotoLibrary.nitro.js.map +1 -0
  20. package/lib/module/index.js +52 -0
  21. package/lib/module/index.js.map +1 -0
  22. package/lib/module/package.json +1 -0
  23. package/lib/typescript/package.json +1 -0
  24. package/lib/typescript/src/ReactNativePhotoLibrary.nitro.d.ts +15 -0
  25. package/lib/typescript/src/ReactNativePhotoLibrary.nitro.d.ts.map +1 -0
  26. package/lib/typescript/src/index.d.ts +18 -0
  27. package/lib/typescript/src/index.d.ts.map +1 -0
  28. package/nitro.json +17 -0
  29. package/nitrogen/generated/android/c++/JHybridReactNativePhotoLibrarySpec.cpp +105 -0
  30. package/nitrogen/generated/android/c++/JHybridReactNativePhotoLibrarySpec.hpp +65 -0
  31. package/nitrogen/generated/android/c++/JPhotoSavePermission.hpp +62 -0
  32. package/nitrogen/generated/android/c++/JPhotoSavePermissionStatus.hpp +61 -0
  33. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativephotolibrary/HybridReactNativePhotoLibrarySpec.kt +65 -0
  34. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativephotolibrary/PhotoSavePermission.kt +56 -0
  35. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativephotolibrary/PhotoSavePermissionStatus.kt +24 -0
  36. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativephotolibrary/reactnativephotolibraryOnLoad.kt +35 -0
  37. package/nitrogen/generated/android/reactnativephotolibrary+autolinking.cmake +81 -0
  38. package/nitrogen/generated/android/reactnativephotolibrary+autolinking.gradle +27 -0
  39. package/nitrogen/generated/android/reactnativephotolibraryOnLoad.cpp +54 -0
  40. package/nitrogen/generated/android/reactnativephotolibraryOnLoad.hpp +34 -0
  41. package/nitrogen/generated/ios/ReactNativePhotoLibrary+autolinking.rb +62 -0
  42. package/nitrogen/generated/ios/ReactNativePhotoLibrary-Swift-Cxx-Bridge.cpp +57 -0
  43. package/nitrogen/generated/ios/ReactNativePhotoLibrary-Swift-Cxx-Bridge.hpp +159 -0
  44. package/nitrogen/generated/ios/ReactNativePhotoLibrary-Swift-Cxx-Umbrella.hpp +51 -0
  45. package/nitrogen/generated/ios/ReactNativePhotoLibraryAutolinking.mm +33 -0
  46. package/nitrogen/generated/ios/ReactNativePhotoLibraryAutolinking.swift +26 -0
  47. package/nitrogen/generated/ios/c++/HybridReactNativePhotoLibrarySpecSwift.cpp +11 -0
  48. package/nitrogen/generated/ios/c++/HybridReactNativePhotoLibrarySpecSwift.hpp +104 -0
  49. package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
  50. package/nitrogen/generated/ios/swift/Func_void_PhotoSavePermission.swift +46 -0
  51. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
  52. package/nitrogen/generated/ios/swift/HybridReactNativePhotoLibrarySpec.swift +57 -0
  53. package/nitrogen/generated/ios/swift/HybridReactNativePhotoLibrarySpec_cxx.swift +183 -0
  54. package/nitrogen/generated/ios/swift/PhotoSavePermission.swift +34 -0
  55. package/nitrogen/generated/ios/swift/PhotoSavePermissionStatus.swift +44 -0
  56. package/nitrogen/generated/shared/c++/HybridReactNativePhotoLibrarySpec.cpp +23 -0
  57. package/nitrogen/generated/shared/c++/HybridReactNativePhotoLibrarySpec.hpp +67 -0
  58. package/nitrogen/generated/shared/c++/PhotoSavePermission.hpp +88 -0
  59. package/nitrogen/generated/shared/c++/PhotoSavePermissionStatus.hpp +80 -0
  60. package/package.json +171 -4
  61. package/src/ReactNativePhotoLibrary.nitro.ts +15 -0
  62. package/src/index.tsx +79 -0
@@ -0,0 +1,67 @@
1
+ ///
2
+ /// HybridReactNativePhotoLibrarySpec.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/HybridObject.hpp>)
11
+ #include <NitroModules/HybridObject.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+
16
+ // Forward declaration of `PhotoSavePermission` to properly resolve imports.
17
+ namespace margelo::nitro::reactnativephotolibrary { struct PhotoSavePermission; }
18
+
19
+ #include "PhotoSavePermission.hpp"
20
+ #include <NitroModules/Promise.hpp>
21
+ #include <string>
22
+
23
+ namespace margelo::nitro::reactnativephotolibrary {
24
+
25
+ using namespace margelo::nitro;
26
+
27
+ /**
28
+ * An abstract base class for `ReactNativePhotoLibrary`
29
+ * Inherit this class to create instances of `HybridReactNativePhotoLibrarySpec` in C++.
30
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
31
+ * @example
32
+ * ```cpp
33
+ * class HybridReactNativePhotoLibrary: public HybridReactNativePhotoLibrarySpec {
34
+ * public:
35
+ * HybridReactNativePhotoLibrary(...): HybridObject(TAG) { ... }
36
+ * // ...
37
+ * };
38
+ * ```
39
+ */
40
+ class HybridReactNativePhotoLibrarySpec: public virtual HybridObject {
41
+ public:
42
+ // Constructor
43
+ explicit HybridReactNativePhotoLibrarySpec(): HybridObject(TAG) { }
44
+
45
+ // Destructor
46
+ ~HybridReactNativePhotoLibrarySpec() override = default;
47
+
48
+ public:
49
+ // Properties
50
+
51
+
52
+ public:
53
+ // Methods
54
+ virtual std::shared_ptr<Promise<PhotoSavePermission>> getSavePermission() = 0;
55
+ virtual std::shared_ptr<Promise<PhotoSavePermission>> requestSavePermission() = 0;
56
+ virtual std::shared_ptr<Promise<void>> saveToLibrary(const std::string& path) = 0;
57
+
58
+ protected:
59
+ // Hybrid Setup
60
+ void loadHybridMethods() override;
61
+
62
+ protected:
63
+ // Tag for logging
64
+ static constexpr auto TAG = "ReactNativePhotoLibrary";
65
+ };
66
+
67
+ } // namespace margelo::nitro::reactnativephotolibrary
@@ -0,0 +1,88 @@
1
+ ///
2
+ /// PhotoSavePermission.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
11
+ #include <NitroModules/JSIConverter.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
16
+ #include <NitroModules/NitroDefines.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/JSIHelpers.hpp>)
21
+ #include <NitroModules/JSIHelpers.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+ #if __has_include(<NitroModules/PropNameIDCache.hpp>)
26
+ #include <NitroModules/PropNameIDCache.hpp>
27
+ #else
28
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
29
+ #endif
30
+
31
+ // Forward declaration of `PhotoSavePermissionStatus` to properly resolve imports.
32
+ namespace margelo::nitro::reactnativephotolibrary { enum class PhotoSavePermissionStatus; }
33
+
34
+ #include "PhotoSavePermissionStatus.hpp"
35
+
36
+ namespace margelo::nitro::reactnativephotolibrary {
37
+
38
+ /**
39
+ * A struct which can be represented as a JavaScript object (PhotoSavePermission).
40
+ */
41
+ struct PhotoSavePermission final {
42
+ public:
43
+ PhotoSavePermissionStatus status SWIFT_PRIVATE;
44
+ bool canAskAgain SWIFT_PRIVATE;
45
+
46
+ public:
47
+ PhotoSavePermission() = default;
48
+ explicit PhotoSavePermission(PhotoSavePermissionStatus status, bool canAskAgain): status(status), canAskAgain(canAskAgain) {}
49
+
50
+ public:
51
+ friend bool operator==(const PhotoSavePermission& lhs, const PhotoSavePermission& rhs) = default;
52
+ };
53
+
54
+ } // namespace margelo::nitro::reactnativephotolibrary
55
+
56
+ namespace margelo::nitro {
57
+
58
+ // C++ PhotoSavePermission <> JS PhotoSavePermission (object)
59
+ template <>
60
+ struct JSIConverter<margelo::nitro::reactnativephotolibrary::PhotoSavePermission> final {
61
+ static inline margelo::nitro::reactnativephotolibrary::PhotoSavePermission fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
62
+ jsi::Object obj = arg.asObject(runtime);
63
+ return margelo::nitro::reactnativephotolibrary::PhotoSavePermission(
64
+ JSIConverter<margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "status"))),
65
+ JSIConverter<bool>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "canAskAgain")))
66
+ );
67
+ }
68
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::reactnativephotolibrary::PhotoSavePermission& arg) {
69
+ jsi::Object obj(runtime);
70
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "status"), JSIConverter<margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus>::toJSI(runtime, arg.status));
71
+ obj.setProperty(runtime, PropNameIDCache::get(runtime, "canAskAgain"), JSIConverter<bool>::toJSI(runtime, arg.canAskAgain));
72
+ return obj;
73
+ }
74
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
75
+ if (!value.isObject()) {
76
+ return false;
77
+ }
78
+ jsi::Object obj = value.getObject(runtime);
79
+ if (!nitro::isPlainObject(runtime, obj)) {
80
+ return false;
81
+ }
82
+ if (!JSIConverter<margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "status")))) return false;
83
+ if (!JSIConverter<bool>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "canAskAgain")))) return false;
84
+ return true;
85
+ }
86
+ };
87
+
88
+ } // namespace margelo::nitro
@@ -0,0 +1,80 @@
1
+ ///
2
+ /// PhotoSavePermissionStatus.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/NitroHash.hpp>)
11
+ #include <NitroModules/NitroHash.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+ #if __has_include(<NitroModules/JSIConverter.hpp>)
16
+ #include <NitroModules/JSIConverter.hpp>
17
+ #else
18
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
+ #endif
20
+ #if __has_include(<NitroModules/NitroDefines.hpp>)
21
+ #include <NitroModules/NitroDefines.hpp>
22
+ #else
23
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
24
+ #endif
25
+
26
+ namespace margelo::nitro::reactnativephotolibrary {
27
+
28
+ /**
29
+ * An enum which can be represented as a JavaScript union (PhotoSavePermissionStatus).
30
+ */
31
+ enum class PhotoSavePermissionStatus {
32
+ GRANTED SWIFT_NAME(granted) = 0,
33
+ DENIED SWIFT_NAME(denied) = 1,
34
+ UNDETERMINED SWIFT_NAME(undetermined) = 2,
35
+ } CLOSED_ENUM;
36
+
37
+ } // namespace margelo::nitro::reactnativephotolibrary
38
+
39
+ namespace margelo::nitro {
40
+
41
+ // C++ PhotoSavePermissionStatus <> JS PhotoSavePermissionStatus (union)
42
+ template <>
43
+ struct JSIConverter<margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus> final {
44
+ static inline margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
45
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
46
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
47
+ case hashString("granted"): return margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus::GRANTED;
48
+ case hashString("denied"): return margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus::DENIED;
49
+ case hashString("undetermined"): return margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus::UNDETERMINED;
50
+ default: [[unlikely]]
51
+ throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum PhotoSavePermissionStatus - invalid value!");
52
+ }
53
+ }
54
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus arg) {
55
+ switch (arg) {
56
+ case margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus::GRANTED: return JSIConverter<std::string>::toJSI(runtime, "granted");
57
+ case margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus::DENIED: return JSIConverter<std::string>::toJSI(runtime, "denied");
58
+ case margelo::nitro::reactnativephotolibrary::PhotoSavePermissionStatus::UNDETERMINED: return JSIConverter<std::string>::toJSI(runtime, "undetermined");
59
+ default: [[unlikely]]
60
+ throw std::invalid_argument("Cannot convert PhotoSavePermissionStatus to JS - invalid value: "
61
+ + std::to_string(static_cast<int>(arg)) + "!");
62
+ }
63
+ }
64
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
65
+ if (!value.isString()) {
66
+ return false;
67
+ }
68
+ std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
69
+ switch (hashString(unionValue.c_str(), unionValue.size())) {
70
+ case hashString("granted"):
71
+ case hashString("denied"):
72
+ case hashString("undetermined"):
73
+ return true;
74
+ default:
75
+ return false;
76
+ }
77
+ }
78
+ };
79
+
80
+ } // namespace margelo::nitro
package/package.json CHANGED
@@ -1,6 +1,173 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-photo-library",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "3.0.166",
4
+ "description": "Add-only photo saving with version-scoped Android write permission",
5
+ "main": "./lib/module/index.js",
6
+ "types": "./lib/typescript/src/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "source": "./src/index.tsx",
10
+ "types": "./lib/typescript/src/index.d.ts",
11
+ "default": "./lib/module/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "src",
17
+ "lib",
18
+ "android",
19
+ "ios",
20
+ "cpp",
21
+ "nitrogen",
22
+ "nitro.json",
23
+ "*.podspec",
24
+ "react-native.config.js",
25
+ "!ios/build",
26
+ "!android/build",
27
+ "!android/gradle",
28
+ "!android/gradlew",
29
+ "!android/gradlew.bat",
30
+ "!android/local.properties",
31
+ "!**/__tests__",
32
+ "!**/__fixtures__",
33
+ "!**/__mocks__",
34
+ "!**/.*",
35
+ "docs",
36
+ "README.md",
37
+ "LICENSE"
38
+ ],
39
+ "scripts": {
40
+ "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
41
+ "prepare": "bob build",
42
+ "nitrogen": "nitrogen",
43
+ "typecheck": "tsc",
44
+ "lint": "eslint \"**/*.{js,ts,tsx}\"",
45
+ "test": "jest",
46
+ "release": "yarn prepare && npm whoami && npm publish --access public"
47
+ },
48
+ "keywords": [
49
+ "react-native",
50
+ "ios",
51
+ "android"
52
+ ],
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+https://github.com/OneKeyHQ/app-modules.git",
56
+ "directory": "native-modules/react-native-photo-library"
57
+ },
58
+ "author": "onekeyfe <huanming@onekey.so> (https://github.com/OneKeyHQ/app-modules)",
59
+ "license": "MIT",
60
+ "bugs": {
61
+ "url": "https://github.com/OneKeyHQ/app-modules/issues"
62
+ },
63
+ "homepage": "https://github.com/OneKeyHQ/app-modules/tree/main/native-modules/react-native-photo-library#readme",
64
+ "publishConfig": {
65
+ "registry": "https://registry.npmjs.org/"
66
+ },
67
+ "devDependencies": {
68
+ "@commitlint/config-conventional": "^19.8.1",
69
+ "@eslint/compat": "^1.3.2",
70
+ "@eslint/eslintrc": "^3.3.1",
71
+ "@eslint/js": "^9.35.0",
72
+ "@react-native/babel-preset": "0.86.2",
73
+ "@react-native/eslint-config": "0.86.2",
74
+ "@release-it/conventional-changelog": "^10.0.1",
75
+ "@types/jest": "^29.5.14",
76
+ "@types/react": "^19.2.0",
77
+ "commitlint": "^19.8.1",
78
+ "del-cli": "^6.0.0",
79
+ "eslint": "^9.35.0",
80
+ "eslint-config-prettier": "^10.1.8",
81
+ "eslint-plugin-prettier": "^5.5.4",
82
+ "jest": "^29.7.0",
83
+ "lefthook": "^2.0.3",
84
+ "nitrogen": "0.37.0",
85
+ "prettier": "^2.8.8",
86
+ "react": "19.2.3",
87
+ "react-native": "patch:react-native@npm%3A0.86.2#~/.yarn/patches/react-native-npm-0.86.2-c9d546616f.patch",
88
+ "react-native-builder-bob": "^0.40.13",
89
+ "react-native-nitro-modules": "0.37.0",
90
+ "release-it": "^19.0.4",
91
+ "turbo": "^2.5.6",
92
+ "typescript": "^5.9.2"
93
+ },
94
+ "peerDependencies": {
95
+ "react": "*",
96
+ "react-native": "*",
97
+ "react-native-nitro-modules": "0.37.0"
98
+ },
99
+ "react-native-builder-bob": {
100
+ "source": "src",
101
+ "output": "lib",
102
+ "targets": [
103
+ [
104
+ "custom",
105
+ {
106
+ "script": "nitrogen",
107
+ "clean": "nitrogen/"
108
+ }
109
+ ],
110
+ [
111
+ "module",
112
+ {
113
+ "esm": true
114
+ }
115
+ ],
116
+ [
117
+ "typescript",
118
+ {
119
+ "project": "tsconfig.build.json"
120
+ }
121
+ ]
122
+ ]
123
+ },
124
+ "prettier": {
125
+ "quoteProps": "consistent",
126
+ "singleQuote": true,
127
+ "tabWidth": 2,
128
+ "trailingComma": "es5",
129
+ "useTabs": false
130
+ },
131
+ "jest": {
132
+ "preset": "react-native",
133
+ "modulePathIgnorePatterns": [
134
+ "<rootDir>/example/node_modules",
135
+ "<rootDir>/lib/"
136
+ ]
137
+ },
138
+ "commitlint": {
139
+ "extends": [
140
+ "@commitlint/config-conventional"
141
+ ]
142
+ },
143
+ "release-it": {
144
+ "git": {
145
+ "commitMessage": "chore: release ${version}",
146
+ "tagName": "v${version}"
147
+ },
148
+ "npm": {
149
+ "publish": true
150
+ },
151
+ "github": {
152
+ "release": true
153
+ },
154
+ "plugins": {
155
+ "@release-it/conventional-changelog": {
156
+ "preset": {
157
+ "name": "angular"
158
+ }
159
+ }
160
+ }
161
+ },
162
+ "create-react-native-library": {
163
+ "type": "nitro-module",
164
+ "languages": "kotlin-swift",
165
+ "tools": [
166
+ "eslint",
167
+ "jest",
168
+ "lefthook",
169
+ "release-it"
170
+ ],
171
+ "version": "0.56.0"
172
+ }
173
+ }
@@ -0,0 +1,15 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules';
2
+
3
+ export type PhotoSavePermissionStatus = 'granted' | 'denied' | 'undetermined';
4
+
5
+ export interface PhotoSavePermission {
6
+ status: PhotoSavePermissionStatus;
7
+ canAskAgain: boolean;
8
+ }
9
+
10
+ export interface ReactNativePhotoLibrary
11
+ extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
12
+ getSavePermission(): Promise<PhotoSavePermission>;
13
+ requestSavePermission(): Promise<PhotoSavePermission>;
14
+ saveToLibrary(path: string): Promise<void>;
15
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,79 @@
1
+ import { NitroModules } from 'react-native-nitro-modules';
2
+
3
+ import type {
4
+ PhotoSavePermission,
5
+ ReactNativePhotoLibrary,
6
+ } from './ReactNativePhotoLibrary.nitro';
7
+
8
+ export type {
9
+ PhotoSavePermission,
10
+ PhotoSavePermissionStatus,
11
+ } from './ReactNativePhotoLibrary.nitro';
12
+
13
+ let hybridObject: ReactNativePhotoLibrary | undefined;
14
+
15
+ function native(): ReactNativePhotoLibrary {
16
+ if (!hybridObject) {
17
+ hybridObject = NitroModules.createHybridObject<ReactNativePhotoLibrary>(
18
+ 'ReactNativePhotoLibrary'
19
+ );
20
+ }
21
+ return hybridObject;
22
+ }
23
+
24
+ const ERROR_CODES = [
25
+ 'E_NO_LIBRARY_PERMISSION',
26
+ 'E_NO_IMAGE_DATA_FOUND',
27
+ 'E_CANNOT_SAVE_IMAGE',
28
+ 'E_PERMISSION_IN_PROGRESS',
29
+ 'E_ACTIVITY_DOES_NOT_EXIST',
30
+ ] as const;
31
+
32
+ export type PhotoLibraryErrorCode = (typeof ERROR_CODES)[number] | 'E_UNKNOWN';
33
+
34
+ export class PhotoLibraryError extends Error {
35
+ readonly code: PhotoLibraryErrorCode;
36
+
37
+ constructor(code: PhotoLibraryErrorCode, message: string) {
38
+ super(message);
39
+ this.name = 'PhotoLibraryError';
40
+ this.code = code;
41
+ }
42
+ }
43
+
44
+ function toError(error: unknown): PhotoLibraryError {
45
+ if (error instanceof PhotoLibraryError) return error;
46
+ const message = error instanceof Error ? error.message : String(error);
47
+ const match = /\b(E_[A-Z_]+): (.*)/.exec(message);
48
+ const code = ERROR_CODES.find((value) => value === match?.[1]);
49
+ return new PhotoLibraryError(
50
+ code ?? 'E_UNKNOWN',
51
+ code ? match?.[2]?.trim() ?? '' : 'Photo library operation failed'
52
+ );
53
+ }
54
+
55
+ export async function getSavePermission(): Promise<PhotoSavePermission> {
56
+ try {
57
+ return await native().getSavePermission();
58
+ } catch (error) {
59
+ throw toError(error);
60
+ }
61
+ }
62
+
63
+ export async function requestSavePermission(): Promise<PhotoSavePermission> {
64
+ try {
65
+ return await native().requestSavePermission();
66
+ } catch (error) {
67
+ throw toError(error);
68
+ }
69
+ }
70
+
71
+ export async function saveToLibrary(path: string): Promise<void> {
72
+ try {
73
+ await native().saveToLibrary(path);
74
+ } catch (error) {
75
+ throw toError(error);
76
+ }
77
+ }
78
+
79
+ export default { getSavePermission, requestSavePermission, saveToLibrary };