Skip to content

Fix: Image picker crash and restrict selection to images only #1516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions lib/model/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@ abstract class ZulipBinding {
AndroidNotificationHostApi get androidNotificationHost;

/// Pick files from the media library, via package:file_picker.
///
/// This wraps [file_picker.pickFiles].
Future<file_picker.FilePickerResult?> pickFiles({
bool allowMultiple,
bool withReadStream,
file_picker.FileType type,
});
///
/// This wraps [file_picker.pickFiles].
Future<file_picker.FilePickerResult?> pickFiles({
bool allowMultiple,
bool withReadStream,
file_picker.FileType type,
});

/// Pick files from the camera or media library, via package:image_picker.
///
/// This wraps [image_picker.pickImage].
Future<image_picker.XFile?> pickImage({
required image_picker.ImageSource source,
bool requestFullMetadata,
});
/// Pick files from the camera or media library, via package:image_picker.
///
/// This wraps [image_picker.pickImage].
Future<image_picker.XFile?> pickImage({
required image_picker.ImageSource source,
bool requestFullMetadata,
});

/// Enables or disables keeping the screen on, via package:wakelock_plus.
///
Expand Down Expand Up @@ -473,12 +473,12 @@ class LiveZulipBinding extends ZulipBinding {
Future<file_picker.FilePickerResult?> pickFiles({
bool allowMultiple = false,
bool withReadStream = false,
file_picker.FileType type = file_picker.FileType.any,
file_picker.FileType type = file_picker.FileType.image, // Allow any type of file for attachment icon
}) async {
return file_picker.FilePicker.platform.pickFiles(
allowMultiple: allowMultiple,
withReadStream: withReadStream,
type: type,
type: type, // This allows multiple types of files (images, documents, etc.)
);
}

Expand All @@ -487,10 +487,13 @@ class LiveZulipBinding extends ZulipBinding {
required image_picker.ImageSource source,
bool requestFullMetadata = true,
}) async {
return image_picker.ImagePicker()
.pickImage(source: source, requestFullMetadata: requestFullMetadata);
return image_picker.ImagePicker().pickImage(
source: source, // Only images can be selected, from gallery or camera
requestFullMetadata: requestFullMetadata,
);
}


@override
Future<void> toggleWakelock({required bool enable}) async {
return wakelock_plus.WakelockPlus.toggle(enable: enable);
Expand Down
10 changes: 7 additions & 3 deletions lib/widgets/compose_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1003,11 +1003,15 @@ abstract class _AttachUploadsButton extends StatelessWidget {
}
}

Future<Iterable<_File>> _getFilePickerFiles(BuildContext context, FileType type) async {
Future<Iterable<_File>> _getFilePickerFiles(BuildContext context, FileType type, {bool withReadStream= false,}) async {
FilePickerResult? result;
try {
result = await ZulipBinding.instance
.pickFiles(allowMultiple: true, withReadStream: true, type: type);
.pickFiles(
allowMultiple: true,
withReadStream: withReadStream,
type: type,
);
} catch (e) {
if (!context.mounted) return [];
final zulipLocalizations = ZulipLocalizations.of(context);
Expand Down Expand Up @@ -1086,7 +1090,7 @@ class _AttachMediaButton extends _AttachUploadsButton {
@override
Future<Iterable<_File>> getFiles(BuildContext context) async {
// TODO(#114): This doesn't give quite the right UI on Android.
return _getFilePickerFiles(context, FileType.media);
return _getFilePickerFiles(context, FileType.image,withReadStream: true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/widgets/compose_box_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ void main() {
await tester.pump();
final call = testBinding.takePickFilesCalls().single;
check(call.allowMultiple).equals(true);
check(call.type).equals(FileType.media);
check(call.type).equals(FileType.image);

checkNoErrorDialog(tester);

Expand Down