@teamnhz/rn-ui-toolkit 1.1.9 → 1.2.0
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.
|
@@ -4,87 +4,100 @@ import { BottomSheet, Dividers } from "../index";
|
|
|
4
4
|
import { launchCamera, launchImageLibrary } from "react-native-image-picker";
|
|
5
5
|
import ImageCropPicker from "react-native-image-crop-picker";
|
|
6
6
|
import { Image as ImageCompressor, Video as VideoCompressor, } from "react-native-compressor";
|
|
7
|
-
import { Colors, Images, Scale, Typography } from "../../styles";
|
|
8
7
|
import { cameraPermissions, galleryPermissions, checkMicroPhonePermission, } from "../../utils/permissions";
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
import { Colors, Images, Scale, Typography } from "../../styles";
|
|
9
|
+
//--------------------------------------
|
|
10
|
+
// 🟦 FORMAT RESPONSE (single standard)
|
|
11
|
+
//--------------------------------------
|
|
12
|
+
const normalizePath = (p) => p ? p.replace("file://", "") : "";
|
|
13
|
+
const buildResponse = (raw, compressed) => {
|
|
11
14
|
return {
|
|
12
|
-
path:
|
|
13
|
-
originalPath: raw?.path?.
|
|
14
|
-
raw?.uri?.replace("file://", "") ||
|
|
15
|
-
"",
|
|
15
|
+
path: normalizePath(compressed || raw?.path || raw?.uri),
|
|
16
|
+
originalPath: normalizePath(raw?.path || raw?.uri),
|
|
16
17
|
fileName: raw?.fileName || "",
|
|
17
18
|
type: raw?.type || "",
|
|
18
19
|
duration: raw?.duration || undefined,
|
|
19
20
|
};
|
|
20
21
|
};
|
|
21
|
-
const ImagePicker = ({ mediaType,
|
|
22
|
-
|
|
22
|
+
const ImagePicker = ({ mediaType, onSuccess, visible, onClose, enableCompression = true, }) => {
|
|
23
|
+
//--------------------------------------
|
|
24
|
+
// 🟦 SEND RAW (loading true)
|
|
25
|
+
//--------------------------------------
|
|
23
26
|
const sendRaw = (raw) => {
|
|
24
|
-
onClose();
|
|
25
27
|
onSuccess({
|
|
26
28
|
loading: true,
|
|
27
29
|
data: buildResponse(raw),
|
|
28
30
|
});
|
|
29
31
|
};
|
|
30
|
-
|
|
32
|
+
//--------------------------------------
|
|
33
|
+
// 🟦 SEND FINAL COMPRESSED
|
|
34
|
+
//--------------------------------------
|
|
31
35
|
const sendFinal = (raw, compressedPath) => {
|
|
32
36
|
onSuccess({
|
|
33
37
|
loading: false,
|
|
34
38
|
data: buildResponse(raw, compressedPath),
|
|
35
39
|
});
|
|
36
40
|
};
|
|
37
|
-
|
|
41
|
+
//--------------------------------------
|
|
42
|
+
// 🟦 COMPRESS IMAGE
|
|
43
|
+
//--------------------------------------
|
|
38
44
|
const compressImage = async (path) => {
|
|
39
45
|
try {
|
|
40
|
-
|
|
46
|
+
return await ImageCompressor.compress(path, {
|
|
41
47
|
maxWidth: 1080,
|
|
42
48
|
quality: 0.7,
|
|
43
49
|
});
|
|
44
|
-
return compressed;
|
|
45
50
|
}
|
|
46
51
|
catch {
|
|
47
52
|
return path;
|
|
48
53
|
}
|
|
49
54
|
};
|
|
50
|
-
|
|
55
|
+
//--------------------------------------
|
|
56
|
+
// 🟦 COMPRESS VIDEO
|
|
57
|
+
//--------------------------------------
|
|
51
58
|
const compressVideo = async (uri) => {
|
|
52
59
|
try {
|
|
53
|
-
|
|
60
|
+
return await VideoCompressor.compress(uri, {
|
|
54
61
|
compressionMethod: "auto",
|
|
55
62
|
});
|
|
56
|
-
return compressed;
|
|
57
63
|
}
|
|
58
64
|
catch {
|
|
59
65
|
return uri;
|
|
60
66
|
}
|
|
61
67
|
};
|
|
62
|
-
|
|
68
|
+
//--------------------------------------
|
|
69
|
+
// 🟦 TAKE PHOTO / VIDEO FROM CAMERA
|
|
70
|
+
//--------------------------------------
|
|
63
71
|
const handleCamera = async () => {
|
|
64
72
|
await cameraPermissions(async (granted) => {
|
|
65
73
|
if (!granted)
|
|
66
74
|
return;
|
|
75
|
+
// PHOTO FROM CAMERA
|
|
67
76
|
if (mediaType === "photo") {
|
|
68
77
|
try {
|
|
69
|
-
const
|
|
70
|
-
|
|
78
|
+
const img = await ImageCropPicker.openCamera({
|
|
79
|
+
mediaType: "photo",
|
|
80
|
+
cropping: true,
|
|
81
|
+
});
|
|
82
|
+
sendRaw(img);
|
|
71
83
|
const compressed = enableCompression
|
|
72
|
-
? await compressImage(
|
|
73
|
-
:
|
|
74
|
-
sendFinal(
|
|
84
|
+
? await compressImage(img.path)
|
|
85
|
+
: img.path;
|
|
86
|
+
sendFinal(img, compressed);
|
|
75
87
|
}
|
|
76
88
|
catch (e) {
|
|
77
|
-
console.log(e);
|
|
89
|
+
console.log("Camera Photo Error", e);
|
|
78
90
|
}
|
|
79
91
|
}
|
|
92
|
+
// VIDEO FROM CAMERA
|
|
80
93
|
else {
|
|
81
94
|
const mic = await checkMicroPhonePermission();
|
|
82
95
|
if (!mic)
|
|
83
96
|
return;
|
|
84
|
-
launchCamera({ mediaType: "video"
|
|
85
|
-
|
|
97
|
+
launchCamera({ mediaType: "video" }, async (res) => {
|
|
98
|
+
const raw = res?.assets?.[0];
|
|
99
|
+
if (!raw)
|
|
86
100
|
return;
|
|
87
|
-
const raw = res.assets[0];
|
|
88
101
|
sendRaw(raw);
|
|
89
102
|
const compressed = enableCompression
|
|
90
103
|
? await compressVideo(raw.uri)
|
|
@@ -94,29 +107,36 @@ const ImagePicker = ({ mediaType, isMultiSelect = false, onSuccess, visible, onC
|
|
|
94
107
|
}
|
|
95
108
|
});
|
|
96
109
|
};
|
|
97
|
-
|
|
110
|
+
//--------------------------------------
|
|
111
|
+
// 🟦 PICK FROM GALLERY
|
|
112
|
+
//--------------------------------------
|
|
98
113
|
const handleGallery = async () => {
|
|
99
114
|
await galleryPermissions(async (granted) => {
|
|
100
115
|
if (!granted)
|
|
101
116
|
return;
|
|
117
|
+
// PHOTO FROM GALLERY (CROP PICKER)
|
|
102
118
|
if (mediaType === "photo") {
|
|
103
119
|
try {
|
|
104
|
-
const
|
|
105
|
-
|
|
120
|
+
const img = await ImageCropPicker.openPicker({
|
|
121
|
+
mediaType: "photo",
|
|
122
|
+
cropping: true,
|
|
123
|
+
});
|
|
124
|
+
sendRaw(img);
|
|
106
125
|
const compressed = enableCompression
|
|
107
|
-
? await compressImage(
|
|
108
|
-
:
|
|
109
|
-
sendFinal(
|
|
126
|
+
? await compressImage(img.path)
|
|
127
|
+
: img.path;
|
|
128
|
+
sendFinal(img, compressed);
|
|
110
129
|
}
|
|
111
130
|
catch (e) {
|
|
112
|
-
console.log(e);
|
|
131
|
+
console.log("Gallery Photo Error", e);
|
|
113
132
|
}
|
|
114
133
|
}
|
|
134
|
+
// VIDEO FROM GALLERY
|
|
115
135
|
else {
|
|
116
136
|
launchImageLibrary({ mediaType: "video" }, async (res) => {
|
|
117
|
-
|
|
137
|
+
const raw = res?.assets?.[0];
|
|
138
|
+
if (!raw)
|
|
118
139
|
return;
|
|
119
|
-
const raw = res.assets[0];
|
|
120
140
|
sendRaw(raw);
|
|
121
141
|
const compressed = enableCompression
|
|
122
142
|
? await compressVideo(raw.uri)
|
|
@@ -126,6 +146,7 @@ const ImagePicker = ({ mediaType, isMultiSelect = false, onSuccess, visible, onC
|
|
|
126
146
|
}
|
|
127
147
|
});
|
|
128
148
|
};
|
|
149
|
+
//--------------------------------------
|
|
129
150
|
return (React.createElement(BottomSheet, { visible: visible, onClose: onClose, height: 230 },
|
|
130
151
|
React.createElement(View, { style: styles.container },
|
|
131
152
|
React.createElement(TouchableOpacity, { style: styles.row, onPress: handleCamera },
|