Skip to content

Feature -> Develop | Added Local Storage option #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/vaahextendflutter/base/base_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '../env/env.dart';
import '../services/api.dart';
import '../services/notification/internal/notification.dart';
import '../services/notification/push/notification.dart';
import '../services/storage/local/storage.dart';
import 'root_assets_controller.dart';

class BaseController extends GetxController {
Expand Down Expand Up @@ -47,6 +48,8 @@ class BaseController extends GetxController {
await InternalNotifications.init();
PushNotifications.askPermission();

await LocalDatabaseStorage.init();

// Sentry Initialization (And/ Or) Running main app
if (null != config.sentryConfig && config.sentryConfig!.dsn.isNotEmpty) {
await SentryFlutter.init(
Expand Down
4 changes: 4 additions & 0 deletions lib/vaahextendflutter/env/env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:json_annotation/json_annotation.dart';
import '../services/logging_library/logging_library.dart';
import 'logging.dart';
import 'notification.dart';
import 'storage.dart';

part 'env.g.dart';

Expand Down Expand Up @@ -62,6 +63,7 @@ class EnvironmentConfig {
this.oneSignalConfig,
this.pusherConfig,
required this.showDebugPanel,
required this.localDatabaseStorageType,
required this.debugPanelColor,
});

Expand All @@ -82,6 +84,7 @@ class EnvironmentConfig {
final OneSignalConfig? oneSignalConfig;
final PusherConfig? pusherConfig;
final bool showDebugPanel;
final LocalDatabaseStorageType localDatabaseStorageType;
@JsonKey(fromJson: _colorFromJson, toJson: _colorToJson)
final Color debugPanelColor;

Expand Down Expand Up @@ -123,6 +126,7 @@ class EnvironmentConfig {
pushNotificationsServiceType: PushNotificationsServiceType.none,
internalNotificationsServiceType: InternalNotificationsServiceType.none,
showDebugPanel: true,
localDatabaseStorageType: LocalDatabaseStorageType.none,
debugPanelColor: Colors.black.withOpacity(0.8),
);
}
Expand Down
9 changes: 9 additions & 0 deletions lib/vaahextendflutter/env/env.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/vaahextendflutter/env/storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum LocalDatabaseStorageType { hive, none }
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
abstract class LocalStorageService {
static Future<LocalStorageService> init() async {
throw UnimplementedError();
}

Future<void> addCollection({
required String collectionName,
});

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

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

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

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

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

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

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

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

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

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

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

Future<void> deleteAll({required String collectionName});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
import 'dart:io';

import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';

import 'base_storage.dart';

/// A class implementing LocalStorageService interface using Hive as storage backend.
class LocalHiveStorage implements LocalStorageService {
static Future<LocalHiveStorage> init() async {
final Directory docsDirectory = await getApplicationDocumentsDirectory();
final String storagePath = '${docsDirectory.path}/vaahflutter';
final Directory storageDirectory = await Directory(storagePath).create(
recursive: true,
);

Hive.init(storageDirectory.path);
return LocalHiveStorage();
}

final Map<String, Future<Box>> _collections = {
'vaah-flutter-box': Hive.openBox('vaah-flutter-box'),
};

@override
Future<void> addCollection({
required String collectionName,
}) async {
if (!_collections.containsKey(collectionName)) {
_collections[collectionName] = Hive.openBox(collectionName);
}
}

@override
Future<void> create({
required String collectionName,
required String key,
required String value,
}) async {
if (!_collections.containsKey(collectionName)) {
return;
}
final Box box = await _collections[collectionName]!;
if (box.containsKey(key)) {}

await box.put(key, value);
}

@override
Future<void> createMany({
required String collectionName,
required Map<String, String> values,
}) async {
final List<String> errors = [];
final List<String> success = [];
for (final String key in values.keys) {
try {
await create(
collectionName: collectionName,
key: key,
value: values[key]!,
);
success.add(key);
} catch (e, st) {
errors.add(key);
}
}
return;
}

@override
Future<String?> read({
required String collectionName,
required String key,
}) async {
if (!_collections.containsKey(collectionName)) {
throw 'no collection data found';
}
final Box box = await _collections[collectionName]!;
if (!box.containsKey(key)) {
throw 'no data found';
}

return box.get(key);
}

@override
Future<Map<String, String>> readMany({
required String collectionName,
required List<String> keys,
}) async {
final Map<String, String?> success = {};
final List<String> errors = [];
for (final String key in keys) {
try {
final String? result = await read(collectionName: collectionName, key: key);
if (result != null) {
success[key] = result;
} else {
errors.add(key);
}
} catch (e, st) {
errors.add(key);
}
}
return {};
}

@override
Future<Map<String, String>> readAll({required String collectionName}) async {
if (!_collections.containsKey(collectionName)) {
throw 'no collection found';
}
final Box box = await _collections[collectionName]!;
return box.toMap().map(
(key, value) {
return MapEntry(
key.toString(),
value.toString(),
);
},
);
}

@override
Future<void> update({
required String collectionName,
required String key,
required String value,
}) async {
if (!_collections.containsKey(collectionName)) {
throw 'no collection found';
}
final Box box = await _collections[collectionName]!;
if (!box.containsKey(key)) {
throw 'no key found';
}
return await box.put(key, value);
}

@override
Future<void> updateMany({
required String collectionName,
required Map<String, String> values,
}) async {
final List<String> errors = [];
final List<String> success = [];
for (final String key in values.keys) {
try {
await update(
collectionName: collectionName,
key: key,
value: values[key]!,
);
success.add(key);
} catch (e, st) {
errors.add(key);
}
}
}

@override
Future<void> createOrUpdate({
required String collectionName,
required String key,
required String value,
}) async {
if (!_collections.containsKey(collectionName)) {
throw 'no collection found';
}
final Box box = await _collections[collectionName]!;
if (!box.containsKey(key)) {
return create(collectionName: collectionName, key: key, value: value);
}
return update(collectionName: collectionName, key: key, value: value);
}

@override
Future<void> createOrUpdateMany({
required String collectionName,
required Map<String, String> values,
}) async {
final List<String> errors = [];
final List<String> success = [];
for (final String key in values.keys) {
try {
await createOrUpdate(
collectionName: collectionName,
key: key,
value: values[key]!,
);
success.add(key);
} catch (e, st) {
errors.add(key);
}
}
}

@override
Future<void> delete({required String collectionName, dynamic key}) async {
if (!_collections.containsKey(collectionName)) {
throw 'no collection found';
}
final Box box = await _collections[collectionName]!;
if (!box.containsKey(key)) {
throw 'no key found';
}
await box.delete(key);
}

@override
Future<void> deleteMany({
required String collectionName,
List<String> keys = const [],
}) async {
final List<String> errors = [];
if (!_collections.containsKey(collectionName)) {
throw 'no collection found';
}
final List<String> nonExistingKeys = [];
final List<String> existingKeys = [];
final Box box = await _collections[collectionName]!;
for (int i = 0; i < keys.length; i++) {
if (!box.containsKey(keys[i])) {
errors.add(keys[i]);
nonExistingKeys.add(keys[i]);
} else {
existingKeys.add(keys[i]);
}
}
if (nonExistingKeys.isEmpty) {
await box.deleteAll(keys);
} else {
await box.deleteAll(existingKeys);
// Notify about errors
}
}

@override
Future<void> deleteAll({required String collectionName}) async {
if (!_collections.containsKey(collectionName)) {
throw 'no collection found';
}
final Box box = await _collections[collectionName]!;
await box.clear();
}
}
Loading