Skip to content

Commit

Permalink
chore: upgrade to off-dart 3.0.0 (#4686)
Browse files Browse the repository at this point in the history
Impacted files:
* `background_task.dart`: minor refactoring
* `background_task_crop.dart`: added the `UriProductHelper` parameter
* `background_task_details.dart`: added the `UriProductHelper` parameter
* `background_task_download_products.dart`: added the `UriProductHelper` parameter
* `background_task_image.dart`: added the `UriProductHelper` parameter
* `background_task_top_barcodes.dart`: added the `UriProductHelper` parameter
* `background_task_unselect.dart`: added the `UriProductHelper` parameter
* `forgot_password_page.dart`: added the `UriProductHelper` parameter
* `ocr_ingredients_helper.dart`: added the `UriProductHelper` parameter
* `ocr_packaging_helper.dart`: added the `UriProductHelper` parameter
* `onboarding_data_product.dart`: added the `UriProductHelper` parameter
* `ordered_nutrients_cache.dart`: added the `UriProductHelper` parameter
* `paged_product_query.dart`: added the `UriProductHelper` parameter
* `product_cards_helper.dart`: now using an off-dart method
* `product_image_server_button.dart`: added the `UriProductHelper` parameter
* `product_list_import_export.dart`: added the `UriProductHelper` parameter
* `product_list_page.dart`: added the `UriProductHelper` parameter
* `product_query.dart`: added a static `UriProductHelper` that more or less replaces the `QueryType`; minor refactoring
* `product_refresher.dart`: added the `UriProductHelper` parameter
* `pubspec.lock`: wtf
* `pubspec.yaml`: upgraded off-dart to 3.0.0
* `random_questions_query.dart`: `countries` instead of `country`
* `sign_up_page.dart`: added the `UriProductHelper` parameter
* `temp_product_list_share_helper.dart`: now using a `UriProductHelper`
* `user_management_provider.dart`: added the `UriProductHelper` parameter
* `user_preferences_account.dart`: added the `UriProductHelper` parameter
* `user_preferences_dev_debug_info.dart`: minor refactoring
* `user_preferences_dev_mode.dart`: minor refactoring
  • Loading branch information
monsieurtanuki committed Oct 4, 2023
1 parent bde4950 commit 2b49c87
Show file tree
Hide file tree
Showing 28 changed files with 91 additions and 61 deletions.
6 changes: 5 additions & 1 deletion packages/smooth_app/lib/background/background_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:smooth_app/background/background_task_manager.dart';
import 'package:smooth_app/background/background_task_refresh_later.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/duration_constants.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/widgets/smooth_floating_message.dart';

/// Abstract background task.
Expand Down Expand Up @@ -142,7 +143,8 @@ abstract class BackgroundTask {
OpenFoodFactsLanguage getLanguage() => LanguageHelper.fromJson(languageCode);

@protected
OpenFoodFactsCountry? getCountry() => CountryHelper.fromJson(country);
OpenFoodFactsCountry? getCountry() =>
OpenFoodFactsCountry.fromOffTag(country);

@protected
User getUser() => User.fromJson(jsonDecode(user) as Map<String, dynamic>);
Expand All @@ -160,4 +162,6 @@ abstract class BackgroundTask {
/// We return true only in rare cases. Typically, when we split an task in
/// subtasks that call the next one at the end.
bool get hasImmediateNextTask => false;

UriProductHelper get uriProductHelper => ProductQuery.uriProductHelper;
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
x2: productImage.x2!,
y2: productImage.y2!,
user: getUser(),
uriHelper: uriProductHelper,
);
if (imageUrl == null) {
throw Exception('Could not select picture');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode {
packagingsComplete: product.packagingsComplete,
language: getLanguage(),
country: getCountry(),
uriHelper: uriProductHelper,
);
if (result.status != ProductResultV3.statusSuccess &&
result.status != ProductResultV3.statusWarning) {
Expand All @@ -153,6 +154,7 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode {
product,
language: getLanguage(),
country: getCountry(),
uriHelper: uriProductHelper,
);
if (status.status != 1) {
throw Exception('Could not save product - ${status.error}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
country: ProductQuery.getCountry(),
version: ProductQuery.productQueryVersion,
),
uriHelper: uriProductHelper,
);
final List<Product>? downloadedProducts = searchResult.products;
if (downloadedProducts == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,11 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
imageUri: Uri.parse(path),
);

final Status status = await OpenFoodAPIClient.addProductImage(user, image);
final Status status = await OpenFoodAPIClient.addProductImage(
user,
image,
uriHelper: uriProductHelper,
);
if (status.status == 'status ok') {
// successfully uploaded a new picture and set it as field+language
return;
Expand All @@ -337,6 +341,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
imgid: '$imageId',
angle: ImageAngle.NOON,
user: user,
uriHelper: uriProductHelper,
);
if (imageUrl == null) {
throw Exception('Could not select picture');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class BackgroundTaskTopBarcodes extends BackgroundTaskProgressing {
country: ProductQuery.getCountry(),
version: ProductQuery.productQueryVersion,
),
uriHelper: uriProductHelper,
);
if (searchResult.products == null || searchResult.count == null) {
throw Exception('Cannot download top barcodes');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class BackgroundTaskUnselect extends BackgroundTaskBarcode {
imageField: ImageField.fromOffTag(imageField)!,
language: getLanguage(),
user: getUser(),
uriHelper: uriProductHelper,
);

/// Returns a product with "unselected" image.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/duration_constants.dart';
import 'package:smooth_app/helpers/app_helper.dart';
import 'package:smooth_app/pages/product/common/product_refresher.dart';
import 'package:smooth_app/query/product_query.dart';

/// Helper around a product we download, store and reuse at onboarding.
class OnboardingDataProduct extends AbstractOnboardingData<Product> {
Expand Down Expand Up @@ -38,6 +39,7 @@ class OnboardingDataProduct extends AbstractOnboardingData<Product> {
ProductRefresher().getBarcodeQueryConfiguration(
AbstractOnboardingData.barcode,
),
uriHelper: ProductQuery.uriProductHelper,
).timeout(SnackBarDuration.long);

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class UserManagementProvider with ChangeNotifier {
Future<bool> login(User user) async {
final LoginStatus? loginStatus;
try {
loginStatus = await OpenFoodAPIClient.login2(user);
loginStatus = await OpenFoodAPIClient.login2(
user,
uriHelper: ProductQuery.uriProductHelper,
);
} catch (e) {
throw Exception(e);
}
Expand Down Expand Up @@ -107,6 +110,7 @@ class UserManagementProvider with ChangeNotifier {
userId: user.userId,
password: user.password,
),
uriHelper: ProductQuery.uriProductHelper,
);
if (loginStatus == null) {
// No internet or sever down
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ProductListImportExport {
ProductRefresher().getBarcodeListQueryConfiguration(
barcodes.toList(growable: false),
),
uriHelper: ProductQuery.uriProductHelper,
);

return searchResult.products ?? <Product>[];
Expand Down
19 changes: 5 additions & 14 deletions packages/smooth_app/lib/helpers/product_cards_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ ProductImageData getProductImageData(
if (productImage != null) {
// we found a localized version for this image
imageLanguage = language;
imageUrl = getLocalizedProductImageUrl(product, productImage);
imageUrl = ImageHelper.getLocalizedProductImageUrl(
product.barcode!,
productImage,
imageSize: ImageSize.DISPLAY,
);
} else {
imageLanguage = null;
imageUrl = forceLanguage ? null : imageField.getUrl(product);
Expand Down Expand Up @@ -281,19 +285,6 @@ List<MapEntry<ProductImageData, ImageProvider?>> getSelectedImages(
return result.entries.toList();
}

String _getImageRoot() =>
OpenFoodAPIConfiguration.globalQueryType == QueryType.PROD
? 'https://images.openfoodfacts.org/images/products'
: 'https://images.openfoodfacts.net/images/products';

String getLocalizedProductImageUrl(
final Product product,
final ProductImage productImage,
) =>
'${_getImageRoot()}/'
'${ImageHelper.getBarcodeSubPath(product.barcode!)}/'
'${ImageHelper.getProductImageFilename(productImage, imageSize: ImageSize.DISPLAY)}';

/// Returns the languages for which [imageField] has images for that [product].
Iterable<OpenFoodFactsLanguage> getProductImageLanguages(
final Product product,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/query/product_query.dart';

// TODO(m123): Move this to off-dart
Uri shareProductList(List<String> barcodes) {
Expand All @@ -9,7 +10,7 @@ Uri shareProductList(List<String> barcodes) {
}

return UriHelper.replaceSubdomain(
UriHelper.getUri(
ProductQuery.uriProductHelper.getUri(
path: 'products/$buffer',
addUserAgentParameters: false,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class UserPreferencesAccount extends AbstractUserPreferences {
final SearchResult result = await OpenFoodAPIClient.searchProducts(
user,
configuration,
queryType: OpenFoodAPIConfiguration.globalQueryType,
uriHelper: ProductQuery.uriProductHelper,
);
return result.count;
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class _UserPreferencesDebugInfoState extends State<UserPreferencesDebugInfo> {
'IsLoggedIn': ProductQuery.isLoggedIn().toString(),
'UUID': OpenFoodAPIConfiguration.uuid.toString(),
'Matomo Visitor ID': AnalyticsHelper.matomoVisitorId,
'QueryType': OpenFoodAPIConfiguration.globalQueryType.toString(),
'QueryType': ProductQuery.uriProductHelper.isTestMode
? 'QueryType.TEST'
: 'QueryType.PROD',
'Domain': ProductQuery.uriProductHelper.domain,
'UserAgent-name': '${OpenFoodAPIConfiguration.userAgent?.name}',
'UserAgent-system': '${OpenFoodAPIConfiguration.userAgent?.system}',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
);

static const String userPreferencesFlagProd = '__devWorkingOnProd';
static const String userPreferencesTestEnvHost = '__testEnvHost';
static const String userPreferencesTestEnvDomain = '__testEnvHost';
static const String userPreferencesFlagEditIngredients = '__editIngredients';
static const String userPreferencesFlagBoostedComparison =
'__boostedComparison';
Expand Down Expand Up @@ -102,7 +102,7 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
UserPreferencesItemTile(
title: appLocalizations.dev_preferences_environment_switch_title,
trailing: DropdownButton<bool>(
value: OpenFoodAPIConfiguration.globalQueryType == QueryType.PROD,
value: userPreferences.getFlag(userPreferencesFlagProd) ?? true,
elevation: 16,
onChanged: (bool? newValue) async {
await userPreferences.setFlag(userPreferencesFlagProd, newValue);
Expand All @@ -123,9 +123,11 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
UserPreferencesItemTile(
title: appLocalizations.dev_preferences_test_environment_title,
subtitle: appLocalizations.dev_preferences_test_environment_subtitle(
'${OpenFoodAPIConfiguration.uriScheme}://${OpenFoodAPIConfiguration.uriTestHost}/',
ProductQuery.getTestUriProductHelper(userPreferences)
.getPostUri(path: '')
.toString(),
),
onTap: () async => _changeTestEnvHost(),
onTap: () async => _changeTestEnvDomain(),
),
UserPreferencesItemSwitch(
title: appLocalizations.dev_preferences_edit_ingredients_title,
Expand Down Expand Up @@ -362,10 +364,10 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
),
);

Future<void> _changeTestEnvHost() async {
Future<void> _changeTestEnvDomain() async {
_textFieldController.text =
userPreferences.getDevModeString(userPreferencesTestEnvHost) ??
OpenFoodAPIConfiguration.uriTestHost;
userPreferences.getDevModeString(userPreferencesTestEnvDomain) ??
uriHelperFoodTest.domain;
final bool? result = await showDialog<bool>(
context: context,
builder: (final BuildContext context) => SmoothAlertDialog(
Expand All @@ -383,7 +385,7 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
);
if (result == true) {
await userPreferences.setDevModeString(
userPreferencesTestEnvHost, _textFieldController.text);
userPreferencesTestEnvDomain, _textFieldController.text);
ProductQuery.setQueryType(userPreferences);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ class _ProductListPageState extends State<ProductListPage>
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
ProductQuery.getUser(),
ProductRefresher().getBarcodeListQueryConfiguration(barcodes),
uriHelper: ProductQuery.uriProductHelper,
);
final List<Product>? freshProducts = searchResult.products;
if (freshProducts == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class ProductRefresher {
try {
final ProductResultV3 result = await OpenFoodAPIClient.getProductV3(
getBarcodeQueryConfiguration(barcode),
uriHelper: ProductQuery.uriProductHelper,
);
if (result.product != null) {
await DaoProduct(localDatabase).put(result.product!);
Expand All @@ -180,11 +181,7 @@ class ProductRefresher {
connectivityResult: connectivityResult,
);
}
// TODO(monsieurtanuki): make things cleaner with off-dart
final String host =
OpenFoodAPIConfiguration.globalQueryType == QueryType.PROD
? OpenFoodAPIConfiguration.uriProdHost
: OpenFoodAPIConfiguration.uriTestHost;
final String host = ProductQuery.uriProductHelper.host;
final PingData result = await Ping(host, count: 1).stream.first;
return FetchedProduct.error(
exceptionString: e.toString(),
Expand All @@ -205,6 +202,7 @@ class ProductRefresher {
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
ProductQuery.getUser(),
getBarcodeListQueryConfiguration(barcodes),
uriHelper: ProductQuery.uriProductHelper,
);
if (searchResult.products == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/background/background_task_details.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/pages/product/ocr_helper.dart';
import 'package:smooth_app/query/product_query.dart';

/// OCR Helper for ingredients.
class OcrIngredientsHelper extends OcrHelper {
Expand Down Expand Up @@ -73,6 +74,7 @@ class OcrIngredientsHelper extends OcrHelper {
getUser(),
product.barcode!,
language,
uriHelper: ProductQuery.uriProductHelper,
);
return result.ingredientsTextFromImage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/background/background_task_details.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/pages/product/ocr_helper.dart';
import 'package:smooth_app/query/product_query.dart';

/// OCR Helper for packaging.
class OcrPackagingHelper extends OcrHelper {
Expand Down Expand Up @@ -75,6 +76,7 @@ class OcrPackagingHelper extends OcrHelper {
getUser(),
product.barcode!,
language,
uriHelper: ProductQuery.uriProductHelper,
);
return result.textFromImage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class OrderedNutrientsCache {
final String string = await OpenFoodAPIClient.getOrderedNutrientsJsonString(
country: ProductQuery.getCountry(),
language: ProductQuery.getLanguage(),
uriHelper: ProductQuery.uriProductHelper,
);
final OrderedNutrients result = OrderedNutrients.fromJson(
jsonDecode(string) as Map<String, dynamic>,
Expand All @@ -70,6 +71,6 @@ class OrderedNutrientsCache {
return 'nutrients.pl'
'/${country.offTag}'
'/${language.code}'
'/${OpenFoodAPIConfiguration.globalQueryType}';
'/${ProductQuery.uriProductHelper.domain}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ProductImageServerButton extends StatelessWidget {
future: OpenFoodAPIClient.getProductImageIds(
barcode,
user: ProductQuery.getUser(),
uriHelper: ProductQuery.uriProductHelper,
),
context: context,
title: appLocalizations.edit_photo_select_existing_download_label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class _ForgotPasswordPageState extends State<ForgotPasswordPage>
_userIdController.text,
country: ProductQuery.getCountry(),
language: ProductQuery.getLanguage(),
uriHelper: ProductQuery.uriProductHelper,
);
if (status.status == 200) {
_send = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ class _SignUpPageState extends State<SignUpPage> with TraceableClientMixin {
orgName: _foodProducer ? _brandController.trimmedText : null,
country: ProductQuery.getCountry(),
language: ProductQuery.getLanguage(),
uriHelper: ProductQuery.uriProductHelper,
),
title: appLocalisations.sign_up_page_action_doing_it,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/query/paged_product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract class PagedProductQuery {
OpenFoodAPIClient.searchProducts(
ProductQuery.getUser(),
getQueryConfiguration(),
queryType: OpenFoodAPIConfiguration.globalQueryType,
uriHelper: ProductQuery.uriProductHelper,
);

AbstractQueryConfiguration getQueryConfiguration();
Expand Down
Loading

0 comments on commit 2b49c87

Please sign in to comment.