Skip to content

Commit 3ec8c09

Browse files
committed
compose [nfc]: Split abstract base class out from _AttachFileButton
The other upload buttons will extend this new base class too. Prompted by Greg's review: zulip#70 (comment)
1 parent 847001c commit 3ec8c09

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

lib/widgets/compose_box.dart

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -280,41 +280,63 @@ Future<void> _uploadFiles({
280280
}
281281
}
282282

283-
class _AttachFileButton extends StatelessWidget {
284-
const _AttachFileButton({required this.contentController, required this.contentFocusNode});
283+
abstract class _AttachUploadsButton extends StatelessWidget {
284+
const _AttachUploadsButton({required this.contentController, required this.contentFocusNode});
285285

286286
final ContentTextEditingController contentController;
287287
final FocusNode contentFocusNode;
288288

289+
IconData get icon;
290+
Future<Iterable<_File>?> getFiles(BuildContext context);
291+
289292
void _handlePress(BuildContext context) async {
293+
final files = await getFiles(context);
294+
if (files == null) {
295+
return; // Nothing to do (getFiles handles user feedback)
296+
}
297+
298+
if (context.mounted) {} // https://github.com/dart-lang/linter/issues/4007
299+
else {
300+
return;
301+
}
302+
303+
await _uploadFiles(
304+
context: context,
305+
contentController: contentController,
306+
contentFocusNode: contentFocusNode,
307+
files: files);
308+
}
309+
310+
@override
311+
Widget build(BuildContext context) {
312+
return IconButton(icon: Icon(icon), onPressed: () => _handlePress(context));
313+
}
314+
}
315+
316+
class _AttachFileButton extends _AttachUploadsButton {
317+
const _AttachFileButton({required super.contentController, required super.contentFocusNode});
318+
319+
@override
320+
IconData get icon => Icons.attach_file;
321+
322+
@override
323+
Future<Iterable<_File>?> getFiles(BuildContext context) async {
290324
FilePickerResult? result;
291325
try {
292326
result = await FilePicker.platform.pickFiles(allowMultiple: true, withReadStream: true);
293327
} catch (e) {
294328
// TODO(i18n)
295329
showErrorDialog(context: context, title: 'Error', message: e.toString());
296-
return;
330+
return null;
297331
}
298332
if (result == null) {
299-
return; // User cancelled; do nothing
300-
}
301-
302-
if (context.mounted) {} // https://github.com/dart-lang/linter/issues/4007
303-
else {
304-
return;
333+
return null; // User cancelled; do nothing
305334
}
306335

307-
final Iterable<_File> files = result.files.map((f) {
336+
return result.files.map((f) {
308337
assert(f.readStream != null); // We passed `withReadStream: true` to pickFiles.
309338
return _File(content: f.readStream!, length: f.size, filename: f.name);
310339
});
311-
await _uploadFiles(context: context, contentController: contentController, contentFocusNode: contentFocusNode,
312-
files: files);
313-
}
314-
315-
@override
316-
Widget build(BuildContext context) {
317-
return IconButton(icon: const Icon(Icons.attach_file), onPressed: () => _handlePress(context));
318340
}
319341
}
320342

0 commit comments

Comments
 (0)