diff --git a/packages/smooth_app/lib/background/background_task_hunger_games.dart b/packages/smooth_app/lib/background/background_task_hunger_games.dart index 54b53cbc91b..070bb72124e 100644 --- a/packages/smooth_app/lib/background/background_task_hunger_games.dart +++ b/packages/smooth_app/lib/background/background_task_hunger_games.dart @@ -115,7 +115,7 @@ class BackgroundTaskHungerGames extends BackgroundTaskBarcode { @override Future upload() async { final InsightAnnotation? annotation = - _getInsightAnnotation(insightAnnotation); + InsightAnnotation.fromInt(insightAnnotation); if (annotation == null) { // very unlikely return; @@ -126,15 +126,4 @@ class BackgroundTaskHungerGames extends BackgroundTaskBarcode { deviceId: OpenFoodAPIConfiguration.uuid, ); } - - // TODO(monsieurtanuki): move to off-dart - static InsightAnnotation? _getInsightAnnotation(final int annotation) { - for (final InsightAnnotation insightAnnotation - in InsightAnnotation.values) { - if (annotation == insightAnnotation.value) { - return insightAnnotation; - } - } - return null; - } } diff --git a/packages/smooth_app/lib/background/background_task_image.dart b/packages/smooth_app/lib/background/background_task_image.dart index 107520dc448..3830bd27d50 100644 --- a/packages/smooth_app/lib/background/background_task_image.dart +++ b/packages/smooth_app/lib/background/background_task_image.dart @@ -56,12 +56,8 @@ class BackgroundTaskImage extends BackgroundTaskUpload { } // cf. https://github.com/openfoodfacts/smooth-app/issues/4219 - // TODO(monsieurtanuki): move to off-dart - static const int minimumWidth = 640; - static const int minimumHeight = 160; - static bool isPictureBigEnough(final num width, final num height) => - width >= minimumWidth || height >= minimumHeight; + width >= ImageHelper.minimumWidth || height >= ImageHelper.minimumHeight; /// Adds the background task about uploading a product image. static Future addTask( diff --git a/packages/smooth_app/lib/database/dao_hive_product.dart b/packages/smooth_app/lib/database/dao_hive_product.dart deleted file mode 100644 index 0a714a761fd..00000000000 --- a/packages/smooth_app/lib/database/dao_hive_product.dart +++ /dev/null @@ -1,81 +0,0 @@ -import 'dart:async'; -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] -class _ProductAdapter extends TypeAdapter { - @override - final int typeId = 1; - - @override - Product read(BinaryReader reader) => - Product.fromJson(jsonDecode(reader.readString()) as Map); - - @override - void write(BinaryWriter writer, Product obj) => - writer.writeString(jsonEncode(obj.toJson())); -} - -// TODO(monsieurtanuki): remove when old enough (today is 2022-06-16) -/// Where we store the products as "barcode => product". -@Deprecated('use [DaoProduct] instead') -class DaoHiveProduct extends AbstractDao implements DaoProductMigrationSource { - @Deprecated('use [DaoProduct] instead') - DaoHiveProduct(final LocalDatabase localDatabase) : super(localDatabase); - - static const String _hiveBoxName = 'products'; - - @override - Future init() async => Hive.openLazyBox(_hiveBoxName); - - @override - void registerAdapter() => Hive.registerAdapter(_ProductAdapter()); - - LazyBox _getBox() => Hive.lazyBox(_hiveBoxName); - - Future get(final String barcode) async => _getBox().get(barcode); - - @override - Future> getAll(final List barcodes) async { - final LazyBox box = _getBox(); - final Map result = {}; - for (final String barcode in barcodes) { - final Product? product = await box.get(barcode); - if (product != null) { - result[barcode] = product; - } - } - return result; - } - - Future put(final Product product) async => putAll([product]); - - Future putAll(final Iterable products) async { - final Map upserts = {}; - for (final Product product in products) { - upserts[product.barcode!] = product; - } - await _getBox().putAll(upserts); - } - - @override - Future> getAllKeys() async { - final LazyBox box = _getBox(); - final List result = []; - for (final dynamic key in box.keys) { - result.add(key.toString()); - } - return result; - } - - // Just for the migration - @override - Future deleteAll(final List barcodes) async { - final LazyBox box = _getBox(); - await box.deleteAll(barcodes); - } -} diff --git a/packages/smooth_app/lib/database/dao_unzipped_product.dart b/packages/smooth_app/lib/database/dao_unzipped_product.dart deleted file mode 100644 index ad5d431b8f7..00000000000 --- a/packages/smooth_app/lib/database/dao_unzipped_product.dart +++ /dev/null @@ -1,160 +0,0 @@ -import 'dart:async'; -import 'dart:convert'; -import 'dart:math'; -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'; - -// TODO(monsieurtanuki): remove when old enough (today is 2022-07-07) -@Deprecated('use [DaoProduct] instead') -class DaoUnzippedProduct extends AbstractSqlDao - implements - BulkDeletable, - DaoProductMigrationSource, - DaoProductMigrationDestination { - @Deprecated('use [DaoProduct] instead') - DaoUnzippedProduct(final LocalDatabase localDatabase) : super(localDatabase); - - static const String _TABLE_PRODUCT = 'product'; - static const String _TABLE_PRODUCT_COLUMN_BARCODE = 'barcode'; - static const String _TABLE_PRODUCT_COLUMN_JSON = 'encoded_json'; - - static FutureOr onUpgrade( - final Database db, - final int oldVersion, - final int newVersion, - ) async { - if (oldVersion < 1) { - await db.execute('create table $_TABLE_PRODUCT(' - // cf. https://www.sqlite.org/lang_conflict.html - '$_TABLE_PRODUCT_COLUMN_BARCODE TEXT PRIMARY KEY on conflict replace' - ',$_TABLE_PRODUCT_COLUMN_JSON TEXT NOT NULL' - ')'); - } - } - - Future get(final String barcode) async { - final Map map = await getAll([barcode]); - return map[barcode]; - } - - @override - Future> getAll(final List barcodes) async { - final Map result = {}; - if (barcodes.isEmpty) { - return result; - } - for (int start = 0; - start < barcodes.length; - start += BulkManager.SQLITE_MAX_VARIABLE_NUMBER) { - final int size = min( - barcodes.length - start, - BulkManager.SQLITE_MAX_VARIABLE_NUMBER, - ); - final List> queryResults = - await localDatabase.database.query( - _TABLE_PRODUCT, - columns: [ - _TABLE_PRODUCT_COLUMN_BARCODE, - _TABLE_PRODUCT_COLUMN_JSON, - ], - where: '$_TABLE_PRODUCT_COLUMN_BARCODE in(? ${',?' * (size - 1)})', - whereArgs: barcodes.sublist(start, start + size), - ); - for (final Map row in queryResults) { - result[row[_TABLE_PRODUCT_COLUMN_BARCODE] as String] = - _getProductFromQueryResult(row); - } - } - return result; - } - - Future put(final Product product) async => putAll([product]); - - /// Replaces products in database - @override - Future putAll(final Iterable products) async => - localDatabase.database.transaction( - (final Transaction transaction) async => - _bulkReplaceLoop(transaction, products), - ); - - @override - Future> getAllKeys() async { - final List result = []; - final List> queryResults = - await localDatabase.database.query( - _TABLE_PRODUCT, - columns: [ - _TABLE_PRODUCT_COLUMN_BARCODE, - ], - ); - if (queryResults.isEmpty) { - return result; - } - for (final Map row in queryResults) { - result.add(row[_TABLE_PRODUCT_COLUMN_BARCODE] as String); - } - return result; - } - - /// Replaces product data in bulk mode. - /// - /// Unfortunately it's a replace (=delete if already exists, then insert), - /// not an upsert (=insert if possible, or update if already exists). - /// "upsert" is not really supported for the moment on sqflite. - /// The direct impact is we shouldn't use foreign key constraints on - /// `product.barcode`. - Future _bulkReplaceLoop( - final DatabaseExecutor databaseExecutor, - final Iterable products, - ) async { - final BulkManager bulkManager = BulkManager(); - final List insertParameters = []; - for (final Product product in products) { - insertParameters.add(product.barcode); - insertParameters.add(json.encode(product.toJson())); - } - await bulkManager.insert( - bulkInsertable: this, - parameters: insertParameters, - databaseExecutor: databaseExecutor, - ); - } - - @override - List getInsertColumns() => [ - _TABLE_PRODUCT_COLUMN_BARCODE, - _TABLE_PRODUCT_COLUMN_JSON, - ]; - - @override - String getDeleteWhere(final List deleteWhereArgs) => - '$_TABLE_PRODUCT_COLUMN_BARCODE in (?${',?' * (deleteWhereArgs.length - 1)})'; - - @override - String getTableName() => _TABLE_PRODUCT; - - Product _getProductFromQueryResult(final Map row) { - final String encodedJson = row[_TABLE_PRODUCT_COLUMN_JSON] as String; - final Map decodedJson = - json.decode(encodedJson) as Map; - return Product.fromJson(decodedJson); - } - - @override - Future deleteAll(final List barcodes) async { - final BulkManager bulkManager = BulkManager(); - localDatabase.database.transaction( - (final Transaction transaction) async => bulkManager.delete( - bulkDeletable: this, - parameters: barcodes, - databaseExecutor: transaction, - ), - ); - } -} diff --git a/packages/smooth_app/lib/database/local_database.dart b/packages/smooth_app/lib/database/local_database.dart index 21db1927c71..fe1d8676315 100644 --- a/packages/smooth_app/lib/database/local_database.dart +++ b/packages/smooth_app/lib/database/local_database.dart @@ -9,17 +9,14 @@ import 'package:smooth_app/background/background_task_manager.dart'; import 'package:smooth_app/data_models/up_to_date_product_list_provider.dart'; import 'package:smooth_app/data_models/up_to_date_product_provider.dart'; import 'package:smooth_app/database/abstract_dao.dart'; -import 'package:smooth_app/database/dao_hive_product.dart'; import 'package:smooth_app/database/dao_instant_string.dart'; import 'package:smooth_app/database/dao_int.dart'; import 'package:smooth_app/database/dao_product.dart'; import 'package:smooth_app/database/dao_product_list.dart'; -import 'package:smooth_app/database/dao_product_migration.dart'; import 'package:smooth_app/database/dao_string.dart'; import 'package:smooth_app/database/dao_string_list.dart'; import 'package:smooth_app/database/dao_string_list_map.dart'; import 'package:smooth_app/database/dao_transient_operation.dart'; -import 'package:smooth_app/database/dao_unzipped_product.dart'; import 'package:smooth_app/database/dao_work_barcode.dart'; import 'package:sqflite/sqflite.dart'; @@ -74,7 +71,6 @@ class LocalDatabase extends ChangeNotifier { // only hive from there await Hive.initFlutter(); final List daos = [ - DaoHiveProduct(localDatabase), DaoProductList(localDatabase), DaoStringList(localDatabase), DaoString(localDatabase), @@ -91,14 +87,7 @@ class LocalDatabase extends ChangeNotifier { } // Migrations here - await DaoProductMigration.migrate( - source: DaoHiveProduct(localDatabase), - destination: DaoUnzippedProduct(localDatabase), - ); - await DaoProductMigration.migrate( - source: DaoUnzippedProduct(localDatabase), - destination: DaoProduct(localDatabase), - ); + // (no migration for the moment) return localDatabase; } @@ -112,7 +101,6 @@ class LocalDatabase extends ChangeNotifier { final int oldVersion, final int newVersion, ) async { - await DaoUnzippedProduct.onUpgrade(db, oldVersion, newVersion); await DaoProduct.onUpgrade(db, oldVersion, newVersion); await DaoWorkBarcode.onUpgrade(db, oldVersion, newVersion); } diff --git a/packages/smooth_app/lib/pages/crop_page.dart b/packages/smooth_app/lib/pages/crop_page.dart index 83aad485168..ac90e5c9e9f 100644 --- a/packages/smooth_app/lib/pages/crop_page.dart +++ b/packages/smooth_app/lib/pages/crop_page.dart @@ -294,8 +294,8 @@ class _CropPageState extends State { title: appLocalizations.crop_page_too_small_image_title, body: Text( appLocalizations.crop_page_too_small_image_message( - BackgroundTaskImage.minimumWidth, - BackgroundTaskImage.minimumHeight, + ImageHelper.minimumWidth, + ImageHelper.minimumHeight, width, height, ),