From 2fad76c697fb5bb25d7e5ccce66bb700e053a045 Mon Sep 17 00:00:00 2001 From: Anton Stubenbord Date: Fri, 12 May 2023 13:00:56 +0200 Subject: [PATCH] fix: Add providers to bulk edit routes --- lib/core/navigation/push_routes.dart | 168 ++++++++++++++++++ lib/features/app_drawer/view/app_drawer.dart | 1 - .../documents/cubit/documents_state.dart | 3 +- .../documents/view/pages/documents_page.dart | 2 +- .../document_selection_sliver_app_bar.dart | 153 +--------------- lib/features/home/view/home_route.dart | 17 +- 6 files changed, 188 insertions(+), 156 deletions(-) diff --git a/lib/core/navigation/push_routes.dart b/lib/core/navigation/push_routes.dart index fc3e9d6..fbae8a3 100644 --- a/lib/core/navigation/push_routes.dart +++ b/lib/core/navigation/push_routes.dart @@ -10,6 +10,9 @@ import 'package:paperless_mobile/core/database/tables/local_user_app_state.dart' import 'package:paperless_mobile/core/notifier/document_changed_notifier.dart'; import 'package:paperless_mobile/core/repository/label_repository.dart'; import 'package:paperless_mobile/core/repository/user_repository.dart'; +import 'package:paperless_mobile/features/document_bulk_action/cubit/document_bulk_action_cubit.dart'; +import 'package:paperless_mobile/features/document_bulk_action/view/widgets/fullscreen_bulk_edit_label_page.dart'; +import 'package:paperless_mobile/features/document_bulk_action/view/widgets/fullscreen_bulk_edit_tags_widget.dart'; import 'package:paperless_mobile/features/document_search/cubit/document_search_cubit.dart'; import 'package:paperless_mobile/features/document_search/view/document_search_page.dart'; import 'package:paperless_mobile/features/home/view/model/api_version.dart'; @@ -20,6 +23,7 @@ import 'package:paperless_mobile/features/saved_view/cubit/saved_view_cubit.dart import 'package:paperless_mobile/features/saved_view/view/add_saved_view_page.dart'; import 'package:paperless_mobile/features/saved_view_details/cubit/saved_view_details_cubit.dart'; import 'package:paperless_mobile/features/saved_view_details/view/saved_view_details_page.dart'; +import 'package:paperless_mobile/generated/l10n/app_localizations.dart'; import 'package:paperless_mobile/routes/document_details_route.dart'; import 'package:provider/provider.dart'; @@ -161,3 +165,167 @@ Future pushLinkedDocumentsView(BuildContext context, {required DocumentFil ), ); } + +Future pushBulkEditCorrespondentRoute( + BuildContext context, { + required List selection, +}) { + return Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => MultiProvider( + providers: [ + ..._getRequiredBulkEditProviders(context), + ], + builder: (_, __) => BlocProvider( + create: (_) => DocumentBulkActionCubit( + context.read(), + context.read(), + context.read(), + selection: selection, + ), + child: BlocBuilder( + builder: (context, state) { + return FullscreenBulkEditLabelPage( + options: state.correspondents, + selection: state.selection, + labelMapper: (document) => document.correspondent, + leadingIcon: const Icon(Icons.person_outline), + hintText: S.of(context)!.startTyping, + onSubmit: context.read().bulkModifyCorrespondent, + assignMessageBuilder: (int count, String name) { + return S.of(context)!.bulkEditCorrespondentAssignMessage( + name, + count, + ); + }, + removeMessageBuilder: (int count) { + return S.of(context)!.bulkEditCorrespondentRemoveMessage(count); + }, + ); + }, + ), + ), + ), + ), + ); +} + +Future pushBulkEditStoragePathRoute( + BuildContext context, { + required List selection, +}) { + return Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => MultiProvider( + providers: [ + ..._getRequiredBulkEditProviders(context), + ], + builder: (_, __) => BlocProvider( + create: (_) => DocumentBulkActionCubit( + context.read(), + context.read(), + context.read(), + selection: selection, + ), + child: BlocBuilder( + builder: (context, state) { + return FullscreenBulkEditLabelPage( + options: state.storagePaths, + selection: state.selection, + labelMapper: (document) => document.storagePath, + leadingIcon: const Icon(Icons.folder_outlined), + hintText: S.of(context)!.startTyping, + onSubmit: context.read().bulkModifyStoragePath, + assignMessageBuilder: (int count, String name) { + return S.of(context)!.bulkEditStoragePathAssignMessage( + count, + name, + ); + }, + removeMessageBuilder: (int count) { + return S.of(context)!.bulkEditStoragePathRemoveMessage(count); + }, + ); + }, + ), + ), + ), + ), + ); +} + +Future pushBulkEditTagsRoute( + BuildContext context, { + required List selection, +}) { + return Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => MultiProvider( + providers: [ + ..._getRequiredBulkEditProviders(context), + ], + builder: (_, __) => BlocProvider( + create: (_) => DocumentBulkActionCubit( + context.read(), + context.read(), + context.read(), + selection: selection, + ), + child: Builder(builder: (context) { + return const FullscreenBulkEditTagsWidget(); + }), + ), + ), + ), + ); +} + +Future pushBulkEditDocumentTypeRoute(BuildContext context, + {required List selection}) { + return Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => MultiProvider( + providers: [ + ..._getRequiredBulkEditProviders(context), + ], + builder: (_, __) => BlocProvider( + create: (_) => DocumentBulkActionCubit( + context.read(), + context.read(), + context.read(), + selection: selection, + ), + child: BlocBuilder( + builder: (context, state) { + return FullscreenBulkEditLabelPage( + options: state.documentTypes, + selection: state.selection, + labelMapper: (document) => document.documentType, + leadingIcon: const Icon(Icons.description_outlined), + hintText: S.of(context)!.startTyping, + onSubmit: context.read().bulkModifyDocumentType, + assignMessageBuilder: (int count, String name) { + return S.of(context)!.bulkEditDocumentTypeAssignMessage( + count, + name, + ); + }, + removeMessageBuilder: (int count) { + return S.of(context)!.bulkEditDocumentTypeRemoveMessage(count); + }, + ); + }, + ), + ), + ), + ), + ); +} + +List _getRequiredBulkEditProviders(BuildContext context) { + return [ + Provider.value(value: context.read()), + Provider.value(value: context.read()), + Provider.value(value: context.read()), + ]; +} diff --git a/lib/features/app_drawer/view/app_drawer.dart b/lib/features/app_drawer/view/app_drawer.dart index 103e314..fb58374 100644 --- a/lib/features/app_drawer/view/app_drawer.dart +++ b/lib/features/app_drawer/view/app_drawer.dart @@ -17,7 +17,6 @@ class AppDrawer extends StatelessWidget { @override Widget build(BuildContext context) { return SafeArea( - top: true, child: Drawer( child: Column( children: [ diff --git a/lib/features/documents/cubit/documents_state.dart b/lib/features/documents/cubit/documents_state.dart index 516cc02..527d4d3 100644 --- a/lib/features/documents/cubit/documents_state.dart +++ b/lib/features/documents/cubit/documents_state.dart @@ -83,8 +83,7 @@ class DocumentsState extends DocumentPagingState { ); } - factory DocumentsState.fromJson(Map json) => - _$DocumentsStateFromJson(json); + factory DocumentsState.fromJson(Map json) => _$DocumentsStateFromJson(json); Map toJson() => _$DocumentsStateToJson(this); } diff --git a/lib/features/documents/view/pages/documents_page.dart b/lib/features/documents/view/pages/documents_page.dart index 75452b2..3e2199a 100644 --- a/lib/features/documents/view/pages/documents_page.dart +++ b/lib/features/documents/view/pages/documents_page.dart @@ -107,7 +107,7 @@ class _DocumentsPageState extends State with SingleTickerProvider }, builder: (context, connectivityState) { return SafeArea( - top: context.read().state.selection.isEmpty, + top: context.watch().state.selection.isEmpty, child: Scaffold( drawer: const AppDrawer(), floatingActionButton: BlocBuilder( diff --git a/lib/features/documents/view/widgets/selection/document_selection_sliver_app_bar.dart b/lib/features/documents/view/widgets/selection/document_selection_sliver_app_bar.dart index b3513de..30f67a5 100644 --- a/lib/features/documents/view/widgets/selection/document_selection_sliver_app_bar.dart +++ b/lib/features/documents/view/widgets/selection/document_selection_sliver_app_bar.dart @@ -1,10 +1,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:paperless_api/paperless_api.dart'; +import 'package:paperless_mobile/core/navigation/push_routes.dart'; import 'package:paperless_mobile/extensions/flutter_extensions.dart'; -import 'package:paperless_mobile/features/document_bulk_action/cubit/document_bulk_action_cubit.dart'; -import 'package:paperless_mobile/features/document_bulk_action/view/widgets/fullscreen_bulk_edit_label_page.dart'; -import 'package:paperless_mobile/features/document_bulk_action/view/widgets/fullscreen_bulk_edit_tags_widget.dart'; import 'package:paperless_mobile/features/documents/cubit/documents_cubit.dart'; import 'package:paperless_mobile/features/documents/view/widgets/selection/bulk_delete_confirmation_dialog.dart'; import 'package:paperless_mobile/generated/l10n/app_localizations.dart'; @@ -35,15 +33,12 @@ class DocumentSelectionSliverAppBar extends StatelessWidget { onPressed: () async { final shouldDelete = await showDialog( context: context, - builder: (context) => - BulkDeleteConfirmationDialog(state: state), + builder: (context) => BulkDeleteConfirmationDialog(state: state), ) ?? false; if (shouldDelete) { try { - await context - .read() - .bulkDelete(state.selection); + await context.read().bulkDelete(state.selection); showSnackBar( context, S.of(context)!.documentsSuccessfullyDeleted, @@ -66,140 +61,22 @@ class DocumentSelectionSliverAppBar extends StatelessWidget { ActionChip( label: Text(S.of(context)!.correspondent), avatar: const Icon(Icons.edit), - onPressed: () async { - await Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => BlocProvider( - create: (context) => DocumentBulkActionCubit( - context.read(), - context.read(), - context.read(), - selection: state.selection, - ), - child: BlocBuilder( - builder: (context, state) { - return FullscreenBulkEditLabelPage( - options: state.correspondents, - selection: state.selection, - labelMapper: (document) => document.correspondent, - leadingIcon: const Icon(Icons.person_outline), - hintText: S.of(context)!.startTyping, - onSubmit: context - .read() - .bulkModifyCorrespondent, - assignMessageBuilder: (int count, String name) { - return S - .of(context)! - .bulkEditCorrespondentAssignMessage( - name, - count, - ); - }, - removeMessageBuilder: (int count) { - return S - .of(context)! - .bulkEditCorrespondentRemoveMessage(count); - }, - ); - }, - ), - ), - ), - ); + onPressed: () { + pushBulkEditCorrespondentRoute(context, selection: state.selection); }, ).paddedOnly(left: 8, right: 4), ActionChip( label: Text(S.of(context)!.documentType), avatar: const Icon(Icons.edit), onPressed: () async { - await Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => BlocProvider( - create: (context) => DocumentBulkActionCubit( - context.read(), - context.read(), - context.read(), - selection: state.selection, - ), - child: BlocBuilder( - builder: (context, state) { - return FullscreenBulkEditLabelPage( - options: state.documentTypes, - selection: state.selection, - labelMapper: (document) => document.documentType, - leadingIcon: - const Icon(Icons.description_outlined), - hintText: S.of(context)!.startTyping, - onSubmit: context - .read() - .bulkModifyDocumentType, - assignMessageBuilder: (int count, String name) { - return S - .of(context)! - .bulkEditDocumentTypeAssignMessage( - count, - name, - ); - }, - removeMessageBuilder: (int count) { - return S - .of(context)! - .bulkEditDocumentTypeRemoveMessage(count); - }, - ); - }, - ), - ), - ), - ); + pushBulkEditDocumentTypeRoute(context, selection: state.selection); }, ).paddedOnly(left: 8, right: 4), ActionChip( label: Text(S.of(context)!.storagePath), avatar: const Icon(Icons.edit), onPressed: () async { - await Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => BlocProvider( - create: (context) => DocumentBulkActionCubit( - context.read(), - context.read(), - context.read(), - selection: state.selection, - ), - child: BlocBuilder( - builder: (context, state) { - return FullscreenBulkEditLabelPage( - options: state.storagePaths, - selection: state.selection, - labelMapper: (document) => document.storagePath, - leadingIcon: const Icon(Icons.folder_outlined), - hintText: S.of(context)!.startTyping, - onSubmit: context - .read() - .bulkModifyStoragePath, - assignMessageBuilder: (int count, String name) { - return S - .of(context)! - .bulkEditStoragePathAssignMessage( - count, - name, - ); - }, - removeMessageBuilder: (int count) { - return S - .of(context)! - .bulkEditStoragePathRemoveMessage(count); - }, - ); - }, - ), - ), - ), - ); + pushBulkEditStoragePathRoute(context, selection: state.selection); }, ).paddedOnly(left: 8, right: 4), _buildBulkEditTagsChip(context).paddedOnly(left: 4, right: 4), @@ -215,21 +92,7 @@ class DocumentSelectionSliverAppBar extends StatelessWidget { label: Text(S.of(context)!.tags), avatar: const Icon(Icons.edit), onPressed: () { - Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => BlocProvider( - create: (context) => DocumentBulkActionCubit( - context.read(), - context.read(), - context.read(), - selection: state.selection, - ), - child: Builder(builder: (context) { - return const FullscreenBulkEditTagsWidget(); - }), - ), - ), - ); + pushBulkEditTagsRoute(context, selection: state.selection); }, ); } diff --git a/lib/features/home/view/home_route.dart b/lib/features/home/view/home_route.dart index 0324c3c..03be9a3 100644 --- a/lib/features/home/view/home_route.dart +++ b/lib/features/home/view/home_route.dart @@ -109,13 +109,16 @@ class HomeRoute extends StatelessWidget { providers: [ ProxyProvider3( - update: (context, docApi, notifier, labelRepo, previous) => DocumentsCubit( - docApi, - notifier, - labelRepo, - Hive.box(HiveBoxes.localUserAppState) - .get(currentLocalUserId)!, - )..reload(), + update: (context, docApi, notifier, labelRepo, previous) { + print("UPDATED DOCUMENT CUBIT"); //TODO: REMOVE + return DocumentsCubit( + docApi, + notifier, + labelRepo, + Hive.box(HiveBoxes.localUserAppState) + .get(currentLocalUserId)!, + )..reload(); + }, ), Provider(create: (context) => DocumentScannerCubit()), ProxyProvider4