Skip to content

Commit d307b10

Browse files
committed
change collection to box
1 parent 99c6ec0 commit d307b10

File tree

2 files changed

+31
-50
lines changed

2 files changed

+31
-50
lines changed

lib/utils/hive_util.dart

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ import '../models/repositories.dart';
1010
class HiveUtil {
1111
HiveUtil._();
1212

13-
static const hiveData = 'hive';
14-
1513
static const userToken = 'user_token';
1614

17-
static BoxCollection? _collection;
18-
19-
static BoxCollection get collection => _collection!;
15+
static Box<UserAuthen>? _box;
16+
static Box<UserAuthen> get box => _box!;
2017

2118
static Future<void> init() async {
2219
const secureStorage = FlutterSecureStorage();
@@ -41,47 +38,29 @@ class HiveUtil {
4138
static Future<void> initByKey(List<int> key, String path) async {
4239
Hive.registerAdapter<UserAuthen>(UserAuthenAdapter());
4340

44-
_collection = await BoxCollection.open(
45-
hiveData,
46-
{userToken},
41+
_box = await Hive.openBox<UserAuthen>(
42+
HiveUtil.userToken,
43+
encryptionCipher: HiveAesCipher(key),
4744
path: '${path.replaceFirst(RegExp(r'[\\/]$'), '')}/',
48-
key: HiveAesCipher(key),
4945
);
5046
}
5147

52-
static Set<String> get boxNames => collection.boxNames;
53-
54-
static void close() {
55-
collection.close();
56-
}
57-
58-
static Future<void> deleteFromDisk() {
59-
return collection.deleteFromDisk();
48+
/// open another box
49+
static Future<Box<V>> openBox<V>(String name) {
50+
assert(name != HiveUtil.userToken, 'Please use HiveUtil.box');
51+
return Hive.openBox<V>(
52+
name,
53+
path: box.path,
54+
);
6055
}
6156

62-
static String get name => collection.name;
57+
static String get name => box.name;
6358

64-
static Future<CollectionBox<V>> openBox<V>(
65-
String name, {
66-
bool preload = false,
67-
CollectionBox<V> Function(String, BoxCollection)? boxCreator,
68-
}) {
69-
return collection.openBox<V>(
70-
name,
71-
preload: preload,
72-
boxCreator: boxCreator,
73-
);
59+
static void close() {
60+
box.close();
7461
}
7562

76-
static Future<void> transaction(
77-
Future<void> Function() action, {
78-
List<String>? boxNames,
79-
bool readOnly = false,
80-
}) {
81-
return collection.transaction(
82-
action,
83-
boxNames: boxNames,
84-
readOnly: readOnly,
85-
);
63+
static Future<void> deleteFromDisk() {
64+
return box.deleteFromDisk();
8665
}
8766
}

test/hive_test.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ import 'dart:io';
33
import 'package:flutter_test/flutter_test.dart';
44
import 'package:juejin/exports.dart';
55

6+
const hiveKey = [
7+
69, 65, 214, 189, 167, 95, 128, 69, //
8+
125, 194, 153, 172, 57, 105, 57, 253,
9+
20, 174, 135, 53, 77, 167, 78, 244,
10+
248, 168, 30, 29, 185, 35, 89, 139,
11+
];
12+
13+
final hivePath = '${Directory.current.path}/test/hive';
14+
615
void main() {
7-
HiveUtil.initByKey(
8-
[
9-
69, 65, 214, 189, 167, 95, 128, 69, //
10-
125, 194, 153, 172, 57, 105, 57, 253,
11-
20, 174, 135, 53, 77, 167, 78, 244,
12-
248, 168, 30, 29, 185, 35, 89, 139,
13-
],
14-
'${Directory.current.path}/test/hive',
15-
);
1616
test('user_token', () async {
17-
final box = await HiveUtil.openBox<UserAuthen>(HiveUtil.userToken);
17+
await HiveUtil.initByKey(hiveKey, hivePath);
18+
19+
final box = HiveUtil.box;
1820

19-
expect(box.name, '${HiveUtil.hiveData}_${HiveUtil.userToken}');
21+
expect(box.name, HiveUtil.userToken);
2022

2123
await box.clear();
2224
await box.put('token', UserAuthen(token: 'test-token'));
2325

24-
final token = await box.get('token');
26+
final token = box.get('token');
2527
expect(token?.token, 'test-token');
2628

2729
HiveUtil.close();

0 commit comments

Comments
 (0)