Skip to content

Commit

Permalink
feat: 5326 - add prices to proof from gallery (and thumbnails) (#5670)
Browse files Browse the repository at this point in the history
* feat: 5326 - add prices to proof from gallery (and thumbnails)

New files:
* `background_task_add_other_price.dart`: Background task about adding prices to an existing proof.
* `background_task_price.dart`: Abstract background task about adding prices.

Impacted files:
* `background_task_add_price.dart`: refactored using new class `BackgroundTaskPrice`
* `get_prices_model.dart`: new web app root address
* `operation_type.dart`: added the new background task
* `osm_location.dart`: minor refactoring
* `price_currency_selector.dart`: additional case - proof instead of input; refactored using model
* `price_date_card.dart`: additional case - proof instead of input
* `price_location_card.dart`: additional case - proof instead of input
* `price_model.dart`: added specific constructor and background task for the "existing proof" case
* `price_product_widget.dart`: minor refactoring
* `price_proof_card.dart`: additional case - proof instead of input
* `price_proof_page.dart`: added a FAB to add price from an existing proof; now displaying the thumbnail and then the full proof
* `price_user_button.dart`: new web app root address
* `prices_proofs_page.dart`: proof thumnails; new web app root address
* `prices_users_page.dart`: new web app root address
* `product_price_add_page.dart`: now accepting a model as parameter, in order to deal with both old proofs and new prices
* `pubspec.lock`: wtf
* `pubspec.yaml`: upgraded to `openfoodfacts` `3.16.0`, for proof thumbnails
* `user_preferences_account.dart`: new web app root address

* More flexibility with potentially null data

Impacted files:
* `osm_location.dart`: more flexibility with potentially null data
* `price_model.dart`: more flexibility with potentially null data; better display of thumbnail image
* `price_proof_page.dart`: more flexibility with potentially null data
  • Loading branch information
monsieurtanuki authored Oct 7, 2024
1 parent ed191b7 commit 19ed4f0
Show file tree
Hide file tree
Showing 20 changed files with 673 additions and 337 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task.dart';
import 'package:smooth_app/background/background_task_price.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';

/// Background task about adding prices to an existing proof.
class BackgroundTaskAddOtherPrice extends BackgroundTaskPrice {
BackgroundTaskAddOtherPrice._({
required super.processName,
required super.uniqueId,
required super.stamp,
// single
required this.proofId,
required super.date,
required super.currency,
required super.locationOSMId,
required super.locationOSMType,
// multi
required super.barcodes,
required super.pricesAreDiscounted,
required super.prices,
required super.pricesWithoutDiscount,
});

BackgroundTaskAddOtherPrice.fromJson(super.json)
: proofId = json[_jsonTagProofId] as int,
super.fromJson();

static const String _jsonTagProofId = 'proofId';

static const OperationType _operationType = OperationType.addOtherPrice;

final int proofId;

@override
Map<String, dynamic> toJson() {
final Map<String, dynamic> result = super.toJson();
result[_jsonTagProofId] = proofId;
return result;
}

/// Adds the background task about uploading a product image.
static Future<void> addTask({
required final BuildContext context,
required final int proofId,
required final DateTime date,
required final Currency currency,
required final int locationOSMId,
required final LocationOSMType locationOSMType,
required final List<String> barcodes,
required final List<bool> pricesAreDiscounted,
required final List<double> prices,
required final List<double?> pricesWithoutDiscount,
}) async {
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final String uniqueId = await _operationType.getNewKey(localDatabase);
final BackgroundTask task = _getNewTask(
uniqueId: uniqueId,
proofId: proofId,
date: date,
currency: currency,
locationOSMId: locationOSMId,
locationOSMType: locationOSMType,
barcodes: barcodes,
pricesAreDiscounted: pricesAreDiscounted,
prices: prices,
pricesWithoutDiscount: pricesWithoutDiscount,
);
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
}

/// Returns a new background task about changing a product.
static BackgroundTaskAddOtherPrice _getNewTask({
required final String uniqueId,
required final int proofId,
required final DateTime date,
required final Currency currency,
required final int locationOSMId,
required final LocationOSMType locationOSMType,
required final List<String> barcodes,
required final List<bool> pricesAreDiscounted,
required final List<double> prices,
required final List<double?> pricesWithoutDiscount,
}) =>
BackgroundTaskAddOtherPrice._(
uniqueId: uniqueId,
processName: _operationType.processName,
proofId: proofId,
date: date,
currency: currency,
locationOSMId: locationOSMId,
locationOSMType: locationOSMType,
barcodes: barcodes,
pricesAreDiscounted: pricesAreDiscounted,
prices: prices,
pricesWithoutDiscount: pricesWithoutDiscount,
stamp: BackgroundTaskPrice.getStamp(
date: date,
locationOSMId: locationOSMId,
locationOSMType: locationOSMType,
),
);

@override
Future<void> execute(final LocalDatabase localDatabase) async {
final String bearerToken = await getBearerToken();

await addPrices(
bearerToken: bearerToken,
proofId: proofId,
);

await closeSession(bearerToken: bearerToken);
}
}
Loading

0 comments on commit 19ed4f0

Please sign in to comment.