|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:crop_image/crop_image.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; |
| 6 | +import 'package:image_picker/image_picker.dart'; |
| 7 | +import 'package:provider/provider.dart'; |
| 8 | +import 'package:smooth_app/background/background_task_upload.dart'; |
| 9 | +import 'package:smooth_app/database/dao_int.dart'; |
| 10 | +import 'package:smooth_app/database/local_database.dart'; |
| 11 | +import 'package:smooth_app/generic_lib/buttons/smooth_large_button_with_icon.dart'; |
| 12 | +import 'package:smooth_app/generic_lib/design_constants.dart'; |
| 13 | +import 'package:smooth_app/generic_lib/widgets/smooth_card.dart'; |
| 14 | +import 'package:smooth_app/helpers/database_helper.dart'; |
| 15 | +import 'package:smooth_app/pages/crop_helper.dart'; |
| 16 | +import 'package:smooth_app/pages/crop_parameters.dart'; |
| 17 | +import 'package:smooth_app/pages/prices/price_add_helper.dart'; |
| 18 | +import 'package:smooth_app/pages/prices/price_model.dart'; |
| 19 | + |
| 20 | +/// Card that displays the bulk proof button for price adding. |
| 21 | +class PriceBulkProofCard extends StatefulWidget { |
| 22 | + const PriceBulkProofCard(this.formKey); |
| 23 | + |
| 24 | + final GlobalKey<FormState> formKey; |
| 25 | + |
| 26 | + @override |
| 27 | + State<PriceBulkProofCard> createState() => _PriceBulkProofCardState(); |
| 28 | +} |
| 29 | + |
| 30 | +class _PriceBulkProofCardState extends State<PriceBulkProofCard> { |
| 31 | + String _text = ''; |
| 32 | + |
| 33 | + @override |
| 34 | + Widget build(BuildContext context) { |
| 35 | + final PriceModel model = context.watch<PriceModel>(); |
| 36 | + final AppLocalizations appLocalizations = AppLocalizations.of(context); |
| 37 | + return SmoothCardWithRoundedHeader( |
| 38 | + title: appLocalizations.prices_bulk_proof_upload_subtitle, |
| 39 | + leading: const Icon(Icons.document_scanner_rounded), |
| 40 | + contentPadding: const EdgeInsetsDirectional.symmetric( |
| 41 | + horizontal: SMALL_SPACE, |
| 42 | + vertical: MEDIUM_SPACE, |
| 43 | + ), |
| 44 | + child: Column( |
| 45 | + children: <Widget>[ |
| 46 | + ListTile( |
| 47 | + trailing: const Icon(Icons.warning), |
| 48 | + title: Text( |
| 49 | + appLocalizations.prices_bulk_proof_upload_warning, |
| 50 | + ), |
| 51 | + ), |
| 52 | + SmoothLargeButtonWithIcon( |
| 53 | + text: appLocalizations.prices_bulk_proof_upload_select, |
| 54 | + leadingIcon: const Icon(Icons.add), |
| 55 | + onPressed: model.location == null |
| 56 | + ? null |
| 57 | + : () async => _selectAndUpload(model: model), |
| 58 | + ), |
| 59 | + if (_text.isNotEmpty) Text(_text), |
| 60 | + ], |
| 61 | + ), |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + Future<void> _selectAndUpload({ |
| 66 | + required PriceModel model, |
| 67 | + }) async { |
| 68 | + final PriceAddHelper priceAddHelper = PriceAddHelper(context); |
| 69 | + final LocalDatabase localDatabase = context.read<LocalDatabase>(); |
| 70 | + const int imageQuality = 80; |
| 71 | + final Directory directory = await BackgroundTaskUpload.getDirectory(); |
| 72 | + const String BULK_PROOF_IMAGE_SEQUENCE_KEY = 'bulk_proof_image_sequence'; |
| 73 | + final Rect cropRect = CropHelper.getLocalCropRectFromRect( |
| 74 | + CropHelper.fullImageCropRect, |
| 75 | + ); |
| 76 | + |
| 77 | + setState(() => _text = ''); |
| 78 | + final List<XFile> xFiles = await ImagePicker().pickMultiImage( |
| 79 | + imageQuality: imageQuality, |
| 80 | + requestFullMetadata: false, |
| 81 | + ); |
| 82 | + if (xFiles.isEmpty) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (!await priceAddHelper.acceptsWarning()) { |
| 87 | + return; |
| 88 | + } |
| 89 | + if (!mounted) { |
| 90 | + return; |
| 91 | + } |
| 92 | + late int index; |
| 93 | + final int count = xFiles.length; |
| 94 | + try { |
| 95 | + index = 0; |
| 96 | + for (final XFile xFile in xFiles) { |
| 97 | + index++; |
| 98 | + final int sequenceNumber = await getNextSequenceNumber( |
| 99 | + DaoInt(localDatabase), |
| 100 | + BULK_PROOF_IMAGE_SEQUENCE_KEY, |
| 101 | + ); |
| 102 | + final String path = xFile.path; |
| 103 | + final File temporaryFile = File(path); |
| 104 | + final int pos = path.lastIndexOf(Platform.pathSeparator); |
| 105 | + final String filename = path.substring(pos + 1); |
| 106 | + final File toBeUploadedFile = File( |
| 107 | + '${directory.path}/bulk_proof_${sequenceNumber}_$filename', |
| 108 | + ); |
| 109 | + setState( |
| 110 | + () => _text = 'Locally copying file #$index/$count', |
| 111 | + ); |
| 112 | + await temporaryFile.copy(toBeUploadedFile.path); |
| 113 | + await temporaryFile.delete(); |
| 114 | + |
| 115 | + setState(() => _text = 'Preparing upload #$index/$count'); |
| 116 | + model.cropParameters = CropParameters( |
| 117 | + fullFile: toBeUploadedFile, |
| 118 | + smallCroppedFile: null, |
| 119 | + rotation: CropRotation.up.degrees, |
| 120 | + cropRect: cropRect, |
| 121 | + eraserCoordinates: null, |
| 122 | + ); |
| 123 | + if (!mounted) { |
| 124 | + return; |
| 125 | + } |
| 126 | + if (!await priceAddHelper.check(model, widget.formKey)) { |
| 127 | + return; |
| 128 | + } |
| 129 | + if (!mounted) { |
| 130 | + return; |
| 131 | + } |
| 132 | + await model.addTask(context); |
| 133 | + model.clearProof(); |
| 134 | + } |
| 135 | + } catch (e) { |
| 136 | + setState(() => _text = 'Failed at image #$index/$count'); |
| 137 | + return; |
| 138 | + } |
| 139 | + setState(() => _text = ''); |
| 140 | + } |
| 141 | +} |
0 commit comments