Skip to content

Commit

Permalink
fix: 4663 - use the latest password for background tasks + refactoring (
Browse files Browse the repository at this point in the history
#4869)

* fix: 4663 - use the latest password for background tasks + refactoring

Impacted files:
* `background_task.dart`: use the latest password; now we compute user and country from that class, and language most of the time too
* `background_task_barcode.dart`: removed user and country parameters; replaced language
* `background_task_crop.dart`: removed user and country parameters; replaced language
* `background_task_details.dart`: removed user, language and country parameters
* `background_task_download_products.dart`: removed user, language and country parameters
* `background_task_full_refresh.dart`: removed user, language and country parameters
* `background_task_hunger_games.dart`: removed user, language and country parameters
* `background_task_image.dart`: removed user and country parameters; replaced language
* `background_task_offline.dart`: removed user, language and country parameters
* `background_task_paged.dart`: removed user, language and country parameters
* `background_task_progressing.dart`: removed user, language and country parameters
* `background_task_refresh_later.dart`: removed user, language and country parameters
* `background_task_top_barcodes.dart`: removed user, language and country parameters
* `background_task_unselect.dart`: removed user, language and country parameters
* `background_task_upload.dart`: removed user and country parameters; replaced language
* `paged_to_be_completed_product_query.dart`: minor refactoring
* `paged_user_product_query.dart`: minor refactoring

* fix: 4663 - lint fix

---------

Co-authored-by: Pierre Slamich <[email protected]>
  • Loading branch information
monsieurtanuki and teolemon authored Dec 21, 2023
1 parent a2937db commit 28262ca
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 112 deletions.
27 changes: 24 additions & 3 deletions packages/smooth_app/lib/background/background_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ import 'package:smooth_app/widgets/smooth_floating_message.dart';

/// Abstract background task.
abstract class BackgroundTask {
const BackgroundTask({
BackgroundTask({
required this.processName,
required this.uniqueId,
required this.stamp,
final OpenFoodFactsLanguage? language,
}) // TODO(monsieurtanuki): don't store the password in a clear format...
: user = jsonEncode(ProductQuery.getUser().toJson()),
country = ProductQuery.getCountry().offTag,
languageCode = (language ?? ProductQuery.getLanguage()).offTag;

BackgroundTask._({
required this.processName,
required this.uniqueId,
required this.languageCode,
Expand All @@ -22,7 +32,7 @@ abstract class BackgroundTask {
});

BackgroundTask.fromJson(Map<String, dynamic> json)
: this(
: this._(
processName: json[_jsonTagProcessName] as String,
uniqueId: json[_jsonTagUniqueId] as String,
languageCode: json[_jsonTagLanguageCode] as String,
Expand Down Expand Up @@ -146,7 +156,16 @@ abstract class BackgroundTask {
OpenFoodFactsCountry.fromOffTag(country);

@protected
User getUser() => User.fromJson(jsonDecode(user) as Map<String, dynamic>);
User getUser() {
final User storedUser =
User.fromJson(jsonDecode(user) as Map<String, dynamic>);
final User currentUser = ProductQuery.getUser();
if (storedUser.userId == currentUser.userId) {
// with a latest password.
return currentUser;
}
return storedUser;
}

/// Checks that everything is fine and fix things if needed + if possible.
///
Expand All @@ -162,5 +181,7 @@ abstract class BackgroundTask {
/// subtasks that call the next one at the end.
bool get hasImmediateNextTask => false;

// TODO(monsieurtanuki): store the uriProductHelper as well
@protected
UriProductHelper get uriProductHelper => ProductQuery.uriProductHelper;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import 'package:smooth_app/pages/product/common/product_refresher.dart';

/// Abstract background task that involves a single barcode.
abstract class BackgroundTaskBarcode extends BackgroundTask {
const BackgroundTaskBarcode({
BackgroundTaskBarcode({
required super.processName,
required super.uniqueId,
required super.languageCode,
required super.user,
required super.country,
super.language,
required super.stamp,
required this.barcode,
});
Expand Down
12 changes: 3 additions & 9 deletions packages/smooth_app/lib/background/background_task_crop.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
Expand All @@ -10,17 +9,14 @@ import 'package:smooth_app/background/background_task_refresh_later.dart';
import 'package:smooth_app/background/background_task_upload.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/query/product_query.dart';

/// Background task about product image crop from existing file.
class BackgroundTaskCrop extends BackgroundTaskUpload {
const BackgroundTaskCrop._({
BackgroundTaskCrop._({
required super.processName,
required super.uniqueId,
required super.barcode,
required super.languageCode,
required super.user,
required super.country,
required super.language,
required super.stamp,
required super.imageField,
required super.croppedPath,
Expand Down Expand Up @@ -121,9 +117,7 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
cropY1: cropY1,
cropX2: cropX2,
cropY2: cropY2,
languageCode: language.code,
user: jsonEncode(ProductQuery.getUser().toJson()),
country: ProductQuery.getCountry().offTag,
language: language,
stamp: BackgroundTaskUpload.getStamp(
barcode,
imageField.offTag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task_barcode.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/query/product_query.dart';

/// Stamps we can put on [BackgroundTaskDetails].
///
Expand All @@ -33,13 +32,10 @@ enum BackgroundTaskDetailsStamp {

/// Background task that changes product details (data, but no image upload).
class BackgroundTaskDetails extends BackgroundTaskBarcode {
const BackgroundTaskDetails._({
BackgroundTaskDetails._({
required super.processName,
required super.uniqueId,
required super.barcode,
required super.languageCode,
required super.user,
required super.country,
required super.stamp,
required this.inputMap,
});
Expand Down Expand Up @@ -111,10 +107,7 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode {
uniqueId: uniqueId,
processName: _operationType.processName,
barcode: minimalistProduct.barcode!,
languageCode: ProductQuery.getLanguage().code,
inputMap: jsonEncode(minimalistProduct.toJson()),
user: jsonEncode(ProductQuery.getUser().toJson()),
country: ProductQuery.getCountry().offTag,
stamp: getStamp(minimalistProduct.barcode!, stamp.tag),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:flutter/painting.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
Expand All @@ -16,9 +14,6 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
BackgroundTaskDownloadProducts._({
required super.processName,
required super.uniqueId,
required super.languageCode,
required super.user,
required super.country,
required super.stamp,
required super.work,
required super.pageSize,
Expand Down Expand Up @@ -86,9 +81,6 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
BackgroundTaskDownloadProducts._(
processName: _operationType.processName,
uniqueId: uniqueId,
languageCode: ProductQuery.getLanguage().offTag,
user: jsonEncode(ProductQuery.getUser().toJson()),
country: ProductQuery.getCountry().offTag,
stamp: ';offlineProducts;$work',
work: work,
pageSize: pageSize,
Expand Down Expand Up @@ -121,7 +113,7 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
fields.remove(ProductField.KNOWLEDGE_PANELS);
}
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
ProductQuery.getUser(),
getUser(),
ProductSearchQueryConfiguration(
fields: fields,
parametersList: <Parameter>[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
Expand All @@ -12,16 +10,12 @@ import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/dao_work_barcode.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/query/product_query.dart';

/// Background task about refreshing all the already downloaded products.
class BackgroundTaskFullRefresh extends BackgroundTaskPaged {
BackgroundTaskFullRefresh._({
required super.processName,
required super.uniqueId,
required super.languageCode,
required super.user,
required super.country,
required super.stamp,
required super.pageSize,
});
Expand Down Expand Up @@ -64,9 +58,6 @@ class BackgroundTaskFullRefresh extends BackgroundTaskPaged {
BackgroundTaskFullRefresh._(
processName: _operationType.processName,
uniqueId: uniqueId,
languageCode: ProductQuery.getLanguage().offTag,
user: jsonEncode(ProductQuery.getUser().toJson()),
country: ProductQuery.getCountry().offTag,
stamp: ';fullRefresh',
pageSize: pageSize,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
Expand All @@ -8,17 +6,13 @@ import 'package:smooth_app/background/background_task_barcode.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/helpers/robotoff_insight_helper.dart';
import 'package:smooth_app/query/product_query.dart';

/// Background task about answering a hunger games question.
class BackgroundTaskHungerGames extends BackgroundTaskBarcode {
const BackgroundTaskHungerGames._({
BackgroundTaskHungerGames._({
required super.processName,
required super.uniqueId,
required super.barcode,
required super.languageCode,
required super.user,
required super.country,
required super.stamp,
required this.insightId,
required this.insightAnnotation,
Expand Down Expand Up @@ -86,9 +80,6 @@ class BackgroundTaskHungerGames extends BackgroundTaskBarcode {
processName: _operationType.processName,
uniqueId: uniqueId,
barcode: barcode,
languageCode: ProductQuery.getLanguage().offTag,
user: jsonEncode(ProductQuery.getUser().toJson()),
country: ProductQuery.getCountry().offTag,
stamp: _getStamp(barcode, insightId),
insightId: insightId,
insightAnnotation: insightAnnotation,
Expand Down
12 changes: 3 additions & 9 deletions packages/smooth_app/lib/background/background_task_image.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
Expand All @@ -16,17 +15,14 @@ import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/data_models/up_to_date_changes.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/helpers/image_compute_container.dart';
import 'package:smooth_app/query/product_query.dart';

/// Background task about product image upload.
class BackgroundTaskImage extends BackgroundTaskUpload {
const BackgroundTaskImage._({
BackgroundTaskImage._({
required super.processName,
required super.uniqueId,
required super.barcode,
required super.languageCode,
required super.user,
required super.country,
required super.language,
required super.stamp,
required super.imageField,
required super.croppedPath,
Expand Down Expand Up @@ -131,9 +127,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
cropY1: cropY1,
cropX2: cropX2,
cropY2: cropY2,
languageCode: language.code,
user: jsonEncode(ProductQuery.getUser().toJson()),
country: ProductQuery.getCountry().offTag,
language: language,
stamp: BackgroundTaskUpload.getStamp(
barcode,
imageField.offTag,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
Expand All @@ -9,16 +7,12 @@ import 'package:smooth_app/background/background_task_top_barcodes.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/dao_work_barcode.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/query/product_query.dart';

/// Main background task about pre-downloading top n products for offline usage.
class BackgroundTaskOffline extends BackgroundTaskProgressing {
BackgroundTaskOffline._({
required super.processName,
required super.uniqueId,
required super.languageCode,
required super.user,
required super.country,
required super.stamp,
required super.work,
required super.pageSize,
Expand Down Expand Up @@ -70,9 +64,6 @@ class BackgroundTaskOffline extends BackgroundTaskProgressing {
BackgroundTaskOffline._(
processName: _operationType.processName,
uniqueId: uniqueId,
languageCode: ProductQuery.getLanguage().offTag,
user: jsonEncode(ProductQuery.getUser().toJson()),
country: ProductQuery.getCountry().offTag,
stamp: ';offline',
work: work,
pageSize: pageSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import 'package:smooth_app/background/background_task.dart';

/// Abstract background task with paged actions.
abstract class BackgroundTaskPaged extends BackgroundTask {
const BackgroundTaskPaged({
BackgroundTaskPaged({
required super.processName,
required super.uniqueId,
required super.languageCode,
required super.user,
required super.country,
required super.stamp,
required this.pageSize,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import 'package:smooth_app/background/background_task_paged.dart';

/// Abstract background task with work in progress actions.
abstract class BackgroundTaskProgressing extends BackgroundTaskPaged {
const BackgroundTaskProgressing({
BackgroundTaskProgressing({
required super.processName,
required super.uniqueId,
required super.languageCode,
required super.user,
required super.country,
required super.stamp,
required super.pageSize,
required this.work,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import 'dart:convert';

import 'package:flutter/painting.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/background/background_task_barcode.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/query/product_query.dart';

/// Background task that triggers a product refresh "a bit later".
///
/// Typical use-case is after uploading an image. It takes roughly 10 minutes
/// before Robotoff provides new questions: we should then refresh the product.
/// cf. https://github.com/openfoodfacts/smooth-app/issues/3380
class BackgroundTaskRefreshLater extends BackgroundTaskBarcode {
const BackgroundTaskRefreshLater._({
BackgroundTaskRefreshLater._({
required super.processName,
required super.uniqueId,
required super.barcode,
required super.languageCode,
required super.user,
required super.country,
required super.stamp,
required this.timestamp,
});
Expand Down Expand Up @@ -79,9 +72,6 @@ class BackgroundTaskRefreshLater extends BackgroundTaskBarcode {
uniqueId: uniqueId,
processName: _operationType.processName,
barcode: barcode,
languageCode: ProductQuery.getLanguage().code,
user: jsonEncode(ProductQuery.getUser().toJson()),
country: ProductQuery.getCountry().offTag,
timestamp: LocalDatabase.nowInMillis(),
stamp: _getStamp(barcode),
);
Expand Down
Loading

0 comments on commit 28262ca

Please sign in to comment.