|
| 1 | +import 'package:hive/hive.dart'; |
| 2 | + |
| 3 | +import 'base_service.dart'; |
| 4 | + |
| 5 | +/// A class implementing LocalStorageService interface using Hive as storage backend. |
| 6 | +class LocalStorageWithHive implements LocalStorageService { |
| 7 | + final Map<String, Box> _collections = {}; |
| 8 | + |
| 9 | + @override |
| 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); |
| 14 | + } |
| 15 | + |
| 16 | + @override |
| 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); |
| 26 | + } |
| 27 | + |
| 28 | + @override |
| 29 | + Future<void> createMany({ |
| 30 | + String collectionName = 'vaah-flutter-hive-box', |
| 31 | + required Map<String, String> values, |
| 32 | + }) async { |
| 33 | + for (String k in values.keys) { |
| 34 | + await create(collectionName: collectionName, key: k, value: values[k]!); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @override |
| 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.'); |
| 44 | + |
| 45 | + String? result = _collections[collectionName]!.get(key); |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + @override |
| 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.'); |
| 55 | + |
| 56 | + if (keys.isNotEmpty) { |
| 57 | + Map<String, String?> result = {}; |
| 58 | + for (String k in keys) { |
| 59 | + result[k] = await read(collectionName: collectionName, key: k); |
| 60 | + } |
| 61 | + return result; |
| 62 | + } else { |
| 63 | + return {}; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @override |
| 68 | + Future<Map<String, String?>> readAll({String collectionName = 'vaah-flutter-hive-box'}) async { |
| 69 | + assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.'); |
| 70 | + |
| 71 | + Map<String, String?> result = _collections[collectionName]! |
| 72 | + .toMap() |
| 73 | + .map((key, value) => MapEntry(key.toString(), value?.toString())); |
| 74 | + return result; |
| 75 | + } |
| 76 | + |
| 77 | + @override |
| 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); |
| 87 | + } |
| 88 | + |
| 89 | + @override |
| 90 | + Future<void> updateMany({ |
| 91 | + String collectionName = 'vaah-flutter-hive-box', |
| 92 | + required Map<String, String> values, |
| 93 | + }) async { |
| 94 | + for (String k in values.keys) { |
| 95 | + await update(collectionName: collectionName, key: k, value: values[k]!); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @override |
| 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); |
| 108 | + } |
| 109 | + |
| 110 | + @override |
| 111 | + Future<void> createOrUpdateMany({ |
| 112 | + String collectionName = 'vaah-flutter-hive-box', |
| 113 | + required Map<String, String> values, |
| 114 | + }) async { |
| 115 | + for (String k in values.keys) { |
| 116 | + await createOrUpdate(collectionName: collectionName, key: k, value: values[k]!); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @override |
| 121 | + Future<void> delete({String collectionName = 'vaah-flutter-hive-box', dynamic key}) async { |
| 122 | + assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.'); |
| 123 | + |
| 124 | + await _collections[collectionName]!.delete(key); |
| 125 | + } |
| 126 | + |
| 127 | + @override |
| 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.'); |
| 133 | + |
| 134 | + if (keys.isNotEmpty) { |
| 135 | + _collections[collectionName]!.deleteAll(keys); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @override |
| 140 | + Future<void> deleteAll({String collectionName = 'vaah-flutter-hive-box'}) async { |
| 141 | + assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.'); |
| 142 | + |
| 143 | + await _collections[collectionName]!.clear(); |
| 144 | + } |
| 145 | +} |
0 commit comments