Skip to content

Commit

Permalink
-added localdatabase.
Browse files Browse the repository at this point in the history
  • Loading branch information
FarhanRiaaz committed Jun 22, 2022
1 parent 1194c4b commit 9350711
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 11 deletions.
17 changes: 7 additions & 10 deletions lib/data/local/constants.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
class DBConstants {
DBConstants._();
// Store Name
static const String STORE_NAME = 'homebox';
static const String USER_INFO_STORE = 'userInfo';
static const String EVENT_HISTORY = 'eventHistory';
static const String FIXEDCODE_STORE = 'fixcode';
static const String NOTIFICATION_STORE = 'notification';
static const String STATUS_STORE = 'status';
static const String ACCESS_STORE = 'access';
static const String HISTORY_STORE = 'history';
static const String LOGS_STORE = 'logs';


// DB Name
static const DB_NAME = 'homebox.db';
static const DB_NAME = 'technologies.db';

// Fields
static const FIELD_ID = 'id';
}
}

// local db
//view model and provider stuff
// UI and widgets and designs
39 changes: 39 additions & 0 deletions lib/di/modules/local_module.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'dart:async';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sembast/sembast.dart';
import 'package:sembast/sembast_io.dart';
import 'package:technologies_login_example/data/local/constants.dart';
import 'package:technologies_login_example/utils/encryption/xxtea.dart';

import 'netwok_module.dart';

abstract class LocalModule extends NetworkModule {
///
/// Calling it multiple times will return the same instance.
static Future<Database> provideDatabase() async {
// Key for encryption
var encryptionKey = "";

// Get a platform-specific directory where persistent app data can be stored
final appDocumentDir = await getApplicationDocumentsDirectory();

// Path with the form: /platform-specific-directory/demo.db
final dbPath = join(appDocumentDir.path, DBConstants.DB_NAME);

// Check to see if encryption is set, then provide codec
// else init normal db with path
Database database;
if (encryptionKey.isNotEmpty) {
// Initialize the encryption codec with a user password
var codec = getXXTeaCodec(password: encryptionKey);
database = await databaseFactoryIo.openDatabase(dbPath, codec: codec);
} else {
database = await databaseFactoryIo.openDatabase(dbPath);
}

// Return database instance
return database;
}
}
63 changes: 63 additions & 0 deletions lib/utils/encryption/xxtea.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:convert';

import 'package:sembast/sembast.dart';
import 'package:xxtea/xxtea.dart';

class _XXTeaEncoder extends Converter<Map<String, dynamic>, String> {
final String key;

_XXTeaEncoder(this.key);

@override
String convert(Map<String, dynamic> input) =>
xxtea.encryptToString(json.encode(input), key)!;
}

class _XXTeaDecoder extends Converter<String, Map<String, dynamic>> {
final String key;

_XXTeaDecoder(this.key);

@override
Map<String, dynamic> convert(String input) {
var result = json.decode(xxtea.decryptToString(input, key)!);
if (result is Map) {
return result.cast<String, dynamic>();
}
throw FormatException('invalid input $input');
}
}

/// Simple encryption codec using xxtea
/// It requires a password to encrypt/decrypt the data
class _XXTeaCodec extends Codec<Map<String, dynamic>, String> {
late _XXTeaEncoder _encoder;
late _XXTeaDecoder _decoder;

/// A non null [password] to use for the encryption/decryption
_XXTeaCodec(String password) {
_encoder = _XXTeaEncoder(password);
_decoder = _XXTeaDecoder(password);
}

@override
Converter<String, Map<String, dynamic>> get decoder => _decoder;

@override
Converter<Map<String, dynamic>, String> get encoder => _encoder;
}

/// Create a codec to use when opening an encrypted sembast database
///
/// The usage is then
///
/// ```dart
/// // Initialize the encryption codec with a user password
/// var codec = getXXTeaCodec(password: '[your_user_password]');
/// // Open the database with the codec
/// Database db = await factory.openDatabase(dbPath, codec: codec);
///
/// // ...your database is ready to use as encrypted
/// ```
SembastCodec getXXTeaCodec({required String password}) =>
SembastCodec(signature: 'xxtea', codec: _XXTeaCodec(password));
37 changes: 36 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,41 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
path_provider:
dependency: "direct main"
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
path_provider_android:
dependency: transitive
description:
name: path_provider_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.14"
path_provider_ios:
dependency: transitive
description:
name: path_provider_ios
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.7"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
path_provider_platform_interface:
dependency: transitive
description:
Expand Down Expand Up @@ -497,6 +525,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0+1"
xxtea:
dependency: "direct main"
description:
name: xxtea
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
yaml:
dependency: transitive
description:
Expand All @@ -506,4 +541,4 @@ packages:
version: "3.1.1"
sdks:
dart: ">=2.16.2 <3.0.0"
flutter: ">=2.8.0"
flutter: ">=2.8.1"
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ dependencies:
# Database
sembast: ^3.2.0+1

# Path Provider
path_provider: ^2.0.11
# Encryption
xxtea: ^2.1.0
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 9350711

Please sign in to comment.