This commit is contained in:
Mohammed Al-Samarraie
2026-01-16 14:42:43 +03:00
parent 56e2c0ffaa
commit 2fd5aff0c2
4 changed files with 363 additions and 56 deletions

View File

@@ -1,5 +1,6 @@
import 'dart:io';
import 'package:dio/dio.dart';
import '../../core/error/exceptions.dart';
import '../../core/network/api_client.dart';
import '../dto/attendance_response_dto.dart';
@@ -24,17 +25,62 @@ class AttendanceRemoteDataSourceImpl implements AttendanceRemoteDataSource {
required String employeeId,
required File faceImage,
}) async {
final formData = FormData.fromMap({
'EmployeeId': employeeId,
'FaceImage': await MultipartFile.fromFile(faceImage.path),
});
try {
final formData = FormData.fromMap({
'EmployeeId': employeeId,
'FaceImage': await MultipartFile.fromFile(faceImage.path),
});
final response = await apiClient.post(
'/Attendance/login',
data: formData,
options: Options(contentType: 'multipart/form-data'),
);
return AttendanceResponseDto.fromJson(response.data);
final response = await apiClient.post(
'/Attendance/login',
data: formData,
options: Options(contentType: 'multipart/form-data'),
);
if (response.statusCode == 200 || response.statusCode == 201) {
final responseData = response.data;
if (responseData is Map<String, dynamic>) {
return AttendanceResponseDto.fromJson(responseData);
} else {
throw ServerException(
message: 'استجابة غير صحيحة من الخادم',
statusCode: response.statusCode,
);
}
} else {
throw ServerException(
message: 'فشل تسجيل الدخول',
statusCode: response.statusCode,
);
}
} on DioException catch (e) {
if (e.type == DioExceptionType.connectionTimeout ||
e.type == DioExceptionType.receiveTimeout) {
throw NetworkException(message: 'انتهت مهلة الاتصال');
} else if (e.type == DioExceptionType.connectionError) {
throw NetworkException(message: 'لا يوجد اتصال بالانترنيت');
} else if (e.response?.statusCode == 500) {
throw ServerException(message: 'خطأ في الخادم يرجى المحاولة لاحقا');
} else if (e.response != null) {
final message =
e.response?.data?['message'] ??
e.response?.data?['error'] ??
'فشل تسجيل الدخول';
throw ServerException(
message: message.toString(),
statusCode: e.response?.statusCode,
);
} else {
throw NetworkException(message: 'خطأ في الانترنيت يرجى المحاولة لاحقا');
}
} catch (e) {
if (e is ServerException || e is NetworkException) {
rethrow;
}
throw ServerException(message: 'خطأ غير متوقع');
}
}
@override
@@ -42,17 +88,61 @@ class AttendanceRemoteDataSourceImpl implements AttendanceRemoteDataSource {
required String employeeId,
required File faceImage,
}) async {
final formData = FormData.fromMap({
'EmployeeId': employeeId,
'FaceImage': await MultipartFile.fromFile(faceImage.path),
});
try {
final formData = FormData.fromMap({
'EmployeeId': employeeId,
'FaceImage': await MultipartFile.fromFile(faceImage.path),
});
final response = await apiClient.post(
'/Attendance/logout',
data: formData,
options: Options(contentType: 'multipart/form-data'),
);
final response = await apiClient.post(
'/Attendance/logout',
data: formData,
options: Options(contentType: 'multipart/form-data'),
);
return AttendanceResponseDto.fromJson(response.data);
if (response.statusCode == 200 || response.statusCode == 201) {
final responseData = response.data;
if (responseData is Map<String, dynamic>) {
return AttendanceResponseDto.fromJson(responseData);
} else {
throw ServerException(
message: 'استجابة غير صحيحة من الخادم',
statusCode: response.statusCode,
);
}
} else {
throw ServerException(
message: 'فشل تسجيل الخروج',
statusCode: response.statusCode,
);
}
} on DioException catch (e) {
if (e.type == DioExceptionType.connectionTimeout ||
e.type == DioExceptionType.receiveTimeout) {
throw NetworkException(message: 'انتهت مهلة الاتصال');
} else if (e.type == DioExceptionType.connectionError) {
throw NetworkException(message: 'لا يوجد اتصال بالانترنيت');
} else if (e.response?.statusCode == 500) {
throw ServerException(message: 'خطأ في الخادم يرجى المحاولة لاحقا');
} else if (e.response != null) {
final message =
e.response?.data?['message'] ??
e.response?.data?['error'] ??
'فشل تسجيل الخروج';
throw ServerException(
message: message.toString(),
statusCode: e.response?.statusCode,
);
} else {
throw NetworkException(message: 'خطأ في الانترنيت يرجى المحاولة لاحقا');
}
} catch (e) {
if (e is ServerException || e is NetworkException) {
rethrow;
}
throw ServerException(message: 'خطأ غير متوقع');
}
}
}

View File

@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'dart:async';
import 'dart:io';
import '../../core/error/exceptions.dart';
class OvalCameraCapturePage extends StatefulWidget {
final bool isLogin;
@@ -23,6 +24,7 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
bool _isCameraInitialized = false;
String? _errorMessage;
bool _isSuccess = false;
bool _isLoading = false;
Timer? _timer;
@override
@@ -33,6 +35,11 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
Future<void> _initializeCamera() async {
try {
setState(() {
_errorMessage = null;
_isCameraInitialized = false;
});
// Dispose existing controller if any
await _cameraController?.dispose();
_cameraController = null;
@@ -42,8 +49,9 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
// Check if cameras list is available
if (cameras.isEmpty) {
if (!mounted) return;
setState(() {
_errorMessage = "لا توجد كاميرات متاحة";
_errorMessage = "لا توجد كاميرات متاحة على هذا الجهاز";
_isCameraInitialized = false;
});
return;
@@ -60,8 +68,9 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
if (cameras.isNotEmpty) {
selectedCamera = cameras.first;
} else {
if (!mounted) return;
setState(() {
_errorMessage = "لا توجد كاميرات متاحة";
_errorMessage = "لا توجد كاميرات متاحة على هذا الجهاز";
_isCameraInitialized = false;
});
return;
@@ -85,11 +94,33 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
});
_startScan();
} on CameraException catch (e) {
if (!mounted) return;
String errorMessage;
switch (e.code) {
case 'CameraAccessDenied':
errorMessage = "تم رفض الوصول إلى الكاميرا. يرجى السماح بالوصول في الإعدادات";
break;
case 'CameraAccessDeniedWithoutPrompt':
errorMessage = "تم رفض الوصول إلى الكاميرا. يرجى السماح بالوصول في الإعدادات";
break;
case 'CameraAccessRestricted':
errorMessage = "الوصول إلى الكاميرا مقيد";
break;
case 'AudioAccessDenied':
errorMessage = "تم رفض الوصول إلى الميكروفون";
break;
default:
errorMessage = "خطأ في تهيئة الكاميرا: ${e.description ?? 'خطأ غير معروف'}";
}
setState(() {
_errorMessage = errorMessage;
_isCameraInitialized = false;
});
} catch (e) {
if (!mounted) return;
setState(() {
_errorMessage = "خطأ في تهيئة الكاميرا: $e";
_errorMessage = "حدث خطأ غير متوقع أثناء تهيئة الكاميرا";
_isCameraInitialized = false;
});
print("Error initializing camera: $e");
@@ -98,46 +129,104 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
Future<void> _startScan() async {
try {
setState(() {
_isLoading = true;
_errorMessage = null;
});
// Simulate scanning delay
await Future.delayed(const Duration(seconds: 2));
if (!mounted ||
_cameraController == null ||
!_cameraController!.value.isInitialized) {
if (mounted) {
setState(() {
_isLoading = false;
_errorMessage = "الكاميرا غير مهيأة. يرجى المحاولة مرة أخرى";
});
}
return;
}
final xFile = await _cameraController!.takePicture();
final file = File(xFile.path);
// Check if file exists and is readable
if (!await file.exists()) {
if (mounted) {
setState(() {
_isLoading = false;
_errorMessage = "فشل حفظ الصورة. يرجى المحاولة مرة أخرى";
});
}
return;
}
// Call the onCapture callback which may throw exceptions
await widget.onCapture(file);
if (mounted) {
setState(() {
_isSuccess = true;
_isLoading = false;
});
// Auto-close after 2 seconds
Future.delayed(const Duration(seconds: 2), () {
if (mounted) {
Navigator.of(context).pop();
Navigator.of(context).pop(true);
}
});
}
} on ServerException catch (e) {
if (mounted) {
setState(() {
_isLoading = false;
_errorMessage = e.message;
});
}
} on NetworkException catch (e) {
if (mounted) {
setState(() {
_isLoading = false;
_errorMessage = e.message;
});
}
} on CameraException catch (e) {
if (mounted) {
String errorMessage;
switch (e.code) {
case 'captureAlreadyActive':
errorMessage = "جاري التقاط صورة بالفعل. يرجى الانتظار";
break;
case 'pictureTakingInProgress':
errorMessage = "جاري التقاط صورة. يرجى الانتظار";
break;
default:
errorMessage = "فشل التقاط الصورة: ${e.description ?? 'خطأ غير معروف'}";
}
setState(() {
_isLoading = false;
_errorMessage = errorMessage;
});
}
} on FileSystemException catch (e) {
if (mounted) {
setState(() {
_isLoading = false;
_errorMessage = "فشل حفظ الصورة. يرجى التحقق من مساحة التخزين";
});
}
print("File system error: $e");
} catch (e) {
if (mounted) {
setState(() {
_errorMessage = e.toString(); // Show error to user
});
// Auto-close or let user retry? For now let's just show error.
// If we want to auto-close on error:
Future.delayed(const Duration(seconds: 3), () {
if (mounted) {
Navigator.of(context).pop();
}
_isLoading = false;
_errorMessage = "حدث خطأ غير متوقع. يرجى المحاولة مرة أخرى";
});
}
print("Unexpected error in _startScan: $e");
}
}
@@ -154,7 +243,8 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
backgroundColor: Color(0xff000000),
body:
_errorMessage != null
// Show error screen only if camera failed to initialize
_errorMessage != null && !_isCameraInitialized
? Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
@@ -177,17 +267,39 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
textAlign: TextAlign.center,
),
SizedBox(height: 24),
ElevatedButton(
onPressed: _initializeCamera,
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xffE8001A),
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(
horizontal: 32,
vertical: 12,
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _initializeCamera,
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xffE8001A),
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(
horizontal: 32,
vertical: 12,
),
),
child: Text("إعادة المحاولة"),
),
),
child: Text("إعادة المحاولة"),
SizedBox(width: 16),
OutlinedButton(
onPressed: () {
if (mounted) {
Navigator.of(context).pop(false);
}
},
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white,
side: BorderSide(color: Colors.white70),
padding: EdgeInsets.symmetric(
horizontal: 32,
vertical: 12,
),
),
child: Text("إلغاء"),
),
],
),
],
),
@@ -239,9 +351,13 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
? (widget.isLogin
? "تم تسجيل دخولك بنجاح"
: "تم تسجيل خروجك بنجاح")
: (widget.isLogin
? "يتم تسجيل الدخول ..."
: "يتم تسجيل الخروج ..."),
: _isLoading
? (widget.isLogin
? "يتم تسجيل الدخول ..."
: "يتم تسجيل الخروج ...")
: (widget.isLogin
? "جاهز للتقاط الصورة"
: "جاهز للتقاط الصورة"),
style: const TextStyle(
color: Colors.white,
fontSize: 18,
@@ -266,6 +382,81 @@ class _OvalCameraCapturePageState extends State<OvalCameraCapturePage> {
),
),
// Error overlay (shown when error occurs during scanning)
if (_errorMessage != null && _isCameraInitialized)
Positioned.fill(
child: Container(
color: Colors.black.withOpacity(0.7),
child: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.error_outline,
size: 64,
color: Colors.red,
),
SizedBox(height: 16),
Text(
_errorMessage!,
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
SizedBox(height: 24),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
setState(() {
_errorMessage = null;
_isLoading = false;
});
_startScan();
},
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xffE8001A),
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(
horizontal: 32,
vertical: 12,
),
),
child: Text("إعادة المحاولة"),
),
SizedBox(width: 16),
OutlinedButton(
onPressed: () {
if (mounted) {
Navigator.of(context).pop(false);
}
},
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white,
side: BorderSide(color: Colors.white70),
padding: EdgeInsets.symmetric(
horizontal: 32,
vertical: 12,
),
),
child: Text("إلغاء"),
),
],
),
],
),
),
),
),
),
// // Capture button
// Positioned(
// bottom: 60,