@teamnhz/rn-ui-toolkit 1.1.9 → 1.2.1

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