Skip to content
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

Optimization #6

Merged
merged 5 commits into from
Apr 18, 2024
Merged
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
25 changes: 1 addition & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ preferences.listen().listen((event) {
});

//listen only specific key
preferences.listen(key: 'counter').listen((event) {
preferences.stream(key: 'counter').listen((event) {
print("key $event updated");
});

Expand All @@ -117,26 +117,3 @@ preferences.listen(key: 'counter').listen((event) {
// Remove data for the 'counter' key.
final success = await preferences.remove('counter');
```


### Temporary mode


`Temporary mode is when data is stored in a Map data structure until you manually synchronize it, which helps you avoid excessive memory writes. Use this when you are unsure if you need the data or want to filter it before saving.`

```dart
preferences.startTemporaryMode();

await preferences.setString('key1', 'value1');
await preferences.setString('key2', 'value2');
await preferences.setString('key3', 'value3');
await preferences.setString('key4', 'value4');
await preferences.setString('key5', 'value5');
await preferences.setString('key6', 'value6');

await preferences.stopTemporaryMode();
final bool = preferences.isTemporaryModeEnabled;

await preferences.sync();

```
12 changes: 2 additions & 10 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ void main(List<String> args) async {
final preferences = JtPreferences.getInstance();

//listen only one key
preferences.listen(key: 'averageName').listen((event) {
preferences.stream(key: 'averageName').listen((event) {
print("key $event updated");
});

//listen all changes
preferences.listen().listen((event) {
preferences.stream().listen((event) {
print("key $event updated");
});

Expand All @@ -34,14 +34,6 @@ void main(List<String> args) async {
print(object?.name);
print(object?.age);

preferences.startTemporaryMode();

preferences.setString('key', 'value');
preferences.setString('key1', 'value1');

preferences.stopTemporaryMode();
final bool = preferences.isTemporaryModeEnabled;


preferences.sync();
}
1 change: 0 additions & 1 deletion jt_pref/jtpreferences.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions jt_pref/jtpreferences_enc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3mKfcr5wDWaf++Yk0HRLog==
9 changes: 0 additions & 9 deletions lib/src/core/mode_manager.dart

This file was deleted.

27 changes: 11 additions & 16 deletions lib/src/core/preferences.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import 'package:jt_preferences/src/core/mode_manager.dart';

import 'writable.dart';

abstract class Preferences implements Set, Get, Clear, Listener, ModeManager {
//
}
abstract class Preferences implements Set, Get, Clear, StreamListener {}

abstract class Set {
Future<bool> setString(String key, String? value);
Expand All @@ -25,24 +21,23 @@ abstract class Clear {
}

abstract class Get {
Future<String?> getString(String key);
String? getString(String key);

Future<int?> getInt(String key);
int? getInt(String key);

Future<double?> getDouble(String key);
double? getDouble(String key);

Future<bool?> getBoolean(String key);
bool? getBoolean(String key);

Future<bool> contains(String key);
bool contains(String key);

Future<Map<String, dynamic>> getAll();
Map<String, dynamic> getAll();

Future<List<String>> getKeys();
List<String> getKeys();

Future<T?> getObject<T>(
String key, T Function(Map<String, dynamic> map) parse);
T? getObject<T>(String key, T Function(Map<String, dynamic> map) parse);
}

abstract class Listener<T> {
Stream<String> listen({String? key});
abstract class StreamListener<T> {
Stream<String> stream({String? key});
}
2 changes: 1 addition & 1 deletion lib/src/di/lib_di.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void initDependencies(String path, {String? encryptionKey}) {
injector.registerSingleton<Mapper>(DataMapper());

if (encryptionKey != null) {
injector.registerSingleton<Encryption>(
injector.registerSingleton<IEncryptor>(
AesEncryptionImpl(encryptionKey: encryptionKey));

injector.registerSingleton<DirectoryProvider>(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/encryption/aes_encryption.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:encrypt/encrypt.dart';

import 'encryption.dart';

abstract class AesEncryption implements Encryption {}
abstract class AesEncryption implements IEncryptor {}

class AesEncryptionImpl implements AesEncryption {
final String _encryptionKey;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/encryption/encryption.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'decrypt.dart';
import 'encrypt.dart';

abstract class Encryption implements Encrypt, Decrypt {
abstract class IEncryptor implements Encrypt, Decrypt {
String get key;
}
4 changes: 2 additions & 2 deletions lib/src/file/dir/encrypted_directory_provider_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:jt_preferences/src/encryption/encryption.dart';
import 'package:jt_preferences/src/file/dir/directory_provider.dart';

class EncryptedDirectoryProviderImpl extends DirectoryProvider {
final Encryption _encryptor;
final IEncryptor _encryptor;

@override
Future<String> getFilesDir(String rootPath) async {
Expand All @@ -23,6 +23,6 @@ class EncryptedDirectoryProviderImpl extends DirectoryProvider {
}

EncryptedDirectoryProviderImpl({
required Encryption encryptor,
required IEncryptor encryptor,
}) : _encryptor = encryptor;
}
51 changes: 8 additions & 43 deletions lib/src/file/encrypted_file_manager_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,35 @@ import 'dir/directory_provider.dart';

class EncryptedFileManager extends FileManager {
final DirectoryProvider _directoryProvider;
final Encryption _encryptor;
final IEncryptor _encryptor;
final Mapper _mapper;
final String _rootPath;
bool _saveTemporary = false;
final Map<String, dynamic> _temp = {};

@override
Future<Map<String, dynamic>> read() async {
if (_saveTemporary) {
return _temp;
} else {
final file = File(await _directoryProvider.getFilesDir(_rootPath));
final data = await file.readAsString();
final decrypted = _encryptor.decrypt(Encrypted.from64(data));
final map = _mapper.decode(decrypted);
return map;
}
final file = File(await _directoryProvider.getFilesDir(_rootPath));
final data = await file.readAsString();
final decrypted = _encryptor.decrypt(Encrypted.from64(data));
final map = _mapper.decode(decrypted);
return map;
}

@override
Future<bool> write(Map<String, dynamic> data) async {
if (_saveTemporary) {
_temp.clear();
_temp.addAll(data);
return true;
}
await save(data);
return true;
}

@override
Future<bool> save(Map<String, dynamic> map) async {
final path = await _directoryProvider.getFilesDir(_rootPath);
final file = File(path);
final encrypted = _encryptor.encrypt(_mapper.encode(map));
final encrypted = _encryptor.encrypt(_mapper.encode(data));
await file.writeAsString(encrypted.base64, mode: FileMode.write);
return true;
}

EncryptedFileManager({
required DirectoryProvider directoryProvider,
required Encryption encryptor,
required IEncryptor encryptor,
required Mapper mapper,
required String rootPath,
}) : _directoryProvider = directoryProvider,
_encryptor = encryptor,
_mapper = mapper,
_rootPath = rootPath;

@override
void startTemporaryMode() {
_saveTemporary = true;
}

@override
void stopTemporaryMode() {
_saveTemporary = false;
}

@override
Future<bool> sync() async {
return await save(_temp);
}

@override
bool get isTemporaryModeEnabled => _saveTemporary;
}
8 changes: 1 addition & 7 deletions lib/src/file/file_manager.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import 'package:jt_preferences/src/core/mode_manager.dart';

import '../core/read.dart';
import '../core/write.dart';

abstract class FileManager
implements
Read<Map<String, dynamic>>,
Write<Map<String, dynamic>>,
ModeManager {
Future<bool> save(Map<String, dynamic> map);
implements Read<Map<String, dynamic>>, Write<Map<String, dynamic>> {
}
57 changes: 8 additions & 49 deletions lib/src/file/file_manager_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,21 @@ class FileManagerImpl extends FileManager {
final DirectoryProvider _directoryProvider;
final Mapper _mapper;
final String _rootPath;
bool _saveTemporary = false;
Map<String, dynamic> _temp = {};

@override
Future<Map<String, dynamic>> read() async {
if (_saveTemporary) {
return _temp;
} else {
final file = File(await _directoryProvider.getFilesDir(_rootPath));
final json = await file.readAsString();
final map = _mapper.decode(json);
return map;
}
final file = File(await _directoryProvider.getFilesDir(_rootPath));
final json = await file.readAsString();
final map = _mapper.decode(json);
return map;
}

@override
Future<bool> write(Map<String, dynamic> data) async {
if (_saveTemporary) {
_temp = data;
return true;
} else {
await save(data);
return true;
}
final path = await _directoryProvider.getFilesDir(_rootPath);
final file = File(path);
await file.writeAsString(_mapper.encode(data), mode: FileMode.write);
return true;
}

FileManagerImpl({
Expand All @@ -42,36 +33,4 @@ class FileManagerImpl extends FileManager {
_mapper = mapper,
_rootPath = rootPath;

@override
void startTemporaryMode() {
_saveTemporary = true;
}

@override
void stopTemporaryMode() {
_saveTemporary = false;
}

@override
Future<bool> sync() async {
_saveTemporary = false;
final cache = await read();
cache.addAll(_temp);
final res = await save(cache);
if (res) {
_temp.clear();
}
return true;
}

@override
Future<bool> save(Map<String, dynamic> map) async {
final path = await _directoryProvider.getFilesDir(_rootPath);
final file = File(path);
await file.writeAsString(_mapper.encode(map), mode: FileMode.write);
return true;
}

@override
bool get isTemporaryModeEnabled => _saveTemporary;
}
Loading
Loading