Skip to content

Commit ba27e5c

Browse files
committed
Updated: LocalStorage with new structure...
...updated the documentation comments
1 parent f80f573 commit ba27e5c

File tree

1 file changed

+145
-56
lines changed

1 file changed

+145
-56
lines changed

lib/vaahextendflutter/services/storage/local/storage.dart

Lines changed: 145 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,69 @@ LocalStorageService get instanceLocal {
1919
abstract class LocalStorage {
2020
static final LocalStorageService _instanceLocal = instanceLocal;
2121

22-
/// Initializes the [LocalStorage].
23-
/// In the case of [LocalStorageWithHive], it creates a [Directory] using the path_provide package,
24-
/// initializes hive at that directory and opens a box with name [name] provided during [LocalStorage]
25-
/// creation.
22+
/// Adds a new collection (which is basically a Hive Box) with [collectionName] in [LocalStorage].
23+
/// In the case of [LocalStorageWithHive], it opens a box with name [collectionName] provided.
24+
///
2625
/// It's not required in the case of [LocalStorageWithFlutterSecureStorage].
2726
///
2827
/// Example:
2928
/// ```dart
30-
/// Storage.init();
29+
/// LocalStorage.add('posts');
30+
/// //used only with Hive
3131
/// ```
32-
static Future<void> init() {
33-
return _instanceLocal.init();
32+
Future<void> add(String collectionName) {
33+
return _instanceLocal.add(collectionName);
3434
}
3535

36-
/// Creates a new item in the [LocalStorage].
36+
/// Creates a new item in the [collectionName] of [LocalStorage].
3737
///
3838
/// To create a single key-value pair pass the [key] and the [value] as String, the
3939
/// String could be a JSON String or a simple text according to your requirement.
40-
/// If the key is already present in the [LocalStorage] it's vlaue will be overwritten.
40+
/// If the key is already present in the [LocalStorage] an assertion error will be thrown.
4141
///
4242
/// Example:
43+
///
44+
/// Hive
45+
/// ```dart
46+
/// await LocalStorage.create(collectionName: 'posts',key: 'key', value: 'value');
47+
/// ```
48+
/// Flutter Secure Storage
4349
/// ```dart
44-
/// await Storage.create(key: 'key', value: 'value');
50+
/// await LocalStorage.create(key: 'key', value: 'value');
4551
/// ```
46-
static Future<void> create(String name, {required String key, required String value}) {
47-
return _instanceLocal.create(key: key, value: value);
52+
static Future<void> create({
53+
String collectionName = 'vaah-flutter-box',
54+
required String key,
55+
required String value,
56+
}) {
57+
return _instanceLocal.create(collectionName: collectionName, key: key, value: value);
4858
}
4959

50-
/// Creates new items in the [LocalStorage].
60+
/// Creates new items in the[collectionName] of [LocalStorage].
5161
/// If you want to create multiple entries pass the [values] as a Map<String, String>, then it
5262
/// will create all the key-value pairs from the [values] map.
53-
/// If any key from the [values] is already present in the [LocalStorage] it's value will be
54-
/// overwritten.
63+
/// If any key from the [values] is already present in the [LocalStorage] an assertion error will
64+
/// be thrown.
5565
///
5666
/// Example:
67+
///
68+
/// Hive
5769
/// ```dart
58-
/// await Storage.createMany(values: {
70+
/// await LocalStorage.createMany(
71+
/// collectionName: 'posts',
72+
/// values: {
73+
/// 'key1': 'Value1',
74+
/// 'key2': 'Value2',
75+
/// 'key3': 'Value3',
76+
/// 'key4': 'Value4',
77+
/// 'key5': 'Value5',
78+
/// //...
79+
/// },
80+
/// );
81+
/// ```
82+
/// Flutter Secure Storage
83+
/// ```dart
84+
/// await LocalStorage.createMany(values: {
5985
/// 'key1': 'Value1',
6086
/// 'key2': 'Value2',
6187
/// 'key3': 'Value3',
@@ -65,62 +91,87 @@ abstract class LocalStorage {
6591
/// },
6692
/// );
6793
/// ```
68-
static Future<void> createMany({required Map<String, String> values}) {
69-
return _instanceLocal.createMany(values: values);
94+
static Future<void> createMany({
95+
String collectionName = 'vaah-flutter-box',
96+
required Map<String, String> values,
97+
}) {
98+
return _instanceLocal.createMany(collectionName: collectionName, values: values);
7099
}
71100

72101
/// Reads the value of the item at [key] from the [LocalStorage] and returns the value.
73102
///
74103
/// Read a single value by passing [key] as String, it will return the value as String?.
104+
/// If the [key] already exists an assertion error will be thrown.
75105
///
76106
/// Example:
77107
/// ```dart
78-
/// await Storage.read(key: 'key');
108+
/// await LocalStorage.read(collectionName: 'posts', key: 'key');
109+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
79110
/// ```
80-
static Future<String?> read({required String key}) {
81-
return _instanceLocal.read(key: key);
111+
static Future<String?> read({String collectionName = 'vaah-flutter-box', required String key}) {
112+
return _instanceLocal.read(collectionName: collectionName, key: key);
82113
}
83114

84-
/// Reads multiple values, pass the List of [keys] as argument. It will return the value as
85-
/// Map<String, String?>.
115+
/// Reads multiple values from [collectionName], pass the List of [keys] as argument. It will
116+
/// return the value as Map<String, String?>.
86117
///
87118
/// Example:
88119
/// ```dart
89-
/// await Storage.readMany(keys: [
120+
/// await LocalStorage.readMany(
121+
/// collectionName: 'posts',
122+
/// keys: [
90123
/// 'key1',
91124
/// 'key2',
92125
/// //...
93126
/// ],
94127
/// );
128+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
95129
/// ```
96-
static Future<Map<String, String?>> readMany({required List<String> keys}) {
97-
return _instanceLocal.readMany(keys: keys);
130+
static Future<Map<String, String?>> readMany({
131+
String collectionName = 'vaah-flutter-box',
132+
required List<String> keys,
133+
}) {
134+
return _instanceLocal.readMany(collectionName: collectionName, keys: keys);
98135
}
99136

100-
/// It will return all the values from that [LocalStorage] as Map<String, String?>.
101-
static Future<Map<String, String?>> readAll() {
102-
return _instanceLocal.readAll();
137+
/// It will return all the values from that collection [collectionName] of [LocalStorage] as
138+
/// Map<String, String?>.
139+
///
140+
/// Exapmle:
141+
/// ```dart
142+
/// await LocalStorage.readAll(collectionName: 'posts');
143+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
144+
/// ```
145+
static Future<Map<String, String?>> readAll({String collectionName = 'vaah-flutter-box'}) {
146+
return _instanceLocal.readAll(collectionName: collectionName);
103147
}
104148

105-
/// Updates an item in the [LocalStorage].
149+
/// Updates an item in [collectionName] of the [LocalStorage].
106150
///
107151
/// To update a single key-value pair pass the [key] and the [value] as String, the
108152
///
109153
/// Example:
110154
/// ```dart
111-
/// await Storage.update(key: 'key', value: 'value');
155+
/// await LocalStorage.update(collectionName: 'posts', key: 'key', value: 'value');
156+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
112157
/// ```
113-
static Future<void> update({required String key, required String value}) {
114-
return _instanceLocal.update(key: key, value: value);
158+
static Future<void> update({
159+
String collectionName = 'vaah-flutter-box',
160+
required String key,
161+
required String value,
162+
}) {
163+
return _instanceLocal.update(collectionName: collectionName, key: key, value: value);
115164
}
116165

117-
/// Updates items in the [LocalStorage].
166+
/// Updates items in [collectionName] of [LocalStorage].
118167
/// If you want to update multiple entries pass the [values] as a Map<String, String>, then it
119168
/// will update all the key-value pairs in the [values] map.
120169
///
121170
/// Example:
122171
/// ```dart
123-
/// await Storage.updateMany(values: {
172+
/// await LocalStorage.updateMany(
173+
/// collectionName: 'posts',
174+
/// values: {
124175
/// 'key1': 'Value1',
125176
/// 'key2': 'Value2',
126177
/// 'key3': 'Value3',
@@ -129,33 +180,44 @@ abstract class LocalStorage {
129180
/// //...
130181
/// },
131182
/// );
183+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
132184
/// ```
133-
static Future<void> updateMany({required Map<String, String> values}) {
134-
return _instanceLocal.updateMany(values: values);
185+
static Future<void> updateMany({
186+
String collectionName = 'vaah-flutter-box',
187+
required Map<String, String> values,
188+
}) {
189+
return _instanceLocal.updateMany(collectionName: collectionName, values: values);
135190
}
136191

137-
/// Creates or updates an item in the [LocalStorage].
192+
/// Creates or updates an item in [collectionName] of [LocalStorage].
138193
///
139194
/// To create or update a single key-value pair pass the [key] and the [value] as String, the
140195
/// If the [key] is already present in the [LocalStorage] it's value will be overwritten.
141196
///
142197
/// Example:
143198
/// ```dart
144-
/// await Storage.createOrUpdate(key: 'key', value: 'value');
199+
/// await LocalStorage.createOrUpdate(collectionName:'posts', key: 'key', value: 'value');
200+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
145201
/// ```
146-
static Future<void> createOrUpdate({required String key, required String value}) {
147-
return _instanceLocal.createOrUpdate(key: key, value: value);
202+
static Future<void> createOrUpdate({
203+
String collectionName = 'vaah-flutter-box',
204+
required String key,
205+
required String value,
206+
}) {
207+
return _instanceLocal.createOrUpdate(collectionName: collectionName, key: key, value: value);
148208
}
149209

150-
/// Creates or updates items in the [LocalStorage].
210+
/// Creates or updates items in [collectionName] of [LocalStorage].
151211
/// If you want to create or update multiple entries pass the [values] as a Map<String, String>, then it
152212
/// will create all the key-value pairs from the [values] map.
153213
/// If any key from the [values] is already present in the [LocalStorage] it's value will be
154214
/// overwritten.
155215
///
156216
/// Example:
157217
/// ```dart
158-
/// await Storage.createOrUpdateMany(values: {
218+
/// await LocalStorage.createOrUpdateMany(
219+
/// collectionName:'posts',
220+
/// values: {
159221
/// 'key1': 'Value1',
160222
/// 'key2': 'Value2',
161223
/// 'key3': 'Value3',
@@ -164,38 +226,65 @@ abstract class LocalStorage {
164226
/// //...
165227
/// },
166228
/// );
229+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
167230
/// ```
168-
static Future<void> createOrUpdateMany({required Map<String, String> values}) {
169-
return _instanceLocal.createOrUpdateMany(values: values);
231+
static Future<void> createOrUpdateMany({
232+
String collectionName = 'vaah-flutter-box',
233+
required Map<String, String> values,
234+
}) {
235+
return _instanceLocal.createOrUpdateMany(collectionName: collectionName, values: values);
170236
}
171237

172-
/// Deletes an item at [key].
238+
/// Deletes an item at [key] from [collectionName] in [LocalStorage].
173239
///
174240
/// Example:
175241
/// ```dart
176-
/// await Storage.delete(key: 'key');
242+
/// await LocalStorage.delete(collectionName:'posts', key: 'key');
243+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
177244
/// ```
178-
static Future<void> delete({required String key}) {
179-
return _instanceLocal.delete(key: key);
245+
static Future<void> delete({
246+
String collectionName = 'vaah-flutter-box',
247+
required String key,
248+
}) {
249+
return _instanceLocal.delete(collectionName: collectionName, key: key);
180250
}
181251

182-
/// Deletes item at a key present in [keys], from that [LocalStorage].
252+
/// Deletes item at a key present in [keys], from [collectionName] in [LocalStorage].
183253
///
184254
/// Example:
185255
/// ```dart
186-
/// await Storage.deleteMany(keys: [
256+
/// await LocalStorage.deleteMany(
257+
/// collectionName:'posts',
258+
/// keys: [
187259
/// 'key1',
188260
/// 'key2',
189261
/// //...
190262
/// ],
191263
/// );
264+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
192265
/// ```
193-
static Future<void> deleteMany({List<String> keys = const []}) {
194-
return _instanceLocal.deleteMany(keys: keys);
266+
static Future<void> deleteMany({
267+
String collectionName = 'vaah-flutter-box',
268+
List<String> keys = const [],
269+
}) {
270+
return _instanceLocal.deleteMany(collectionName: collectionName, keys: keys);
195271
}
196272

197-
/// Deletes all the values from that [LocalStorage]
198-
static Future<void> deleteAll() {
199-
return _instanceLocal.deleteAll();
273+
/// Deletes all the values from [collectionName] in [LocalStorage].
274+
///
275+
/// Example:
276+
/// ```dart
277+
/// await LocalStorage.deleteMany(
278+
/// collectionName:'posts',
279+
/// keys: [
280+
/// 'key1',
281+
/// 'key2',
282+
/// //...
283+
/// ],
284+
/// );
285+
/// // Do not provide the collectionName in case of Flutter Secure Storage.
286+
/// ```
287+
static Future<void> deleteAll({String collectionName = 'vaah-flutter-box'}) {
288+
return _instanceLocal.deleteAll(collectionName: collectionName);
200289
}
201290
}

0 commit comments

Comments
 (0)