feat: Implement updated receive share logic

This commit is contained in:
Anton Stubenbord
2023-10-02 23:59:42 +02:00
parent 653344c9ee
commit 37ed8bbb04
47 changed files with 1695 additions and 730 deletions

View File

@@ -0,0 +1,72 @@
import 'dart:io';
import 'package:bloc/bloc.dart';
import 'package:flutter/foundation.dart';
import 'package:paperless_mobile/core/service/file_service.dart';
import 'package:path/path.dart' as p;
import 'package:provider/provider.dart';
part 'receive_share_state.dart';
class ConsumptionChangeNotifier extends ChangeNotifier {
List<File> pendingFiles = [];
ConsumptionChangeNotifier();
Future<void> loadFromConsumptionDirectory({required String userId}) async {
pendingFiles = await _getCurrentFiles(userId);
notifyListeners();
}
/// Creates a local copy of all shared files and reloads all files
/// from the user's consumption directory.
Future<void> addFiles({
required List<File> files,
required String userId,
}) async {
if (files.isEmpty) {
return;
}
final consumptionDirectory =
await FileService.getConsumptionDirectory(userId: userId);
for (final file in files) {
File localFile;
if (file.path.startsWith(consumptionDirectory.path)) {
localFile = file;
} else {
final fileName = p.basename(file.path);
localFile = File(p.join(consumptionDirectory.path, fileName));
await file.copy(localFile.path);
}
}
return loadFromConsumptionDirectory(userId: userId);
}
/// Marks a file as processed by removing it from the queue and deleting the local copy of the file.
Future<void> discardFile(
File file, {
required String userId,
}) async {
final consumptionDirectory =
await FileService.getConsumptionDirectory(userId: userId);
if (file.path.startsWith(consumptionDirectory.path)) {
await file.delete();
}
return loadFromConsumptionDirectory(userId: userId);
}
/// Returns the next file to process of null if no file exists.
Future<File?> getNextFile({required String userId}) async {
final files = await _getCurrentFiles(userId);
if (files.isEmpty) {
return null;
}
return files.first;
}
Future<List<File>> _getCurrentFiles(String userId) async {
final directory = await FileService.getConsumptionDirectory(userId: userId);
final files = await FileService.getAllFiles(directory);
return files;
}
}