Cleaned up code, implemented message queue to notify subscribers of document updates.

This commit is contained in:
Anton Stubenbord
2023-02-06 01:04:13 +01:00
parent 337c178be8
commit 4d7fab1839
111 changed files with 1412 additions and 1029 deletions

View File

@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.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:collection/collection.dart';
import 'package:paperless_mobile/core/repository/state/impl/correspondent_repository_state.dart';
@@ -16,31 +17,28 @@ class EditDocumentCubit extends Cubit<EditDocumentState> {
final DocumentModel _initialDocument;
final PaperlessDocumentsApi _docsApi;
final LabelRepository<Correspondent, CorrespondentRepositoryState>
_correspondentRepository;
final LabelRepository<DocumentType, DocumentTypeRepositoryState>
_documentTypeRepository;
final LabelRepository<StoragePath, StoragePathRepositoryState>
_storagePathRepository;
final LabelRepository<Tag, TagRepositoryState> _tagRepository;
final DocumentChangedNotifier _notifier;
final LabelRepository<Correspondent> _correspondentRepository;
final LabelRepository<DocumentType> _documentTypeRepository;
final LabelRepository<StoragePath> _storagePathRepository;
final LabelRepository<Tag> _tagRepository;
final List<StreamSubscription> _subscriptions = [];
EditDocumentCubit(
DocumentModel document, {
required PaperlessDocumentsApi documentsApi,
required LabelRepository<Correspondent, CorrespondentRepositoryState>
correspondentRepository,
required LabelRepository<DocumentType, DocumentTypeRepositoryState>
documentTypeRepository,
required LabelRepository<StoragePath, StoragePathRepositoryState>
storagePathRepository,
required LabelRepository<Tag, TagRepositoryState> tagRepository,
required LabelRepository<Correspondent> correspondentRepository,
required LabelRepository<DocumentType> documentTypeRepository,
required LabelRepository<StoragePath> storagePathRepository,
required LabelRepository<Tag> tagRepository,
required DocumentChangedNotifier notifier,
}) : _initialDocument = document,
_docsApi = documentsApi,
_correspondentRepository = correspondentRepository,
_documentTypeRepository = documentTypeRepository,
_storagePathRepository = storagePathRepository,
_tagRepository = tagRepository,
_notifier = notifier,
super(
EditDocumentState(
document: document,
@@ -50,6 +48,7 @@ class EditDocumentCubit extends Cubit<EditDocumentState> {
tags: tagRepository.current?.values ?? {},
),
) {
_notifier.subscribe(this, onUpdated: replace);
_subscriptions.add(
_correspondentRepository.values
.listen((v) => emit(state.copyWith(correspondents: v?.values))),
@@ -71,6 +70,8 @@ class EditDocumentCubit extends Cubit<EditDocumentState> {
Future<void> updateDocument(DocumentModel document) async {
final updated = await _docsApi.update(document);
_notifier.notifyUpdated(updated);
// Reload changed labels (documentCount property changes with removal/add)
if (document.documentType != _initialDocument.documentType) {
_documentTypeRepository
@@ -88,7 +89,10 @@ class EditDocumentCubit extends Cubit<EditDocumentState> {
.equals(document.tags, _initialDocument.tags)) {
_tagRepository.findAll(document.tags);
}
emit(state.copyWith(document: updated));
}
void replace(DocumentModel document) {
emit(state.copyWith(document: document));
}
@override
@@ -96,6 +100,7 @@ class EditDocumentCubit extends Cubit<EditDocumentState> {
for (final sub in _subscriptions) {
sub.cancel();
}
_notifier.unsubscribe(this);
return super.close();
}
}