Skip to content

Commit ede412f

Browse files
committed
Update hive_services.dart
1 parent b2ea4a4 commit ede412f

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

lib/services/hive_services.dart

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'package:flutter/foundation.dart';
22
import 'package:hive_flutter/hive_flutter.dart';
33

4+
enum HiveBoxType { normal, lazy }
5+
46
const String kDataBox = "apidash-data";
57
const String kKeyDataBoxIds = "ids";
68

@@ -11,6 +13,17 @@ const String kHistoryMetaBox = "apidash-history-meta";
1113
const String kHistoryBoxIds = "historyIds";
1214
const String kHistoryLazyBox = "apidash-history-lazy";
1315

16+
const String kDashBotBox = "apidash-dashbot-data";
17+
const String kKeyDashBotBoxIds = 'messages';
18+
19+
const kHiveBoxes = [
20+
(kDataBox, HiveBoxType.normal),
21+
(kEnvironmentBox, HiveBoxType.normal),
22+
(kHistoryMetaBox, HiveBoxType.normal),
23+
(kHistoryLazyBox, HiveBoxType.lazy),
24+
(kDashBotBox, HiveBoxType.lazy),
25+
];
26+
1427
Future<bool> initHiveBoxes(
1528
bool initializeUsingPath,
1629
String? workspaceFolderPath,
@@ -34,10 +47,13 @@ Future<bool> initHiveBoxes(
3447

3548
Future<bool> openHiveBoxes() async {
3649
try {
37-
await Hive.openBox(kDataBox);
38-
await Hive.openBox(kEnvironmentBox);
39-
await Hive.openBox(kHistoryMetaBox);
40-
await Hive.openLazyBox(kHistoryLazyBox);
50+
for (var box in kHiveBoxes) {
51+
if (box.$2 == HiveBoxType.normal) {
52+
await Hive.openBox(box.$1);
53+
} else if (box.$2 == HiveBoxType.lazy) {
54+
await Hive.openLazyBox(box.$1);
55+
}
56+
}
4157
return true;
4258
} catch (e) {
4359
debugPrint("ERROR OPEN HIVE BOXES: $e");
@@ -47,17 +63,14 @@ Future<bool> openHiveBoxes() async {
4763

4864
Future<void> clearHiveBoxes() async {
4965
try {
50-
if (Hive.isBoxOpen(kDataBox)) {
51-
await Hive.box(kDataBox).clear();
52-
}
53-
if (Hive.isBoxOpen(kEnvironmentBox)) {
54-
await Hive.box(kEnvironmentBox).clear();
55-
}
56-
if (Hive.isBoxOpen(kHistoryMetaBox)) {
57-
await Hive.box(kHistoryMetaBox).clear();
58-
}
59-
if (Hive.isBoxOpen(kHistoryLazyBox)) {
60-
await Hive.lazyBox(kHistoryLazyBox).clear();
66+
for (var box in kHiveBoxes) {
67+
if (Hive.isBoxOpen(box.$1)) {
68+
if (box.$2 == HiveBoxType.normal) {
69+
await Hive.box(box.$1).clear();
70+
} else if (box.$2 == HiveBoxType.lazy) {
71+
await Hive.lazyBox(box.$1).clear();
72+
}
73+
}
6174
}
6275
} catch (e) {
6376
debugPrint("ERROR CLEAR HIVE BOXES: $e");
@@ -66,17 +79,14 @@ Future<void> clearHiveBoxes() async {
6679

6780
Future<void> deleteHiveBoxes() async {
6881
try {
69-
if (Hive.isBoxOpen(kDataBox)) {
70-
await Hive.box(kDataBox).deleteFromDisk();
71-
}
72-
if (Hive.isBoxOpen(kEnvironmentBox)) {
73-
await Hive.box(kEnvironmentBox).deleteFromDisk();
74-
}
75-
if (Hive.isBoxOpen(kHistoryMetaBox)) {
76-
await Hive.box(kHistoryMetaBox).deleteFromDisk();
77-
}
78-
if (Hive.isBoxOpen(kHistoryLazyBox)) {
79-
await Hive.lazyBox(kHistoryLazyBox).deleteFromDisk();
82+
for (var box in kHiveBoxes) {
83+
if (Hive.isBoxOpen(box.$1)) {
84+
if (box.$2 == HiveBoxType.normal) {
85+
await Hive.box(box.$1).deleteFromDisk();
86+
} else if (box.$2 == HiveBoxType.lazy) {
87+
await Hive.lazyBox(box.$1).deleteFromDisk();
88+
}
89+
}
8090
}
8191
await Hive.close();
8292
} catch (e) {
@@ -91,13 +101,15 @@ class HiveHandler {
91101
late final Box environmentBox;
92102
late final Box historyMetaBox;
93103
late final LazyBox historyLazyBox;
104+
late final LazyBox dashBotBox;
94105

95106
HiveHandler() {
96107
debugPrint("Trying to open Hive boxes");
97108
dataBox = Hive.box(kDataBox);
98109
environmentBox = Hive.box(kEnvironmentBox);
99110
historyMetaBox = Hive.box(kHistoryMetaBox);
100111
historyLazyBox = Hive.lazyBox(kHistoryLazyBox);
112+
dashBotBox = Hive.lazyBox(kDashBotBox);
101113
}
102114

103115
dynamic getIds() => dataBox.get(kKeyDataBoxIds);
@@ -150,6 +162,7 @@ class HiveHandler {
150162
await environmentBox.clear();
151163
await historyMetaBox.clear();
152164
await historyLazyBox.clear();
165+
await dashBotBox.clear();
153166
}
154167

155168
Future<void> removeUnused() async {

0 commit comments

Comments
 (0)