Update hive_services.dart

This commit is contained in:
Ashita Prasad
2025-05-17 23:42:10 +05:30
parent b2ea4a4c16
commit ede412f11b

View File

@@ -1,6 +1,8 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:hive_flutter/hive_flutter.dart'; import 'package:hive_flutter/hive_flutter.dart';
enum HiveBoxType { normal, lazy }
const String kDataBox = "apidash-data"; const String kDataBox = "apidash-data";
const String kKeyDataBoxIds = "ids"; const String kKeyDataBoxIds = "ids";
@@ -11,6 +13,17 @@ const String kHistoryMetaBox = "apidash-history-meta";
const String kHistoryBoxIds = "historyIds"; const String kHistoryBoxIds = "historyIds";
const String kHistoryLazyBox = "apidash-history-lazy"; const String kHistoryLazyBox = "apidash-history-lazy";
const String kDashBotBox = "apidash-dashbot-data";
const String kKeyDashBotBoxIds = 'messages';
const kHiveBoxes = [
(kDataBox, HiveBoxType.normal),
(kEnvironmentBox, HiveBoxType.normal),
(kHistoryMetaBox, HiveBoxType.normal),
(kHistoryLazyBox, HiveBoxType.lazy),
(kDashBotBox, HiveBoxType.lazy),
];
Future<bool> initHiveBoxes( Future<bool> initHiveBoxes(
bool initializeUsingPath, bool initializeUsingPath,
String? workspaceFolderPath, String? workspaceFolderPath,
@@ -34,10 +47,13 @@ Future<bool> initHiveBoxes(
Future<bool> openHiveBoxes() async { Future<bool> openHiveBoxes() async {
try { try {
await Hive.openBox(kDataBox); for (var box in kHiveBoxes) {
await Hive.openBox(kEnvironmentBox); if (box.$2 == HiveBoxType.normal) {
await Hive.openBox(kHistoryMetaBox); await Hive.openBox(box.$1);
await Hive.openLazyBox(kHistoryLazyBox); } else if (box.$2 == HiveBoxType.lazy) {
await Hive.openLazyBox(box.$1);
}
}
return true; return true;
} catch (e) { } catch (e) {
debugPrint("ERROR OPEN HIVE BOXES: $e"); debugPrint("ERROR OPEN HIVE BOXES: $e");
@@ -47,17 +63,14 @@ Future<bool> openHiveBoxes() async {
Future<void> clearHiveBoxes() async { Future<void> clearHiveBoxes() async {
try { try {
if (Hive.isBoxOpen(kDataBox)) { for (var box in kHiveBoxes) {
await Hive.box(kDataBox).clear(); if (Hive.isBoxOpen(box.$1)) {
} if (box.$2 == HiveBoxType.normal) {
if (Hive.isBoxOpen(kEnvironmentBox)) { await Hive.box(box.$1).clear();
await Hive.box(kEnvironmentBox).clear(); } else if (box.$2 == HiveBoxType.lazy) {
} await Hive.lazyBox(box.$1).clear();
if (Hive.isBoxOpen(kHistoryMetaBox)) { }
await Hive.box(kHistoryMetaBox).clear(); }
}
if (Hive.isBoxOpen(kHistoryLazyBox)) {
await Hive.lazyBox(kHistoryLazyBox).clear();
} }
} catch (e) { } catch (e) {
debugPrint("ERROR CLEAR HIVE BOXES: $e"); debugPrint("ERROR CLEAR HIVE BOXES: $e");
@@ -66,17 +79,14 @@ Future<void> clearHiveBoxes() async {
Future<void> deleteHiveBoxes() async { Future<void> deleteHiveBoxes() async {
try { try {
if (Hive.isBoxOpen(kDataBox)) { for (var box in kHiveBoxes) {
await Hive.box(kDataBox).deleteFromDisk(); if (Hive.isBoxOpen(box.$1)) {
} if (box.$2 == HiveBoxType.normal) {
if (Hive.isBoxOpen(kEnvironmentBox)) { await Hive.box(box.$1).deleteFromDisk();
await Hive.box(kEnvironmentBox).deleteFromDisk(); } else if (box.$2 == HiveBoxType.lazy) {
} await Hive.lazyBox(box.$1).deleteFromDisk();
if (Hive.isBoxOpen(kHistoryMetaBox)) { }
await Hive.box(kHistoryMetaBox).deleteFromDisk(); }
}
if (Hive.isBoxOpen(kHistoryLazyBox)) {
await Hive.lazyBox(kHistoryLazyBox).deleteFromDisk();
} }
await Hive.close(); await Hive.close();
} catch (e) { } catch (e) {
@@ -91,6 +101,7 @@ class HiveHandler {
late final Box environmentBox; late final Box environmentBox;
late final Box historyMetaBox; late final Box historyMetaBox;
late final LazyBox historyLazyBox; late final LazyBox historyLazyBox;
late final LazyBox dashBotBox;
HiveHandler() { HiveHandler() {
debugPrint("Trying to open Hive boxes"); debugPrint("Trying to open Hive boxes");
@@ -98,6 +109,7 @@ class HiveHandler {
environmentBox = Hive.box(kEnvironmentBox); environmentBox = Hive.box(kEnvironmentBox);
historyMetaBox = Hive.box(kHistoryMetaBox); historyMetaBox = Hive.box(kHistoryMetaBox);
historyLazyBox = Hive.lazyBox(kHistoryLazyBox); historyLazyBox = Hive.lazyBox(kHistoryLazyBox);
dashBotBox = Hive.lazyBox(kDashBotBox);
} }
dynamic getIds() => dataBox.get(kKeyDataBoxIds); dynamic getIds() => dataBox.get(kKeyDataBoxIds);
@@ -150,6 +162,7 @@ class HiveHandler {
await environmentBox.clear(); await environmentBox.clear();
await historyMetaBox.clear(); await historyMetaBox.clear();
await historyLazyBox.clear(); await historyLazyBox.clear();
await dashBotBox.clear();
} }
Future<void> removeUnused() async { Future<void> removeUnused() async {