Skip to content

Commit f80f573

Browse files
committed
Updated: with new structure
Fixed: removed Impl Updated: add method, parameter collectionName Updated: hive with new structure Updated: NoOpStorage with new structure Updated: FSS impl with new structure
1 parent 735d09e commit f80f573

File tree

6 files changed

+235
-165
lines changed

6 files changed

+235
-165
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
abstract class LocalStorageService {
2-
Future<void> init();
2+
Future<void> add(String collectionName);
33

4-
Future<void> create({required String key, required String value});
4+
Future<void> create({String collectionName, required String key, required String value});
55

6-
Future<void> createMany({required Map<String, String> values});
6+
Future<void> createMany({String collectionName, required Map<String, String> values});
77

8-
Future<String?> read({required String key});
8+
Future<String?> read({String collectionName, required String key});
99

10-
Future<Map<String, String?>> readMany({required List<String> keys});
10+
Future<Map<String, String?>> readMany({String collectionName, required List<String> keys});
1111

12-
Future<Map<String, String?>> readAll();
12+
Future<Map<String, String?>> readAll({String collectionName});
1313

14-
Future<void> update({required String key, required String value});
14+
Future<void> update({String collectionName, required String key, required String value});
1515

16-
Future<void> updateMany({required Map<String, String> values});
16+
Future<void> updateMany({String collectionName, required Map<String, String> values});
1717

18-
Future<void> createOrUpdate({required String key, required String value});
18+
Future<void> createOrUpdate({String collectionName, required String key, required String value});
1919

20-
Future<void> createOrUpdateMany({required Map<String, String> values});
20+
Future<void> createOrUpdateMany({String collectionName, required Map<String, String> values});
2121

22-
Future<void> delete({required String key});
22+
Future<void> delete({String collectionName, required String key});
2323

24-
Future<void> deleteMany({List<String> keys = const []});
24+
Future<void> deleteMany({String collectionName, List<String> keys = const []});
2525

26-
Future<void> deleteAll();
26+
Future<void> deleteAll({String collectionName});
2727
}

lib/vaahextendflutter/services/storage/local/services/flutter_secure_storage_impl.dart renamed to lib/vaahextendflutter/services/storage/local/services/flutter_secure_storage.dart

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,43 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
22

33
import 'base_service.dart';
44

5-
class FlutterSecureStorageImpl implements LocalStorageService {
5+
/// A class implementing LocalStorageService interface using FLutter Secure Storage as storage
6+
/// backend.
7+
class LocalStorageWithFlutterSecureStorage implements LocalStorageService {
68
final _storage = const FlutterSecureStorage();
79

810
@override
9-
Future<void> init() async {}
11+
Future<void> add(String name) async {}
1012

1113
@override
12-
Future<void> create({required String key, required String value}) async {
14+
Future<void> create({
15+
String collectionName = '',
16+
required String key,
17+
required String value,
18+
}) async {
1319
assert(!(await _storage.containsKey(key: key)), 'The key "$key" already exists.');
1420

1521
await _storage.write(key: key, value: value);
1622
}
1723

1824
@override
19-
Future<void> createMany({required Map<String, String> values}) async {
25+
Future<void> createMany({String collectionName = '', required Map<String, String> values}) async {
2026
for (String k in values.keys) {
2127
await create(key: k, value: values[k]!);
2228
}
2329
}
2430

2531
@override
26-
Future<String?> read({required String key}) async {
32+
Future<String?> read({String collectionName = '', required String key}) async {
2733
String? result = await _storage.read(key: key);
2834
return result;
2935
}
3036

3137
@override
32-
Future<Map<String, String?>> readMany({required List<String> keys}) async {
38+
Future<Map<String, String?>> readMany({
39+
String collectionName = '',
40+
required List<String> keys,
41+
}) async {
3342
if (keys.isNotEmpty) {
3443
Map<String, String?> result = {};
3544
for (String k in keys) {
@@ -42,44 +51,55 @@ class FlutterSecureStorageImpl implements LocalStorageService {
4251
}
4352

4453
@override
45-
Future<Map<String, String?>> readAll() async {
54+
Future<Map<String, String?>> readAll({String collectionName = ''}) async {
4655
final Map<String, String> result = await _storage.readAll();
4756
return result;
4857
}
4958

5059
@override
51-
Future<void> update({required String key, required String value}) async {
60+
Future<void> update({
61+
String collectionName = '',
62+
required String key,
63+
required String value,
64+
}) async {
5265
assert(await _storage.containsKey(key: key), 'The key "$key" does not exists.');
5366

5467
await _storage.write(key: key, value: value);
5568
}
5669

5770
@override
58-
Future<void> updateMany({required Map<String, String> values}) async {
71+
Future<void> updateMany({String collectionName = '', required Map<String, String> values}) async {
5972
for (String k in values.keys) {
6073
await update(key: k, value: values[k]!);
6174
}
6275
}
6376

6477
@override
65-
Future<void> createOrUpdate({required String key, required String value}) async {
78+
Future<void> createOrUpdate({
79+
String collectionName = '',
80+
required String key,
81+
required String value,
82+
}) async {
6683
await _storage.write(key: key, value: value);
6784
}
6885

6986
@override
70-
Future<void> createOrUpdateMany({required Map<String, String> values}) async {
87+
Future<void> createOrUpdateMany({
88+
String collectionName = '',
89+
required Map<String, String> values,
90+
}) async {
7191
for (String k in values.keys) {
7292
await createOrUpdate(key: k, value: values[k]!);
7393
}
7494
}
7595

7696
@override
77-
Future<void> delete({required String key}) async {
97+
Future<void> delete({String collectionName = '', required String key}) async {
7898
await _storage.delete(key: key);
7999
}
80100

81101
@override
82-
Future<void> deleteMany({List<String> keys = const []}) async {
102+
Future<void> deleteMany({String collectionName = '', List<String> keys = const []}) async {
83103
if (keys.isNotEmpty) {
84104
for (String k in keys) {
85105
await _storage.delete(key: k);
@@ -88,7 +108,7 @@ class FlutterSecureStorageImpl implements LocalStorageService {
88108
}
89109

90110
@override
91-
Future<void> deleteAll() async {
111+
Future<void> deleteAll({String collectionName = ''}) async {
92112
await _storage.deleteAll();
93113
}
94114
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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

Comments
 (0)