This commit is contained in:
Hamza-Ayed
2025-09-08 00:19:57 +03:00
parent 8c7f3e3a75
commit 7e904ae460
146 changed files with 761 additions and 327 deletions

View File

@@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:jwt_decoder/jwt_decoder.dart';
import 'package:sefer_driver/controller/functions/network/net_guard.dart';
import 'package:secure_string_operations/secure_string_operations.dart';
import 'package:sefer_driver/constant/box_name.dart';
import 'package:sefer_driver/constant/links.dart';
@@ -12,11 +13,14 @@ import 'package:sefer_driver/env/env.dart';
import '../../constant/api_key.dart';
import '../../constant/char_map.dart';
import '../../constant/info.dart';
import '../../views/widgets/error_snakbar.dart';
import '../../print.dart';
import 'gemeni.dart';
import 'upload_image.dart';
class CRUD {
final NetGuard _netGuard = NetGuard();
/// Stores the signature of the last logged error to prevent duplicates.
static String _lastErrorSignature = '';
@@ -82,6 +86,17 @@ class CRUD {
Map<String, dynamic>? payload,
required Map<String, String> headers,
}) async {
// ✅ 1. Check for internet connection before making any request.
if (!await _netGuard.hasInternet(mustReach: Uri.parse(link))) {
// ✅ 2. If no internet, show a notification to the user (only once every 15s).
_netGuard.notifyOnce((title, msg) {
mySnackeBarError(
msg); // Using your existing snackbar for notifications.
});
// ✅ 3. Return a specific status to indicate no internet.
return 'no_internet';
}
var url = Uri.parse(link);
try {
var response = await http.post(
@@ -212,6 +227,78 @@ class CRUD {
);
}
Future<dynamic> postWalletMtn(
{required String link, Map<String, dynamic>? payload}) async {
final s = await LoginDriverController().getJwtWallet();
final hmac = box.read(BoxName.hmac);
final url = Uri.parse(link);
try {
final response = await http.post(
url,
body: payload, // form-urlencoded مناسب لـ filterRequest
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer $s",
"X-HMAC-Auth": hmac.toString(),
},
);
print('req: ${response.request}');
print('status: ${response.statusCode}');
print('body: ${response.body}');
print('payload: $payload');
Map<String, dynamic> wrap(String status, {Object? message, int? code}) {
return {
'status': status,
'message': message,
'code': code ?? response.statusCode,
};
}
if (response.statusCode == 200) {
try {
final jsonData = jsonDecode(response.body);
// نتوقع الآن شكل موحّد من السيرفر: {status, message, data?}
return jsonData;
} catch (e) {
return wrap('failure',
message: 'JSON decode error', code: response.statusCode);
}
} else if (response.statusCode == 401) {
try {
final jsonData = jsonDecode(response.body);
if (jsonData is Map && jsonData['error'] == 'Token expired') {
await Get.put(LoginDriverController()).getJWT();
return {
'status': 'failure',
'message': 'token_expired',
'code': 401
};
}
return wrap('failure', message: jsonData);
} catch (_) {
return wrap('failure', message: response.body);
}
} else {
// غير 200 ارجع التفاصيل
try {
final jsonData = jsonDecode(response.body);
return wrap('failure', message: jsonData);
} catch (_) {
return wrap('failure', message: response.body);
}
}
} catch (e) {
return {
'status': 'failure',
'message': 'HTTP request error: $e',
'code': -1
};
}
}
Future<dynamic> get({
required String link,
Map<String, dynamic>? payload,
@@ -820,3 +907,32 @@ class CRUD {
return json.decode(response.body);
}
}
/// Custom exception for when there is no internet connection.
class NoInternetException implements Exception {
final String message;
NoInternetException(
[this.message =
'No internet connection. Please check your network and try again.']);
@override
String toString() => message;
}
/// Custom exception for when the network is too slow (request times out).
class WeakNetworkException implements Exception {
final String message;
WeakNetworkException(
[this.message =
'Your network connection is too slow. Please try again later.']);
@override
String toString() => message;
}
class ApiException implements Exception {
final String message;
final int? statusCode;
ApiException(this.message, [this.statusCode]);
@override
String toString() =>
"ApiException: $message (Status Code: ${statusCode ?? 'N/A'})";
}