Skip to content

Commit ff9f2e5

Browse files
authored
Merge pull request #57 from sendbird/feat/image-compression
[UIKIT-2514] Feat/image compression
2 parents 093abf9 + 5815a58 commit ff9f2e5

File tree

39 files changed

+1198
-887
lines changed

39 files changed

+1198
-887
lines changed

.yarn/releases/yarn-1.19.0.cjs

Lines changed: 710 additions & 710 deletions
Large diffs are not rendered by default.

docs-validation/1_introduction/NativeModules.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as FileAccess from 'react-native-file-access';
2222
import * as ImagePicker from 'react-native-image-picker';
2323
import * as Permissions from 'react-native-permissions';
2424
import * as CreateThumbnail from 'react-native-create-thumbnail';
25+
import * as ImageResizer from '@bam.tech/react-native-image-resizer';
2526

2627
const NativeClipboardService = createNativeClipboardService(Clipboard);
2728
const NativeNotificationService = createNativeNotificationService({
@@ -38,6 +39,7 @@ const NativeFileService = createNativeFileService({
3839
const NativeMediaService = createNativeMediaService({
3940
VideoComponent: Video,
4041
thumbnailModule: CreateThumbnail,
42+
imageResizerModule: ImageResizer,
4143
});
4244
/** ------------------ **/
4345

@@ -53,6 +55,7 @@ import * as ExpoMediaLibrary from 'expo-media-library';
5355
import * as ExpoNotifications from 'expo-notifications';
5456
import * as ExpoAV from 'expo-av';
5557
import * as ExpoVideoThumbnail from 'expo-video-thumbnails';
58+
import * as ExpoImageManipulator from 'expo-image-manipulator';
5659

5760
const ExpoNotificationService = createExpoNotificationService(ExpoNotifications);
5861
const ExpoClipboardService = createExpoClipboardService(ExpoClipboard);
@@ -64,6 +67,8 @@ const ExpoFileService = createExpoFileService({
6467
});
6568
const ExpoMediaService = createExpoMediaService({
6669
avModule: ExpoAV,
67-
thumbnailModule: ExpoVideoThumbnail
70+
thumbnailModule: ExpoVideoThumbnail,
71+
imageManipulator: ExpoImageManipulator,
72+
fsModule: ExpoFS,
6873
})
6974
/** ------------------ **/

docs-validation/1_introduction/SendYourFirstMessage.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as FileAccess from 'react-native-file-access';
2020
import * as ImagePicker from 'react-native-image-picker';
2121
import * as Permissions from 'react-native-permissions';
2222
import * as CreateThumbnail from 'react-native-create-thumbnail';
23+
import * as ImageResizer from '@bam.tech/react-native-image-resizer';
2324

2425
const ClipboardService = createNativeClipboardService(Clipboard);
2526
const NotificationService = createNativeNotificationService({
@@ -36,6 +37,7 @@ const FileService = createNativeFileService({
3637
const MediaService = createNativeMediaService({
3738
VideoComponent: Video,
3839
thumbnailModule: CreateThumbnail,
40+
imageResizerModule: ImageResizer,
3941
});
4042
/** ------------------ **/
4143

@@ -179,6 +181,7 @@ const App2 = () => {
179181
file: FileService,
180182
notification: NotificationService,
181183
clipboard: ClipboardService,
184+
media: MediaService,
182185
}}
183186
>
184187
<Navigation />

docs-validation/2_features/FileSharing.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,21 @@ function _stringResource(str: StringSet) {
8484
* Image compression
8585
* {@link https://sendbird.com/docs/uikit/v3/react-native/features/file-sharing#2-image-compression}
8686
* */
87-
// @ts-ignore
88-
import ImageResizer from 'react-native-image-resizer';
87+
import { SendbirdUIKitContainer } from '@sendbird/uikit-react-native';
8988

90-
const GroupChannelScreen2 = () => {
89+
const App = () => {
9190
return (
92-
// @ts-ignore
93-
<GroupChannelFragment
94-
onBeforeSendFileMessage={async (params) => {
95-
if (params.file && 'uri' in params.file) {
96-
if (isImageFile(params.file.name)) {
97-
const { uri, size } = await ImageResizer.createResizedImage(params.file.uri);
98-
params.file = { ...params.file, uri, size };
99-
}
100-
}
101-
102-
return params;
91+
<SendbirdUIKitContainer
92+
appId={'APP_ID'}
93+
// @ts-ignore
94+
platformServices={{}}
95+
chatOptions={{
96+
enableImageCompression: true
97+
}}
98+
imageCompression={{
99+
compressionRate: 0.5,
100+
width: 600,
101+
height: 600
103102
}}
104103
/>
105104
);

docs-validation/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
88
},
99
"dependencies": {
10+
"@bam.tech/react-native-image-resizer": "^3.0.4",
1011
"@react-native-async-storage/async-storage": "^1.15.17",
1112
"@react-native-camera-roll/camera-roll": "^5.0.4",
1213
"@react-native-clipboard/clipboard": "^1.8.5",

packages/uikit-react-native/README.md

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ UIKit for React-Native can be installed through either `yarn` or `npm`
4545
**Install dependencies**
4646

4747
```sh
48-
npm i @sendbird/uikit-react-native \
49-
@sendbird/chat \
50-
date-fns \
51-
react-native-safe-area-context \
52-
@react-native-community/netinfo
48+
npm install @sendbird/uikit-react-native \
49+
@sendbird/chat \
50+
date-fns \
51+
react-native-safe-area-context \
52+
@react-native-community/netinfo \
53+
@react-native-async-storage/async-storage
5354
```
5455

5556
**Linking native modules**
@@ -121,6 +122,7 @@ const App = () => {
121122
file: FileService,
122123
notification: NotificationService,
123124
clipboard: ClipboardService,
125+
media: MediaService,
124126
}}
125127
>
126128
{/* ... */}
@@ -136,17 +138,20 @@ In order to implement the interfaces to your React Native app more easily, we pr
136138
137139
**Using React Native CLI**
138140

139-
You can use `createNativeClipboardService`, `createNativeNotificationService` and `createNativeFileService` helper functions with below native modules.
141+
You can use `createNativeClipboardService`, `createNativeNotificationService`, `createNativeFileService` and `createNativeMediaService` helper functions with below native modules.
140142

141143
```sh
142-
npm install react-native-permissions \
144+
npm install react-native-video \
145+
react-native-permissions \
146+
react-native-file-access \
143147
react-native-image-picker \
144148
react-native-document-picker \
145-
@react-native-camera-roll/camera-roll \
146-
react-native-file-access \
149+
react-native-create-thumbnail \
147150
@react-native-clipboard/clipboard \
151+
@react-native-camera-roll/camera-roll \
148152
@react-native-firebase/app \
149153
@react-native-firebase/messaging \
154+
@bam.tech/react-native-image-resizer
150155

151156
npx pod-install
152157
```
@@ -155,36 +160,47 @@ npx pod-install
155160
import Clipboard from '@react-native-clipboard/clipboard';
156161
import { CameraRoll } from '@react-native-camera-roll/camera-roll';
157162
import RNFBMessaging from '@react-native-firebase/messaging';
163+
import Video from 'react-native-video';
158164
import * as DocumentPicker from 'react-native-document-picker';
159165
import * as FileAccess from 'react-native-file-access';
160166
import * as ImagePicker from 'react-native-image-picker';
161167
import * as Permissions from 'react-native-permissions';
168+
import * as CreateThumbnail from 'react-native-create-thumbnail';
169+
import * as ImageResizer from '@bam.tech/react-native-image-resizer';
162170

163-
const ClipboardService = createNativeClipboardService(Clipboard);
164-
const NotificationService = createNativeNotificationService({
171+
const NativeClipboardService = createNativeClipboardService(Clipboard);
172+
const NativeNotificationService = createNativeNotificationService({
165173
messagingModule: RNFBMessaging,
166174
permissionModule: Permissions,
167175
});
168-
const FileService = createNativeFileService({
176+
const NativeFileService = createNativeFileService({
169177
fsModule: FileAccess,
170178
permissionModule: Permissions,
171179
imagePickerModule: ImagePicker,
172180
mediaLibraryModule: CameraRoll,
173181
documentPickerModule: DocumentPicker,
174182
});
183+
const NativeMediaService = createNativeMediaService({
184+
VideoComponent: Video,
185+
thumbnailModule: CreateThumbnail,
186+
imageResizerModule: ImageResizer,
187+
});
175188
```
176189

177190
**Using Expo CLI**
178191

179-
You can use `createExpoClipboardService`, `createExpoNotificationService` and `createExpoFileService` helper functions with below expo modules.
192+
You can use `createExpoClipboardService`, `createExpoNotificationService`, `createExpoFileService` and `createExpoMediaService` helper functions with below expo modules.
180193

181194
```sh
182195
expo install expo-image-picker \
183196
expo-document-picker \
184197
expo-media-library \
185198
expo-file-system \
186199
expo-clipboard \
187-
expo-notifications
200+
expo-notifications \
201+
expo-av \
202+
expo-video-thumbnails \
203+
expo-image-manipulator
188204
```
189205

190206
```ts
@@ -194,15 +210,24 @@ import * as ExpoFS from 'expo-file-system';
194210
import * as ExpoImagePicker from 'expo-image-picker';
195211
import * as ExpoMediaLibrary from 'expo-media-library';
196212
import * as ExpoNotifications from 'expo-notifications';
213+
import * as ExpoAV from 'expo-av';
214+
import * as ExpoVideoThumbnail from 'expo-video-thumbnails';
215+
import * as ExpoImageManipulator from 'expo-image-manipulator';
197216

198-
const NotificationService = createExpoNotificationService(ExpoNotifications);
199-
const ClipboardService = createExpoClipboardService(ExpoClipboard);
200-
const FileService = createExpoFileService({
217+
const ExpoNotificationService = createExpoNotificationService(ExpoNotifications);
218+
const ExpoClipboardService = createExpoClipboardService(ExpoClipboard);
219+
const ExpoFileService = createExpoFileService({
201220
fsModule: ExpoFS,
202221
imagePickerModule: ExpoImagePicker,
203222
mediaLibraryModule: ExpoMediaLibrary,
204223
documentPickerModule: ExpoDocumentPicker,
205224
});
225+
const ExpoMediaService = createExpoMediaService({
226+
avModule: ExpoAV,
227+
thumbnailModule: ExpoVideoThumbnail,
228+
imageManipulator: ExpoImageManipulator,
229+
fsModule: ExpoFS,
230+
})
206231
```
207232

208233
### Local caching (optional)

packages/uikit-react-native/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@sendbird/uikit-utils": "2.2.0"
4747
},
4848
"devDependencies": {
49+
"@bam.tech/react-native-image-resizer": "^3.0.4",
4950
"@react-native-camera-roll/camera-roll": "^5.0.4",
5051
"@react-native-clipboard/clipboard": "^1.8.5",
5152
"@react-native-community/netinfo": "^9.3.0",
@@ -59,6 +60,7 @@
5960
"expo-clipboard": "^2.1.1",
6061
"expo-document-picker": "^10.1.3",
6162
"expo-file-system": "^13.1.4",
63+
"expo-image-manipulator": "^11.0.0",
6264
"expo-image-picker": "^12.0.2",
6365
"expo-media-library": "^14.0.1",
6466
"expo-notifications": "^0.14.1",
@@ -79,6 +81,7 @@
7981
"typescript": "4.9.4"
8082
},
8183
"peerDependencies": {
84+
"@bam.tech/react-native-image-resizer": ">=3.0.0",
8285
"@react-native-camera-roll/camera-roll": ">=5.0.0",
8386
"@react-native-clipboard/clipboard": ">=1.8.5",
8487
"@react-native-community/netinfo": ">=9.3.0",
@@ -89,6 +92,7 @@
8992
"expo-clipboard": ">=2.1.1",
9093
"expo-document-picker": ">=10.1.3",
9194
"expo-file-system": ">=13.1.4",
95+
"expo-image-manipulator": ">=11.0.0",
9296
"expo-image-picker": ">=12.0.2",
9397
"expo-notifications": ">=0.14.1",
9498
"expo-video-thumbnails": ">=6.4.0",
@@ -103,6 +107,9 @@
103107
"react-native-video": ">=5.2.0"
104108
},
105109
"peerDependenciesMeta": {
110+
"@bam.tech/react-native-image-resizer": {
111+
"optional": true
112+
},
106113
"@react-native-camera-roll/camera-roll": {
107114
"optional": true
108115
},
@@ -124,6 +131,9 @@
124131
"expo-file-system": {
125132
"optional": true
126133
},
134+
"expo-image-manipulator": {
135+
"optional": true
136+
},
127137
"expo-image-picker": {
128138
"optional": true
129139
},

packages/uikit-react-native/src/components/FileViewer.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,8 @@ const FileViewer = ({
6363
const fileType = getFileType(fileMessage.type || getFileExtension(fileMessage.url));
6464

6565
useEffect(() => {
66-
if (!mediaService?.VideoComponent || fileType === 'file') {
67-
onClose();
68-
}
69-
}, [mediaService]);
66+
if (fileType === 'file') onClose();
67+
}, []);
7068

7169
const fileViewer = useIIFE(() => {
7270
switch (fileType) {
@@ -83,7 +81,6 @@ const FileViewer = ({
8381

8482
case 'video':
8583
case 'audio': {
86-
if (!mediaService?.VideoComponent) return null;
8784
return (
8885
<mediaService.VideoComponent
8986
source={{ uri: fileMessage.url }}

packages/uikit-react-native/src/components/MessageRenderer/FileMessage/ImageFileMessage.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ const ImageFileMessage = ({ message, children, variant }: FileMessageProps) => {
4444
const key = useRetry(imageNotFound);
4545

4646
return (
47-
<View style={[styles.bubbleContainer, { backgroundColor: colors.ui.message[variant].enabled.background }]}>
47+
<View
48+
style={[
49+
styles.bubbleContainer,
50+
{ backgroundColor: imageNotFound ? colors.onBackground04 : colors.ui.message[variant].enabled.background },
51+
]}
52+
>
4853
<View style={style}>
4954
<Image
5055
key={key}

packages/uikit-react-native/src/components/MessageRenderer/FileMessage/VideoFileMessage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const useRetry = (videoFileUrl: string, retryCount = 5) => {
1515
const { mediaService } = usePlatformService();
1616

1717
const fetchThumbnail = () => {
18-
return mediaService?.getVideoThumbnail({ url: videoFileUrl, timeMills: 1000 }).then((result) => {
18+
return mediaService.getVideoThumbnail({ url: videoFileUrl, timeMills: 1000 }).then((result) => {
1919
setState({ loading: false, thumbnail: result?.path ?? null });
2020
});
2121
};

0 commit comments

Comments
 (0)