Skip to content

Commit

Permalink
Updated: Local Storage
Browse files Browse the repository at this point in the history
Updated: made message nullable


Updated: response class


Updated: readability


Added: local storage extendable class


Updated: return type


Updated: flow of parameter passing
  • Loading branch information
we-mohd-i001 committed Jul 23, 2024
1 parent 259ad86 commit 196c193
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 61 deletions.
114 changes: 114 additions & 0 deletions lib/vaahextendflutter/services/storage/local/extendable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import '../storage_response.dart';
import 'storage.dart';

/// [LocalStorageExtendable] can be extended to have more class features other than the available
/// features.
///
/// Example:
///
/// ```dart
/// class ELS extends LocalStorageExtendable {
/// void myFunction(){
/// // logic
/// }
/// }
///
/// final ELS els = ELS();
///
/// void useCase(){
/// final response = await els.create(
/// collectionName: 'posts',
/// key: 'key',
/// value: 'value',
/// ); // existing method
///
/// final newResponse = await els.myFuction(); // added method
/// }
///
/// ```
class LocalStorageExtendable {
static const String defaultCollectionName = 'vaah-fluter-box';

StorageResponse add(String collectionName) {
return LocalStorage.add(collectionName);
}

Future<StorageResponse> create({
String collectionName = defaultCollectionName,
required String key,
required String value,
}) {
return LocalStorage.create(key: key, value: value);
}

Future<StorageResponse> createMany({
String collectionName = defaultCollectionName,
required Map<String, String> values,
}) {
return LocalStorage.createMany(values: values);
}

Future<StorageResponse> read({
String collectionName = defaultCollectionName,
required String key,
}) {
return LocalStorage.read(collectionName: collectionName, key: key);
}

Future<StorageResponse> readMany({
String collectionName = defaultCollectionName,
required List<String> keys,
}) {
return LocalStorage.readMany(keys: keys);
}

Future<StorageResponse> readAll({String collectionName = defaultCollectionName}) {
return LocalStorage.readAll(collectionName: collectionName);
}

Future<StorageResponse> update({
String collectionName = defaultCollectionName,
required String key,
required String value,
}) {
return LocalStorage.update(key: key, value: value);
}

Future<StorageResponse> updateMany({
String collectionName = defaultCollectionName,
required Map<String, String> values,
}) {
return LocalStorage.updateMany(values: values);
}

Future<StorageResponse> createOrUpdate({
String collectionName = defaultCollectionName,
required String key,
required String value,
}) {
return LocalStorage.createOrUpdate(key: key, value: value);
}

Future<StorageResponse> createOrUpdateMany({
String collectionName = defaultCollectionName,
required Map<String, String> values,
}) {
return LocalStorage.createOrUpdateMany(values: values);
}

Future<StorageResponse> delete(
{String collectionName = defaultCollectionName, required String key}) {
return LocalStorage.delete(collectionName: collectionName, key: key);
}

Future<StorageResponse> deleteMany({
String collectionName = defaultCollectionName,
List<String> keys = const [],
}) {
return LocalStorage.deleteMany(keys: keys);
}

Future<StorageResponse> deleteAll({String collectionName = defaultCollectionName}) {
return LocalStorage.deleteAll(collectionName: collectionName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,50 @@ abstract class LocalStorageService {
StorageResponse add(String collectionName);

Future<StorageResponse> create({
String collectionName,
required String collectionName,
required String key,
required String value,
});

Future<StorageResponse> createMany({String collectionName, required Map<String, String> values});
Future<StorageResponse> createMany({
required String collectionName,
required Map<String, String> values,
});

Future<StorageResponse> read({String collectionName, required String key});
Future<StorageResponse> read({required String collectionName, required String key});

Future<StorageResponse> readMany({String collectionName, required List<String> keys});
Future<StorageResponse> readMany({required String collectionName, required List<String> keys});

Future<StorageResponse> readAll({String collectionName});
Future<StorageResponse> readAll({required String collectionName});

Future<StorageResponse> update(
{String collectionName, required String key, required String value});
Future<StorageResponse> update({
required String collectionName,
required String key,
required String value,
});

Future<StorageResponse> updateMany({String collectionName, required Map<String, String> values});
Future<StorageResponse> updateMany({
required String collectionName,
required Map<String, String> values,
});

Future<StorageResponse> createOrUpdate({
String collectionName,
required String collectionName,
required String key,
required String value,
});

Future<StorageResponse> createOrUpdateMany({
String collectionName,
required String collectionName,
required Map<String, String> values,
});

Future<StorageResponse> delete({String collectionName, required String key});
Future<StorageResponse> delete({required String collectionName, required String key});

Future<StorageResponse> deleteMany({String collectionName, List<String> keys = const []});
Future<StorageResponse> deleteMany({
required String collectionName,
List<String> keys = const [],
});

Future<StorageResponse> deleteAll({String collectionName});
Future<StorageResponse> deleteAll({required String collectionName});
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ class LocalStorageWithFlutterSecureStorage implements LocalStorageService {
}
try {
await _storage.write(key: key, value: value);
return StorageResponse(
data: value,
message: 'Entry created with key: "$key"',
);
return StorageResponse(data: value, message: 'Entry created with key: "$key"');
} catch (e) {
return StorageResponse(
errors: [
StorageError(
message: 'Failed to create entry at key: "$key".',
message: 'Failed to create entry at key: "$key": $e',
failedKey: key,
stackTrace: StackTrace.current,
),
Expand Down Expand Up @@ -258,9 +255,8 @@ class LocalStorageWithFlutterSecureStorage implements LocalStorageService {
if (result.hasData) {
remainigEntries--;
success.add(result.data);
} else if (result.hasError) {
errors.add(result.errors!.first);
}
errors.add(result.errors!.first);
}
if (errors.isEmpty) {
return StorageResponse(data: success, message: 'Set all given entries.');
Expand Down Expand Up @@ -289,7 +285,7 @@ class LocalStorageWithFlutterSecureStorage implements LocalStorageService {
}
try {
await _storage.delete(key: key);
return StorageResponse(data: key, message: 'Deleted value at key: $key');
return StorageResponse(message: 'Deleted value at key: $key', isSuccess: true);
} catch (e) {
final error = StorageError(
failedKey: key,
Expand All @@ -308,28 +304,25 @@ class LocalStorageWithFlutterSecureStorage implements LocalStorageService {
if (keys.isNotEmpty) {
int remainingKeys = keys.length;
List<StorageError> errors = [];
List<String> success = [];
for (String k in keys) {
final result = await delete(collectionName: collectionName, key: k);
if (result.hasData) {
success.add(result.data);
remainingKeys--;
} else if (result.hasError) {
errors.add(result.errors!.first);
}
errors.add(result.errors!.first);
}
if (errors.isEmpty) {
return StorageResponse(
data: success,
message: 'Deleted multiple entries with associated keys: $keys',
isSuccess: true,
);
} else if (remainingKeys == keys.length) {
return StorageResponse(errors: errors);
}
return StorageResponse(
data: success,
message: 'Deleted ${keys.length - remainingKeys}/${keys.length}',
errors: errors,
isSuccess: true,
);
}
return StorageResponse(
Expand All @@ -347,11 +340,11 @@ class LocalStorageWithFlutterSecureStorage implements LocalStorageService {
Future<StorageResponse> deleteAll({String collectionName = ''}) async {
try {
await _storage.deleteAll();
return const StorageResponse(data: null, message: 'Deleted all entries.');
return const StorageResponse(message: 'Deleted all entries.', isSuccess: true);
} catch (e) {
return StorageResponse(errors: [
StorageError(
message: e.toString(),
message: 'Delete failed: ${e.toString()}',
failedKey: '',
stackTrace: StackTrace.current,
)
Expand Down
Loading

0 comments on commit 196c193

Please sign in to comment.