Files
paperless-mobile/lib/features/document_scan/cubit/document_scanner_cubit.dart
Anton Stubenbord 6566b2b8d7 feat: Rework error handling, upgrade dio, fixed bugs
- Fix grey screen bug when adding labels from documnet upload
- Add more permission checks to conditionally show widgets
2023-07-22 14:17:48 +02:00

58 lines
1.5 KiB
Dart

import 'dart:developer';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/service/file_service.dart';
import 'package:paperless_mobile/features/notifications/services/local_notification_service.dart';
class DocumentScannerCubit extends Cubit<List<File>> {
final LocalNotificationService _notificationService;
DocumentScannerCubit(this._notificationService) : super(const []);
void addScan(File file) => emit([...state, file]);
void removeScan(int fileIndex) {
try {
state[fileIndex].deleteSync();
final scans = [...state];
scans.removeAt(fileIndex);
emit(scans);
} catch (_) {
throw const PaperlessApiException(ErrorCode.scanRemoveFailed);
}
}
void reset() {
try {
for (final doc in state) {
doc.deleteSync();
if (kDebugMode) {
log('[ScannerCubit]: Removed ${doc.path}');
}
}
imageCache.clear();
emit([]);
} catch (_) {
throw const PaperlessApiException(ErrorCode.scanRemoveFailed);
}
}
Future<void> saveToFile(
Uint8List bytes,
String fileName,
String locale,
) async {
var file = await FileService.saveToFile(bytes, fileName);
_notificationService.notifyFileSaved(
filename: fileName,
filePath: file.path,
finished: true,
locale: locale,
);
}
}