Update: 2026-05-07 03:06:15
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:get/get.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import '../../../core/services/upload_progress_service.dart';
|
||||
import '../../../core/utils/logger.dart';
|
||||
import '../../../core/utils/app_snackbar.dart';
|
||||
import '../../../core/services/image_processing_service.dart';
|
||||
@@ -22,6 +23,8 @@ class ScannerController extends GetxController {
|
||||
var isBatchDone = false.obs;
|
||||
|
||||
final InvoiceUploadService _uploadService = InvoiceUploadService();
|
||||
final UploadProgressService _progressService =
|
||||
Get.find<UploadProgressService>();
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@@ -33,12 +36,19 @@ class ScannerController extends GetxController {
|
||||
void _initFcmListener() {
|
||||
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
||||
final data = message.data;
|
||||
if (data['type'] == 'batch_progress' && data['batch_id'] == currentBatchId.value) {
|
||||
processedImagesCount.value = int.tryParse(data['processed'].toString()) ?? 0;
|
||||
if (data['type'] == 'batch_progress' &&
|
||||
data['batch_id'] == currentBatchId.value) {
|
||||
processedImagesCount.value =
|
||||
int.tryParse(data['processed'].toString()) ?? 0;
|
||||
totalImagesCount.value = int.tryParse(data['total'].toString()) ?? 0;
|
||||
|
||||
|
||||
// Update global progress service
|
||||
_progressService.updateProcessingProgress(
|
||||
processedImagesCount.value, totalImagesCount.value);
|
||||
|
||||
if (processedImagesCount.value >= totalImagesCount.value) {
|
||||
isBatchDone.value = true;
|
||||
isBatchDone.value = true;
|
||||
_progressService.complete();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -60,18 +70,17 @@ class ScannerController extends GetxController {
|
||||
|
||||
Future<void> addImage(String imagePath) async {
|
||||
File originalFile = File(imagePath);
|
||||
// Add to UI immediately so the user doesn't wait
|
||||
capturedImages.add(originalFile);
|
||||
int index = capturedImages.length - 1;
|
||||
|
||||
// Process in background without showing full-screen loader
|
||||
ImageProcessingService.processInvoiceImage(originalFile).then((processedFile) {
|
||||
ImageProcessingService.processInvoiceImage(originalFile)
|
||||
.then((processedFile) {
|
||||
if (processedFile != null && index < capturedImages.length) {
|
||||
capturedImages[index] = processedFile;
|
||||
AppLogger.print('Finished processing image in background. Replaced in batch.');
|
||||
AppLogger.print('Finished processing image in background.');
|
||||
}
|
||||
}).catchError((e) {
|
||||
AppLogger.error('Failed to process image in background', e);
|
||||
AppLogger.error('Failed to process image in background', e);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -90,89 +99,67 @@ class ScannerController extends GetxController {
|
||||
try {
|
||||
isProcessing.value = true;
|
||||
uploadProgress.value = 0.0;
|
||||
|
||||
// Fetch a valid company ID dynamically to prevent 403 Forbidden
|
||||
|
||||
String companyId = fallbackCompanyId;
|
||||
String companyName = 'شركة غير محددة';
|
||||
|
||||
if (companyId == 'mock_company_id_123' || companyId.isEmpty) {
|
||||
final res = await DioClient().client.get('companies');
|
||||
if (res.data['success'] == true && res.data['data'] != null && res.data['data'].isNotEmpty) {
|
||||
companyId = res.data['data'][0]['id'];
|
||||
AppLogger.print('Dynamically fetched company: $companyId');
|
||||
if (companies.isNotEmpty) {
|
||||
companyId = companies[0]['id'];
|
||||
companyName = companies[0]['name'] ?? 'شركتي';
|
||||
} else {
|
||||
AppSnackbar.showError('خطأ', 'لا توجد شركات مسجلة في حسابك');
|
||||
isProcessing.value = false;
|
||||
return;
|
||||
final res = await DioClient().client.get('companies');
|
||||
if (res.data['success'] == true &&
|
||||
res.data['data'] != null &&
|
||||
res.data['data'].isNotEmpty) {
|
||||
companyId = res.data['data'][0]['id'];
|
||||
companyName = res.data['data'][0]['name'] ?? 'شركتي';
|
||||
} else {
|
||||
AppSnackbar.showError('خطأ', 'لا توجد شركات مسجلة في حسابك');
|
||||
isProcessing.value = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
final comp = companies.firstWhereOrNull((c) => c['id'] == companyId);
|
||||
if (comp != null) companyName = comp['name'] ?? 'شركتي';
|
||||
}
|
||||
|
||||
AppLogger.print('Uploading batch of ${capturedImages.length} images to company $companyId...');
|
||||
|
||||
AppLogger.print(
|
||||
'Uploading batch of ${capturedImages.length} images to company $companyId...');
|
||||
|
||||
// Start global progress
|
||||
_progressService.startUpload(companyName, capturedImages.length);
|
||||
|
||||
final batchId = await _uploadService.uploadBatch(
|
||||
companyId: companyId,
|
||||
images: capturedImages,
|
||||
onProgress: (current, total) {
|
||||
uploadProgress.value = current / total;
|
||||
_progressService.updateProgress(uploadProgress.value, current);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
if (batchId != null) {
|
||||
currentBatchId.value = batchId;
|
||||
totalImagesCount.value = capturedImages.length;
|
||||
processedImagesCount.value = 0;
|
||||
|
||||
// Clear scanner state and go back to dashboard
|
||||
capturedImages.clear();
|
||||
uploadProgress.value = 0.0;
|
||||
|
||||
Get.dialog(
|
||||
AlertDialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
title: const Center(
|
||||
child: Text('جاري المعالجة ⏳',
|
||||
style: TextStyle(fontFamily: 'El Messiri', fontWeight: FontWeight.bold, fontSize: 18)
|
||||
)
|
||||
),
|
||||
content: Obx(() => Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Text('يتم الآن تدقيق الفواتير عبر الذكاء الاصطناعي...',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontFamily: 'El Messiri', fontSize: 14)
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
if (!isBatchDone.value) ...[
|
||||
LinearProgressIndicator(
|
||||
value: totalImagesCount.value > 0 ? processedImagesCount.value / totalImagesCount.value : 0,
|
||||
backgroundColor: Colors.grey[200],
|
||||
valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFF0F4C81)),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Text('${processedImagesCount.value} من ${totalImagesCount.value}',
|
||||
style: const TextStyle(fontFamily: 'El Messiri', fontWeight: FontWeight.bold)
|
||||
),
|
||||
] else ...[
|
||||
const Icon(Icons.check_circle, color: Colors.green, size: 50),
|
||||
const SizedBox(height: 10),
|
||||
const Text('اكتملت المعالجة بنجاح!',
|
||||
style: TextStyle(fontFamily: 'El Messiri', color: Colors.green, fontWeight: FontWeight.bold)
|
||||
),
|
||||
],
|
||||
],
|
||||
)),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Get.back(); // close dialog
|
||||
Get.back(); // go back to dashboard
|
||||
},
|
||||
child: const Text('إغلاق', style: TextStyle(fontFamily: 'El Messiri', fontWeight: FontWeight.bold)),
|
||||
)
|
||||
],
|
||||
),
|
||||
barrierDismissible: false,
|
||||
);
|
||||
isProcessing.value = false;
|
||||
|
||||
_progressService.startProcessing();
|
||||
Get.back(); // Go back to dashboard, progress will show in overlay
|
||||
AppSnackbar.showSuccess(
|
||||
'تم البدء', 'تم رفع الصور بنجاح، جاري استخراج البيانات في الخلفية');
|
||||
} else {
|
||||
_progressService.fail();
|
||||
AppSnackbar.showError('خطأ', 'فشل رفع الفواتير، يرجى المحاولة لاحقاً');
|
||||
}
|
||||
} catch (e) {
|
||||
_progressService.fail();
|
||||
AppLogger.error('Failed to upload batch', e);
|
||||
AppSnackbar.showError('خطأ', 'حدث خطأ غير متوقع أثناء الرفع');
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user