1
1
import 'package:flutter/foundation.dart' ;
2
2
import 'package:hive_flutter/hive_flutter.dart' ;
3
3
4
+ enum HiveBoxType { normal, lazy }
5
+
4
6
const String kDataBox = "apidash-data" ;
5
7
const String kKeyDataBoxIds = "ids" ;
6
8
@@ -11,6 +13,17 @@ const String kHistoryMetaBox = "apidash-history-meta";
11
13
const String kHistoryBoxIds = "historyIds" ;
12
14
const String kHistoryLazyBox = "apidash-history-lazy" ;
13
15
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
+
14
27
Future <bool > initHiveBoxes (
15
28
bool initializeUsingPath,
16
29
String ? workspaceFolderPath,
@@ -34,10 +47,13 @@ Future<bool> initHiveBoxes(
34
47
35
48
Future <bool > openHiveBoxes () async {
36
49
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
+ }
41
57
return true ;
42
58
} catch (e) {
43
59
debugPrint ("ERROR OPEN HIVE BOXES: $e " );
@@ -47,17 +63,14 @@ Future<bool> openHiveBoxes() async {
47
63
48
64
Future <void > clearHiveBoxes () async {
49
65
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
+ }
61
74
}
62
75
} catch (e) {
63
76
debugPrint ("ERROR CLEAR HIVE BOXES: $e " );
@@ -66,17 +79,14 @@ Future<void> clearHiveBoxes() async {
66
79
67
80
Future <void > deleteHiveBoxes () async {
68
81
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
+ }
80
90
}
81
91
await Hive .close ();
82
92
} catch (e) {
@@ -91,13 +101,15 @@ class HiveHandler {
91
101
late final Box environmentBox;
92
102
late final Box historyMetaBox;
93
103
late final LazyBox historyLazyBox;
104
+ late final LazyBox dashBotBox;
94
105
95
106
HiveHandler () {
96
107
debugPrint ("Trying to open Hive boxes" );
97
108
dataBox = Hive .box (kDataBox);
98
109
environmentBox = Hive .box (kEnvironmentBox);
99
110
historyMetaBox = Hive .box (kHistoryMetaBox);
100
111
historyLazyBox = Hive .lazyBox (kHistoryLazyBox);
112
+ dashBotBox = Hive .lazyBox (kDashBotBox);
101
113
}
102
114
103
115
dynamic getIds () => dataBox.get (kKeyDataBoxIds);
@@ -150,6 +162,7 @@ class HiveHandler {
150
162
await environmentBox.clear ();
151
163
await historyMetaBox.clear ();
152
164
await historyLazyBox.clear ();
165
+ await dashBotBox.clear ();
153
166
}
154
167
155
168
Future <void > removeUnused () async {
0 commit comments