fix: Add labels to each cubit using repositories and state properties, remove label cubits

This commit is contained in:
Anton Stubenbord
2023-04-04 20:30:25 +02:00
parent 78fbd042a6
commit a2388b014b
95 changed files with 4790 additions and 1823 deletions

View File

@@ -6,6 +6,7 @@ import 'package:flutter/foundation.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:paperless_mobile/features/settings/model/view_type.dart';
import 'package:json_annotation/json_annotation.dart';
@@ -19,10 +20,18 @@ class DocumentsCubit extends HydratedCubit<DocumentsState>
@override
final PaperlessDocumentsApi api;
final LabelRepository _labelRepository;
@override
final DocumentChangedNotifier notifier;
DocumentsCubit(this.api, this.notifier) : super(const DocumentsState()) {
DocumentsCubit(this.api, this.notifier, this._labelRepository)
: super(DocumentsState(
correspondents: _labelRepository.state.correspondents,
documentTypes: _labelRepository.state.documentTypes,
storagePaths: _labelRepository.state.storagePaths,
tags: _labelRepository.state.tags,
)) {
notifier.subscribe(
this,
onUpdated: (document) {
@@ -45,6 +54,17 @@ class DocumentsCubit extends HydratedCubit<DocumentsState>
);
},
);
_labelRepository.subscribe(
this,
onStateChanged: (labels) => emit(
state.copyWith(
correspondents: labels.correspondents,
documentTypes: labels.documentTypes,
storagePaths: labels.storagePaths,
tags: labels.tags,
),
),
);
}
Future<void> bulkDelete(List<DocumentModel> documents) async {
@@ -101,6 +121,7 @@ class DocumentsCubit extends HydratedCubit<DocumentsState>
@override
Future<void> close() {
notifier.unsubscribe(this);
_labelRepository.unsubscribe(this);
return super.close();
}

View File

@@ -1,10 +1,15 @@
part of 'documents_cubit.dart';
@JsonSerializable()
@JsonSerializable(ignoreUnannotated: true)
class DocumentsState extends DocumentPagingState {
@JsonKey(includeFromJson: false, includeToJson: false)
final List<DocumentModel> selection;
final Map<int, Correspondent> correspondents;
final Map<int, DocumentType> documentTypes;
final Map<int, Tag> tags;
final Map<int, StoragePath> storagePaths;
@JsonKey()
final ViewType viewType;
const DocumentsState({
@@ -14,6 +19,10 @@ class DocumentsState extends DocumentPagingState {
super.filter = const DocumentFilter(),
super.hasLoaded = false,
super.isLoading = false,
this.correspondents = const {},
this.documentTypes = const {},
this.tags = const {},
this.storagePaths = const {},
});
List<int> get selectedIds => selection.map((e) => e.id).toList();
@@ -25,6 +34,10 @@ class DocumentsState extends DocumentPagingState {
DocumentFilter? filter,
List<DocumentModel>? selection,
ViewType? viewType,
Map<int, Correspondent>? correspondents,
Map<int, DocumentType>? documentTypes,
Map<int, Tag>? tags,
Map<int, StoragePath>? storagePaths,
}) {
return DocumentsState(
hasLoaded: hasLoaded ?? this.hasLoaded,
@@ -33,6 +46,10 @@ class DocumentsState extends DocumentPagingState {
filter: filter ?? this.filter,
selection: selection ?? this.selection,
viewType: viewType ?? this.viewType,
correspondents: correspondents ?? this.correspondents,
documentTypes: documentTypes ?? this.documentTypes,
tags: tags ?? this.tags,
storagePaths: storagePaths ?? this.storagePaths,
);
}