feat+fix: Add option to set default download/share file type, fix typo in filter to query string, disable unused options in document filter

This commit is contained in:
Anton Stubenbord
2023-04-28 20:45:47 +02:00
parent 14c850ece6
commit bea0ab94be
20 changed files with 337 additions and 197 deletions

View File

@@ -62,6 +62,7 @@ class _BulkEditLabelBottomSheetState<T extends Label> extends State<BulkEditLabe
options: widget.availableOptionsSelector(state),
labelText: widget.formFieldLabel,
prefixIcon: widget.formFieldPrefixIcon,
allowSelectUnassigned: true,
),
),
const SizedBox(height: 8),

View File

@@ -1,25 +1,70 @@
import 'package:flutter/material.dart';
import 'package:paperless_mobile/features/settings/view/widgets/radio_settings_dialog.dart';
import 'package:paperless_mobile/core/widgets/dialog_utils/dialog_cancel_button.dart';
import 'package:paperless_mobile/features/settings/model/file_download_type.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
class SelectFileTypeDialog extends StatelessWidget {
const SelectFileTypeDialog({super.key});
class SelectFileTypeDialog extends StatefulWidget {
final void Function(FileDownloadType downloadType) onRememberSelection;
const SelectFileTypeDialog({super.key, required this.onRememberSelection});
@override
State<SelectFileTypeDialog> createState() => _SelectFileTypeDialogState();
}
class _SelectFileTypeDialogState extends State<SelectFileTypeDialog> {
bool _rememberSelection = false;
FileDownloadType _downloadType = FileDownloadType.original;
@override
Widget build(BuildContext context) {
return RadioSettingsDialog(
titleText: S.of(context)!.chooseFiletype,
options: [
RadioOption(
value: true,
label: S.of(context)!.original,
),
RadioOption(
value: false,
label: S.of(context)!.archivedPdf,
return AlertDialog(
title: Text(S.of(context)!.chooseFiletype),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
RadioListTile(
value: FileDownloadType.original,
groupValue: _downloadType,
onChanged: (value) {
if (value != null) {
setState(() => _downloadType = value);
}
},
title: Text(S.of(context)!.original),
),
RadioListTile(
value: FileDownloadType.archived,
groupValue: _downloadType,
onChanged: (value) {
if (value != null) {
setState(() => _downloadType = value);
}
},
title: Text(S.of(context)!.archivedPdf),
),
Divider(),
CheckboxListTile(
controlAffinity: ListTileControlAffinity.leading,
value: _rememberSelection,
onChanged: (value) => setState(() => _rememberSelection = value ?? false),
title: Text(
"Remember my decision",
style: Theme.of(context).textTheme.labelMedium,
), //TODO: INTL
),
],
),
actions: [
const DialogCancelButton(),
ElevatedButton(
child: Text(S.of(context)!.select),
onPressed: () {
if (_rememberSelection) {
widget.onRememberSelection(_downloadType);
}
Navigator.of(context).pop(_downloadType == FileDownloadType.original);
},
),
],
initialValue: false,
);
}
}

View File

@@ -1,4 +1,3 @@
import 'package:badges/badges.dart' as b;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:open_filex/open_filex.dart';

View File

@@ -1,11 +1,14 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
import 'package:paperless_mobile/extensions/flutter_extensions.dart';
import 'package:paperless_mobile/features/document_details/cubit/document_details_cubit.dart';
import 'package:paperless_mobile/features/document_details/view/dialogs/select_file_type_dialog.dart';
import 'package:paperless_mobile/core/database/tables/global_settings.dart';
import 'package:paperless_mobile/features/settings/model/file_download_type.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
import 'package:paperless_mobile/helpers/message_helpers.dart';
@@ -50,25 +53,46 @@ class _DocumentDownloadButtonState extends State<DocumentDownloadButton> {
Future<void> _onDownload(DocumentModel document) async {
try {
final downloadOriginal = await showDialog<bool>(
context: context,
builder: (context) => const SelectFileTypeDialog(),
);
if (downloadOriginal == null) {
// Download was cancelled
return;
final globalSettings = Hive.box<GlobalSettings>(HiveBoxes.globalSettings).getValue()!;
bool original;
switch (globalSettings.defaultDownloadType) {
case FileDownloadType.original:
original = true;
break;
case FileDownloadType.archived:
original = false;
break;
case FileDownloadType.alwaysAsk:
final isOriginal = await showDialog<bool>(
context: context,
builder: (context) => SelectFileTypeDialog(
onRememberSelection: (downloadType) {
globalSettings.defaultDownloadType = downloadType;
globalSettings.save();
},
),
);
if (isOriginal == null) {
return;
} else {
original = isOriginal;
}
break;
}
if (Platform.isAndroid && androidInfo!.version.sdkInt! <= 29) {
final isGranted = await askForPermission(Permission.storage);
if (!isGranted) {
return;
//TODO: Tell user to grant permissions
//TODO: Ask user to grant permissions
}
}
setState(() => _isDownloadPending = true);
await context.read<DocumentDetailsCubit>().downloadDocument(
downloadOriginal: downloadOriginal,
locale: context.read<GlobalSettings>().preferredLocaleSubtag,
downloadOriginal: original,
locale: globalSettings.preferredLocaleSubtag,
);
// showSnackBar(context, S.of(context)!.documentSuccessfullyDownloaded);
} on PaperlessServerException catch (error, stackTrace) {

View File

@@ -1,11 +1,15 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/constants.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/extensions/flutter_extensions.dart';
import 'package:paperless_mobile/features/document_details/cubit/document_details_cubit.dart';
import 'package:paperless_mobile/features/document_details/view/dialogs/select_file_type_dialog.dart';
import 'package:paperless_mobile/features/settings/model/file_download_type.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
import 'package:paperless_mobile/helpers/message_helpers.dart';
import 'package:paperless_mobile/helpers/permission_helpers.dart';
@@ -39,22 +43,41 @@ class _DocumentShareButtonState extends State<DocumentShareButton> {
child: CircularProgressIndicator(),
)
: const Icon(Icons.share),
onPressed: widget.document != null && widget.enabled
? () => _onShare(widget.document!)
: null,
onPressed:
widget.document != null && widget.enabled ? () => _onShare(widget.document!) : null,
).paddedOnly(right: 4);
}
Future<void> _onShare(DocumentModel document) async {
try {
final shareOriginal = await showDialog<bool>(
context: context,
builder: (context) => const SelectFileTypeDialog(),
);
if (shareOriginal == null) {
// Download was cancelled
return;
final globalSettings = Hive.box<GlobalSettings>(HiveBoxes.globalSettings).getValue()!;
bool original;
switch (globalSettings.defaultShareType) {
case FileDownloadType.original:
original = true;
break;
case FileDownloadType.archived:
original = false;
break;
case FileDownloadType.alwaysAsk:
final isOriginal = await showDialog<bool>(
context: context,
builder: (context) => SelectFileTypeDialog(
onRememberSelection: (downloadType) {
globalSettings.defaultShareType = downloadType;
globalSettings.save();
},
),
);
if (isOriginal == null) {
return;
} else {
original = isOriginal;
}
break;
}
if (Platform.isAndroid && androidInfo!.version.sdkInt! < 30) {
final isGranted = await askForPermission(Permission.storage);
if (!isGranted) {
@@ -62,9 +85,9 @@ class _DocumentShareButtonState extends State<DocumentShareButton> {
}
}
setState(() => _isDownloadPending = true);
await context
.read<DocumentDetailsCubit>()
.shareDocument(shareOriginal: shareOriginal);
await context.read<DocumentDetailsCubit>().shareDocument(
shareOriginal: original,
);
} on PaperlessServerException catch (error, stackTrace) {
showErrorMessage(context, error, stackTrace);
} catch (error) {

View File

@@ -116,6 +116,7 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
: const IdQueryParameter.unset(),
name: fkCorrespondent,
prefixIcon: const Icon(Icons.person_outlined),
allowSelectUnassigned: true,
),
if (_filteredSuggestions?.hasSuggestedCorrespondents ?? false)
_buildSuggestionsSkeleton<int>(
@@ -151,6 +152,7 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
options: state.documentTypes,
name: _DocumentEditPageState.fkDocumentType,
prefixIcon: const Icon(Icons.description_outlined),
allowSelectUnassigned: true,
),
if (_filteredSuggestions?.hasSuggestedDocumentTypes ?? false)
_buildSuggestionsSkeleton<int>(
@@ -183,6 +185,7 @@ class _DocumentEditPageState extends State<DocumentEditPage> {
: const IdQueryParameter.unset(),
name: fkStoragePath,
prefixIcon: const Icon(Icons.folder_outlined),
allowSelectUnassigned: true,
),
],
).padded(),

View File

@@ -191,6 +191,7 @@ class _DocumentUploadPreparationPageState extends State<DocumentUploadPreparatio
name: DocumentModel.correspondentKey,
options: state.correspondents,
prefixIcon: const Icon(Icons.person_outline),
allowSelectUnassigned: true,
),
// Document type
LabelFormField<DocumentType>(
@@ -205,6 +206,7 @@ class _DocumentUploadPreparationPageState extends State<DocumentUploadPreparatio
name: DocumentModel.documentTypeKey,
options: state.documentTypes,
prefixIcon: const Icon(Icons.description_outlined),
allowSelectUnassigned: true,
),
TagsFormField(
name: DocumentModel.tagsKey,

View File

@@ -24,17 +24,14 @@ class DocumentFilterForm extends StatefulWidget {
formKey.currentState?.save();
final v = formKey.currentState!.value;
return DocumentFilter(
correspondent:
v[DocumentFilterForm.fkCorrespondent] as IdQueryParameter? ??
DocumentFilter.initial.correspondent,
correspondent: v[DocumentFilterForm.fkCorrespondent] as IdQueryParameter? ??
DocumentFilter.initial.correspondent,
documentType: v[DocumentFilterForm.fkDocumentType] as IdQueryParameter? ??
DocumentFilter.initial.documentType,
storagePath: v[DocumentFilterForm.fkStoragePath] as IdQueryParameter? ??
DocumentFilter.initial.storagePath,
tags:
v[DocumentModel.tagsKey] as TagsQuery? ?? DocumentFilter.initial.tags,
query: v[DocumentFilterForm.fkQuery] as TextQuery? ??
DocumentFilter.initial.query,
tags: v[DocumentModel.tagsKey] as TagsQuery? ?? DocumentFilter.initial.tags,
query: v[DocumentFilterForm.fkQuery] as TextQuery? ?? DocumentFilter.initial.query,
created: (v[DocumentFilterForm.fkCreatedAt] as DateRangeQuery),
added: (v[DocumentFilterForm.fkAddedAt] as DateRangeQuery),
asnQuery: initialFilter.asnQuery,
@@ -139,15 +136,12 @@ class _DocumentFilterFormState extends State<DocumentFilterForm> {
}
void _checkQueryConstraints() {
final filter =
DocumentFilterForm.assembleFilter(widget.formKey, widget.initialFilter);
final filter = DocumentFilterForm.assembleFilter(widget.formKey, widget.initialFilter);
if (filter.forceExtendedQuery) {
setState(() => _allowOnlyExtendedQuery = true);
final queryField =
widget.formKey.currentState?.fields[DocumentFilterForm.fkQuery];
final queryField = widget.formKey.currentState?.fields[DocumentFilterForm.fkQuery];
queryField?.didChange(
(queryField.value as TextQuery?)
?.copyWith(queryType: QueryType.extended),
(queryField.value as TextQuery?)?.copyWith(queryType: QueryType.extended),
);
} else {
setState(() => _allowOnlyExtendedQuery = false);
@@ -161,6 +155,7 @@ class _DocumentFilterFormState extends State<DocumentFilterForm> {
labelText: S.of(context)!.documentType,
initialValue: widget.initialFilter.documentType,
prefixIcon: const Icon(Icons.description_outlined),
allowSelectUnassigned: false,
);
}
@@ -171,6 +166,7 @@ class _DocumentFilterFormState extends State<DocumentFilterForm> {
labelText: S.of(context)!.correspondent,
initialValue: widget.initialFilter.correspondent,
prefixIcon: const Icon(Icons.person_outline),
allowSelectUnassigned: false,
);
}
@@ -181,6 +177,7 @@ class _DocumentFilterFormState extends State<DocumentFilterForm> {
labelText: S.of(context)!.storagePath,
initialValue: widget.initialFilter.storagePath,
prefixIcon: const Icon(Icons.folder_outlined),
allowSelectUnassigned: false,
);
}

View File

@@ -15,6 +15,7 @@ class FullscreenLabelForm<T extends Label> extends StatefulWidget {
final Widget leadingIcon;
final String? addNewLabelText;
final bool autofocus;
final bool allowSelectUnassigned;
FullscreenLabelForm({
super.key,
@@ -27,6 +28,7 @@ class FullscreenLabelForm<T extends Label> extends StatefulWidget {
required this.leadingIcon,
this.addNewLabelText,
this.autofocus = true,
this.allowSelectUnassigned = true,
}) : assert(
!(initialValue?.isOnlyAssigned() ?? false) || showAnyAssignedOption,
),
@@ -248,16 +250,26 @@ class _FullscreenLabelFormState<T extends Label> extends State<FullscreenLabelFo
);
}
final title = option.whenOrNull(
notAssigned: () => S.of(context)!.notAssigned,
anyAssigned: () => S.of(context)!.anyAssigned,
fromId: (id) => widget.options[id]!.name,
return option.whenOrNull(
notAssigned: () => ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(S.of(context)!.notAssigned),
onTap: onTap,
),
anyAssigned: () => ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(S.of(context)!.anyAssigned),
onTap: onTap,
),
fromId: (id) => ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(widget.options[id]!.name),
onTap: onTap,
enabled: widget.allowSelectUnassigned ? true : widget.options[id]!.documentCount == 0,
),
)!; // Never null, since we already return on unset before
return ListTile(
selected: highlight,
selectedTileColor: Theme.of(context).focusColor,
title: Text(title),
onTap: onTap,
);
}
}

View File

@@ -27,6 +27,7 @@ class LabelFormField<T extends Label> extends StatelessWidget {
final bool showAnyAssignedOption;
final List<T> suggestions;
final String? addLabelText;
final bool allowSelectUnassigned;
const LabelFormField({
Key? key,
@@ -42,6 +43,7 @@ class LabelFormField<T extends Label> extends StatelessWidget {
this.showAnyAssignedOption = true,
this.suggestions = const [],
this.addLabelText,
required this.allowSelectUnassigned,
}) : super(key: key);
String _buildText(BuildContext context, IdQueryParameter? value) {
@@ -100,6 +102,7 @@ class LabelFormField<T extends Label> extends StatelessWidget {
),
),
openBuilder: (context, closeForm) => FullscreenLabelForm<T>(
allowSelectUnassigned: allowSelectUnassigned,
addNewLabelText: addLabelText,
leadingIcon: prefixIcon,
onCreateNewLabel: addLabelPageBuilder != null

View File

@@ -0,0 +1,14 @@
import 'package:hive/hive.dart';
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
part 'file_download_type.g.dart';
@HiveType(typeId: HiveTypeIds.fileDownloadType)
enum FileDownloadType {
@HiveField(1)
original,
@HiveField(2)
archived,
@HiveField(3)
alwaysAsk;
}

View File

@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:paperless_mobile/features/settings/view/widgets/color_scheme_option_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/default_download_file_type_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/default_share_file_type_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/language_selection_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/theme_mode_setting.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
@@ -12,10 +14,10 @@ class ApplicationSettingsPage extends StatelessWidget {
return Scaffold(
appBar: AppBar(
title: Text(S.of(context)!.applicationSettings),
actions: [
actions: const [
Padding(
padding: const EdgeInsets.all(16.0),
child: const Icon(Icons.public),
padding: EdgeInsets.all(16.0),
child: Icon(Icons.public),
)
],
),
@@ -24,6 +26,9 @@ class ApplicationSettingsPage extends StatelessWidget {
LanguageSelectionSetting(),
ThemeModeSetting(),
ColorSchemeOptionSetting(),
Divider(),
DefaultDownloadFileTypeSetting(),
DefaultShareFileTypeSetting(),
],
),
);

View File

@@ -0,0 +1,64 @@
import 'package:flutter/material.dart';
import 'package:paperless_mobile/features/settings/model/file_download_type.dart';
import 'package:paperless_mobile/features/settings/view/widgets/global_settings_builder.dart';
import 'package:paperless_mobile/features/settings/view/widgets/radio_settings_dialog.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
class DefaultDownloadFileTypeSetting extends StatelessWidget {
const DefaultDownloadFileTypeSetting({super.key});
@override
Widget build(BuildContext context) {
return GlobalSettingsBuilder(
builder: (context, settings) {
return ListTile(
title: Text("Default download file type"),
subtitle: Text(
_downloadFileTypeToString(context, settings.defaultDownloadType),
),
onTap: () async {
final selectedValue = await showDialog<FileDownloadType>(
context: context,
builder: (context) {
return RadioSettingsDialog<FileDownloadType>(
titleText: "Default download file type",
options: [
RadioOption(
value: FileDownloadType.alwaysAsk,
label: "Always ask",
),
RadioOption(
value: FileDownloadType.original,
label: S.of(context)!.original,
),
RadioOption(
value: FileDownloadType.archived,
label: S.of(context)!.archivedPdf,
),
],
initialValue: settings.defaultDownloadType,
);
},
);
if (selectedValue != null) {
settings
..defaultDownloadType = selectedValue
..save();
}
},
);
},
);
}
String _downloadFileTypeToString(BuildContext context, FileDownloadType type) {
switch (type) {
case FileDownloadType.original:
return S.of(context)!.original;
case FileDownloadType.archived:
return S.of(context)!.archivedPdf;
case FileDownloadType.alwaysAsk:
return "Always ask";
}
}
}

View File

@@ -0,0 +1,64 @@
import 'package:flutter/material.dart';
import 'package:paperless_mobile/features/settings/model/file_download_type.dart';
import 'package:paperless_mobile/features/settings/view/widgets/global_settings_builder.dart';
import 'package:paperless_mobile/features/settings/view/widgets/radio_settings_dialog.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
class DefaultShareFileTypeSetting extends StatelessWidget {
const DefaultShareFileTypeSetting({super.key});
@override
Widget build(BuildContext context) {
return GlobalSettingsBuilder(
builder: (context, settings) {
return ListTile(
title: Text("Default share file type"),
subtitle: Text(
_downloadFileTypeToString(context, settings.defaultShareType),
),
onTap: () async {
final selectedValue = await showDialog<FileDownloadType>(
context: context,
builder: (context) {
return RadioSettingsDialog<FileDownloadType>(
titleText: "Default share file type",
options: [
RadioOption(
value: FileDownloadType.alwaysAsk,
label: "Always ask",
),
RadioOption(
value: FileDownloadType.original,
label: S.of(context)!.original,
),
RadioOption(
value: FileDownloadType.archived,
label: S.of(context)!.archivedPdf,
),
],
initialValue: settings.defaultShareType,
);
},
);
if (selectedValue != null) {
settings
..defaultDownloadType = selectedValue
..save();
}
},
);
},
);
}
String _downloadFileTypeToString(BuildContext context, FileDownloadType type) {
switch (type) {
case FileDownloadType.original:
return S.of(context)!.original;
case FileDownloadType.archived:
return S.of(context)!.archivedPdf;
case FileDownloadType.alwaysAsk:
return "Always ask";
}
}
}

View File

@@ -51,8 +51,7 @@ class _RadioSettingsDialogState<T> extends State<RadioSettingsDialog<T>> {
mainAxisSize: MainAxisSize.min,
children: [
if (widget.descriptionText != null)
Text(widget.descriptionText!,
style: Theme.of(context).textTheme.bodySmall),
Text(widget.descriptionText!, style: Theme.of(context).textTheme.bodySmall),
...widget.options.map(_buildOptionListTile),
if (widget.footer != null) widget.footer!,
],