Skip to content

Commit 0fa5453

Browse files
chrisbobbegnprice
authored andcommitted
binding: Add pickFiles, wrapping file_picker.pickFiles
1 parent 6ee6ef2 commit 0fa5453

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

lib/model/binding.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:device_info_plus/device_info_plus.dart' as device_info_plus;
2+
import 'package:file_picker/file_picker.dart' as file_picker;
23
import 'package:firebase_core/firebase_core.dart' as firebase_core;
34
import 'package:firebase_messaging/firebase_messaging.dart' as firebase_messaging;
45
import 'package:flutter/foundation.dart';
@@ -11,6 +12,8 @@ import '../log.dart';
1112
import '../widgets/store.dart';
1213
import 'store.dart';
1314

15+
export 'package:file_picker/file_picker.dart' show FilePickerResult, FileType, PlatformFile;
16+
1417
/// Alias for [url_launcher.LaunchMode].
1518
typedef UrlLaunchMode = url_launcher.LaunchMode;
1619

@@ -152,6 +155,15 @@ abstract class ZulipBinding {
152155

153156
/// Wraps the [AndroidNotificationHostApi] constructor.
154157
AndroidNotificationHostApi get androidNotificationHost;
158+
159+
/// Pick files from the media library, via package:file_picker.
160+
///
161+
/// This wraps [file_picker.pickFiles].
162+
Future<file_picker.FilePickerResult?> pickFiles({
163+
bool allowMultiple,
164+
bool withReadStream,
165+
file_picker.FileType type,
166+
});
155167
}
156168

157169
/// Like [device_info_plus.BaseDeviceInfo], but without things we don't use.
@@ -389,4 +401,17 @@ class LiveZulipBinding extends ZulipBinding {
389401

390402
@override
391403
AndroidNotificationHostApi get androidNotificationHost => AndroidNotificationHostApi();
404+
405+
@override
406+
Future<file_picker.FilePickerResult?> pickFiles({
407+
bool allowMultiple = false,
408+
bool withReadStream = false,
409+
file_picker.FileType type = file_picker.FileType.any,
410+
}) async {
411+
return file_picker.FilePicker.platform.pickFiles(
412+
allowMultiple: allowMultiple,
413+
withReadStream: withReadStream,
414+
type: type,
415+
);
416+
}
392417
}

lib/widgets/compose_box.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:app_settings/app_settings.dart';
2-
import 'package:file_picker/file_picker.dart';
32
import 'package:flutter/material.dart';
43
import 'package:flutter/services.dart';
54
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
@@ -8,6 +7,7 @@ import 'package:image_picker/image_picker.dart';
87
import '../api/exception.dart';
98
import '../api/model/model.dart';
109
import '../api/route/messages.dart';
10+
import '../model/binding.dart';
1111
import '../model/compose.dart';
1212
import '../model/narrow.dart';
1313
import '../model/store.dart';
@@ -542,7 +542,7 @@ abstract class _AttachUploadsButton extends StatelessWidget {
542542
Future<Iterable<_File>> _getFilePickerFiles(BuildContext context, FileType type) async {
543543
FilePickerResult? result;
544544
try {
545-
result = await FilePicker.platform
545+
result = await ZulipBinding.instance
546546
.pickFiles(allowMultiple: true, withReadStream: true, type: type);
547547
} catch (e) {
548548
if (!context.mounted) return [];

test/model/binding.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class TestZulipBinding extends ZulipBinding {
7373
_resetPackageInfo();
7474
_resetFirebase();
7575
_resetNotifications();
76+
_resetPickFiles();
7677
}
7778

7879
/// The current global store offered to a [GlobalStoreWidget].
@@ -285,6 +286,48 @@ class TestZulipBinding extends ZulipBinding {
285286
FakeAndroidNotificationHostApi get androidNotificationHost {
286287
return (_androidNotificationHostApi ??= FakeAndroidNotificationHostApi());
287288
}
289+
290+
/// The value that `ZulipBinding.instance.pickFiles()` should return.
291+
///
292+
/// See also [takePickFilesCalls].
293+
FilePickerResult? pickFilesResult;
294+
295+
void _resetPickFiles() {
296+
pickFilesResult = null;
297+
_pickFilesCalls = null;
298+
}
299+
300+
/// Consume the log of calls made to `ZulipBinding.instance.pickFiles()`.
301+
///
302+
/// This returns a list of the arguments to all calls made
303+
/// to `ZulipBinding.instance.pickFiles()` since the last call to
304+
/// either this method or [reset].
305+
///
306+
/// See also [pickFilesResult].
307+
List<({
308+
bool? allowMultiple,
309+
bool? withReadStream,
310+
FileType? type,
311+
})> takePickFilesCalls() {
312+
final result = _pickFilesCalls;
313+
_pickFilesCalls = null;
314+
return result ?? [];
315+
}
316+
List<({
317+
bool? allowMultiple,
318+
bool? withReadStream,
319+
FileType? type,
320+
})>? _pickFilesCalls;
321+
322+
@override
323+
Future<FilePickerResult?> pickFiles({
324+
bool? allowMultiple,
325+
bool? withReadStream,
326+
FileType? type,
327+
}) async {
328+
(_pickFilesCalls ??= []).add((allowMultiple: allowMultiple, withReadStream: withReadStream, type: type));
329+
return pickFilesResult;
330+
}
288331
}
289332

290333
class FakeFirebaseMessaging extends Fake implements FirebaseMessaging {

0 commit comments

Comments
 (0)