Skip to content

Commit

Permalink
fix: 4807 - putting back a useless hive table, just in case (#4821)
Browse files Browse the repository at this point in the history
* fix: 4807 - putting back a useless hive table, just in case

* fix: 4807
  • Loading branch information
monsieurtanuki committed Nov 19, 2023
1 parent 6e0034b commit 0a5b18d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
82 changes: 82 additions & 0 deletions packages/smooth_app/lib/database/dao_hive_product.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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<Product> {
@override
final int typeId = 1;

@override
Product read(BinaryReader reader) =>
Product.fromJson(jsonDecode(reader.readString()) as Map<String, dynamic>);

@override
void write(BinaryWriter writer, Product obj) =>
writer.writeString(jsonEncode(obj.toJson()));
}

/// /!\ Stupid class not be used anymore (from 2022-06-16)
/// 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 {
@Deprecated('use [DaoProduct] instead')
DaoHiveProduct(final LocalDatabase localDatabase) : super(localDatabase);

static const String _hiveBoxName = 'products';

@override
Future<void> init() async => Hive.openLazyBox<Product>(_hiveBoxName);

@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);
}
}
2 changes: 2 additions & 0 deletions packages/smooth_app/lib/database/local_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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';
Expand Down Expand Up @@ -71,6 +72,7 @@ class LocalDatabase extends ChangeNotifier {
// only hive from there
await Hive.initFlutter();
final List<AbstractDao> daos = <AbstractDao>[
DaoHiveProduct(localDatabase),
DaoProductList(localDatabase),
DaoStringList(localDatabase),
DaoString(localDatabase),
Expand Down

0 comments on commit 0a5b18d

Please sign in to comment.