Skip to content

Commit

Permalink
feat: 4947 - added "download language" to product table
Browse files Browse the repository at this point in the history
Deleted file:
* `dao_product_migration.dart`

Impacted files:
* `background_task_download_products.dart`: added language parameter
* `dao_hive_product.dart`: removed dead code
* `dao_product.dart`: added table column language `lc`
* `local_database.dart`: upgraded the database version
* `onboarding_data_product.dart`: added language parameter
* `product_list_page.dart`: added language parameter
* `product_refresher.dart`: added language parameter
* `query_product_list_supplier.dart`: added language parameter
  • Loading branch information
monsieurtanuki committed Jan 4, 2024
1 parent 72ac084 commit 37620a8
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
if (downloadFlag & flagMaskExcludeKP != 0) {
fields.remove(ProductField.KNOWLEDGE_PANELS);
}
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
getUser(),
ProductSearchQueryConfiguration(
Expand All @@ -121,7 +122,7 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
const PageNumber(page: 1),
BarcodeParameter.list(barcodes),
],
language: ProductQuery.getLanguage(),
language: language,
country: ProductQuery.getCountry(),
version: ProductQuery.productQueryVersion,
),
Expand All @@ -134,7 +135,7 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
final DaoProduct daoProduct = DaoProduct(localDatabase);
for (final Product product in downloadedProducts) {
if (await _shouldBeUpdated(daoProduct, product.barcode!)) {
await daoProduct.put(product);
await daoProduct.put(product, language);
}
}
final int deleted = await daoWorkBarcode.deleteBarcodes(work, barcodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class OnboardingDataProduct extends AbstractOnboardingData<Product> {
OpenFoodAPIClient.getProductString(
ProductRefresher().getBarcodeQueryConfiguration(
AbstractOnboardingData.barcode,
ProductQuery.getLanguage(),
),
uriHelper: ProductQuery.uriProductHelper,
).timeout(SnackBarDuration.long);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class QueryProductListSupplier extends ProductListSupplier {
productList.setAll(searchResult.products!);
productList.totalSize = searchResult.count ?? 0;
partialProductList.add(productList);
await DaoProduct(localDatabase).putAll(searchResult.products!);
await DaoProduct(localDatabase).putAll(
searchResult.products!,
productQuery.language,
);
}
await DaoProductList(localDatabase).put(productList);
return null;
Expand Down
47 changes: 1 addition & 46 deletions packages/smooth_app/lib/database/dao_hive_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:convert';
import 'package:hive/hive.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/database/abstract_dao.dart';
import 'package:smooth_app/database/dao_product_migration.dart';
import 'package:smooth_app/database/local_database.dart';

/// Hive type adapter for [Product]
Expand All @@ -24,7 +23,7 @@ class _ProductAdapter extends TypeAdapter<Product> {
/// But Hive needs it - it doesn't like data to be removed...
/// Where we store the products as "barcode => product".
@Deprecated('use [DaoProduct] instead')
class DaoHiveProduct extends AbstractDao implements DaoProductMigrationSource {
class DaoHiveProduct extends AbstractDao {
@Deprecated('use [DaoProduct] instead')
DaoHiveProduct(final LocalDatabase localDatabase) : super(localDatabase);

Expand All @@ -35,48 +34,4 @@ class DaoHiveProduct extends AbstractDao implements DaoProductMigrationSource {

@override
void registerAdapter() => Hive.registerAdapter(_ProductAdapter());

LazyBox<Product> _getBox() => Hive.lazyBox<Product>(_hiveBoxName);

Future<Product?> get(final String barcode) async => _getBox().get(barcode);

@override
Future<Map<String, Product>> getAll(final List<String> barcodes) async {
final LazyBox<Product> box = _getBox();
final Map<String, Product> result = <String, Product>{};
for (final String barcode in barcodes) {
final Product? product = await box.get(barcode);
if (product != null) {
result[barcode] = product;
}
}
return result;
}

Future<void> put(final Product product) async => putAll(<Product>[product]);

Future<void> putAll(final Iterable<Product> products) async {
final Map<String, Product> upserts = <String, Product>{};
for (final Product product in products) {
upserts[product.barcode!] = product;
}
await _getBox().putAll(upserts);
}

@override
Future<List<String>> getAllKeys() async {
final LazyBox<Product> box = _getBox();
final List<String> result = <String>[];
for (final dynamic key in box.keys) {
result.add(key.toString());
}
return result;
}

// Just for the migration
@override
Future<void> deleteAll(final List<String> barcodes) async {
final LazyBox<Product> box = _getBox();
await box.deleteAll(barcodes);
}
}
36 changes: 27 additions & 9 deletions packages/smooth_app/lib/database/dao_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/database/abstract_sql_dao.dart';
import 'package:smooth_app/database/bulk_deletable.dart';
import 'package:smooth_app/database/bulk_manager.dart';
import 'package:smooth_app/database/dao_product_migration.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:sqflite/sqflite.dart';

class DaoProduct extends AbstractSqlDao
implements BulkDeletable, DaoProductMigrationDestination {
class DaoProduct extends AbstractSqlDao implements BulkDeletable {
DaoProduct(super.localDatabase);

static const String _TABLE_PRODUCT = 'gzipped_product';
static const String _TABLE_PRODUCT_COLUMN_BARCODE = 'barcode';
static const String _TABLE_PRODUCT_COLUMN_GZIPPED_JSON =
'encoded_gzipped_json';
static const String _TABLE_PRODUCT_COLUMN_LAST_UPDATE = 'last_update';
static const String _TABLE_PRODUCT_COLUMN_LANGUAGE = 'lc';

static const List<String> _columns = <String>[
_TABLE_PRODUCT_COLUMN_BARCODE,
_TABLE_PRODUCT_COLUMN_GZIPPED_JSON,
_TABLE_PRODUCT_COLUMN_LAST_UPDATE,
_TABLE_PRODUCT_COLUMN_LANGUAGE,
];

static FutureOr<void> onUpgrade(
Expand All @@ -41,6 +41,10 @@ class DaoProduct extends AbstractSqlDao
',$_TABLE_PRODUCT_COLUMN_LAST_UPDATE INT NOT NULL'
')');
}
if (oldVersion < 4) {
await db.execute('alter table $_TABLE_PRODUCT add column '
'$_TABLE_PRODUCT_COLUMN_LANGUAGE TEXT');
}
}

/// Returns the [Product] that matches the [barcode], or null.
Expand Down Expand Up @@ -87,17 +91,28 @@ class DaoProduct extends AbstractSqlDao
return result;
}

Future<void> put(final Product product) async => putAll(<Product>[product]);
Future<void> put(
final Product product,
final OpenFoodFactsLanguage language,
) async =>
putAll(
<Product>[product],
language,
);

/// Replaces products in database
@override
Future<void> putAll(final Iterable<Product> products) async =>
Future<void> putAll(
final Iterable<Product> products,
final OpenFoodFactsLanguage language,
) async =>
localDatabase.database.transaction(
(final Transaction transaction) async =>
_bulkReplaceLoop(transaction, products),
(final Transaction transaction) async => _bulkReplaceLoop(
transaction,
products,
language,
),
);

@override
Future<List<String>> getAllKeys() async {
final List<String> result = <String>[];
final List<Map<String, dynamic>> queryResults =
Expand Down Expand Up @@ -126,6 +141,7 @@ class DaoProduct extends AbstractSqlDao
Future<void> _bulkReplaceLoop(
final DatabaseExecutor databaseExecutor,
final Iterable<Product> products,
final OpenFoodFactsLanguage language,
) async {
final int lastUpdate = LocalDatabase.nowInMillis();
final BulkManager bulkManager = BulkManager();
Expand All @@ -138,6 +154,7 @@ class DaoProduct extends AbstractSqlDao
),
);
insertParameters.add(lastUpdate);
insertParameters.add(language.offTag);
}
await bulkManager.insert(
bulkInsertable: this,
Expand Down Expand Up @@ -223,6 +240,7 @@ class DaoProduct extends AbstractSqlDao
await localDatabase.database.rawQuery('''
select sum(length($_TABLE_PRODUCT_COLUMN_BARCODE)) +
sum(length($_TABLE_PRODUCT_COLUMN_LAST_UPDATE)) +
sum(length($_TABLE_PRODUCT_COLUMN_LANGUAGE)) +
sum(length($_TABLE_PRODUCT_COLUMN_GZIPPED_JSON))
from $_TABLE_PRODUCT
'''),
Expand Down
55 changes: 0 additions & 55 deletions packages/smooth_app/lib/database/dao_product_migration.dart

This file was deleted.

2 changes: 1 addition & 1 deletion packages/smooth_app/lib/database/local_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class LocalDatabase extends ChangeNotifier {
final String databasePath = join(databasesRootPath, 'smoothie.db');
final Database database = await openDatabase(
databasePath,
version: 3,
version: 4,
singleInstance: true,
onUpgrade: _onUpgrade,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,16 +478,20 @@ class _ProductListPageState extends State<ProductListPage>
final LocalDatabase localDatabase,
) async {
try {
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
ProductQuery.getUser(),
ProductRefresher().getBarcodeListQueryConfiguration(barcodes),
ProductRefresher().getBarcodeListQueryConfiguration(
barcodes,
language,
),
uriHelper: ProductQuery.uriProductHelper,
);
final List<Product>? freshProducts = searchResult.products;
if (freshProducts == null) {
return false;
}
await DaoProduct(localDatabase).putAll(freshProducts);
await DaoProduct(localDatabase).putAll(freshProducts, language);
localDatabase.upToDate.setLatestDownloadedProducts(freshProducts);
final RobotoffInsightHelper robotoffInsightHelper =
RobotoffInsightHelper(localDatabase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,25 @@ class ProductRefresher {
/// Returns the standard configuration for barcode product query.
ProductQueryConfiguration getBarcodeQueryConfiguration(
final String barcode,
final OpenFoodFactsLanguage language,
) =>
ProductQueryConfiguration(
barcode,
fields: ProductQuery.fields,
language: ProductQuery.getLanguage(),
language: language,
country: ProductQuery.getCountry(),
version: ProductQuery.productQueryVersion,
);

/// Returns the standard configuration for several barcodes product query.
ProductSearchQueryConfiguration getBarcodeListQueryConfiguration(
final List<String> barcodes, {
final List<String> barcodes,
final OpenFoodFactsLanguage language, {
final List<ProductField>? fields,
}) =>
ProductSearchQueryConfiguration(
fields: fields ?? ProductQuery.fields,
language: ProductQuery.getLanguage(),
language: language,
country: ProductQuery.getCountry(),
parametersList: <Parameter>[
BarcodeParameter.list(barcodes),
Expand Down Expand Up @@ -159,12 +161,16 @@ class ProductRefresher {
required final String barcode,
}) async {
try {
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
final ProductResultV3 result = await OpenFoodAPIClient.getProductV3(
getBarcodeQueryConfiguration(barcode),
getBarcodeQueryConfiguration(
barcode,
language,
),
uriHelper: ProductQuery.uriProductHelper,
);
if (result.product != null) {
await DaoProduct(localDatabase).put(result.product!);
await DaoProduct(localDatabase).put(result.product!, language);
localDatabase.upToDate.setLatestDownloadedProduct(result.product!);
localDatabase.notifyListeners();
return FetchedProduct.found(result.product!);
Expand Down Expand Up @@ -198,15 +204,16 @@ class ProductRefresher {
final List<String> barcodes,
) async {
try {
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
ProductQuery.getUser(),
getBarcodeListQueryConfiguration(barcodes),
getBarcodeListQueryConfiguration(barcodes, language),
uriHelper: ProductQuery.uriProductHelper,
);
if (searchResult.products == null) {
return null;
}
await DaoProduct(localDatabase).putAll(searchResult.products!);
await DaoProduct(localDatabase).putAll(searchResult.products!, language);
localDatabase.upToDate
.setLatestDownloadedProducts(searchResult.products!);
localDatabase.notifyListeners();
Expand Down

0 comments on commit 37620a8

Please sign in to comment.