mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-08 18:08:07 -06:00
feat: Implement updated receive share logic
This commit is contained in:
105
lib/features/sharing/model/share_intent_queue.dart
Normal file
105
lib/features/sharing/model/share_intent_queue.dart
Normal file
@@ -0,0 +1,105 @@
|
||||
import 'dart:collection';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:paperless_mobile/core/config/hive/hive_extensions.dart';
|
||||
import 'package:paperless_mobile/core/service/file_service.dart';
|
||||
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
class ShareIntentQueue extends ChangeNotifier {
|
||||
final Map<String, Queue<File>> _queues = {};
|
||||
|
||||
ShareIntentQueue._();
|
||||
|
||||
static final instance = ShareIntentQueue._();
|
||||
|
||||
Future<void> initialize() async {
|
||||
final users = Hive.localUserAccountBox.values;
|
||||
for (final user in users) {
|
||||
final userId = user.id;
|
||||
debugPrint("Locating remaining files to be uploaded for $userId...");
|
||||
final consumptionDir =
|
||||
await FileService.getConsumptionDirectory(userId: userId);
|
||||
final files = await FileService.getAllFiles(consumptionDir);
|
||||
debugPrint(
|
||||
"Found ${files.length} files to be uploaded for $userId. Adding to queue...");
|
||||
getQueue(userId).addAll(files);
|
||||
}
|
||||
}
|
||||
|
||||
void add(
|
||||
File file, {
|
||||
required String userId,
|
||||
}) =>
|
||||
addAll([file], userId: userId);
|
||||
|
||||
Future<void> addAll(
|
||||
Iterable<File> files, {
|
||||
required String userId,
|
||||
}) async {
|
||||
if (files.isEmpty) {
|
||||
return;
|
||||
}
|
||||
final consumptionDirectory =
|
||||
await FileService.getConsumptionDirectory(userId: userId);
|
||||
final copiedFiles = await Future.wait([
|
||||
for (var file in files)
|
||||
file.copy('${consumptionDirectory.path}/${p.basename(file.path)}')
|
||||
]);
|
||||
|
||||
debugPrint(
|
||||
"Adding received files to queue: ${files.map((e) => e.path).join(",")}",
|
||||
);
|
||||
getQueue(userId).addAll(copiedFiles);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Removes and returns the first item in the requested user's queue if it exists.
|
||||
File? pop(String userId) {
|
||||
if (hasUnhandledFiles(userId: userId)) {
|
||||
final file = getQueue(userId).removeFirst();
|
||||
notifyListeners();
|
||||
return file;
|
||||
// Don't notify listeners, only when new item is added.
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> onConsumed(File file) {
|
||||
debugPrint(
|
||||
"File ${file.path} successfully consumed. Delelting local copy.");
|
||||
return file.delete();
|
||||
}
|
||||
|
||||
Future<void> discard(File file) {
|
||||
debugPrint("Discarding file ${file.path}.");
|
||||
return file.delete();
|
||||
}
|
||||
|
||||
/// Returns whether the queue of the requested user contains files waiting for processing.
|
||||
bool hasUnhandledFiles({
|
||||
required String userId,
|
||||
}) =>
|
||||
getQueue(userId).isNotEmpty;
|
||||
|
||||
int unhandledFileCount({
|
||||
required String userId,
|
||||
}) =>
|
||||
getQueue(userId).length;
|
||||
|
||||
Queue<File> getQueue(String userId) {
|
||||
if (!_queues.containsKey(userId)) {
|
||||
_queues[userId] = Queue<File>();
|
||||
}
|
||||
return _queues[userId]!;
|
||||
}
|
||||
}
|
||||
|
||||
class UserAwareShareMediaFile {
|
||||
final String userId;
|
||||
final SharedMediaFile sharedFile;
|
||||
|
||||
UserAwareShareMediaFile(this.userId, this.sharedFile);
|
||||
}
|
||||
Reference in New Issue
Block a user