Skip to content

Commit 2142a06

Browse files
committed
compose_box: Use pickMultipleMedia for attach-media on Android
Fixes part of zulip#855. Fixes zulip#1510. The attach-media button previously used 'FileType.media' from the 'file_picker' package on all platforms. On Android, that opens a general file browser instead of a media gallery. Switch to 'pickMultipleMedia' from 'image_picker', on Android. On iOS, file_picker already presents the native media picker, while image_picker would re-encode picked images to JPEG, discarding the original file. See: zulip#2331 (review) On Android, the new code can still open a general file browser on certain Android versions. This is to be fixed in subsequent commits by enabling the Android Photo Picker. See: flutter/flutter#184826
1 parent a1e4687 commit 2142a06

2 files changed

Lines changed: 120 additions & 8 deletions

File tree

‎lib/widgets/compose_box.dart‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:math';
33
import 'dart:ui';
44

55
import 'package:app_settings/app_settings.dart';
6+
import 'package:flutter/foundation.dart';
67
import 'package:flutter/material.dart';
78
import 'package:flutter/scheduler.dart';
89
import 'package:flutter/services.dart';
@@ -1202,8 +1203,27 @@ class _AttachMediaButton extends _AttachUploadsButton {
12021203

12031204
@override
12041205
Future<Iterable<FileToUpload>> getFiles(BuildContext context) async {
1206+
if (defaultTargetPlatform == TargetPlatform.iOS) {
1207+
// On iOS, this gives the native media picker. Switching to
1208+
// `image_picker` would be a regression: it re-encodes picked images
1209+
// to JPEG instead of giving the original files. See:
1210+
// https://github.com/zulip/zulip-flutter/pull/2331#pullrequestreview-4604793479
1211+
return _getFilePickerFiles(context, FileType.media);
1212+
}
1213+
12051214
// TODO(#114): This doesn't give quite the right UI on Android.
1206-
return _getFilePickerFiles(context, FileType.media);
1215+
final List<XFile> results;
1216+
try {
1217+
results = await ZulipBinding.instance.pickMultipleMedia(
1218+
requestFullMetadata: false);
1219+
} catch (e) {
1220+
if (!context.mounted) return [];
1221+
showErrorDialog(context: context,
1222+
title: ZulipLocalizations.of(context).errorDialogTitle,
1223+
message: e.toString());
1224+
return [];
1225+
}
1226+
return Future.wait(results.map(_fileFromXFile));
12071227
}
12081228
}
12091229

‎test/widgets/compose_box_test.dart‎

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,103 @@ void main() {
12191219
}
12201220

12211221
group('attach from media library', () {
1222-
testWidgets('success', (tester) async {
1222+
group('Android (uses image_picker)', () {
1223+
testWidgets('success', (tester) async {
1224+
await prepare(tester);
1225+
checkAppearsLoading(tester, false);
1226+
1227+
testBinding.pickMultipleMediaResult = [XFile.fromData(
1228+
// TODO test inference of MIME type when it's missing here
1229+
mimeType: 'image/jpeg',
1230+
utf8.encode('asdf'),
1231+
name: 'image.jpg',
1232+
length: 12345,
1233+
path: '/private/var/mobile/Containers/Data/Application/foo/tmp/image.jpg',
1234+
)];
1235+
connection.prepare(delay: const Duration(seconds: 1), json:
1236+
UploadFileResult(url: '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg').toJson());
1237+
1238+
await tester.tap(find.byIcon(ZulipIcons.image));
1239+
await tester.pump();
1240+
final call = testBinding.takePickMultipleMediaCalls().single;
1241+
check(call.requestFullMetadata).equals(false);
1242+
1243+
checkNoDialog(tester);
1244+
1245+
check(controller!.content.text)
1246+
.equals('see image: [Uploading image.jpg…]()\n\n');
1247+
// (the request is checked more thoroughly in API tests)
1248+
check(connection.lastRequest!).isA<http.MultipartRequest>()
1249+
..method.equals('POST')
1250+
..files.single.which((it) => it
1251+
..field.equals('file')
1252+
..length.equals(12345)
1253+
..filename.equals('image.jpg')
1254+
..contentType.asString.equals('image/jpeg')
1255+
..has<Future<List<int>>>((f) => f.finalize().toBytes(), 'contents')
1256+
.completes((it) => it.deepEquals(['asdf'.codeUnits].expand((l) => l)))
1257+
);
1258+
checkAppearsLoading(tester, true);
1259+
1260+
await tester.pump(const Duration(seconds: 1));
1261+
check(controller!.content.text)
1262+
.equals('see image: [image.jpg](/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg)\n\n');
1263+
checkAppearsLoading(tester, false);
1264+
}, variant: const TargetPlatformVariant({TargetPlatform.android}));
1265+
1266+
testWidgets('multiple files', (tester) async {
1267+
await prepare(tester);
1268+
checkAppearsLoading(tester, false);
1269+
1270+
testBinding.pickMultipleMediaResult = [
1271+
XFile.fromData(
1272+
mimeType: 'image/jpeg',
1273+
utf8.encode('asdf'),
1274+
name: 'image.jpg',
1275+
length: 12345,
1276+
path: '/private/var/mobile/Containers/Data/Application/foo/tmp/image.jpg'),
1277+
XFile.fromData(
1278+
mimeType: 'image/gif',
1279+
utf8.encode('asdf'),
1280+
name: 'test.gif',
1281+
length: 12345,
1282+
path: '/private/var/mobile/Containers/Data/Application/foo/tmp/test.gif'),
1283+
];
1284+
connection.prepare(delay: const Duration(seconds: 1), json:
1285+
UploadFileResult(url: '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg').toJson());
1286+
connection.prepare(delay: const Duration(seconds: 1), json:
1287+
UploadFileResult(url: '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/test.gif').toJson());
1288+
1289+
await tester.tap(find.byIcon(ZulipIcons.image));
1290+
await tester.pump();
1291+
final call = testBinding.takePickMultipleMediaCalls().single;
1292+
check(call.requestFullMetadata).equals(false);
1293+
1294+
checkNoDialog(tester);
1295+
1296+
check(controller!.content.text).equals(
1297+
'see image: [Uploading image.jpg…]()\n\n[Uploading test.gif…]()\n\n');
1298+
checkAppearsLoading(tester, true);
1299+
1300+
await tester.pump(const Duration(seconds: 1));
1301+
check(controller!.content.text).equals(
1302+
'see image: [image.jpg](/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg)\n\n[Uploading test.gif…]()\n\n');
1303+
checkAppearsLoading(tester, true);
1304+
1305+
await tester.pump(const Duration(seconds: 1));
1306+
check(controller!.content.text).equals(
1307+
'see image: [image.jpg](/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg)\n\n[test.gif](/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/test.gif)\n\n');
1308+
checkAppearsLoading(tester, false);
1309+
}, variant: const TargetPlatformVariant({TargetPlatform.android}));
1310+
},
1311+
// These tests fail on Windows because [XFile.name] splits on
1312+
// [Platform.pathSeparator], corresponding to the actual host platform
1313+
// the test is running on, instead of the path separator for the
1314+
// target platform the test is simulating.
1315+
// TODO(upstream): unskip after fix to https://github.com/flutter/flutter/issues/161073
1316+
skip: Platform.isWindows);
1317+
1318+
testWidgets('iOS (uses file_picker): success', (tester) async {
12231319
await prepare(tester);
12241320
checkAppearsLoading(tester, false);
12251321

@@ -1261,7 +1357,7 @@ void main() {
12611357
check(controller!.content.text)
12621358
.equals('see image: [image.jpg](/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/image.jpg)\n\n');
12631359
checkAppearsLoading(tester, false);
1264-
});
1360+
}, variant: const TargetPlatformVariant({TargetPlatform.iOS}));
12651361

12661362
// TODO test what happens when selecting/uploading fails
12671363
});
@@ -1313,10 +1409,6 @@ void main() {
13131409

13141410
// TODO test what happens when capturing/uploading fails
13151411
},
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.
13201412
// TODO(upstream): unskip after fix to https://github.com/flutter/flutter/issues/161073
13211413
skip: Platform.isWindows);
13221414

@@ -1338,7 +1430,7 @@ void main() {
13381430
)]);
13391431
connection.prepare(json: UploadFileResult(url:
13401432
'/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/한국어 파일.txt').toJson());
1341-
await tester.tap(find.byIcon(ZulipIcons.image));
1433+
await tester.tap(find.byIcon(ZulipIcons.attach_file));
13421434
await tester.pump();
13431435
check(controller!.content.text)
13441436
.equals('[Uploading 한국어 파일.txt…]()\n\n');

0 commit comments

Comments
 (0)