feat : refresh login
test: core
This commit is contained in:
112
packages/core/test/infrastructure/local/hive_local_storage.dart
Normal file
112
packages/core/test/infrastructure/local/hive_local_storage.dart
Normal file
@@ -0,0 +1,112 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
import 'i_local_storage.dart';
|
||||
|
||||
class HiveLocalStorage implements ILocalStorage {
|
||||
HiveLocalStorage() {
|
||||
Hive.initFlutter();
|
||||
}
|
||||
|
||||
final Map<String, Box> _boxes = {};
|
||||
|
||||
@override
|
||||
Future init() async => await Hive.initFlutter();
|
||||
|
||||
@override
|
||||
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,
|
||||
encryptionCipher: encryptionCipher,
|
||||
crashRecovery: crashRecovery,
|
||||
);
|
||||
_boxes[boxName] = box;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
T? read<T>({required String boxName, required String key}) {
|
||||
try {
|
||||
Box? box = getBox(boxName);
|
||||
return box?.get(key) as T?;
|
||||
} on Exception catch (e) {
|
||||
eLog(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> add({required String boxName, required dynamic value}) async {
|
||||
Box<dynamic>? box = getBox(boxName);
|
||||
await box?.add(value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> addAll({
|
||||
required String boxName,
|
||||
required Iterable values,
|
||||
}) async {
|
||||
Box<dynamic>? box = getBox(boxName);
|
||||
await box?.addAll(values);
|
||||
}
|
||||
|
||||
Box<T>? getBox<T>(String boxName) {
|
||||
final box = _boxes[boxName];
|
||||
if (box is Box<T>) {
|
||||
return box;
|
||||
} else {
|
||||
throw Exception('Box $boxName is not of exist');
|
||||
}
|
||||
}
|
||||
|
||||
@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 = getBox(boxName);
|
||||
await box?.delete(key);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> save({
|
||||
required String boxName,
|
||||
required String key,
|
||||
required value,
|
||||
}) async {
|
||||
Box<dynamic>? box = getBox(boxName);
|
||||
await box?.put(key, value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveAll({required String boxName, required Map entries}) async {
|
||||
Box<dynamic>? box = getBox(boxName);
|
||||
await box?.putAll(entries);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveAt({
|
||||
required String boxName,
|
||||
required int index,
|
||||
required value,
|
||||
}) async {
|
||||
Box<dynamic>? box = getBox(boxName);
|
||||
await box?.putAt(index, value);
|
||||
}
|
||||
}
|
||||
44
packages/core/test/infrastructure/local/i_local_storage.dart
Normal file
44
packages/core/test/infrastructure/local/i_local_storage.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hive_ce/hive.dart';
|
||||
|
||||
abstract class ILocalStorage<E> {
|
||||
Future<void> init();
|
||||
|
||||
Future<void> openBox<T>(
|
||||
String boxName, {
|
||||
HiveCipher? encryptionCipher,
|
||||
bool crashRecovery = true,
|
||||
String? path,
|
||||
Uint8List? bytes,
|
||||
String? collection,
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
23
packages/core/test/infrastructure/remote/dio_form_data.dart
Normal file
23
packages/core/test/infrastructure/remote/dio_form_data.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'interfaces/i_form_data.dart';
|
||||
|
||||
class DioFormData implements IFormData {
|
||||
final FormData _formData = FormData();
|
||||
|
||||
@override
|
||||
void addFile(String field, Uint8List bytes, String filename) {
|
||||
_formData.files.add(MapEntry(
|
||||
field,
|
||||
MultipartFile.fromBytes(bytes, filename: filename),
|
||||
));
|
||||
}
|
||||
|
||||
@override
|
||||
void addField(String key, String value) {
|
||||
_formData.fields.add(MapEntry(key, value));
|
||||
}
|
||||
|
||||
FormData get raw => _formData;
|
||||
}
|
||||
20
packages/core/test/infrastructure/remote/dio_response.dart
Normal file
20
packages/core/test/infrastructure/remote/dio_response.dart
Normal file
@@ -0,0 +1,20 @@
|
||||
import 'interfaces/i_http_response.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class DioResponse<T> implements IHttpResponse<T> {
|
||||
final Response<dynamic> _response;
|
||||
|
||||
DioResponse(this._response);
|
||||
|
||||
@override
|
||||
T? get data => _response.data;
|
||||
|
||||
@override
|
||||
int get statusCode => _response.statusCode ?? 0;
|
||||
|
||||
@override
|
||||
Map<String, dynamic>? get headers => _response.headers.map;
|
||||
|
||||
@override
|
||||
bool get isSuccessful => statusCode >= 200 && statusCode < 300;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
abstract class IFormData{
|
||||
void addFile(String field, Uint8List bytes, String filename);
|
||||
void addField(String key, String value);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
|
||||
import 'dart:typed_data';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'i_form_data.dart';
|
||||
import 'i_http_response.dart';
|
||||
|
||||
abstract class IHttpClient {
|
||||
Future<void> init();
|
||||
|
||||
Future<IHttpResponse<T>> get<T>(
|
||||
String path, {
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Map<String, String>? headers,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
});
|
||||
|
||||
|
||||
Future<IHttpResponse<T>> post<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Map<String, String>? headers,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
});
|
||||
|
||||
Future<IHttpResponse<T>> put<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Map<String, String>? headers,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
});
|
||||
|
||||
Future<IHttpResponse<T>> delete<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Map<String, String>? headers,
|
||||
});
|
||||
|
||||
Future<IHttpResponse<T>> download<T>(
|
||||
String url, {
|
||||
ProgressCallback? onReceiveProgress,
|
||||
});
|
||||
|
||||
Future<IHttpResponse<T>> upload<T>(
|
||||
String path, {
|
||||
required IFormData formData,
|
||||
Map<String, String>? headers,
|
||||
ProgressCallback? onSendProgress,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
abstract class IHttpResponse<T> {
|
||||
T? get data;
|
||||
int get statusCode;
|
||||
Map<String, dynamic>? get headers;
|
||||
bool get isSuccessful;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
abstract class IRemote<T>{
|
||||
Future<T> init();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user