-
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 4807 - putting back a useless hive table, just in case (#4821)
* fix: 4807 - putting back a useless hive table, just in case * fix: 4807
- Loading branch information
1 parent
6e0034b
commit 0a5b18d
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters