|
| 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