mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-09 16:07:57 -06:00
fix: Improve receiving shares
This commit is contained in:
@@ -28,13 +28,15 @@ class FileThumbnail extends StatefulWidget {
|
||||
|
||||
class _FileThumbnailState extends State<FileThumbnail> {
|
||||
late String? mimeType;
|
||||
|
||||
late final Future<Uint8List?> _fileBytes;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
mimeType = widget.file != null
|
||||
? mime.lookupMimeType(widget.file!.path)
|
||||
: mime.lookupMimeType('', headerBytes: widget.bytes);
|
||||
_fileBytes = widget.file?.readAsBytes().then(_convertPdfToPng) ??
|
||||
_convertPdfToPng(widget.bytes!);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -45,8 +47,7 @@ class _FileThumbnailState extends State<FileThumbnail> {
|
||||
height: widget.height,
|
||||
child: Center(
|
||||
child: FutureBuilder<Uint8List?>(
|
||||
future: widget.file?.readAsBytes().then(_convertPdfToPng) ??
|
||||
_convertPdfToPng(widget.bytes!),
|
||||
future: _fileBytes,
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData) {
|
||||
return const SizedBox.shrink();
|
||||
|
||||
@@ -10,15 +10,16 @@ import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
|
||||
import 'package:paperless_mobile/core/config/hive/hive_extensions.dart';
|
||||
import 'package:paperless_mobile/core/database/tables/local_user_account.dart';
|
||||
import 'package:paperless_mobile/core/service/connectivity_status_service.dart';
|
||||
import 'package:paperless_mobile/features/document_upload/view/document_upload_preparation_page.dart';
|
||||
import 'package:paperless_mobile/features/notifications/services/local_notification_service.dart';
|
||||
import 'package:paperless_mobile/features/sharing/cubit/receive_share_cubit.dart';
|
||||
import 'package:paperless_mobile/features/sharing/view/dialog/discard_shared_file_dialog.dart';
|
||||
import 'package:paperless_mobile/features/tasks/cubit/task_status_cubit.dart';
|
||||
import 'package:paperless_mobile/features/sharing/view/dialog/pending_files_info_dialog.dart';
|
||||
import 'package:paperless_mobile/features/tasks/model/pending_tasks_notifier.dart';
|
||||
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
|
||||
import 'package:paperless_mobile/helpers/message_helpers.dart';
|
||||
import 'package:paperless_mobile/routes/typed/branches/scanner_route.dart';
|
||||
import 'package:paperless_mobile/routes/typed/branches/upload_queue_route.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
|
||||
|
||||
@@ -39,57 +40,40 @@ class _UploadQueueShellState extends State<UploadQueueShell> {
|
||||
ReceiveSharingIntent.getInitialMedia().then(_onReceiveSharedFiles);
|
||||
_subscription =
|
||||
ReceiveSharingIntent.getMediaStream().listen(_onReceiveSharedFiles);
|
||||
context.read<PendingTasksNotifier>().addListener(_onTasksChanged);
|
||||
|
||||
// WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
||||
// context.read<ReceiveShareCubit>().loadFromConsumptionDirectory(
|
||||
// userId: context.read<LocalUserAccount>().id,
|
||||
// );
|
||||
// final state = context.read<ReceiveShareCubit>().state;
|
||||
// print("Current state is " + state.toString());
|
||||
// final files = state.files;
|
||||
// if (files.isNotEmpty) {
|
||||
// showSnackBar(
|
||||
// WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
// final notifier = context.read<ConsumptionChangeNotifier>();
|
||||
// await notifier.isInitialized;
|
||||
// final pendingFiles = notifier.pendingFiles;
|
||||
// if (pendingFiles.isEmpty) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// final shouldProcess = await showDialog<bool>(
|
||||
// context: context,
|
||||
// builder: (context) =>
|
||||
// PendingFilesInfoDialog(pendingFiles: pendingFiles),
|
||||
// ) ??
|
||||
// false;
|
||||
// if (shouldProcess) {
|
||||
// final userId = context.read<LocalUserAccount>().id;
|
||||
// await consumeLocalFiles(
|
||||
// context,
|
||||
// "You have ${files.length} shared files waiting to be uploaded.",
|
||||
// action: SnackBarActionConfig(
|
||||
// label: "Show me",
|
||||
// onPressed: () {
|
||||
// UploadQueueRoute().push(context);
|
||||
// },
|
||||
// ),
|
||||
// files: pendingFiles,
|
||||
// userId: userId,
|
||||
// );
|
||||
// // showDialog(
|
||||
// // context: context,
|
||||
// // builder: (context) => AlertDialog(
|
||||
// // title: Text("Pending files"),
|
||||
// // content: Text(
|
||||
// // "You have ${files.length} files waiting to be uploaded.",
|
||||
// // ),
|
||||
// // actions: [
|
||||
// // TextButton(
|
||||
// // child: Text(S.of(context)!.gotIt),
|
||||
// // onPressed: () {
|
||||
// // Navigator.pop(context);
|
||||
// // UploadQueueRoute().push(context);
|
||||
// // },
|
||||
// // ),
|
||||
// // ],
|
||||
// // ),
|
||||
// // );
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
context.read<PendingTasksNotifier>().addListener(_onTasksChanged);
|
||||
}
|
||||
|
||||
void _onTasksChanged() {
|
||||
final taskNotifier = context.read<PendingTasksNotifier>();
|
||||
final userId = context.read<LocalUserAccount>().id;
|
||||
for (var task in taskNotifier.value.values) {
|
||||
context.read<LocalNotificationService>().notifyTaskChanged(task);
|
||||
context
|
||||
.read<LocalNotificationService>()
|
||||
.notifyTaskChanged(task, userId: userId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,23 +87,18 @@ class _UploadQueueShellState extends State<UploadQueueShell> {
|
||||
files: files,
|
||||
userId: userId,
|
||||
);
|
||||
final localFiles = notifier.pendingFiles;
|
||||
for (int i = 0; i < localFiles.length; i++) {
|
||||
final file = localFiles[i];
|
||||
await consumeLocalFile(
|
||||
context,
|
||||
file: file,
|
||||
userId: userId,
|
||||
exitAppAfterConsumed: i == localFiles.length - 1,
|
||||
);
|
||||
}
|
||||
consumeLocalFiles(
|
||||
context,
|
||||
files: files,
|
||||
userId: userId,
|
||||
exitAppAfterConsumed: true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription?.cancel();
|
||||
context.read<PendingTasksNotifier>().removeListener(_onTasksChanged);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -135,28 +114,43 @@ Future<void> consumeLocalFile(
|
||||
required String userId,
|
||||
bool exitAppAfterConsumed = false,
|
||||
}) async {
|
||||
final filename = p.basename(file.path);
|
||||
final hasInternetConnection =
|
||||
await context.read<ConnectivityStatusService>().isConnectedToInternet();
|
||||
if (!hasInternetConnection) {
|
||||
showSnackBar(
|
||||
context,
|
||||
"Could not consume $filename", //TODO: INTL
|
||||
details: S.of(context)!.youreOffline,
|
||||
);
|
||||
return;
|
||||
}
|
||||
final consumptionNotifier = context.read<ConsumptionChangeNotifier>();
|
||||
final taskNotifier = context.read<PendingTasksNotifier>();
|
||||
final ioFile = File(file.path);
|
||||
// if (!await ioFile.exists()) {
|
||||
// Fluttertoast.showToast(
|
||||
// msg: S.of(context)!.couldNotAccessReceivedFile,
|
||||
// toastLength: Toast.LENGTH_LONG,
|
||||
// );
|
||||
// }
|
||||
|
||||
final bytes = ioFile.readAsBytes();
|
||||
final bytes = file.readAsBytes();
|
||||
final shouldDirectlyUpload =
|
||||
Hive.globalSettingsBox.getValue()!.skipDocumentPreprarationOnUpload;
|
||||
if (shouldDirectlyUpload) {
|
||||
final taskId = await context.read<PaperlessDocumentsApi>().create(
|
||||
await bytes,
|
||||
filename: p.basename(file.path),
|
||||
title: p.basenameWithoutExtension(file.path),
|
||||
);
|
||||
consumptionNotifier.discardFile(file, userId: userId);
|
||||
if (taskId != null) {
|
||||
taskNotifier.listenToTaskChanges(taskId);
|
||||
try {
|
||||
final taskId = await context.read<PaperlessDocumentsApi>().create(
|
||||
await bytes,
|
||||
filename: filename,
|
||||
title: p.basenameWithoutExtension(file.path),
|
||||
);
|
||||
consumptionNotifier.discardFile(file, userId: userId);
|
||||
if (taskId != null) {
|
||||
taskNotifier.listenToTaskChanges(taskId);
|
||||
}
|
||||
} catch (error) {
|
||||
await Fluttertoast.showToast(
|
||||
msg: S.of(context)!.couldNotUploadDocument,
|
||||
);
|
||||
return;
|
||||
} finally {
|
||||
if (exitAppAfterConsumed) {
|
||||
SystemNavigator.pop();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
final result = await DocumentUploadRoute(
|
||||
@@ -193,3 +187,20 @@ Future<void> consumeLocalFile(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> consumeLocalFiles(
|
||||
BuildContext context, {
|
||||
required List<File> files,
|
||||
required String userId,
|
||||
bool exitAppAfterConsumed = false,
|
||||
}) async {
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
final file = files[i];
|
||||
await consumeLocalFile(
|
||||
context,
|
||||
file: file,
|
||||
userId: userId,
|
||||
exitAppAfterConsumed: exitAppAfterConsumed && (i == files.length - 1),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user