Skip to content

Commit

Permalink
fix: 3018 - now there's only one place where we upload pictures from (#…
Browse files Browse the repository at this point in the history
…3323)

Deleted file:
* `picture_capture_helper.dart`: moved code to `ConfirmAndUploadPicture`, that is now the only place where we upload images

Impacted files:
* `add_new_product_page.dart`: minor refactoring
* `background_task_details.dart`: minor refactoring
* `background_task_image.dart`: minor refactoring
* `confirm_and_upload_picture.dart`: minor refactoring; moved here code from `picture_capture_helper.dart`
* `edit_ingredients_page.dart`: replaced `uploadCapturedPicture` with a call to page `ConfirmAndUploadPicture`
* `image_crop_page.dart`: refactored into a single `startImageCropping` method
* `image_upload_page.dart`: replaced `uploadCapturedPicture` with a call to page `ConfirmAndUploadPicture`
* `new_crop_page.dart`: minor refactoring
* `product_image_gallery_view.dart`: replaced `uploadCapturedPicture` with a call to page `ConfirmAndUploadPicture`; refactoring around refresh
* `product_image_viewer.dart`: minor refactoring
* `up_to_date_product_provider.dart: removed old-fashioned code
  • Loading branch information
monsieurtanuki authored Nov 15, 2022
1 parent 7e4822e commit 3d944bb
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ class BackgroundTaskDetails extends AbstractBackgroundTask {
}

/// Returns a new background task about changing a product.
///
/// Either [productEditTask] or [productEditTasks] must be populated;
/// we need that for classification purpose (and unique id computation).
static BackgroundTaskDetails _getNewTask(
final Product minimalistProduct,
final String uniqueId,
Expand Down
15 changes: 15 additions & 0 deletions packages/smooth_app/lib/background/background_task_image.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:smooth_app/background/abstract_background_task.dart';
import 'package:smooth_app/generic_lib/duration_constants.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:task_manager/task_manager.dart';

Expand Down Expand Up @@ -69,6 +72,7 @@ class BackgroundTaskImage extends AbstractBackgroundTask {
final String barcode, {
required final ImageField imageField,
required final File imageFile,
required final State<StatefulWidget> widget,
}) async {
// For "OTHER" images we randomize the id with timestamp
// so that it runs separately.
Expand All @@ -93,6 +97,17 @@ class BackgroundTaskImage extends AbstractBackgroundTask {
uniqueId: uniqueId,
),
);
if (!widget.mounted) {
return;
}
ScaffoldMessenger.of(widget.context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(widget.context).image_upload_queued,
),
duration: SnackBarDuration.medium,
),
);
}

/// Uploads the product image.
Expand Down
21 changes: 15 additions & 6 deletions packages/smooth_app/lib/cards/data_cards/image_upload_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/data_models/product_image_data.dart';
import 'package:smooth_app/helpers/picture_capture_helper.dart';
import 'package:smooth_app/pages/image_crop_page.dart';
import 'package:smooth_app/pages/product/confirm_and_upload_picture.dart';
import 'package:smooth_app/pages/product/product_image_gallery_view.dart';

// TODO(monsieurtanuki): rename that class, like `ProductImageCarouselItem`
/// Displays a product image in the carousel: access to gallery, or new image.
///
/// If the image exists, it's displayed and a tap gives access to the gallery.
/// If not, a "add image" button is displayed.
class ImageUploadCard extends StatefulWidget {
const ImageUploadCard({
required this.product,
Expand Down Expand Up @@ -44,11 +49,15 @@ class _ImageUploadCardState extends State<ImageUploadCard> {
if (!mounted) {
return;
}
await uploadCapturedPicture(
widget: this,
barcode: widget.product.barcode!,
imageField: widget.productImageData.imageField,
imageUri: croppedImageFile.uri,
await Navigator.push<File>(
context,
MaterialPageRoute<File>(
builder: (BuildContext context) => ConfirmAndUploadPicture(
barcode: widget.product.barcode!,
imageField: widget.productImageData.imageField,
initialPhoto: croppedImageFile,
),
),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class UpToDateProductProvider {

final LocalDatabase localDatabase;

// TODO(monsieurtanuki): remove ASAP
final Map<String, Product> _map = <String, Product>{};

/// For a given barcode, maps the changes.
final UpToDateChanges _changes;

Expand All @@ -31,19 +28,6 @@ class UpToDateProductProvider {
/// because we cannot cache all products in memory.
final Map<String, int> _interestingBarcodes = <String, int>{};

// TODO(monsieurtanuki): remove ASAP
Product? get(final Product product) => _map[product.barcode!];

// TODO(monsieurtanuki): remove ASAP
Product? getFromBarcode(final String barcode) => _map[barcode];

// TODO(monsieurtanuki): remove ASAP
void set(final Product product) {
_map[product.barcode!] = product;
_timestamps[product.barcode!] = LocalDatabase.nowInMillis();
localDatabase.notifyListeners();
}

/// Returns true if at least one barcode was refreshed after the [timestamp].
bool needsRefresh(final int? latestTimestamp, final List<String> barcodes) {
if (latestTimestamp == null) {
Expand Down
67 changes: 0 additions & 67 deletions packages/smooth_app/lib/helpers/picture_capture_helper.dart

This file was deleted.

49 changes: 17 additions & 32 deletions packages/smooth_app/lib/pages/image_crop_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,8 @@ import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/helpers/camera_helper.dart';
import 'package:smooth_app/pages/crop_helper.dart';

/// Crops an image from an existing file.
Future<File?> startImageCroppingNoPick(
final BuildContext context, {
required final File existingImage,
}) async {
final NavigatorState navigator = Navigator.of(context);
final CropHelper cropHelper = CropHelper.getCurrent(context);
await _showScreenBetween(navigator);

// ignore: use_build_context_synchronously
final String? croppedPath = await cropHelper.getCroppedPath(
context,
existingImage.path,
);

await _hideScreenBetween(navigator);

if (croppedPath == null) {
return null;
}

return File(croppedPath);
}

/// Picks an image file from gallery or camera.
Future<XFile?> pickImageFile(final State<StatefulWidget> widget) async {
Future<XFile?> _pickImageFile(final State<StatefulWidget> widget) async {
final UserPictureSource? source = await _getUserPictureSource(widget.context);
if (source == null) {
return null;
Expand Down Expand Up @@ -102,8 +78,11 @@ Future<UserPictureSource?> _getUserPictureSource(
);
}

/// Crops an image picked from the gallery or camera.
Future<File?> startImageCropping(final State<StatefulWidget> widget) async {
/// Crops an image, either existing or picked from the gallery or camera.
Future<File?> startImageCropping(
final State<StatefulWidget> widget, {
final File? existingImage,
}) async {
// Show a loading page on the Flutter side
final NavigatorState navigator = Navigator.of(widget.context);
final CropHelper cropHelper = CropHelper.getCurrent(widget.context);
Expand All @@ -112,18 +91,24 @@ Future<File?> startImageCropping(final State<StatefulWidget> widget) async {
if (!widget.mounted) {
return null;
}
final XFile? pickedXFile = await pickImageFile(widget);
if (pickedXFile == null) {
await _hideScreenBetween(navigator);
return null;
final String sourceImagePath;
if (existingImage != null) {
sourceImagePath = existingImage.path;
} else {
final XFile? pickedXFile = await _pickImageFile(widget);
if (pickedXFile == null) {
await _hideScreenBetween(navigator);
return null;
}
sourceImagePath = pickedXFile.path;
}

if (!widget.mounted) {
return null;
}
final String? croppedPath = await cropHelper.getCroppedPath(
widget.context,
pickedXFile.path,
sourceImagePath,
);

await _hideScreenBetween(navigator);
Expand Down
16 changes: 9 additions & 7 deletions packages/smooth_app/lib/pages/product/add_new_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const List<ImageField> _SORTED_IMAGE_FIELD_LIST = <ImageField>[
ImageField.OTHER,
];

/// "Create a product we couldn't find on the server" page.
class AddNewProductPage extends StatefulWidget {
const AddNewProductPage(this.barcode);

Expand Down Expand Up @@ -157,11 +158,11 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
return rows;
}

Widget _buildAddImageButton(BuildContext context, ImageField imageType) {
Widget _buildAddImageButton(BuildContext context, ImageField imageField) {
return Padding(
padding: _ROW_PADDING_TOP,
child: SmoothLargeButtonWithIcon(
text: _getAddPhotoButtonText(context, imageType),
text: _getAddPhotoButtonText(context, imageField),
icon: Icons.camera_alt,
onPressed: () async {
final File? initialPhoto = await startImageCropping(this);
Expand All @@ -172,19 +173,20 @@ class _AddNewProductPageState extends State<AddNewProductPage> {
// may choose to retake the image.
// TODO(monsieurtanuki): careful, waiting for pop'ed value
//ignore: use_build_context_synchronously
final File? finalPhoto = await Navigator.push<File?>(
final File? finalPhoto = await Navigator.push<File>(
context,
MaterialPageRoute<File?>(
MaterialPageRoute<File>(
builder: (BuildContext context) => ConfirmAndUploadPicture(
barcode: widget.barcode,
imageType: imageType,
imageField: imageField,
initialPhoto: initialPhoto,
),
),
);
if (finalPhoto != null) {
_uploadedImages[imageType] = _uploadedImages[imageType] ?? <File>[];
_uploadedImages[imageType]!.add(initialPhoto);
_uploadedImages[imageField] =
_uploadedImages[imageField] ?? <File>[];
_uploadedImages[imageField]!.add(initialPhoto);
}
},
),
Expand Down
Loading

0 comments on commit 3d944bb

Please sign in to comment.