Update: 2026-05-08 15:18:01
This commit is contained in:
@@ -38,8 +38,12 @@ 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) {
|
||||
final type = data['type'];
|
||||
final batchId = data['batch_id'];
|
||||
|
||||
if (batchId != currentBatchId.value) return;
|
||||
|
||||
if (type == 'invoice_processed' || type == 'batch_progress') {
|
||||
processedImagesCount.value =
|
||||
int.tryParse(data['processed'].toString()) ?? 0;
|
||||
totalImagesCount.value = int.tryParse(data['total'].toString()) ?? 0;
|
||||
@@ -48,9 +52,21 @@ class ScannerController extends GetxController {
|
||||
_progressService.updateProcessingProgress(
|
||||
processedImagesCount.value, totalImagesCount.value);
|
||||
|
||||
if (processedImagesCount.value >= totalImagesCount.value) {
|
||||
// If it's a single invoice, we can navigate directly
|
||||
if (totalImagesCount.value == 1 && data['invoice_id'] != null) {
|
||||
isBatchDone.value = true;
|
||||
_progressService.complete();
|
||||
|
||||
// Open invoice details
|
||||
Get.toNamed('/invoice-detail', arguments: data['invoice_id']);
|
||||
}
|
||||
} else if (type == 'batch_complete') {
|
||||
isBatchDone.value = true;
|
||||
_progressService.complete();
|
||||
|
||||
// Optionally navigate to invoices list or specific invoice
|
||||
if (data['invoice_id'] != null && data['invoice_id'].toString().isNotEmpty) {
|
||||
Get.toNamed('/invoice-detail', arguments: data['invoice_id']);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -137,6 +153,9 @@ class ScannerController extends GetxController {
|
||||
Get.back(); // Go back to dashboard, progress will show in overlay
|
||||
AppSnackbar.showSuccess(
|
||||
'تم البدء', 'تم رفع الصور بنجاح، جاري استخراج البيانات في الخلفية');
|
||||
|
||||
// Start polling for status (Reliable fallback for FCM)
|
||||
_startPolling(batchId);
|
||||
} else {
|
||||
_progressService.fail();
|
||||
AppSnackbar.showError('خطأ', 'فشل رفع الفواتير، يرجى المحاولة لاحقاً');
|
||||
@@ -150,6 +169,46 @@ class ScannerController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
void _startPolling(String batchId) {
|
||||
// Check status every 2 seconds
|
||||
Future.doWhile(() async {
|
||||
// If we are no longer interested in this batch or it's done, stop polling
|
||||
if (currentBatchId.value != batchId || isBatchDone.value) return false;
|
||||
|
||||
try {
|
||||
final res = await DioClient().client.get('batches/status', queryParameters: {'batch_id': batchId});
|
||||
if (res.data['success'] == true) {
|
||||
final batch = res.data['data']['batch'];
|
||||
final items = res.data['data']['items'] as List;
|
||||
|
||||
processedImagesCount.value = int.tryParse(batch['processed_images'].toString()) ?? 0;
|
||||
totalImagesCount.value = int.tryParse(batch['total_images'].toString()) ?? 1;
|
||||
|
||||
_progressService.updateProcessingProgress(processedImagesCount.value, totalImagesCount.value);
|
||||
|
||||
if (batch['status'] == 'done') {
|
||||
isBatchDone.value = true;
|
||||
_progressService.complete();
|
||||
|
||||
// If it's a single invoice, find the invoice_id and navigate
|
||||
if (totalImagesCount.value == 1 && items.isNotEmpty) {
|
||||
final invoiceId = items.first['invoice_id'];
|
||||
if (invoiceId != null) {
|
||||
Get.toNamed('/invoice-detail', arguments: invoiceId);
|
||||
}
|
||||
}
|
||||
return false; // Stop polling
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Polling error', e);
|
||||
}
|
||||
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
return true; // Continue polling
|
||||
});
|
||||
}
|
||||
|
||||
void selectCompany(String id, String name) {
|
||||
selectedCompanyId.value = id;
|
||||
selectedCompanyName.value = name;
|
||||
|
||||
Reference in New Issue
Block a user