@@ -2,60 +2,61 @@ import 'package:hive/hive.dart';
22
33import 'base_service.dart' ;
44
5- /// A class implementing Storage interface using Hive as storage backend.
5+ /// A class implementing LocalStorageService interface using Hive as storage backend.
66class LocalStorageWithHive implements LocalStorageService {
7- final Set <String > _collections = {};
8-
9- final String name;
10-
11- Box ? _box;
12-
13- LocalStorageWithHive ({this .name = 'vaah_flutter_hive_box' });
14-
15- Future <void > add (String name) async {
16- assert (! _collections.contains (name), 'The name "$name " already exists' );
17-
18- _collections.add (name);
19- }
7+ final Map <String , Box > _collections = {};
208
219 @override
22- Future <void > init () async {
23- _box = await Hive .openBox (name);
10+ Future <void > add (String collectionName) async {
11+ assert (! _collections.containsKey (collectionName), 'The Box "$collectionName " already exists' );
12+
13+ _collections[collectionName] = await Hive .openBox (collectionName);
2414 }
2515
2616 @override
27- Future <void > create ({required String key, required String value}) async {
28- assert (_box != null , 'Box is null, not initiized.' );
29- assert (_box! .containsKey (key), 'The key ($key ) already exists.' );
30-
31- _box! .put (key, value);
17+ Future <void > create ({
18+ String collectionName = 'vaah-flutter-hive-box' ,
19+ required String key,
20+ required String value,
21+ }) async {
22+ assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
23+ assert (_collections[collectionName]! .containsKey (key), 'The key ($key ) already exists.' );
24+
25+ await _collections[collectionName]! .put (key, value);
3226 }
3327
3428 @override
35- Future <void > createMany ({required Map <String , String > values}) async {
29+ Future <void > createMany ({
30+ String collectionName = 'vaah-flutter-hive-box' ,
31+ required Map <String , String > values,
32+ }) async {
3633 for (String k in values.keys) {
37- await create (key: k, value: values[k]! );
34+ await create (collectionName : collectionName, key: k, value: values[k]! );
3835 }
3936 }
4037
4138 @override
42- Future <String ?> read ({required String key}) async {
43- assert (_box != null , 'Box is null, not initiized.' );
39+ Future <String ?> read ({
40+ String collectionName = 'vaah-flutter-hive-box' ,
41+ required String key,
42+ }) async {
43+ assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
4444
45- String ? result = _box ! .get (key);
45+ String ? result = _collections[collectionName] ! .get (key);
4646 return result;
4747 }
4848
4949 @override
50- Future <Map <String , String ?>> readMany ({required List <String > keys}) async {
51- assert (_box != null , 'Box is null, not initiized.' );
50+ Future <Map <String , String ?>> readMany ({
51+ String collectionName = 'vaah-flutter-hive-box' ,
52+ required List <String > keys,
53+ }) async {
54+ assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
5255
5356 if (keys.isNotEmpty) {
5457 Map <String , String ?> result = {};
5558 for (String k in keys) {
56- if (_box! .containsKey (k)) {
57- result[k] = _box! .get (k);
58- }
59+ result[k] = await read (collectionName: collectionName, key: k);
5960 }
6061 return result;
6162 } else {
@@ -64,63 +65,81 @@ class LocalStorageWithHive implements LocalStorageService {
6465 }
6566
6667 @override
67- Future <Map <String , String ?>> readAll () async {
68- assert (_box != null , 'Box is null, not initiized .' );
68+ Future <Map <String , String ?>> readAll ({ String collectionName = 'vaah-flutter-hive-box' } ) async {
69+ assert (_collections. containsKey (collectionName) , 'The Box "$ collectionName " does not exists .' );
6970
70- Map <String , String ?> result =
71- _box! .toMap ().map ((key, value) => MapEntry (key.toString (), value? .toString ()));
71+ Map <String , String ?> result = _collections[collectionName]!
72+ .toMap ()
73+ .map ((key, value) => MapEntry (key.toString (), value? .toString ()));
7274 return result;
7375 }
7476
7577 @override
76- Future <void > update ({required String key, required String value}) async {
77- assert (_box != null , 'Box is null, not initiized.' );
78- assert (! _box! .containsKey (key), 'The key ($key ) does not exist.' );
79-
80- _box! .put (key, value);
78+ Future <void > update ({
79+ String collectionName = 'vaah-flutter-hive-box' ,
80+ required String key,
81+ required String value,
82+ }) async {
83+ assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
84+ assert (! _collections[collectionName]! .containsKey (key), 'The key ($key ) does not exist.' );
85+
86+ _collections[collectionName]! .put (key, value);
8187 }
8288
8389 @override
84- Future <void > updateMany ({required Map <String , String > values}) async {
90+ Future <void > updateMany ({
91+ String collectionName = 'vaah-flutter-hive-box' ,
92+ required Map <String , String > values,
93+ }) async {
8594 for (String k in values.keys) {
86- await update (key: k, value: values[k]! );
95+ await update (collectionName : collectionName, key: k, value: values[k]! );
8796 }
8897 }
8998
9099 @override
91- Future <void > createOrUpdate ({required String key, required String value}) async {
92- assert (_box != null , 'Box is null, not initiized.' );
93-
94- _box! .put (key, value);
100+ Future <void > createOrUpdate ({
101+ String collectionName = 'vaah-flutter-hive-box' ,
102+ required String key,
103+ required String value,
104+ }) async {
105+ assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
106+
107+ _collections[collectionName]! .put (key, value);
95108 }
96109
97110 @override
98- Future <void > createOrUpdateMany ({required Map <String , String > values}) async {
111+ Future <void > createOrUpdateMany ({
112+ String collectionName = 'vaah-flutter-hive-box' ,
113+ required Map <String , String > values,
114+ }) async {
99115 for (String k in values.keys) {
100- await createOrUpdate (key: k, value: values[k]! );
116+ await createOrUpdate (collectionName : collectionName, key: k, value: values[k]! );
101117 }
102118 }
103119
104120 @override
105- Future <void > delete ({dynamic key}) async {
106- assert (_box != null , 'Box is null, not initiized .' );
121+ Future <void > delete ({String collectionName = 'vaah-flutter-hive-box' , dynamic key}) async {
122+ assert (_collections. containsKey (collectionName) , 'The Box "$ collectionName " does not exists .' );
107123
108- await _box ! .delete (key);
124+ await _collections[collectionName] ! .delete (key);
109125 }
110126
111127 @override
112- Future <void > deleteMany ({List <String > keys = const []}) async {
113- assert (_box != null , 'Box is null, not initiized.' );
128+ Future <void > deleteMany ({
129+ String collectionName = 'vaah-flutter-hive-box' ,
130+ List <String > keys = const [],
131+ }) async {
132+ assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
114133
115134 if (keys.isNotEmpty) {
116- _box ! .deleteAll (keys);
135+ _collections[collectionName] ! .deleteAll (keys);
117136 }
118137 }
119138
120139 @override
121- Future <void > deleteAll () async {
122- assert (_box != null , 'Box is null, not initiized .' );
140+ Future <void > deleteAll ({ String collectionName = 'vaah-flutter-hive-box' } ) async {
141+ assert (_collections. containsKey (collectionName) , 'The Box "$ collectionName " does not exists .' );
123142
124- await _box ! .clear ();
143+ await _collections[collectionName] ! .clear ();
125144 }
126145}
0 commit comments