Skip to content

Commit 240d165

Browse files
committed
compose_box: Use pickMultipleMedia for the attach-media button
Fixes part of zulip#855. Fixes zulip#1308. Fixes zulip#1510. The previous implementation used 'FileType.media' from the 'file_picker' package. We switched to using 'pickMultipleMedia' from 'image_picker' as it is a first-party package and is more actively supported. On Android, the current code can still open a general file browser that lets user pick non-media files. This is to be fixed in subsequent commits by enabling Android Photo Picker. See: flutter/flutter#184826
1 parent cb5cf36 commit 240d165

2 files changed

Lines changed: 80 additions & 21 deletions

File tree

‎lib/widgets/compose_box.dart‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,18 @@ class _AttachMediaButton extends _AttachUploadsButton {
12031203
@override
12041204
Future<Iterable<FileToUpload>> getFiles(BuildContext context) async {
12051205
// TODO(#114): This doesn't give quite the right UI on Android.
1206-
return _getFilePickerFiles(context, FileType.media);
1206+
final List<XFile> results;
1207+
try {
1208+
results = await ZulipBinding.instance.pickMultipleMedia(
1209+
requestFullMetadata: false);
1210+
} catch (e) {
1211+
if (!context.mounted) return [];
1212+
showErrorDialog(context: context,
1213+
title: ZulipLocalizations.of(context).errorDialogTitle,
1214+
message: e.toString());
1215+
return [];
1216+
}
1217+
return Future.wait(results.map(_fileFromXFile));
12071218
}
12081219
}
12091220

‎test/widgets/compose_box_test.dart‎

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,22 +1223,21 @@ void main() {
12231223
await prepare(tester);
12241224
checkAppearsLoading(tester, false);
12251225

1226-
testBinding.pickFilesResult = FilePickerResult([PlatformFile(
1227-
readStream: Stream.fromIterable(['asdf'.codeUnits]),
1228-
// TODO test inference of MIME type from initial bytes, when
1229-
// it can't be inferred from path
1230-
path: '/private/var/mobile/Containers/Data/Application/foo/tmp/image.jpg',
1226+
testBinding.pickMultipleMediaResult = [XFile.fromData(
1227+
// TODO test inference of MIME type when it's missing here
1228+
mimeType: 'image/jpeg',
1229+
utf8.encode('asdf'),
12311230
name: 'image.jpg',
1232-
size: 12345,
1233-
)]);
1231+
length: 12345,
1232+
path: '/private/var/mobile/Containers/Data/Application/foo/tmp/image.jpg',
1233+
)];
12341234
connection.prepare(delay: const Duration(seconds: 1), json:
12351235
UploadFileResult(url: '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg').toJson());
12361236

12371237
await tester.tap(find.byIcon(ZulipIcons.image));
12381238
await tester.pump();
1239-
final call = testBinding.takePickFilesCalls().single;
1240-
check(call.allowMultiple).equals(true);
1241-
check(call.type).equals(FileType.media);
1239+
final call = testBinding.takePickMultipleMediaCalls().single;
1240+
check(call.requestFullMetadata).equals(false);
12421241

12431242
checkNoDialog(tester);
12441243

@@ -1263,8 +1262,59 @@ void main() {
12631262
checkAppearsLoading(tester, false);
12641263
});
12651264

1265+
testWidgets('multiple files', (tester) async {
1266+
await prepare(tester);
1267+
checkAppearsLoading(tester, false);
1268+
1269+
testBinding.pickMultipleMediaResult = [
1270+
XFile.fromData(
1271+
mimeType: 'image/jpeg',
1272+
utf8.encode('asdf'),
1273+
name: 'image.jpg',
1274+
length: 12345,
1275+
path: '/private/var/mobile/Containers/Data/Application/foo/tmp/image.jpg'),
1276+
XFile.fromData(
1277+
mimeType: 'image/gif',
1278+
utf8.encode('asdf'),
1279+
name: 'test.gif',
1280+
length: 12345,
1281+
path: '/private/var/mobile/Containers/Data/Application/foo/tmp/test.gif'),
1282+
];
1283+
connection.prepare(delay: const Duration(seconds: 1), json:
1284+
UploadFileResult(url: '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg').toJson());
1285+
connection.prepare(delay: const Duration(seconds: 1), json:
1286+
UploadFileResult(url: '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/test.gif').toJson());
1287+
1288+
await tester.tap(find.byIcon(ZulipIcons.image));
1289+
await tester.pump();
1290+
final call = testBinding.takePickMultipleMediaCalls().single;
1291+
check(call.requestFullMetadata).equals(false);
1292+
1293+
checkNoDialog(tester);
1294+
1295+
check(controller!.content.text).equals(
1296+
'see image: [Uploading image.jpg…]()\n\n[Uploading test.gif…]()\n\n');
1297+
checkAppearsLoading(tester, true);
1298+
1299+
await tester.pump(const Duration(seconds: 1));
1300+
check(controller!.content.text).equals(
1301+
'see image: [image.jpg](/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg)\n\n[Uploading test.gif…]()\n\n');
1302+
checkAppearsLoading(tester, true);
1303+
1304+
await tester.pump(const Duration(seconds: 1));
1305+
check(controller!.content.text).equals(
1306+
'see image: [image.jpg](/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg)\n\n[test.gif](/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/test.gif)\n\n');
1307+
checkAppearsLoading(tester, false);
1308+
});
1309+
12661310
// TODO test what happens when selecting/uploading fails
1267-
});
1311+
},
1312+
// This test fails on Windows because [XFile.name] splits on
1313+
// [Platform.pathSeparator], corresponding to the actual host platform
1314+
// the test is running on, instead of the path separator for the
1315+
// target platform the test is simulating.
1316+
// TODO(upstream): unskip after fix to https://github.com/flutter/flutter/issues/161073
1317+
skip: Platform.isWindows);
12681318

12691319
group('attach from camera', () {
12701320
testWidgets('success', (tester) async {
@@ -1313,10 +1363,6 @@ void main() {
13131363

13141364
// TODO test what happens when capturing/uploading fails
13151365
},
1316-
// This test fails on Windows because [XFile.name] splits on
1317-
// [Platform.pathSeparator], corresponding to the actual host platform
1318-
// the test is running on, instead of the path separator for the
1319-
// target platform the test is simulating.
13201366
// TODO(upstream): unskip after fix to https://github.com/flutter/flutter/issues/161073
13211367
skip: Platform.isWindows);
13221368

@@ -1330,12 +1376,12 @@ void main() {
13301376
await prepareComposeBox(tester,
13311377
narrow: narrow, subscriptions: [eg.subscription(channel)]);
13321378

1333-
testBinding.pickFilesResult = FilePickerResult([PlatformFile(
1334-
readStream: Stream.fromIterable(['asdf'.codeUnits]),
1379+
testBinding.pickMultipleMediaResult = [XFile.fromData(
1380+
utf8.encode('asdf'),
13351381
path: '/some/path/한국어 파일.txt',
13361382
name: '한국어 파일.txt',
1337-
size: 4,
1338-
)]);
1383+
length: 4,
1384+
)];
13391385
connection.prepare(json: UploadFileResult(url:
13401386
'/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/한국어 파일.txt').toJson());
13411387
await tester.tap(find.byIcon(ZulipIcons.image));
@@ -1347,7 +1393,9 @@ void main() {
13471393
check(controller!.content.text)
13481394
.equals('[한국어 파일.txt]('
13491395
'/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/한국어 파일.txt)\n\n');
1350-
});
1396+
},
1397+
// TODO(upstream): unskip after fix to https://github.com/flutter/flutter/issues/161073
1398+
skip: Platform.isWindows);
13511399

13521400
group('attach from keyboard', () {
13531401
// This is adapted from:

0 commit comments

Comments
 (0)