mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-09 10:08:00 -06:00
feat: view type stored in view/cubit, itself, cleanup coe
This commit is contained in:
106
lib/features/document_edit/cubit/document_edit_cubit.dart
Normal file
106
lib/features/document_edit/cubit/document_edit_cubit.dart
Normal file
@@ -0,0 +1,106 @@
|
||||
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';
|
||||
import 'package:paperless_mobile/core/repository/state/impl/document_type_repository_state.dart';
|
||||
import 'package:paperless_mobile/core/repository/state/impl/storage_path_repository_state.dart';
|
||||
import 'package:paperless_mobile/core/repository/state/impl/tag_repository_state.dart';
|
||||
|
||||
part 'document_edit_state.dart';
|
||||
|
||||
class DocumentEditCubit extends Cubit<DocumentEditState> {
|
||||
final DocumentModel _initialDocument;
|
||||
final PaperlessDocumentsApi _docsApi;
|
||||
|
||||
final DocumentChangedNotifier _notifier;
|
||||
final LabelRepository<Correspondent> _correspondentRepository;
|
||||
final LabelRepository<DocumentType> _documentTypeRepository;
|
||||
final LabelRepository<StoragePath> _storagePathRepository;
|
||||
final LabelRepository<Tag> _tagRepository;
|
||||
final List<StreamSubscription> _subscriptions = [];
|
||||
|
||||
DocumentEditCubit(
|
||||
DocumentModel document, {
|
||||
required PaperlessDocumentsApi documentsApi,
|
||||
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(
|
||||
DocumentEditState(
|
||||
document: document,
|
||||
correspondents: correspondentRepository.current?.values ?? {},
|
||||
documentTypes: documentTypeRepository.current?.values ?? {},
|
||||
storagePaths: storagePathRepository.current?.values ?? {},
|
||||
tags: tagRepository.current?.values ?? {},
|
||||
),
|
||||
) {
|
||||
_notifier.subscribe(this, onUpdated: replace);
|
||||
_subscriptions.add(
|
||||
_correspondentRepository.values
|
||||
.listen((v) => emit(state.copyWith(correspondents: v?.values))),
|
||||
);
|
||||
_subscriptions.add(
|
||||
_documentTypeRepository.values
|
||||
.listen((v) => emit(state.copyWith(documentTypes: v?.values))),
|
||||
);
|
||||
_subscriptions.add(
|
||||
_storagePathRepository.values
|
||||
.listen((v) => emit(state.copyWith(storagePaths: v?.values))),
|
||||
);
|
||||
_subscriptions.add(
|
||||
_tagRepository.values.listen(
|
||||
(v) => emit(state.copyWith(tags: v?.values)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
.find((document.documentType ?? _initialDocument.documentType)!);
|
||||
}
|
||||
if (document.correspondent != _initialDocument.correspondent) {
|
||||
_correspondentRepository
|
||||
.find((document.correspondent ?? _initialDocument.correspondent)!);
|
||||
}
|
||||
if (document.storagePath != _initialDocument.storagePath) {
|
||||
_storagePathRepository
|
||||
.find((document.storagePath ?? _initialDocument.storagePath)!);
|
||||
}
|
||||
if (!const DeepCollectionEquality.unordered()
|
||||
.equals(document.tags, _initialDocument.tags)) {
|
||||
_tagRepository.findAll(document.tags);
|
||||
}
|
||||
}
|
||||
|
||||
void replace(DocumentModel document) {
|
||||
emit(state.copyWith(document: document));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
for (final sub in _subscriptions) {
|
||||
sub.cancel();
|
||||
}
|
||||
_notifier.unsubscribe(this);
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
43
lib/features/document_edit/cubit/document_edit_state.dart
Normal file
43
lib/features/document_edit/cubit/document_edit_state.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
part of 'document_edit_cubit.dart';
|
||||
|
||||
class DocumentEditState extends Equatable {
|
||||
final DocumentModel document;
|
||||
|
||||
final Map<int, Correspondent> correspondents;
|
||||
final Map<int, DocumentType> documentTypes;
|
||||
final Map<int, StoragePath> storagePaths;
|
||||
final Map<int, Tag> tags;
|
||||
|
||||
const DocumentEditState({
|
||||
required this.correspondents,
|
||||
required this.documentTypes,
|
||||
required this.storagePaths,
|
||||
required this.tags,
|
||||
required this.document,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object> get props => [
|
||||
correspondents,
|
||||
documentTypes,
|
||||
storagePaths,
|
||||
tags,
|
||||
document,
|
||||
];
|
||||
|
||||
DocumentEditState copyWith({
|
||||
Map<int, Correspondent>? correspondents,
|
||||
Map<int, DocumentType>? documentTypes,
|
||||
Map<int, StoragePath>? storagePaths,
|
||||
Map<int, Tag>? tags,
|
||||
DocumentModel? document,
|
||||
}) {
|
||||
return DocumentEditState(
|
||||
document: document ?? this.document,
|
||||
correspondents: correspondents ?? this.correspondents,
|
||||
documentTypes: documentTypes ?? this.documentTypes,
|
||||
storagePaths: storagePaths ?? this.storagePaths,
|
||||
tags: tags ?? this.tags,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user