// Copyright 2021 The Flutter team. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:hive/hive.dart'; class HiveService { static Future addBox(T item, String boxName) async { final openBox = await Hive.openLazyBox( boxName, ); final List existingProducts = await getBoxes(boxName); if (!existingProducts.contains(item)) { await openBox.add(item); return true; } return false; } static Future addBoxes(List items, String boxName) async { final openBox = await Hive.openLazyBox( boxName, ); final Set existingProducts = Set.unmodifiable(await getBoxes(boxName)); for (final item in items) { if (!existingProducts.contains(item)) { await openBox.add(item); } } } static Future deleteBox(T item, String boxName) async { final openBox = await Hive.openLazyBox( boxName, ); final List boxes = await getBoxes(boxName); for (final box in boxes) { if (box == item) { await openBox.deleteAt(boxes.indexOf(item)); } } } static Future updateBox(T item, T newItem, String boxName) async { final openBox = await Hive.openLazyBox( boxName, ); final List boxes = await getBoxes(boxName); for (final box in boxes) { if (box == item) { await openBox.putAt(boxes.indexOf(item), newItem); } } } static Future> getBoxes(String boxName, [String? query]) async { final openBox = await Hive.openLazyBox(boxName); final length = openBox.length; final boxList = [ for (int i = 0; i < length; i++) await openBox.getAt(i) as T ]; return boxList; } }