mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-08 06:07:48 -06:00
feat: Refactor export functionality, add new translations
This commit is contained in:
@@ -41,8 +41,11 @@ class DocumentScannerCubit extends Cubit<List<File>> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveLocally(
|
||||
Uint8List bytes, String fileName, String preferredLocaleSubtag) async {
|
||||
Future<void> saveToFile(
|
||||
Uint8List bytes,
|
||||
String fileName,
|
||||
String preferredLocaleSubtag,
|
||||
) async {
|
||||
var file = await FileService.saveToFile(bytes, fileName);
|
||||
_notificationService.notifyFileSaved(
|
||||
filename: fileName,
|
||||
|
||||
@@ -7,22 +7,19 @@ import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/constants.dart';
|
||||
import 'package:paperless_mobile/core/bloc/connectivity_cubit.dart';
|
||||
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
|
||||
import 'package:paperless_mobile/core/database/tables/global_settings.dart';
|
||||
import 'package:paperless_mobile/core/delegate/customizable_sliver_persistent_header_delegate.dart';
|
||||
import 'package:paperless_mobile/core/global/constants.dart';
|
||||
import 'package:paperless_mobile/core/navigation/push_routes.dart';
|
||||
import 'package:paperless_mobile/core/service/file_description.dart';
|
||||
import 'package:paperless_mobile/core/service/file_service.dart';
|
||||
import 'package:paperless_mobile/core/widgets/dialog_utils/dialog_cancel_button.dart';
|
||||
import 'package:paperless_mobile/core/widgets/dialog_utils/dialog_confirm_button.dart';
|
||||
import 'package:paperless_mobile/features/app_drawer/view/app_drawer.dart';
|
||||
import 'package:paperless_mobile/features/document_scan/cubit/document_scanner_cubit.dart';
|
||||
import 'package:paperless_mobile/features/document_scan/view/widgets/export_scans_dialog.dart';
|
||||
import 'package:paperless_mobile/features/document_scan/view/widgets/scanned_image_item.dart';
|
||||
import 'package:paperless_mobile/features/document_search/view/sliver_search_bar.dart';
|
||||
import 'package:paperless_mobile/features/documents/view/pages/document_view.dart';
|
||||
@@ -34,6 +31,7 @@ import 'package:path/path.dart' as p;
|
||||
import 'package:pdf/pdf.dart';
|
||||
import 'package:pdf/widgets.dart' as pw;
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:sliver_tools/sliver_tools.dart';
|
||||
|
||||
class ScannerPage extends StatefulWidget {
|
||||
const ScannerPage({Key? key}) : super(key: key);
|
||||
@@ -44,13 +42,12 @@ class ScannerPage extends StatefulWidget {
|
||||
|
||||
class _ScannerPageState extends State<ScannerPage>
|
||||
with SingleTickerProviderStateMixin {
|
||||
static const fkFileName = "filename";
|
||||
|
||||
final SliverOverlapAbsorberHandle searchBarHandle =
|
||||
SliverOverlapAbsorberHandle();
|
||||
final SliverOverlapAbsorberHandle actionsHandle =
|
||||
SliverOverlapAbsorberHandle();
|
||||
final _downloadFormKey = GlobalKey<FormBuilderState>();
|
||||
|
||||
final _scrollController = ScrollController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -80,13 +77,8 @@ class _ScannerPageState extends State<ScannerPage>
|
||||
),
|
||||
SliverOverlapAbsorber(
|
||||
handle: actionsHandle,
|
||||
sliver: SliverPersistentHeader(
|
||||
pinned: true,
|
||||
delegate: CustomizableSliverPersistentHeaderDelegate(
|
||||
child: _buildActions(connectedState.isConnected),
|
||||
maxExtent: kTextTabBarHeight,
|
||||
minExtent: kTextTabBarHeight,
|
||||
),
|
||||
sliver: SliverPinnedHeader(
|
||||
child: _buildActions(connectedState.isConnected),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -121,151 +113,117 @@ class _ScannerPageState extends State<ScannerPage>
|
||||
color: Theme.of(context).colorScheme.background,
|
||||
child: SizedBox(
|
||||
height: kTextTabBarHeight,
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
BlocBuilder<DocumentScannerCubit, List<File>>(
|
||||
builder: (context, state) {
|
||||
return TextButton.icon(
|
||||
label: Text(S.of(context)!.previewScan),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.fromLTRB(5, 10, 5, 10),
|
||||
),
|
||||
onPressed: state.isNotEmpty
|
||||
? () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => DocumentView(
|
||||
documentBytes: _assembleFileBytes(
|
||||
state,
|
||||
forcePdf: true,
|
||||
).then((file) => file.bytes),
|
||||
child: BlocBuilder<DocumentScannerCubit, List<File>>(
|
||||
builder: (context, state) {
|
||||
return RawScrollbar(
|
||||
padding: EdgeInsets.fromLTRB(16, 0, 16, 4),
|
||||
interactive: false,
|
||||
thumbVisibility: true,
|
||||
thickness: 2,
|
||||
radius: Radius.circular(2),
|
||||
controller: _scrollController,
|
||||
child: ListView(
|
||||
controller: _scrollController,
|
||||
scrollDirection: Axis.horizontal,
|
||||
children: [
|
||||
SizedBox(width: 12),
|
||||
TextButton.icon(
|
||||
label: Text(S.of(context)!.previewScan),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.fromLTRB(5, 10, 5, 10),
|
||||
),
|
||||
onPressed: state.isNotEmpty
|
||||
? () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => DocumentView(
|
||||
documentBytes: _assembleFileBytes(
|
||||
state,
|
||||
forcePdf: true,
|
||||
).then((file) => file.bytes),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
icon: const Icon(Icons.visibility_outlined),
|
||||
);
|
||||
},
|
||||
),
|
||||
BlocBuilder<DocumentScannerCubit, List<File>>(
|
||||
builder: (context, state) {
|
||||
return TextButton.icon(
|
||||
label: Text(S.of(context)!.export),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.fromLTRB(5, 10, 5, 10),
|
||||
)
|
||||
: null,
|
||||
icon: const Icon(Icons.visibility_outlined),
|
||||
),
|
||||
onPressed: state.isEmpty
|
||||
? null
|
||||
: () {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(S.of(context)!.export),
|
||||
content: FormBuilder(
|
||||
key: _downloadFormKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: FormBuilderTextField(
|
||||
autovalidateMode:
|
||||
AutovalidateMode.always,
|
||||
validator: (value) {
|
||||
if (value?.trim().isEmpty ??
|
||||
true) {
|
||||
return S
|
||||
.of(context)!
|
||||
.thisFieldIsRequired;
|
||||
}
|
||||
if (value?.trim().contains(
|
||||
RegExp(
|
||||
r'[<>:"/|?*]')) ??
|
||||
true) {
|
||||
return S
|
||||
.of(context)!
|
||||
.invalidFilenameCharacter;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText:
|
||||
S.of(context)!.fileName,
|
||||
),
|
||||
name: fkFileName,
|
||||
)),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
const DialogCancelButton(),
|
||||
ElevatedButton(
|
||||
child: Text(S.of(context)!.export),
|
||||
style: ButtonStyle(
|
||||
backgroundColor:
|
||||
MaterialStatePropertyAll(
|
||||
Theme.of(context)
|
||||
.colorScheme
|
||||
.primaryContainer,
|
||||
),
|
||||
foregroundColor:
|
||||
MaterialStatePropertyAll(
|
||||
Theme.of(context)
|
||||
.colorScheme
|
||||
.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
onPressed: () => {
|
||||
if (_downloadFormKey.currentState!
|
||||
.validate())
|
||||
{
|
||||
_onLocalSave().then((value) =>
|
||||
Navigator.of(context).pop())
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
});
|
||||
},
|
||||
icon: const Icon(Icons.download_outlined),
|
||||
);
|
||||
},
|
||||
),
|
||||
BlocBuilder<DocumentScannerCubit, List<File>>(
|
||||
builder: (context, state) {
|
||||
return TextButton.icon(
|
||||
label: Text(S.of(context)!.clearAll),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.fromLTRB(5, 10, 5, 10),
|
||||
SizedBox(width: 8),
|
||||
TextButton.icon(
|
||||
label: Text(S.of(context)!.clearAll),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.fromLTRB(5, 10, 5, 10),
|
||||
),
|
||||
onPressed: state.isEmpty ? null : () => _reset(context),
|
||||
icon: const Icon(Icons.delete_sweep_outlined),
|
||||
),
|
||||
onPressed: state.isEmpty ? null : () => _reset(context),
|
||||
icon: const Icon(Icons.delete_sweep_outlined),
|
||||
);
|
||||
},
|
||||
),
|
||||
BlocBuilder<DocumentScannerCubit, List<File>>(
|
||||
builder: (context, state) {
|
||||
return TextButton.icon(
|
||||
label: Text(S.of(context)!.upload),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.fromLTRB(5, 10, 5, 10),
|
||||
SizedBox(width: 8),
|
||||
TextButton.icon(
|
||||
label: Text(S.of(context)!.upload),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.fromLTRB(5, 10, 5, 10),
|
||||
),
|
||||
onPressed: state.isEmpty || !isConnected
|
||||
? null
|
||||
: () => _onPrepareDocumentUpload(context),
|
||||
icon: const Icon(Icons.upload_outlined),
|
||||
),
|
||||
onPressed: state.isEmpty || !isConnected
|
||||
? null
|
||||
: () => _onPrepareDocumentUpload(context),
|
||||
icon: const Icon(Icons.upload_outlined),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
SizedBox(width: 8),
|
||||
TextButton.icon(
|
||||
label: Text(S.of(context)!.export),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.fromLTRB(5, 10, 5, 10),
|
||||
),
|
||||
onPressed: state.isEmpty ? null : _onSaveToFile,
|
||||
icon: const Icon(Icons.save_alt_outlined),
|
||||
),
|
||||
SizedBox(width: 12),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _onSaveToFile() async {
|
||||
final fileName = await showDialog<String>(
|
||||
context: context,
|
||||
builder: (context) => const ExportScansDialog(),
|
||||
);
|
||||
if (fileName != null) {
|
||||
final cubit = context.read<DocumentScannerCubit>();
|
||||
final file = await _assembleFileBytes(
|
||||
forcePdf: true,
|
||||
context.read<DocumentScannerCubit>().state,
|
||||
);
|
||||
try {
|
||||
final globalSettings =
|
||||
Hive.box<GlobalSettings>(HiveBoxes.globalSettings).getValue()!;
|
||||
if (Platform.isAndroid && androidInfo!.version.sdkInt <= 29) {
|
||||
final isGranted = await askForPermission(Permission.storage);
|
||||
if (!isGranted) {
|
||||
showSnackBar(
|
||||
context,
|
||||
"Please grant permissions for Paperless Mobile to access your filesystem.",
|
||||
action: SnackBarActionConfig(
|
||||
label: "GO",
|
||||
onPressed: openAppSettings,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await cubit.saveToFile(
|
||||
file.bytes,
|
||||
"$fileName.pdf",
|
||||
globalSettings.preferredLocaleSubtag,
|
||||
);
|
||||
} catch (error) {
|
||||
showGenericError(context, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _openDocumentScanner(BuildContext context) async {
|
||||
final isGranted = await askForPermission(Permission.camera);
|
||||
if (!isGranted) {
|
||||
@@ -311,34 +269,6 @@ class _ScannerPageState extends State<ScannerPage>
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onLocalSave() async {
|
||||
final cubit = context.read<DocumentScannerCubit>();
|
||||
final file = await _assembleFileBytes(
|
||||
forcePdf: true,
|
||||
context.read<DocumentScannerCubit>().state,
|
||||
);
|
||||
try {
|
||||
final globalSettings =
|
||||
Hive.box<GlobalSettings>(HiveBoxes.globalSettings).getValue()!;
|
||||
if (Platform.isAndroid && androidInfo!.version.sdkInt <= 29) {
|
||||
final isGranted = await askForPermission(Permission.storage);
|
||||
if (!isGranted) {
|
||||
return;
|
||||
//TODO: Ask user to grant permissions
|
||||
}
|
||||
}
|
||||
final name =
|
||||
_downloadFormKey.currentState?.fields[fkFileName]!.value as String;
|
||||
|
||||
var fileName = "$name.pdf";
|
||||
|
||||
await cubit.saveLocally(
|
||||
file.bytes, fileName, globalSettings.preferredLocaleSubtag);
|
||||
} catch (error) {
|
||||
showGenericError(context, error);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildEmptyState(bool isConnected, List<File> scans) {
|
||||
if (scans.isNotEmpty) {
|
||||
return _buildImageGrid(scans);
|
||||
@@ -369,34 +299,37 @@ class _ScannerPageState extends State<ScannerPage>
|
||||
}
|
||||
|
||||
Widget _buildImageGrid(List<File> scans) {
|
||||
return CustomScrollView(
|
||||
slivers: [
|
||||
SliverOverlapInjector(handle: searchBarHandle),
|
||||
SliverOverlapInjector(handle: actionsHandle),
|
||||
SliverGrid.builder(
|
||||
itemCount: scans.length,
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 3,
|
||||
childAspectRatio: 1 / sqrt(2),
|
||||
crossAxisSpacing: 10,
|
||||
mainAxisSpacing: 10,
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: CustomScrollView(
|
||||
slivers: [
|
||||
SliverOverlapInjector(handle: searchBarHandle),
|
||||
SliverOverlapInjector(handle: actionsHandle),
|
||||
SliverGrid.builder(
|
||||
itemCount: scans.length,
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 3,
|
||||
childAspectRatio: 1 / sqrt(2),
|
||||
crossAxisSpacing: 10,
|
||||
mainAxisSpacing: 10,
|
||||
),
|
||||
itemBuilder: (context, index) {
|
||||
return ScannedImageItem(
|
||||
file: scans[index],
|
||||
onDelete: () async {
|
||||
try {
|
||||
context.read<DocumentScannerCubit>().removeScan(index);
|
||||
} on PaperlessServerException catch (error, stackTrace) {
|
||||
showErrorMessage(context, error, stackTrace);
|
||||
}
|
||||
},
|
||||
index: index,
|
||||
totalNumberOfFiles: scans.length,
|
||||
);
|
||||
},
|
||||
),
|
||||
itemBuilder: (context, index) {
|
||||
return ScannedImageItem(
|
||||
file: scans[index],
|
||||
onDelete: () async {
|
||||
try {
|
||||
context.read<DocumentScannerCubit>().removeScan(index);
|
||||
} on PaperlessServerException catch (error, stackTrace) {
|
||||
showErrorMessage(context, error, stackTrace);
|
||||
}
|
||||
},
|
||||
index: index,
|
||||
totalNumberOfFiles: scans.length,
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:paperless_mobile/core/widgets/dialog_utils/dialog_cancel_button.dart';
|
||||
import 'package:paperless_mobile/core/widgets/dialog_utils/dialog_confirm_button.dart';
|
||||
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
|
||||
|
||||
class ExportScansDialog extends StatefulWidget {
|
||||
const ExportScansDialog({super.key});
|
||||
|
||||
@override
|
||||
State<ExportScansDialog> createState() => _ExportScansDialogState();
|
||||
}
|
||||
|
||||
class _ExportScansDialogState extends State<ExportScansDialog> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String? _filename;
|
||||
late String _placeholder;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final date = DateFormat("yyyy_MM_ddThhmmss").format(DateTime.now());
|
||||
_placeholder = "paperless_mobile_scan_$date";
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
insetPadding: EdgeInsets.all(8),
|
||||
title: Text(S.of(context)!.exportScansToPdf),
|
||||
content: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(S.of(context)!.allScansWillBeMerged),
|
||||
SizedBox(height: 16),
|
||||
TextFormField(
|
||||
onSaved: (newValue) {
|
||||
_filename = newValue;
|
||||
},
|
||||
autofocus: true,
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
validator: (value) {
|
||||
final matches = RegExp(r'[<>:"/\|?*]').allMatches(value!);
|
||||
if (matches.isNotEmpty) {
|
||||
final illegalCharacters = matches
|
||||
.map((match) => match.group(0))
|
||||
.toList()
|
||||
.toSet()
|
||||
.join(" ");
|
||||
return S
|
||||
.of(context)!
|
||||
.invalidFilenameCharacter(illegalCharacters);
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: S.of(context)!.fileName,
|
||||
errorMaxLines: 5,
|
||||
suffixText: ".pdf",
|
||||
hintText: _placeholder,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
const DialogCancelButton(),
|
||||
DialogConfirmButton(
|
||||
label: S.of(context)!.export,
|
||||
onPressed: () async {
|
||||
if (_formKey.currentState?.validate() ?? false) {
|
||||
_formKey.currentState?.save();
|
||||
final effectiveFilename = (_filename?.trim().isEmpty ?? true)
|
||||
? _placeholder
|
||||
: _filename;
|
||||
Navigator.pop(context, effectiveFilename);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ class _ScannedImageItemState extends State<ScannedImageItem> {
|
||||
width: double.infinity,
|
||||
height: 100,
|
||||
child: FittedBox(
|
||||
fit: BoxFit.fill,
|
||||
fit: BoxFit.cover,
|
||||
clipBehavior: Clip.antiAliasWithSaveLayer,
|
||||
alignment: Alignment.center,
|
||||
child: Image.file(
|
||||
|
||||
Reference in New Issue
Block a user