80 lines
2.7 KiB
Dart
80 lines
2.7 KiB
Dart
import 'dart:io';
|
|
import 'package:get/get.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:path/path.dart' as path;
|
|
import '../../../core/utils/logger.dart';
|
|
import '../../../core/utils/app_snackbar.dart';
|
|
import '../../../core/services/image_processing_service.dart';
|
|
import '../../../core/services/invoice_upload_service.dart';
|
|
|
|
class ScannerController extends GetxController {
|
|
var capturedImages = <File>[].obs;
|
|
var isProcessing = false.obs;
|
|
var uploadProgress = 0.0.obs;
|
|
|
|
final InvoiceUploadService _uploadService = InvoiceUploadService();
|
|
|
|
Future<void> addImage(String imagePath) async {
|
|
isProcessing.value = true;
|
|
try {
|
|
File originalFile = File(imagePath);
|
|
// Process image (compress, grayscale, contrast)
|
|
File? processedFile = await ImageProcessingService.processInvoiceImage(originalFile);
|
|
|
|
if (processedFile != null) {
|
|
capturedImages.add(processedFile);
|
|
AppLogger.print('Added processed image to batch. Total: ${capturedImages.length}');
|
|
}
|
|
} finally {
|
|
isProcessing.value = false;
|
|
}
|
|
}
|
|
|
|
void removeImage(int index) {
|
|
if (index >= 0 && index < capturedImages.length) {
|
|
capturedImages.removeAt(index);
|
|
}
|
|
}
|
|
|
|
Future<void> uploadBatch(String companyId) async {
|
|
if (capturedImages.isEmpty) {
|
|
AppSnackbar.showWarning('تنبيه', 'الرجاء تصوير فاتورة واحدة على الأقل');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
isProcessing.value = true;
|
|
uploadProgress.value = 0.0;
|
|
AppLogger.print('Uploading batch of ${capturedImages.length} images...');
|
|
|
|
final batchId = await _uploadService.uploadBatch(
|
|
companyId: companyId,
|
|
images: capturedImages,
|
|
onProgress: (current, total) {
|
|
uploadProgress.value = current / total;
|
|
},
|
|
);
|
|
|
|
if (batchId != null) {
|
|
AppSnackbar.showSuccess('نجاح', 'تم رفع ${capturedImages.length} فواتير للمعالجة بنجاح');
|
|
capturedImages.clear();
|
|
uploadProgress.value = 0.0;
|
|
Get.back(); // Go back to dashboard or previous screen
|
|
} else {
|
|
AppSnackbar.showError('خطأ', 'فشل رفع الفواتير، يرجى المحاولة لاحقاً');
|
|
}
|
|
} catch (e) {
|
|
AppLogger.error('Failed to upload batch', e);
|
|
AppSnackbar.showError('خطأ', 'حدث خطأ غير متوقع أثناء الرفع');
|
|
} finally {
|
|
isProcessing.value = false;
|
|
}
|
|
}
|
|
|
|
Future<String> getSavePath() async {
|
|
final directory = await getTemporaryDirectory();
|
|
final fileName = 'invoice_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
|
return path.join(directory.path, fileName);
|
|
}
|
|
}
|