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

@@ -5,6 +5,7 @@ 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/core/repository/label_repository_state.dart';
import 'package:paperless_mobile/features/paged_document_view/cubit/paged_documents_state.dart';
import 'package:paperless_mobile/features/paged_document_view/cubit/document_paging_bloc_mixin.dart';
@@ -13,9 +14,7 @@ part 'inbox_state.dart';
class InboxCubit extends HydratedCubit<InboxState>
with DocumentPagingBlocMixin {
final LabelRepository<Tag> _tagsRepository;
final LabelRepository<Correspondent> _correspondentRepository;
final LabelRepository<DocumentType> _documentTypeRepository;
final LabelRepository _labelRepository;
final PaperlessDocumentsApi _documentsApi;
@@ -24,27 +23,15 @@ class InboxCubit extends HydratedCubit<InboxState>
final PaperlessServerStatsApi _statsApi;
final List<StreamSubscription> _subscriptions = [];
@override
PaperlessDocumentsApi get api => _documentsApi;
InboxCubit(
this._tagsRepository,
this._documentsApi,
this._correspondentRepository,
this._documentTypeRepository,
this._statsApi,
this._labelRepository,
this.notifier,
) : super(
InboxState(
availableCorrespondents:
_correspondentRepository.current?.values ?? {},
availableDocumentTypes:
_documentTypeRepository.current?.values ?? {},
availableTags: _tagsRepository.current?.values ?? {},
),
) {
) : super(InboxState(labels: _labelRepository.state)) {
notifier.subscribe(
this,
onDeleted: remove,
@@ -60,28 +47,11 @@ class InboxCubit extends HydratedCubit<InboxState>
}
},
);
_subscriptions.add(
_tagsRepository.values.listen((event) {
if (event?.hasLoaded ?? false) {
emit(state.copyWith(availableTags: event!.values));
}
}),
);
_subscriptions.add(
_correspondentRepository.values.listen((event) {
if (event?.hasLoaded ?? false) {
emit(state.copyWith(
availableCorrespondents: event!.values,
));
}
}),
);
_subscriptions.add(
_documentTypeRepository.values.listen((event) {
if (event?.hasLoaded ?? false) {
emit(state.copyWith(availableDocumentTypes: event!.values));
}
}),
_labelRepository.subscribe(
this,
onChanged: (labels) {
emit(state.copyWith(labels: labels));
},
);
refreshItemsInInboxCount(false);
@@ -105,7 +75,7 @@ class InboxCubit extends HydratedCubit<InboxState>
/// Fetches inbox tag ids and loads the inbox items (documents).
///
Future<void> loadInbox() async {
final inboxTags = await _tagsRepository.findAll().then(
final inboxTags = await _labelRepository.findAllTags().then(
(tags) => tags.where((t) => t.isInboxTag ?? false).map((t) => t.id!),
);
@@ -133,7 +103,7 @@ class InboxCubit extends HydratedCubit<InboxState>
///
Future<void> reloadInbox() async {
emit(state.copyWith(hasLoaded: false, isLoading: true));
final inboxTags = await _tagsRepository.findAll().then(
final inboxTags = await _labelRepository.findAllTags().then(
(tags) => tags.where((t) => t.isInboxTag ?? false).map((t) => t.id!),
);
@@ -239,9 +209,7 @@ class InboxCubit extends HydratedCubit<InboxState>
@override
Future<void> close() {
for (var sub in _subscriptions) {
sub.cancel();
}
_labelRepository.unsubscribe(this);
return super.close();
}
}

View File

@@ -4,11 +4,7 @@ part of 'inbox_cubit.dart';
class InboxState extends DocumentPagingState {
final Iterable<int> inboxTags;
final Map<int, Tag> availableTags;
final Map<int, DocumentType> availableDocumentTypes;
final Map<int, Correspondent> availableCorrespondents;
final LabelRepositoryState labels;
final int itemsInInboxCount;
@@ -22,10 +18,8 @@ class InboxState extends DocumentPagingState {
super.filter = const DocumentFilter(),
this.inboxTags = const [],
this.isHintAcknowledged = false,
this.availableTags = const {},
this.availableDocumentTypes = const {},
this.availableCorrespondents = const {},
this.itemsInInboxCount = 0,
this.labels = const LabelRepositoryState(),
});
@override
@@ -37,10 +31,8 @@ class InboxState extends DocumentPagingState {
inboxTags,
documents,
isHintAcknowledged,
availableTags,
availableDocumentTypes,
availableCorrespondents,
itemsInInboxCount,
labels,
];
InboxState copyWith({
@@ -50,9 +42,7 @@ class InboxState extends DocumentPagingState {
List<PagedSearchResult<DocumentModel>>? value,
DocumentFilter? filter,
bool? isHintAcknowledged,
Map<int, Tag>? availableTags,
Map<int, Correspondent>? availableCorrespondents,
Map<int, DocumentType>? availableDocumentTypes,
LabelRepositoryState? labels,
Map<int, FieldSuggestions>? suggestions,
int? itemsInInboxCount,
}) {
@@ -62,11 +52,7 @@ class InboxState extends DocumentPagingState {
value: value ?? super.value,
inboxTags: inboxTags ?? this.inboxTags,
isHintAcknowledged: isHintAcknowledged ?? this.isHintAcknowledged,
availableCorrespondents:
availableCorrespondents ?? this.availableCorrespondents,
availableDocumentTypes:
availableDocumentTypes ?? this.availableDocumentTypes,
availableTags: availableTags ?? this.availableTags,
labels: labels ?? this.labels,
filter: filter ?? super.filter,
itemsInInboxCount: itemsInInboxCount ?? this.itemsInInboxCount,
);