// 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, ); List existingProducts = await getBoxes(boxName); if (!existingProducts.contains(item)) { openBox.add(item); return true; } return false; } static Future addBoxes(List items, String boxName) async { final openBox = await Hive.openLazyBox( boxName, ); List existingProducts = await getBoxes(boxName); for (var item in items) { if (!existingProducts.contains(item)) { openBox.add(item); } } } static Future deleteBox(T item, String boxName) async { final openBox = await Hive.openLazyBox( boxName, ); List boxes = await getBoxes(boxName); for (var box in boxes) { if (box == item) { openBox.deleteAt(boxes.indexOf(item)); } } } static Future updateBox(T item, T newItem, String boxName) async { final openBox = await Hive.openLazyBox( boxName, ); List boxes = await getBoxes(boxName); for (var box in boxes) { if (box == item) { openBox.putAt(boxes.indexOf(item), newItem); } } } static Future> getBoxes(String boxName, [String? query]) async { List boxList = []; final openBox = await Hive.openLazyBox(boxName); int length = openBox.length; for (int i = 0; i < length; i++) { boxList.add(await openBox.getAt(i) as T); } return boxList; } }