From 79ccdd0946519b990554e3049342455d934ccf16 Mon Sep 17 00:00:00 2001 From: Anton Stubenbord Date: Wed, 5 Apr 2023 19:44:58 +0200 Subject: [PATCH] fix: Refactor labels structure --- .../notifier/document_changed_notifier.dart | 6 +- lib/core/repository/label_repository.dart | 15 +++- .../cubit/document_bulk_action_cubit.dart | 8 +- .../cubit/document_details_cubit.dart | 6 +- .../view/pages/document_details_page.dart | 23 +++--- .../cubit/document_edit_cubit.dart | 8 +- .../view/document_edit_page.dart | 36 +++++---- .../document_scan/view/scanner_page.dart | 49 +++++------- .../cubit/document_search_cubit.dart | 17 +++- .../cubit/document_search_state.dart | 22 ++++++ .../view/document_search_page.dart | 5 ++ .../cubit/document_upload_cubit.dart | 46 ++++------- .../document_upload_preparation_page.dart | 12 ++- .../documents/cubit/documents_cubit.dart | 18 ++--- .../documents/view/pages/documents_page.dart | 39 ++++++--- .../widgets/items/document_detailed_item.dart | 6 +- .../widgets/search/document_filter_form.dart | 79 +++++++++---------- .../widgets/search/document_filter_panel.dart | 19 ++++- .../sort_field_selection_bottom_sheet.dart | 43 +++++----- .../view/widgets/sort_documents_button.dart | 4 + .../edit_label/cubit/edit_label_cubit.dart | 4 +- lib/features/home/view/home_page.dart | 8 +- lib/features/inbox/cubit/inbox_cubit.dart | 6 +- lib/features/labels/cubit/label_cubit.dart | 4 +- .../cubit/linked_documents_cubit.dart | 4 +- .../cubit/document_paging_bloc_mixin.dart | 2 +- .../saved_view/cubit/saved_view_cubit.dart | 4 +- .../saved_view/view/add_saved_view_page.dart | 17 +++- .../cubit/saved_view_details_cubit.dart | 2 +- .../cubit/saved_view_details_state.dart | 21 +++++ .../view/saved_view_details_page.dart | 4 + .../cubit/similar_documents_cubit.dart | 26 +++++- .../cubit/similar_documents_state.dart | 21 +++++ .../view/similar_documents_view.dart | 4 + lib/main.dart | 3 +- 35 files changed, 357 insertions(+), 234 deletions(-) diff --git a/lib/core/notifier/document_changed_notifier.dart b/lib/core/notifier/document_changed_notifier.dart index c53dedc..392d1bb 100644 --- a/lib/core/notifier/document_changed_notifier.dart +++ b/lib/core/notifier/document_changed_notifier.dart @@ -23,8 +23,8 @@ class DocumentChangedNotifier { _deleted.add(deleted); } - void subscribe( - dynamic subscriber, { + void addListener( + Object subscriber, { DocumentChangedCallback? onUpdated, DocumentChangedCallback? onDeleted, }) { @@ -41,7 +41,7 @@ class DocumentChangedNotifier { ); } - void unsubscribe(dynamic subscriber) { + void removeListener(Object subscriber) { _subscribers[subscriber]?.forEach((element) { element.cancel(); }); diff --git a/lib/core/repository/label_repository.dart b/lib/core/repository/label_repository.dart index 441993e..074de84 100644 --- a/lib/core/repository/label_repository.dart +++ b/lib/core/repository/label_repository.dart @@ -8,7 +8,9 @@ class LabelRepository extends HydratedCubit { final PaperlessLabelsApi _api; final Map _subscribers = {}; - void subscribe( + LabelRepository(this._api) : super(const LabelRepositoryState()); + + void addListener( Object source, { required void Function(LabelRepositoryState) onChanged, }) { @@ -18,12 +20,19 @@ class LabelRepository extends HydratedCubit { }); } - void unsubscribe(Object source) async { + void removeListener(Object source) async { await _subscribers[source]?.cancel(); _subscribers.remove(source); } - LabelRepository(this._api) : super(const LabelRepositoryState()); + Future initialize() { + return Future.wait([ + findAllCorrespondents(), + findAllDocumentTypes(), + findAllStoragePaths(), + findAllTags(), + ]); + } Future createTag(Tag object) async { final created = await _api.saveTag(object); diff --git a/lib/features/document_bulk_action/cubit/document_bulk_action_cubit.dart b/lib/features/document_bulk_action/cubit/document_bulk_action_cubit.dart index 063d8a4..90cf129 100644 --- a/lib/features/document_bulk_action/cubit/document_bulk_action_cubit.dart +++ b/lib/features/document_bulk_action/cubit/document_bulk_action_cubit.dart @@ -30,7 +30,7 @@ class DocumentBulkActionCubit extends Cubit { tags: _labelRepository.state.tags, ), ) { - _notifier.subscribe( + _notifier.addListener( this, onDeleted: (document) { // Remove items from internal selection after the document was deleted. @@ -43,7 +43,7 @@ class DocumentBulkActionCubit extends Cubit { ); }, ); - _labelRepository.subscribe( + _labelRepository.addListener( this, onChanged: (labels) { emit( @@ -142,8 +142,8 @@ class DocumentBulkActionCubit extends Cubit { @override Future close() { - _notifier.unsubscribe(this); - _labelRepository.unsubscribe(this); + _notifier.removeListener(this); + _labelRepository.removeListener(this); return super.close(); } } diff --git a/lib/features/document_details/cubit/document_details_cubit.dart b/lib/features/document_details/cubit/document_details_cubit.dart index 2b4ca23..3f6c76b 100644 --- a/lib/features/document_details/cubit/document_details_cubit.dart +++ b/lib/features/document_details/cubit/document_details_cubit.dart @@ -31,8 +31,8 @@ class DocumentDetailsCubit extends Cubit { }) : super(DocumentDetailsState( document: initialDocument, )) { - _notifier.subscribe(this, onUpdated: replace); - _labelRepository.subscribe( + _notifier.addListener(this, onUpdated: replace); + _labelRepository.addListener( this, onChanged: (labels) => emit( state.copyWith( @@ -210,7 +210,7 @@ class DocumentDetailsCubit extends Cubit { for (final element in _subscriptions) { await element.cancel(); } - _notifier.unsubscribe(this); + _notifier.removeListener(this); await super.close(); } } diff --git a/lib/features/document_details/view/pages/document_details_page.dart b/lib/features/document_details/view/pages/document_details_page.dart index 0da1044..629b5c8 100644 --- a/lib/features/document_details/view/pages/document_details_page.dart +++ b/lib/features/document_details/view/pages/document_details_page.dart @@ -176,6 +176,7 @@ class _DocumentDetailsPageState extends State { builder: (context, state) { return BlocProvider( create: (context) => SimilarDocumentsCubit( + context.read(), context.read(), context.read(), documentId: state.document.id, @@ -186,7 +187,10 @@ class _DocumentDetailsPageState extends State { document: state.document, itemSpacing: _itemSpacing, queryString: widget.titleAndContentQueryString, - + availableCorrespondents: state.correspondents, + availableDocumentTypes: state.documentTypes, + availableTags: state.tags, + availableStoragePaths: state.storagePaths, ), DocumentContentWidget( isFullContentLoaded: state.isFullContentLoaded, @@ -215,7 +219,7 @@ class _DocumentDetailsPageState extends State { return BlocBuilder( builder: (context, state) { final _filteredSuggestions = - state.suggestions.documentDifference(state.document); + state.suggestions?.documentDifference(state.document); return BlocBuilder( builder: (context, connectivityState) { if (!connectivityState.isConnected) { @@ -223,7 +227,7 @@ class _DocumentDetailsPageState extends State { } return b.Badge( position: b.BadgePosition.topEnd(top: -12, end: -6), - showBadge: _filteredSuggestions.hasSuggestions, + showBadge: _filteredSuggestions?.hasSuggestions ?? false, child: Tooltip( message: S.of(context)!.editDocumentTooltip, preferBelow: false, @@ -234,7 +238,7 @@ class _DocumentDetailsPageState extends State { ), ), badgeContent: Text( - '${_filteredSuggestions.suggestionsCount}', + '${_filteredSuggestions?.suggestionsCount ?? 0}', style: const TextStyle( color: Colors.white, ), @@ -300,13 +304,10 @@ class _DocumentDetailsPageState extends State { providers: [ BlocProvider.value( value: DocumentEditCubit( - document, - documentsApi: context.read(), - correspondentRepository: context.read(), - documentTypeRepository: context.read(), - storagePathRepository: context.read(), - tagRepository: context.read(), - notifier: context.read(), + context.read(), + context.read(), + context.read(), + document: document, ), ), BlocProvider.value( diff --git a/lib/features/document_edit/cubit/document_edit_cubit.dart b/lib/features/document_edit/cubit/document_edit_cubit.dart index 23e3d35..b944684 100644 --- a/lib/features/document_edit/cubit/document_edit_cubit.dart +++ b/lib/features/document_edit/cubit/document_edit_cubit.dart @@ -32,10 +32,10 @@ class DocumentEditCubit extends Cubit { tags: _labelRepository.state.tags, ), ) { - _notifier.subscribe(this, onUpdated: replace); - _labelRepository.subscribe( + _notifier.addListener(this, onUpdated: replace); + _labelRepository.addListener( this, - onStateChanged: (labels) => emit(state.copyWith()), + onChanged: (labels) => emit(state.copyWith()), ); } @@ -71,7 +71,7 @@ class DocumentEditCubit extends Cubit { for (final sub in _subscriptions) { sub.cancel(); } - _notifier.unsubscribe(this); + _notifier.removeListener(this); return super.close(); } } diff --git a/lib/features/document_edit/view/document_edit_page.dart b/lib/features/document_edit/view/document_edit_page.dart index ae91e34..c6fed2a 100644 --- a/lib/features/document_edit/view/document_edit_page.dart +++ b/lib/features/document_edit/view/document_edit_page.dart @@ -21,7 +21,7 @@ import 'package:paperless_mobile/generated/l10n/app_localizations.dart'; import 'package:paperless_mobile/helpers/message_helpers.dart'; class DocumentEditPage extends StatefulWidget { - final FieldSuggestions suggestions; + final FieldSuggestions? suggestions; const DocumentEditPage({ Key? key, required this.suggestions, @@ -43,13 +43,13 @@ class _DocumentEditPageState extends State { final GlobalKey _formKey = GlobalKey(); bool _isSubmitLoading = false; - late final FieldSuggestions _filteredSuggestions; + late final FieldSuggestions? _filteredSuggestions; @override void initState() { super.initState(); _filteredSuggestions = widget.suggestions - .documentDifference(context.read().state.document); + ?.documentDifference(context.read().state.document); } @override @@ -115,12 +115,14 @@ class _DocumentEditPageState extends State { excludeAllowed: false, name: fkTags, selectableOptions: state.tags, - suggestions: _filteredSuggestions.tags - .toSet() + suggestions: (_filteredSuggestions?.tags.toSet() ?? + {}) .difference(state.document.tags.toSet()) .isNotEmpty ? _buildSuggestionsSkeleton( - suggestions: _filteredSuggestions.tags, + suggestions: + (_filteredSuggestions?.tags.toSet() ?? + {}), itemBuilder: (context, itemData) { final tag = state.tags[itemData]!; return ActionChip( @@ -216,8 +218,9 @@ class _DocumentEditPageState extends State { LabelFormField( notAssignedSelectable: false, formBuilderState: _formKey.currentState, - labelCreationWidgetBuilder: (initialValue) => RepositoryProvider( - create: (context) => context.read>(), + labelCreationWidgetBuilder: (initialValue) => + RepositoryProvider.value( + value: context.read(), child: AddCorrespondentPage(initialName: initialValue), ), textFieldLabel: S.of(context)!.correspondent, @@ -226,9 +229,9 @@ class _DocumentEditPageState extends State { name: fkCorrespondent, prefixIcon: const Icon(Icons.person_outlined), ), - if (_filteredSuggestions.hasSuggestedCorrespondents) + if (_filteredSuggestions?.hasSuggestedCorrespondents ?? false) _buildSuggestionsSkeleton( - suggestions: _filteredSuggestions.correspondents, + suggestions: _filteredSuggestions!.correspondents, itemBuilder: (context, itemData) => ActionChip( label: Text(options[itemData]!.name), onPressed: () => _formKey.currentState?.fields[fkCorrespondent] @@ -248,8 +251,9 @@ class _DocumentEditPageState extends State { LabelFormField( notAssignedSelectable: false, formBuilderState: _formKey.currentState, - labelCreationWidgetBuilder: (currentInput) => RepositoryProvider( - create: (context) => context.read>(), + labelCreationWidgetBuilder: (currentInput) => + RepositoryProvider.value( + value: context.read(), child: AddDocumentTypePage( initialName: currentInput, ), @@ -260,9 +264,9 @@ class _DocumentEditPageState extends State { name: fkDocumentType, prefixIcon: const Icon(Icons.description_outlined), ), - if (_filteredSuggestions.hasSuggestedDocumentTypes) + if (_filteredSuggestions?.hasSuggestedDocumentTypes ?? false) _buildSuggestionsSkeleton( - suggestions: _filteredSuggestions.documentTypes, + suggestions: _filteredSuggestions!.documentTypes, itemBuilder: (context, itemData) => ActionChip( label: Text(options[itemData]!.name), onPressed: () => _formKey.currentState?.fields[fkDocumentType] @@ -327,9 +331,9 @@ class _DocumentEditPageState extends State { format: DateFormat.yMMMMd(), initialEntryMode: DatePickerEntryMode.calendar, ), - if (_filteredSuggestions.hasSuggestedDates) + if (_filteredSuggestions?.hasSuggestedDates ?? false) _buildSuggestionsSkeleton( - suggestions: _filteredSuggestions.dates, + suggestions: _filteredSuggestions!.dates, itemBuilder: (context, itemData) => ActionChip( label: Text(DateFormat.yMMMd().format(itemData)), onPressed: () => _formKey.currentState?.fields[fkCreatedDate] diff --git a/lib/features/document_scan/view/scanner_page.dart b/lib/features/document_scan/view/scanner_page.dart index 178be0f..1cc3d96 100644 --- a/lib/features/document_scan/view/scanner_page.dart +++ b/lib/features/document_scan/view/scanner_page.dart @@ -12,7 +12,6 @@ import 'package:paperless_mobile/core/bloc/connectivity_cubit.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/repository/label_repository.dart'; -import 'package:paperless_mobile/core/repository/provider/label_repositories_provider.dart'; import 'package:paperless_mobile/core/service/file_description.dart'; import 'package:paperless_mobile/core/service/file_service.dart'; import 'package:paperless_mobile/features/app_drawer/view/app_drawer.dart'; @@ -198,20 +197,14 @@ class _ScannerPageState extends State ); final uploadResult = await Navigator.of(context).push( MaterialPageRoute( - builder: (_) => LabelRepositoriesProvider( - child: BlocProvider( - create: (context) => DocumentUploadCubit( - documentApi: context.read(), - correspondentRepository: - context.read>(), - documentTypeRepository: - context.read>(), - tagRepository: context.read>(), - ), - child: DocumentUploadPreparationPage( - fileBytes: file.bytes, - fileExtension: file.extension, - ), + builder: (_) => BlocProvider( + create: (context) => DocumentUploadCubit( + context.read(), + context.read(), + ), + child: DocumentUploadPreparationPage( + fileBytes: file.bytes, + fileExtension: file.extension, ), ), ), @@ -316,22 +309,16 @@ class _ScannerPageState extends State } Navigator.of(context).push( MaterialPageRoute( - builder: (_) => LabelRepositoriesProvider( - child: BlocProvider( - create: (context) => DocumentUploadCubit( - documentApi: context.read(), - correspondentRepository: - context.read>(), - documentTypeRepository: - context.read>(), - tagRepository: context.read>(), - ), - child: DocumentUploadPreparationPage( - fileBytes: file.readAsBytesSync(), - filename: fileDescription.filename, - title: fileDescription.filename, - fileExtension: fileDescription.extension, - ), + builder: (_) => BlocProvider( + create: (context) => DocumentUploadCubit( + context.read(), + context.read(), + ), + child: DocumentUploadPreparationPage( + fileBytes: file.readAsBytesSync(), + filename: fileDescription.filename, + title: fileDescription.filename, + fileExtension: fileDescription.extension, ), ), ), diff --git a/lib/features/document_search/cubit/document_search_cubit.dart b/lib/features/document_search/cubit/document_search_cubit.dart index 7fb1e71..035b7f7 100644 --- a/lib/features/document_search/cubit/document_search_cubit.dart +++ b/lib/features/document_search/cubit/document_search_cubit.dart @@ -2,6 +2,7 @@ import 'package:collection/collection.dart'; import 'package:hydrated_bloc/hydrated_bloc.dart'; import 'package:paperless_api/paperless_api.dart'; import 'package:paperless_mobile/core/notifier/document_changed_notifier.dart'; +import 'package:paperless_mobile/core/repository/label_repository.dart'; import 'package:paperless_mobile/features/paged_document_view/cubit/document_paging_bloc_mixin.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:paperless_mobile/features/paged_document_view/cubit/paged_documents_state.dart'; @@ -15,12 +16,22 @@ class DocumentSearchCubit extends HydratedCubit with DocumentPagingBlocMixin { @override final PaperlessDocumentsApi api; + + final LabelRepository _labelRepository; @override final DocumentChangedNotifier notifier; - DocumentSearchCubit(this.api, this.notifier) + DocumentSearchCubit(this.api, this.notifier, this._labelRepository) : super(const DocumentSearchState()) { - notifier.subscribe( + _labelRepository.addListener(this, onChanged: (labels) { + emit(state.copyWith( + correspondents: labels.correspondents, + documentTypes: labels.documentTypes, + tags: labels.tags, + storagePaths: labels.storagePaths, + )); + }); + notifier.addListener( this, onDeleted: remove, onUpdated: replace, @@ -89,7 +100,7 @@ class DocumentSearchCubit extends HydratedCubit @override Future close() { - notifier.unsubscribe(this); + notifier.removeListener(this); return super.close(); } diff --git a/lib/features/document_search/cubit/document_search_state.dart b/lib/features/document_search/cubit/document_search_state.dart index f2cae8c..2809af8 100644 --- a/lib/features/document_search/cubit/document_search_state.dart +++ b/lib/features/document_search/cubit/document_search_state.dart @@ -13,6 +13,12 @@ class DocumentSearchState extends DocumentPagingState { final List suggestions; @JsonKey() final ViewType viewType; + + final Map correspondents; + final Map documentTypes; + final Map tags; + final Map storagePaths; + const DocumentSearchState({ this.view = SearchView.suggestions, this.searchHistory = const [], @@ -22,6 +28,10 @@ class DocumentSearchState extends DocumentPagingState { super.hasLoaded, super.isLoading, super.value, + this.correspondents = const {}, + this.documentTypes = const {}, + this.tags = const {}, + this.storagePaths = const {}, }); @override @@ -31,6 +41,10 @@ class DocumentSearchState extends DocumentPagingState { suggestions, view, viewType, + correspondents, + documentTypes, + tags, + storagePaths, ]; @override @@ -57,6 +71,10 @@ class DocumentSearchState extends DocumentPagingState { List? suggestions, SearchView? view, ViewType? viewType, + Map? correspondents, + Map? documentTypes, + Map? tags, + Map? storagePaths, }) { return DocumentSearchState( value: value ?? this.value, @@ -67,6 +85,10 @@ class DocumentSearchState extends DocumentPagingState { view: view ?? this.view, suggestions: suggestions ?? this.suggestions, viewType: viewType ?? this.viewType, + correspondents: correspondents ?? this.correspondents, + documentTypes: documentTypes ?? this.documentTypes, + tags: tags ?? this.tags, + storagePaths: storagePaths ?? this.storagePaths, ); } diff --git a/lib/features/document_search/view/document_search_page.dart b/lib/features/document_search/view/document_search_page.dart index 8f1d2a7..4f0ad28 100644 --- a/lib/features/document_search/view/document_search_page.dart +++ b/lib/features/document_search/view/document_search_page.dart @@ -21,6 +21,7 @@ Future showDocumentSearchPage(BuildContext context) { create: (context) => DocumentSearchCubit( context.read(), context.read(), + context.read(), ), child: const DocumentSearchPage(), ), @@ -229,6 +230,10 @@ class _DocumentSearchPageState extends State { ), ); }, + correspondents: state.correspondents, + documentTypes: state.documentTypes, + tags: state.tags, + storagePaths: state.storagePaths, ) ], ); diff --git a/lib/features/document_upload/cubit/document_upload_cubit.dart b/lib/features/document_upload/cubit/document_upload_cubit.dart index 3ec4326..91eb17a 100644 --- a/lib/features/document_upload/cubit/document_upload_cubit.dart +++ b/lib/features/document_upload/cubit/document_upload_cubit.dart @@ -5,42 +5,26 @@ import 'package:equatable/equatable.dart'; import 'package:flutter/foundation.dart'; import 'package:paperless_api/paperless_api.dart'; import 'package:paperless_mobile/core/repository/label_repository.dart'; -import 'package:paperless_mobile/core/repository/state/impl/correspondent_repository_state.dart'; -import 'package:paperless_mobile/core/repository/state/impl/document_type_repository_state.dart'; -import 'package:paperless_mobile/core/repository/state/impl/tag_repository_state.dart'; part 'document_upload_state.dart'; class DocumentUploadCubit extends Cubit { final PaperlessDocumentsApi _documentApi; - final LabelRepository _tagRepository; - final LabelRepository _correspondentRepository; - final LabelRepository _documentTypeRepository; + final LabelRepository _labelRepository; - final List _subs = []; - - DocumentUploadCubit({ - required PaperlessDocumentsApi documentApi, - required LabelRepository tagRepository, - required LabelRepository correspondentRepository, - required LabelRepository documentTypeRepository, - }) : _documentApi = documentApi, - _tagRepository = tagRepository, - _correspondentRepository = correspondentRepository, - _documentTypeRepository = documentTypeRepository, - super(const DocumentUploadState()) { - _subs.add(_tagRepository.values.listen( - (tags) => emit(state.copyWith(tags: tags?.values)), - )); - _subs.add(_correspondentRepository.values.listen( - (correspondents) => - emit(state.copyWith(correspondents: correspondents?.values)), - )); - _subs.add(_documentTypeRepository.values.listen( - (documentTypes) => - emit(state.copyWith(documentTypes: documentTypes?.values)), - )); + DocumentUploadCubit(this._labelRepository, this._documentApi) + : super(const DocumentUploadState()) { + _labelRepository.addListener( + this, + onChanged: (labels) { + emit(state.copyWith( + correspondents: labels.correspondents, + documentTypes: labels.documentTypes, + tags: labels.tags, + )); + }, + ); } Future upload( @@ -65,9 +49,7 @@ class DocumentUploadCubit extends Cubit { @override Future close() async { - for (final sub in _subs) { - await sub.cancel(); - } + _labelRepository.removeListener(this); return super.close(); } } diff --git a/lib/features/document_upload/view/document_upload_preparation_page.dart b/lib/features/document_upload/view/document_upload_preparation_page.dart index ca44d33..cdd5b76 100644 --- a/lib/features/document_upload/view/document_upload_preparation_page.dart +++ b/lib/features/document_upload/view/document_upload_preparation_page.dart @@ -192,9 +192,8 @@ class _DocumentUploadPreparationPageState notAssignedSelectable: false, formBuilderState: _formKey.currentState, labelCreationWidgetBuilder: (initialName) => - RepositoryProvider( - create: (context) => - context.read>(), + RepositoryProvider.value( + value: context.read(), child: AddCorrespondentPage(initialName: initialName), ), textFieldLabel: S.of(context)!.correspondent + " *", @@ -207,9 +206,8 @@ class _DocumentUploadPreparationPageState notAssignedSelectable: false, formBuilderState: _formKey.currentState, labelCreationWidgetBuilder: (initialName) => - RepositoryProvider( - create: (context) => - context.read>(), + RepositoryProvider.value( + value: context.read(), child: AddDocumentTypePage(initialName: initialName), ), textFieldLabel: S.of(context)!.documentType + " *", @@ -229,7 +227,7 @@ class _DocumentUploadPreparationPageState "* " + S.of(context)!.uploadInferValuesHint, style: Theme.of(context).textTheme.bodySmall, ), - SizedBox(height: 300), + const SizedBox(height: 300), ].padded(), ), ); diff --git a/lib/features/documents/cubit/documents_cubit.dart b/lib/features/documents/cubit/documents_cubit.dart index e97648c..17194b5 100644 --- a/lib/features/documents/cubit/documents_cubit.dart +++ b/lib/features/documents/cubit/documents_cubit.dart @@ -1,19 +1,17 @@ import 'dart:async'; -import 'dart:developer'; -import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'package:hydrated_bloc/hydrated_bloc.dart'; +import 'package:json_annotation/json_annotation.dart'; import 'package:paperless_api/paperless_api.dart'; import 'package:paperless_mobile/core/notifier/document_changed_notifier.dart'; import 'package:paperless_mobile/core/repository/label_repository.dart'; import 'package:paperless_mobile/features/paged_document_view/cubit/document_paging_bloc_mixin.dart'; -import 'package:paperless_mobile/features/settings/model/view_type.dart'; -import 'package:json_annotation/json_annotation.dart'; import 'package:paperless_mobile/features/paged_document_view/cubit/paged_documents_state.dart'; +import 'package:paperless_mobile/features/settings/model/view_type.dart'; -part 'documents_state.dart'; part 'documents_cubit.g.dart'; +part 'documents_state.dart'; class DocumentsCubit extends HydratedCubit with DocumentPagingBlocMixin { @@ -32,7 +30,7 @@ class DocumentsCubit extends HydratedCubit storagePaths: _labelRepository.state.storagePaths, tags: _labelRepository.state.tags, )) { - notifier.subscribe( + notifier.addListener( this, onUpdated: (document) { replace(document); @@ -54,9 +52,9 @@ class DocumentsCubit extends HydratedCubit ); }, ); - _labelRepository.subscribe( + _labelRepository.addListener( this, - onStateChanged: (labels) => emit( + onChanged: (labels) => emit( state.copyWith( correspondents: labels.correspondents, documentTypes: labels.documentTypes, @@ -120,8 +118,8 @@ class DocumentsCubit extends HydratedCubit @override Future close() { - notifier.unsubscribe(this); - _labelRepository.unsubscribe(this); + notifier.removeListener(this); + _labelRepository.removeListener(this); return super.close(); } diff --git a/lib/features/documents/view/pages/documents_page.dart b/lib/features/documents/view/pages/documents_page.dart index cfb82c6..284bc5a 100644 --- a/lib/features/documents/view/pages/documents_page.dart +++ b/lib/features/documents/view/pages/documents_page.dart @@ -12,11 +12,9 @@ import 'package:paperless_mobile/features/documents/cubit/documents_cubit.dart'; import 'package:paperless_mobile/features/documents/view/widgets/adaptive_documents_view.dart'; import 'package:paperless_mobile/features/documents/view/widgets/documents_empty_state.dart'; import 'package:paperless_mobile/features/documents/view/widgets/search/document_filter_panel.dart'; -import 'package:paperless_mobile/features/documents/view/widgets/selection/bulk_delete_confirmation_dialog.dart'; import 'package:paperless_mobile/features/documents/view/widgets/selection/document_selection_sliver_app_bar.dart'; import 'package:paperless_mobile/features/documents/view/widgets/selection/view_type_selection_widget.dart'; import 'package:paperless_mobile/features/documents/view/widgets/sort_documents_button.dart'; -import 'package:paperless_mobile/features/labels/cubit/providers/labels_bloc_provider.dart'; 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/view/saved_view_list.dart'; @@ -358,6 +356,10 @@ class _DocumentsPageState extends State isLabelClickable: true, isLoading: state.isLoading, selectedDocumentIds: state.selectedIds, + correspondents: state.correspondents, + documentTypes: state.documentTypes, + tags: state.tags, + storagePaths: state.storagePaths, ); }, ), @@ -391,10 +393,16 @@ class _DocumentsPageState extends State void _onCreateSavedView(DocumentFilter filter) async { final newView = await Navigator.of(context).push( MaterialPageRoute( - builder: (context) => LabelsBlocProvider( - child: AddSavedViewPage( - currentFilter: filter, - ), + builder: (context) => BlocBuilder( + builder: (context, state) { + return AddSavedViewPage( + currentFilter: filter, + correspondents: state.correspondents, + documentTypes: state.documentTypes, + storagePaths: state.storagePaths, + tags: state.tags, + ); + }, ), ), ); @@ -428,12 +436,19 @@ class _DocumentsPageState extends State snapSizes: const [0.9, 1], initialChildSize: .9, maxChildSize: 1, - builder: (context, controller) => LabelsBlocProvider( - child: DocumentFilterPanel( - initialFilter: context.read().state.filter, - scrollController: controller, - draggableSheetController: draggableSheetController, - ), + builder: (context, controller) => + BlocBuilder( + builder: (context, state) { + return DocumentFilterPanel( + initialFilter: context.read().state.filter, + scrollController: controller, + draggableSheetController: draggableSheetController, + correspondents: state.correspondents, + documentTypes: state.documentTypes, + storagePaths: state.storagePaths, + tags: state.tags, + ); + }, ), ), ), diff --git a/lib/features/documents/view/widgets/items/document_detailed_item.dart b/lib/features/documents/view/widgets/items/document_detailed_item.dart index b4ac2a5..023e0f1 100644 --- a/lib/features/documents/view/widgets/items/document_detailed_item.dart +++ b/lib/features/documents/view/widgets/items/document_detailed_item.dart @@ -119,7 +119,7 @@ class DocumentDetailedItem extends DocumentItem { textStyle: Theme.of(context).textTheme.titleSmall?.apply( color: Theme.of(context).colorScheme.onSurfaceVariant, ), - correspondent: context.read().correspondent, + correspondent: correspondents[document.correspondent], ), ], ).paddedLTRB(8, 0, 8, 4), @@ -134,13 +134,13 @@ class DocumentDetailedItem extends DocumentItem { textStyle: Theme.of(context).textTheme.titleSmall?.apply( color: Theme.of(context).colorScheme.onSurfaceVariant, ), - documentTypeId: document.documentType, + documentType: documentTypes[document.documentType], ), ], ).paddedLTRB(8, 0, 8, 4), TagsWidget( isMultiLine: false, - tagIds: document.tags, + tags: document.tags.map((e) => tags[e]!).toList(), ).padded(), if (highlights != null) Html( diff --git a/lib/features/documents/view/widgets/search/document_filter_form.dart b/lib/features/documents/view/widgets/search/document_filter_form.dart index 8bb5937..35200a5 100644 --- a/lib/features/documents/view/widgets/search/document_filter_form.dart +++ b/lib/features/documents/view/widgets/search/document_filter_form.dart @@ -49,6 +49,11 @@ class DocumentFilterForm extends StatefulWidget { final DocumentFilter initialFilter; final ScrollController? scrollController; final EdgeInsets padding; + final Map correspondents; + final Map documentTypes; + final Map tags; + final Map storagePaths; + const DocumentFilterForm({ super.key, this.header, @@ -56,6 +61,10 @@ class DocumentFilterForm extends StatefulWidget { required this.initialFilter, this.scrollController, this.padding = const EdgeInsets.symmetric(vertical: 8, horizontal: 16), + required this.correspondents, + required this.documentTypes, + required this.tags, + required this.storagePaths, }); @override @@ -145,47 +154,35 @@ class _DocumentFilterFormState extends State { } Widget _buildDocumentTypeFormField() { - return BlocBuilder, LabelState>( - builder: (context, state) { - return LabelFormField( - formBuilderState: widget.formKey.currentState, - name: DocumentFilterForm.fkDocumentType, - labelOptions: state.labels, - textFieldLabel: S.of(context)!.documentType, - initialValue: widget.initialFilter.documentType, - prefixIcon: const Icon(Icons.description_outlined), - ); - }, + return LabelFormField( + formBuilderState: widget.formKey.currentState, + name: DocumentFilterForm.fkDocumentType, + labelOptions: widget.documentTypes, + textFieldLabel: S.of(context)!.documentType, + initialValue: widget.initialFilter.documentType, + prefixIcon: const Icon(Icons.description_outlined), ); } Widget _buildCorrespondentFormField() { - return BlocBuilder, LabelState>( - builder: (context, state) { - return LabelFormField( - formBuilderState: widget.formKey.currentState, - name: DocumentFilterForm.fkCorrespondent, - labelOptions: state.labels, - textFieldLabel: S.of(context)!.correspondent, - initialValue: widget.initialFilter.correspondent, - prefixIcon: const Icon(Icons.person_outline), - ); - }, + return LabelFormField( + formBuilderState: widget.formKey.currentState, + name: DocumentFilterForm.fkCorrespondent, + labelOptions: widget.correspondents, + textFieldLabel: S.of(context)!.correspondent, + initialValue: widget.initialFilter.correspondent, + prefixIcon: const Icon(Icons.person_outline), ); } Widget _buildStoragePathFormField() { - return BlocBuilder, LabelState>( - builder: (context, state) { - return LabelFormField( - formBuilderState: widget.formKey.currentState, - name: DocumentFilterForm.fkStoragePath, - labelOptions: state.labels, - textFieldLabel: S.of(context)!.storagePath, - initialValue: widget.initialFilter.storagePath, - prefixIcon: const Icon(Icons.folder_outlined), - ); - }, + return LabelFormField( + formBuilderState: widget.formKey.currentState, + name: DocumentFilterForm.fkStoragePath, + labelOptions: widget.storagePaths, + textFieldLabel: S.of(context)!.storagePath, + initialValue: widget.initialFilter.storagePath, + prefixIcon: const Icon(Icons.folder_outlined), ); } @@ -197,16 +194,12 @@ class _DocumentFilterFormState extends State { ); } - BlocBuilder, LabelState> _buildTagsFormField() { - return BlocBuilder, LabelState>( - builder: (context, state) { - return TagFormField( - name: DocumentModel.tagsKey, - initialValue: widget.initialFilter.tags, - allowCreation: false, - selectableOptions: state.labels, - ); - }, + Widget _buildTagsFormField() { + return TagFormField( + name: DocumentModel.tagsKey, + initialValue: widget.initialFilter.tags, + allowCreation: false, + selectableOptions: widget.tags, ); } } diff --git a/lib/features/documents/view/widgets/search/document_filter_panel.dart b/lib/features/documents/view/widgets/search/document_filter_panel.dart index b4d41e3..e255b6d 100644 --- a/lib/features/documents/view/widgets/search/document_filter_panel.dart +++ b/lib/features/documents/view/widgets/search/document_filter_panel.dart @@ -13,11 +13,20 @@ class DocumentFilterPanel extends StatefulWidget { final DocumentFilter initialFilter; final ScrollController scrollController; final DraggableScrollableController draggableSheetController; + final Map correspondents; + final Map documentTypes; + final Map tags; + final Map storagePaths; + const DocumentFilterPanel({ Key? key, required this.initialFilter, required this.scrollController, required this.draggableSheetController, + required this.correspondents, + required this.documentTypes, + required this.tags, + required this.storagePaths, }) : super(key: key); @override @@ -38,10 +47,8 @@ class _DocumentFilterPanelState extends State { void animateTitleByDrag() { setState( - () { - _heightAnimationValue = dp( - ((max(0.9, widget.draggableSheetController.size) - 0.9) / 0.1), 5); - }, + () => _heightAnimationValue = + dp(((max(0.9, widget.draggableSheetController.size) - 0.9) / 0.1), 5), ); } @@ -96,6 +103,10 @@ class _DocumentFilterPanelState extends State { scrollController: widget.scrollController, initialFilter: widget.initialFilter, header: _buildPanelHeader(), + correspondents: widget.correspondents, + documentTypes: widget.documentTypes, + storagePaths: widget.storagePaths, + tags: widget.tags, ), ), ); diff --git a/lib/features/documents/view/widgets/search/sort_field_selection_bottom_sheet.dart b/lib/features/documents/view/widgets/search/sort_field_selection_bottom_sheet.dart index 07de4c3..51669bc 100644 --- a/lib/features/documents/view/widgets/search/sort_field_selection_bottom_sheet.dart +++ b/lib/features/documents/view/widgets/search/sort_field_selection_bottom_sheet.dart @@ -10,6 +10,10 @@ import 'package:paperless_mobile/generated/l10n/app_localizations.dart'; class SortFieldSelectionBottomSheet extends StatefulWidget { final SortOrder initialSortOrder; final SortField? initialSortField; + final Map correspondents; + final Map documentTypes; + final Map tags; + final Map storagePaths; final Future Function(SortField? field, SortOrder order) onSubmit; @@ -18,6 +22,10 @@ class SortFieldSelectionBottomSheet extends StatefulWidget { required this.initialSortOrder, required this.initialSortField, required this.onSubmit, + required this.correspondents, + required this.documentTypes, + required this.tags, + required this.storagePaths, }); @override @@ -67,31 +75,20 @@ class _SortFieldSelectionBottomSheetState Column( children: [ _buildSortOption(SortField.archiveSerialNumber), - BlocBuilder, - LabelState>( - builder: (context, state) { - return _buildSortOption( - SortField.correspondentName, - enabled: state.labels.values.fold( - false, - (previousValue, element) => - previousValue || - (element.documentCount ?? 0) > 0), - ); - }, + _buildSortOption( + SortField.correspondentName, + enabled: widget.correspondents.values.fold( + false, + (previousValue, element) => + previousValue || (element.documentCount ?? 0) > 0), ), _buildSortOption(SortField.title), - BlocBuilder, LabelState>( - builder: (context, state) { - return _buildSortOption( - SortField.documentType, - enabled: state.labels.values.fold( - false, - (previousValue, element) => - previousValue || - (element.documentCount ?? 0) > 0), - ); - }, + _buildSortOption( + SortField.documentType, + enabled: widget.documentTypes.values.fold( + false, + (previousValue, element) => + previousValue || (element.documentCount ?? 0) > 0), ), _buildSortOption(SortField.created), _buildSortOption(SortField.added), diff --git a/lib/features/documents/view/widgets/sort_documents_button.dart b/lib/features/documents/view/widgets/sort_documents_button.dart index 3d72daf..822c69c 100644 --- a/lib/features/documents/view/widgets/sort_documents_button.dart +++ b/lib/features/documents/view/widgets/sort_documents_button.dart @@ -57,6 +57,10 @@ class SortDocumentsButton extends StatelessWidget { sortOrder: order, ), ), + correspondents: state.correspondents, + documentTypes: state.documentTypes, + storagePaths: state.storagePaths, + tags: state.tags, ), ), ), diff --git a/lib/features/edit_label/cubit/edit_label_cubit.dart b/lib/features/edit_label/cubit/edit_label_cubit.dart index ed5c8e3..49a6bdc 100644 --- a/lib/features/edit_label/cubit/edit_label_cubit.dart +++ b/lib/features/edit_label/cubit/edit_label_cubit.dart @@ -13,7 +13,7 @@ class EditLabelCubit extends Cubit with LabelCubitMixin { final LabelRepository labelRepository; EditLabelCubit(this.labelRepository) : super(const EditLabelState()) { - labelRepository.subscribe( + labelRepository.addListener( this, onChanged: (labels) => state.copyWith( correspondents: labels.correspondents, @@ -26,7 +26,7 @@ class EditLabelCubit extends Cubit with LabelCubitMixin { @override Future close() { - labelRepository.unsubscribe(this); + labelRepository.removeListener(this); return super.close(); } diff --git a/lib/features/home/view/home_page.dart b/lib/features/home/view/home_page.dart index 86247d0..fb8d185 100644 --- a/lib/features/home/view/home_page.dart +++ b/lib/features/home/view/home_page.dart @@ -157,10 +157,8 @@ class _HomePageState extends State with WidgetsBindingObserver { MaterialPageRoute( builder: (context) => BlocProvider.value( value: DocumentUploadCubit( - documentApi: context.read(), - tagRepository: context.read(), - correspondentRepository: context.read(), - documentTypeRepository: context.read(), + context.read(), + context.read(), ), child: DocumentUploadPreparationPage( fileBytes: bytes, @@ -239,11 +237,13 @@ class _HomePageState extends State with WidgetsBindingObserver { create: (context) => DocumentsCubit( context.read(), context.read(), + context.read(), ), ), BlocProvider( create: (context) => SavedViewCubit( context.read(), + context.read(), ), ), ], diff --git a/lib/features/inbox/cubit/inbox_cubit.dart b/lib/features/inbox/cubit/inbox_cubit.dart index 9265b76..59fe61f 100644 --- a/lib/features/inbox/cubit/inbox_cubit.dart +++ b/lib/features/inbox/cubit/inbox_cubit.dart @@ -32,7 +32,7 @@ class InboxCubit extends HydratedCubit this._labelRepository, this.notifier, ) : super(InboxState(labels: _labelRepository.state)) { - notifier.subscribe( + notifier.addListener( this, onDeleted: remove, onUpdated: (document) { @@ -47,7 +47,7 @@ class InboxCubit extends HydratedCubit } }, ); - _labelRepository.subscribe( + _labelRepository.addListener( this, onChanged: (labels) { emit(state.copyWith(labels: labels)); @@ -209,7 +209,7 @@ class InboxCubit extends HydratedCubit @override Future close() { - _labelRepository.unsubscribe(this); + _labelRepository.removeListener(this); return super.close(); } } diff --git a/lib/features/labels/cubit/label_cubit.dart b/lib/features/labels/cubit/label_cubit.dart index 52a5e56..d6933d9 100644 --- a/lib/features/labels/cubit/label_cubit.dart +++ b/lib/features/labels/cubit/label_cubit.dart @@ -12,7 +12,7 @@ class LabelCubit extends Cubit with LabelCubitMixin { final LabelRepository labelRepository; LabelCubit(this.labelRepository) : super(const LabelState()) { - labelRepository.subscribe( + labelRepository.addListener( this, onChanged: (labels) { emit(state.copyWith( @@ -27,7 +27,7 @@ class LabelCubit extends Cubit with LabelCubitMixin { @override Future close() { - labelRepository.unsubscribe(this); + labelRepository.removeListener(this); return super.close(); } diff --git a/lib/features/linked_documents/cubit/linked_documents_cubit.dart b/lib/features/linked_documents/cubit/linked_documents_cubit.dart index fc47219..8bce26c 100644 --- a/lib/features/linked_documents/cubit/linked_documents_cubit.dart +++ b/lib/features/linked_documents/cubit/linked_documents_cubit.dart @@ -27,7 +27,7 @@ class LinkedDocumentsCubit extends HydratedCubit this._labelRepository, ) : super(const LinkedDocumentsState()) { updateFilter(filter: filter); - _labelRepository.subscribe( + _labelRepository.addListener( this, onChanged: (labels) { emit(state.copyWith( @@ -38,7 +38,7 @@ class LinkedDocumentsCubit extends HydratedCubit )); }, ); - notifier.subscribe( + notifier.addListener( this, onUpdated: replace, onDeleted: remove, diff --git a/lib/features/paged_document_view/cubit/document_paging_bloc_mixin.dart b/lib/features/paged_document_view/cubit/document_paging_bloc_mixin.dart index 0164cea..c58c1cc 100644 --- a/lib/features/paged_document_view/cubit/document_paging_bloc_mixin.dart +++ b/lib/features/paged_document_view/cubit/document_paging_bloc_mixin.dart @@ -172,7 +172,7 @@ mixin DocumentPagingBlocMixin @override Future close() { - notifier.unsubscribe(this); + notifier.removeListener(this); return super.close(); } } diff --git a/lib/features/saved_view/cubit/saved_view_cubit.dart b/lib/features/saved_view/cubit/saved_view_cubit.dart index 02984a0..d6957c3 100644 --- a/lib/features/saved_view/cubit/saved_view_cubit.dart +++ b/lib/features/saved_view/cubit/saved_view_cubit.dart @@ -21,7 +21,7 @@ class SavedViewCubit extends Cubit { storagePaths: _labelRepository.state.storagePaths, tags: _labelRepository.state.tags, )) { - _labelRepository.subscribe( + _labelRepository.addListener( this, onChanged: (labels) { emit( @@ -76,7 +76,7 @@ class SavedViewCubit extends Cubit { @override Future close() { _savedViewRepository.unsubscribe(this); - _labelRepository.unsubscribe(this); + _labelRepository.removeListener(this); return super.close(); } } diff --git a/lib/features/saved_view/view/add_saved_view_page.dart b/lib/features/saved_view/view/add_saved_view_page.dart index a19475b..a8633a1 100644 --- a/lib/features/saved_view/view/add_saved_view_page.dart +++ b/lib/features/saved_view/view/add_saved_view_page.dart @@ -8,7 +8,18 @@ import 'package:paperless_mobile/generated/l10n/app_localizations.dart'; class AddSavedViewPage extends StatefulWidget { final DocumentFilter currentFilter; - const AddSavedViewPage({super.key, required this.currentFilter}); + final Map correspondents; + final Map documentTypes; + final Map tags; + final Map storagePaths; + const AddSavedViewPage({ + super.key, + required this.currentFilter, + required this.correspondents, + required this.documentTypes, + required this.tags, + required this.storagePaths, + }); @override State createState() => _AddSavedViewPageState(); @@ -72,6 +83,10 @@ class _AddSavedViewPageState extends State { padding: const EdgeInsets.symmetric(vertical: 8), formKey: _filterFormKey, initialFilter: widget.currentFilter, + correspondents: widget.correspondents, + documentTypes: widget.documentTypes, + storagePaths: widget.storagePaths, + tags: widget.tags, ), ), ], diff --git a/lib/features/saved_view_details/cubit/saved_view_details_cubit.dart b/lib/features/saved_view_details/cubit/saved_view_details_cubit.dart index 044b3c2..8697884 100644 --- a/lib/features/saved_view_details/cubit/saved_view_details_cubit.dart +++ b/lib/features/saved_view_details/cubit/saved_view_details_cubit.dart @@ -23,7 +23,7 @@ class SavedViewDetailsCubit extends HydratedCubit this.notifier, { required this.savedView, }) : super(const SavedViewDetailsState()) { - notifier.subscribe( + notifier.addListener( this, onDeleted: remove, onUpdated: replace, diff --git a/lib/features/saved_view_details/cubit/saved_view_details_state.dart b/lib/features/saved_view_details/cubit/saved_view_details_state.dart index 6b99083..b61b7c3 100644 --- a/lib/features/saved_view_details/cubit/saved_view_details_state.dart +++ b/lib/features/saved_view_details/cubit/saved_view_details_state.dart @@ -5,17 +5,30 @@ class SavedViewDetailsState extends DocumentPagingState { @JsonKey() final ViewType viewType; + final Map correspondents; + final Map documentTypes; + final Map tags; + final Map storagePaths; + const SavedViewDetailsState({ this.viewType = ViewType.list, super.filter, super.hasLoaded, super.isLoading, super.value, + this.correspondents = const {}, + this.documentTypes = const {}, + this.tags = const {}, + this.storagePaths = const {}, }); @override List get props => [ viewType, + correspondents, + documentTypes, + tags, + storagePaths, ...super.props, ]; @@ -40,6 +53,10 @@ class SavedViewDetailsState extends DocumentPagingState { List>? value, DocumentFilter? filter, ViewType? viewType, + Map? correspondents, + Map? documentTypes, + Map? tags, + Map? storagePaths, }) { return SavedViewDetailsState( hasLoaded: hasLoaded ?? this.hasLoaded, @@ -47,6 +64,10 @@ class SavedViewDetailsState extends DocumentPagingState { value: value ?? this.value, filter: filter ?? this.filter, viewType: viewType ?? this.viewType, + correspondents: correspondents ?? this.correspondents, + documentTypes: documentTypes ?? this.documentTypes, + tags: tags ?? this.tags, + storagePaths: storagePaths ?? this.storagePaths, ); } diff --git a/lib/features/saved_view_details/view/saved_view_details_page.dart b/lib/features/saved_view_details/view/saved_view_details_page.dart index bd40803..8887803 100644 --- a/lib/features/saved_view_details/view/saved_view_details_page.dart +++ b/lib/features/saved_view_details/view/saved_view_details_page.dart @@ -86,6 +86,10 @@ class _SavedViewDetailsPageState extends State ); }, viewType: state.viewType, + correspondents: state.correspondents, + documentTypes: state.documentTypes, + tags: state.tags, + storagePaths: state.storagePaths, ), if (state.hasLoaded && state.isLoading) const SliverToBoxAdapter( diff --git a/lib/features/similar_documents/cubit/similar_documents_cubit.dart b/lib/features/similar_documents/cubit/similar_documents_cubit.dart index 63e11db..0cbb0a4 100644 --- a/lib/features/similar_documents/cubit/similar_documents_cubit.dart +++ b/lib/features/similar_documents/cubit/similar_documents_cubit.dart @@ -1,6 +1,7 @@ import 'package:bloc/bloc.dart'; import 'package:paperless_api/paperless_api.dart'; import 'package:paperless_mobile/core/notifier/document_changed_notifier.dart'; +import 'package:paperless_mobile/core/repository/label_repository.dart'; import 'package:paperless_mobile/features/paged_document_view/cubit/document_paging_bloc_mixin.dart'; import 'package:paperless_mobile/features/paged_document_view/cubit/paged_documents_state.dart'; @@ -13,19 +14,33 @@ class SimilarDocumentsCubit extends Cubit @override final PaperlessDocumentsApi api; + final LabelRepository _labelRepository; + @override final DocumentChangedNotifier notifier; SimilarDocumentsCubit( this.api, - this.notifier, { + this.notifier, + this._labelRepository, { required this.documentId, }) : super(const SimilarDocumentsState()) { - notifier.subscribe( + notifier.addListener( this, onDeleted: remove, onUpdated: replace, ); + _labelRepository.addListener( + this, + onChanged: (labels) { + emit(state.copyWith( + correspondents: labels.correspondents, + documentTypes: labels.documentTypes, + tags: labels.tags, + storagePaths: labels.storagePaths, + )); + }, + ); } Future initialize() async { @@ -38,4 +53,11 @@ class SimilarDocumentsCubit extends Cubit ); } } + + @override + Future close() { + notifier.removeListener(this); + _labelRepository.removeListener(this); + return super.close(); + } } diff --git a/lib/features/similar_documents/cubit/similar_documents_state.dart b/lib/features/similar_documents/cubit/similar_documents_state.dart index 8a1424d..944c5d0 100644 --- a/lib/features/similar_documents/cubit/similar_documents_state.dart +++ b/lib/features/similar_documents/cubit/similar_documents_state.dart @@ -1,11 +1,20 @@ part of 'similar_documents_cubit.dart'; class SimilarDocumentsState extends DocumentPagingState { + final Map correspondents; + final Map documentTypes; + final Map tags; + final Map storagePaths; + const SimilarDocumentsState({ super.filter, super.hasLoaded, super.isLoading, super.value, + this.correspondents = const {}, + this.documentTypes = const {}, + this.tags = const {}, + this.storagePaths = const {}, }); @override @@ -14,6 +23,10 @@ class SimilarDocumentsState extends DocumentPagingState { hasLoaded, isLoading, value, + correspondents, + documentTypes, + tags, + storagePaths, ]; @override @@ -36,12 +49,20 @@ class SimilarDocumentsState extends DocumentPagingState { bool? isLoading, List>? value, DocumentFilter? filter, + Map? correspondents, + Map? documentTypes, + Map? tags, + Map? storagePaths, }) { return SimilarDocumentsState( hasLoaded: hasLoaded ?? this.hasLoaded, isLoading: isLoading ?? this.isLoading, value: value ?? this.value, filter: filter ?? this.filter, + correspondents: correspondents ?? this.correspondents, + documentTypes: documentTypes ?? this.documentTypes, + tags: tags ?? this.tags, + storagePaths: storagePaths ?? this.storagePaths, ); } } diff --git a/lib/features/similar_documents/view/similar_documents_view.dart b/lib/features/similar_documents/view/similar_documents_view.dart index b386a0f..aeaf02d 100644 --- a/lib/features/similar_documents/view/similar_documents_view.dart +++ b/lib/features/similar_documents/view/similar_documents_view.dart @@ -78,6 +78,10 @@ class _SimilarDocumentsViewState extends State ), ); }, + correspondents: state.correspondents, + documentTypes: state.documentTypes, + tags: state.tags, + storagePaths: state.storagePaths, ); }, ); diff --git a/lib/main.dart b/lib/main.dart index d69ae8c..5028ec5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -21,7 +21,6 @@ import 'package:paperless_mobile/core/bloc/paperless_server_information_cubit.da import 'package:paperless_mobile/core/interceptor/dio_http_error_interceptor.dart'; import 'package:paperless_mobile/core/interceptor/language_header.interceptor.dart'; import 'package:paperless_mobile/core/notifier/document_changed_notifier.dart'; -import 'package:paperless_mobile/core/repository/impl/saved_view_repository_impl.dart'; import 'package:paperless_mobile/core/repository/label_repository.dart'; import 'package:paperless_mobile/core/repository/saved_view_repository.dart'; import 'package:paperless_mobile/core/security/session_manager.dart'; @@ -101,7 +100,7 @@ void main() async { // Create repositories final labelRepository = LabelRepository(labelsApi); - final savedViewRepository = SavedViewRepositoryImpl(savedViewsApi); + final savedViewRepository = SavedViewRepository(savedViewsApi); //Create cubits/blocs final authCubit = AuthenticationCubit(