Skip to content

Commit 735d09e

Browse files
committed
Updated: made the Storage class singleton
1 parent 6c4b0ee commit 735d09e

File tree

6 files changed

+282
-235
lines changed

6 files changed

+282
-235
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
abstract class LocalStorageService {
2+
Future<void> init();
3+
4+
Future<void> create({required String key, required String value});
5+
6+
Future<void> createMany({required Map<String, String> values});
7+
8+
Future<String?> read({required String key});
9+
10+
Future<Map<String, String?>> readMany({required List<String> keys});
11+
12+
Future<Map<String, String?>> readAll();
13+
14+
Future<void> update({required String key, required String value});
15+
16+
Future<void> updateMany({required Map<String, String> values});
17+
18+
Future<void> createOrUpdate({required String key, required String value});
19+
20+
Future<void> createOrUpdateMany({required Map<String, String> values});
21+
22+
Future<void> delete({required String key});
23+
24+
Future<void> deleteMany({List<String> keys = const []});
25+
26+
Future<void> deleteAll();
27+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
22

3-
import '../../storage.dart';
3+
import 'base_service.dart';
44

5-
class FlutterSecureStorageImpl implements Storage {
5+
class FlutterSecureStorageImpl implements LocalStorageService {
66
final _storage = const FlutterSecureStorage();
77

88
@override

lib/vaahextendflutter/services/storage/local/hive/hive_storage_impl.dart renamed to lib/vaahextendflutter/services/storage/local/services/hive_storage_impl.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import 'package:hive/hive.dart';
22

3-
import '../../storage.dart';
3+
import '../storage.dart';
4+
import 'base_service.dart';
45

56
/// A class implementing Storage interface using Hive as storage backend.
6-
class HiveStorageImpl implements Storage {
7+
class HiveStorageImpl implements LocalStorageService {
78
final String name;
89

910
Box? _box;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'base_service.dart';
2+
3+
/// A placeholder storage class when [LocalStorageType.none] is selected in env.dart.
4+
class NoOpStorage implements LocalStorageService {
5+
@override
6+
Future<void> init() async {}
7+
8+
@override
9+
Future<void> create({required String key, required String value}) async {}
10+
11+
@override
12+
Future<void> createMany({required Map<String, String> values}) async {}
13+
14+
@override
15+
Future<String?> read({required String key}) async {
16+
return null;
17+
}
18+
19+
@override
20+
Future<Map<String, String?>> readMany({required List<String> keys}) async {
21+
return {};
22+
}
23+
24+
@override
25+
Future<Map<String, String?>> readAll() async {
26+
return {};
27+
}
28+
29+
@override
30+
Future<void> update({required String key, required String value}) async {}
31+
32+
@override
33+
Future<void> updateMany({required Map<String, String> values}) async {}
34+
35+
@override
36+
Future<void> createOrUpdate({required String key, required String value}) async {}
37+
38+
@override
39+
Future<void> createOrUpdateMany({required Map<String, String> values}) async {}
40+
41+
@override
42+
Future<void> delete({required String key}) async {}
43+
44+
@override
45+
Future<void> deleteMany({List<String> keys = const []}) async {}
46+
47+
@override
48+
Future<void> deleteAll() async {}
49+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import '../../../env.dart';
2+
import 'services/base_service.dart';
3+
import 'services/flutter_secure_storage_impl.dart';
4+
import 'services/hive_storage_impl.dart';
5+
import 'services/no_op_storage.dart';
6+
7+
LocalStorageService get instanceLocal {
8+
final EnvironmentConfig envConfig = EnvironmentConfig.getEnvConfig();
9+
switch (envConfig.localStorageType) {
10+
case LocalStorageType.hive:
11+
return HiveStorageImpl();
12+
case LocalStorageType.flutterSecureStorage:
13+
return FlutterSecureStorageImpl();
14+
default:
15+
return NoOpStorage();
16+
}
17+
}
18+
19+
abstract class LocalStorage {
20+
static final LocalStorageService _instanceLocal = instanceLocal;
21+
22+
/// Initializes the [LocalStorage].
23+
/// In the case of [HiveStorageImpl], 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.
26+
/// It's not required in the case of [FlutterSecureStorageImpl].
27+
///
28+
/// Example:
29+
/// ```dart
30+
/// Storage.init();
31+
/// ```
32+
static Future<void> init() {
33+
return _instanceLocal.init();
34+
}
35+
36+
/// Creates a new item in the [LocalStorage].
37+
///
38+
/// To create a single key-value pair pass the [key] and the [value] as String, the
39+
/// 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.
41+
///
42+
/// Example:
43+
/// ```dart
44+
/// await Storage.create(key: 'key', value: 'value');
45+
/// ```
46+
static Future<void> create(String name, {required String key, required String value}) {
47+
return _instanceLocal.create(key: key, value: value);
48+
}
49+
50+
/// Creates new items in the [LocalStorage].
51+
/// If you want to create multiple entries pass the [values] as a Map<String, String>, then it
52+
/// 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.
55+
///
56+
/// Example:
57+
/// ```dart
58+
/// await Storage.createMany(values: {
59+
/// 'key1': 'Value1',
60+
/// 'key2': 'Value2',
61+
/// 'key3': 'Value3',
62+
/// 'key4': 'Value4',
63+
/// 'key5': 'Value5',
64+
/// //...
65+
/// },
66+
/// );
67+
/// ```
68+
static Future<void> createMany({required Map<String, String> values}) {
69+
return _instanceLocal.createMany(values: values);
70+
}
71+
72+
/// Reads the value of the item at [key] from the [LocalStorage] and returns the value.
73+
///
74+
/// Read a single value by passing [key] as String, it will return the value as String?.
75+
///
76+
/// Example:
77+
/// ```dart
78+
/// await Storage.read(key: 'key');
79+
/// ```
80+
static Future<String?> read({required String key}) {
81+
return _instanceLocal.read(key: key);
82+
}
83+
84+
/// Reads multiple values, pass the List of [keys] as argument. It will return the value as
85+
/// Map<String, String?>.
86+
///
87+
/// Example:
88+
/// ```dart
89+
/// await Storage.readMany(keys: [
90+
/// 'key1',
91+
/// 'key2',
92+
/// //...
93+
/// ],
94+
/// );
95+
/// ```
96+
static Future<Map<String, String?>> readMany({required List<String> keys}) {
97+
return _instanceLocal.readMany(keys: keys);
98+
}
99+
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();
103+
}
104+
105+
/// Updates an item in the [LocalStorage].
106+
///
107+
/// To update a single key-value pair pass the [key] and the [value] as String, the
108+
///
109+
/// Example:
110+
/// ```dart
111+
/// await Storage.update(key: 'key', value: 'value');
112+
/// ```
113+
static Future<void> update({required String key, required String value}) {
114+
return _instanceLocal.update(key: key, value: value);
115+
}
116+
117+
/// Updates items in the [LocalStorage].
118+
/// If you want to update multiple entries pass the [values] as a Map<String, String>, then it
119+
/// will update all the key-value pairs in the [values] map.
120+
///
121+
/// Example:
122+
/// ```dart
123+
/// await Storage.updateMany(values: {
124+
/// 'key1': 'Value1',
125+
/// 'key2': 'Value2',
126+
/// 'key3': 'Value3',
127+
/// 'key4': 'Value4',
128+
/// 'key5': 'Value5',
129+
/// //...
130+
/// },
131+
/// );
132+
/// ```
133+
static Future<void> updateMany({required Map<String, String> values}) {
134+
return _instanceLocal.updateMany(values: values);
135+
}
136+
137+
/// Creates or updates an item in the [LocalStorage].
138+
///
139+
/// To create or update a single key-value pair pass the [key] and the [value] as String, the
140+
/// If the [key] is already present in the [LocalStorage] it's value will be overwritten.
141+
///
142+
/// Example:
143+
/// ```dart
144+
/// await Storage.createOrUpdate(key: 'key', value: 'value');
145+
/// ```
146+
static Future<void> createOrUpdate({required String key, required String value}) {
147+
return _instanceLocal.createOrUpdate(key: key, value: value);
148+
}
149+
150+
/// Creates or updates items in the [LocalStorage].
151+
/// If you want to create or update multiple entries pass the [values] as a Map<String, String>, then it
152+
/// will create all the key-value pairs from the [values] map.
153+
/// If any key from the [values] is already present in the [LocalStorage] it's value will be
154+
/// overwritten.
155+
///
156+
/// Example:
157+
/// ```dart
158+
/// await Storage.createOrUpdateMany(values: {
159+
/// 'key1': 'Value1',
160+
/// 'key2': 'Value2',
161+
/// 'key3': 'Value3',
162+
/// 'key4': 'Value4',
163+
/// 'key5': 'Value5',
164+
/// //...
165+
/// },
166+
/// );
167+
/// ```
168+
static Future<void> createOrUpdateMany({required Map<String, String> values}) {
169+
return _instanceLocal.createOrUpdateMany(values: values);
170+
}
171+
172+
/// Deletes an item at [key].
173+
///
174+
/// Example:
175+
/// ```dart
176+
/// await Storage.delete(key: 'key');
177+
/// ```
178+
static Future<void> delete({required String key}) {
179+
return _instanceLocal.delete(key: key);
180+
}
181+
182+
/// Deletes item at a key present in [keys], from that [LocalStorage].
183+
///
184+
/// Example:
185+
/// ```dart
186+
/// await Storage.deleteMany(keys: [
187+
/// 'key1',
188+
/// 'key2',
189+
/// //...
190+
/// ],
191+
/// );
192+
/// ```
193+
static Future<void> deleteMany({List<String> keys = const []}) {
194+
return _instanceLocal.deleteMany(keys: keys);
195+
}
196+
197+
/// Deletes all the values from that [LocalStorage]
198+
static Future<void> deleteAll() {
199+
return _instanceLocal.deleteAll();
200+
}
201+
}

0 commit comments

Comments
 (0)