fix : change app architecture

feat : add some method to local storage
This commit is contained in:
2025-05-14 09:42:44 +03:30
parent e11ef1990c
commit feb3df6a06
50 changed files with 1007 additions and 70 deletions

View File

@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';
import 'i_local_storage.dart';
@@ -13,39 +14,40 @@ class HiveLocalStorage implements ILocalStorage {
Future init() async => await Hive.initFlutter();
@override
Future<void> openBox<T>(String boxName) async {
Future<void> openBox<T>(
String boxName, {
HiveCipher? encryptionCipher,
bool crashRecovery = true,
String? path,
Uint8List? bytes,
String? collection,
}) async {
if (!_boxes.containsKey(boxName)) {
final box = await Hive.openBox<T>(boxName);
final box = await Hive.openBox<T>(
boxName,
encryptionCipher: encryptionCipher,
crashRecovery: crashRecovery,
);
_boxes[boxName] = box;
}
}
@override
Future<void> delete(String boxName, String key) async {
Box<dynamic>? box = await getBox(boxName);
await box.delete(key);
}
@override
Future<T?> read<T>(String boxName, String key) async {
Future<T?> read<T>({required String boxName,required String key}) async {
Box? box = await getBox(boxName);
return box.get(key) as T?;
}
@override
Future<void> save(String boxName, String key, value) async {
Box<dynamic>? box = await getBox(boxName);
await box.put(key, value);
}
@override
Future<void> add(String boxName, value) async {
Future<void> add({required String boxName,required dynamic value}) async {
Box<dynamic>? box = await getBox(boxName);
await box.add(value);
}
@override
Future<void> addAll(String boxName, Iterable values) async {
Future<void> addAll({required String boxName,required Iterable values}) async {
Box<dynamic>? box = await getBox(boxName);
await box.addAll(values);
}
@@ -58,4 +60,47 @@ class HiveLocalStorage implements ILocalStorage {
throw Exception('Box $boxName is not of expected type $T');
}
}
@override
Future<void> clear(String boxName) async{
await _boxes[boxName]?.clear();
}
@override
Future<void> close(String boxName) async => await _boxes[boxName]?.close();
@override
Future<void> deleteValue({
required String boxName,
required String key,
}) async {
Box<dynamic>? box = await getBox(boxName);
await box.delete(key);
}
@override
Future<void> save({
required String boxName,
required String key,
required value,
}) async{
Box<dynamic>? box = await getBox(boxName);
await box.put(key, value);
}
@override
Future<void> saveAll({required String boxName, required Map entries}) async{
Box<dynamic>? box = await getBox(boxName);
await box.putAll(entries);
}
@override
Future<void> saveAt({
required String boxName,
required int index,
required value,
}) async{
Box<dynamic>? box = await getBox(boxName);
await box.putAt(index, value);
}
}

View File

@@ -1,11 +1,44 @@
abstract class ILocalStorage<E>{
import 'package:flutter/foundation.dart';
import 'package:hive_ce/hive.dart';
abstract class ILocalStorage<E> {
Future<void> init();
Future<void> openBox<T>(String boxName);
Future<void> save(String boxName, String key, dynamic value);
Future<T?> read<T>(String boxName, String key);
Future<void> delete(String boxName, String key);
Future<void> add(String boxName, E value);
Future<void> addAll(String boxName, Iterable<E> values);
Future<void> openBox<T>(
String boxName, {
HiveCipher? encryptionCipher,
bool crashRecovery = true,
String? path,
Uint8List? bytes,
String? collection,
});
}
Future<T?> read<T>({required String boxName, required String key});
Future<void> deleteValue({required String boxName, required String key});
Future<void> add({required String boxName, required E value});
Future<void> addAll({required String boxName, required Iterable<E> values});
Future<void> clear(String boxName);
Future<void> close(String boxName);
Future<void> save({
required String boxName,
required String key,
required dynamic value,
});
Future<void> saveAt({
required String boxName,
required int index,
required dynamic value,
});
Future<void> saveAll({
required String boxName,
required Map<dynamic, E> entries,
});
}