feat: fix bug where tags were overwritten

This commit is contained in:
Anton Stubenbord
2023-12-16 14:10:09 +01:00
parent 029cc99582
commit 0bec1d594a
6 changed files with 114 additions and 24 deletions

View File

@@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/features/logging/data/logger.dart';
class LabelRepository extends ChangeNotifier {
final PaperlessLabelsApi _api;
@@ -57,8 +58,29 @@ class LabelRepository extends ChangeNotifier {
}
Future<Iterable<Tag>> findAllTags([Iterable<int>? ids]) async {
logger.fd(
"Loading ${ids?.isEmpty ?? true ? "all" : "a subset of"} tags"
"${ids?.isEmpty ?? true ? "" : " (${ids!.join(",")})"}...",
className: runtimeType.toString(),
methodName: "findAllTags",
);
final data = await _api.getTags(ids);
if (ids?.isNotEmpty ?? false) {
logger.fd(
"Successfully updated subset of tags: ${ids!.join(",")}",
className: runtimeType.toString(),
methodName: "findAllTags",
);
// Only update the tags that were requested, keep existing ones.
tags = {...tags, for (var tag in data) tag.id!: tag};
} else {
logger.fd(
"Successfully updated all tags.",
className: runtimeType.toString(),
methodName: "findAllTags",
);
tags = {for (var tag in data) tag.id!: tag};
}
notifyListeners();
return data;
}

View File

@@ -5,6 +5,7 @@ import 'package:freezed_annotation/freezed_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/logging/data/logger.dart';
part 'document_edit_state.dart';
part 'document_edit_cubit.freezed.dart';
@@ -25,37 +26,91 @@ class DocumentEditCubit extends Cubit<DocumentEditState> {
_notifier.addListener(
this,
onUpdated: (doc) {
emit(state.copyWith(document: doc));
emit(state.copyWith(
document: doc,
suggestions: null,
));
loadFieldSuggestions();
},
ids: [document.id],
);
}
Future<void> updateDocument(DocumentModel document) async {
logger.fi(
"Updating document ${document.id}...",
className: runtimeType.toString(),
methodName: "updateDocument",
);
final updated = await _docsApi.update(document);
logger.fi(
"Document ${document.id} successfully updated.",
className: runtimeType.toString(),
methodName: "updateDocument",
);
_notifier.notifyUpdated(updated);
// Reload changed labels (documentCount property changes with removal/add)
if (document.documentType != _initialDocument.documentType) {
logger.fd(
"Document type assigned to document ${document.id} has changed "
"(${_initialDocument.documentType} -> ${document.documentType}). "
"Reloading document type ${document.documentType}...",
className: runtimeType.toString(),
methodName: "updateDocument",
);
_labelRepository.findDocumentType(
(document.documentType ?? _initialDocument.documentType)!);
(document.documentType ?? _initialDocument.documentType)!,
);
}
if (document.correspondent != _initialDocument.correspondent) {
logger.fd(
"Correspondent assigned to document ${document.id} has changed "
"(${_initialDocument.correspondent} -> ${document.correspondent}). "
"Reloading correspondent ${document.correspondent}...",
className: runtimeType.toString(),
methodName: "updateDocument",
);
_labelRepository.findCorrespondent(
(document.correspondent ?? _initialDocument.correspondent)!);
}
if (document.storagePath != _initialDocument.storagePath) {
logger.fd(
"Storage path assigned to document ${document.id} has changed "
"(${_initialDocument.storagePath} -> ${document.storagePath}). "
"Reloading storage path ${document.storagePath}...",
className: runtimeType.toString(),
methodName: "updateDocument",
);
_labelRepository.findStoragePath(
(document.storagePath ?? _initialDocument.storagePath)!);
}
if (!const DeepCollectionEquality.unordered()
.equals(document.tags, _initialDocument.tags)) {
_labelRepository.findAllTags(document.tags);
.equals(document.tags.toList(), _initialDocument.tags.toList())) {
final tagsToReload = {...document.tags, ..._initialDocument.tags};
logger.fd(
"Tags assigned to document ${document.id} have changed "
"(${_initialDocument.tags.join(",")} -> ${document.tags.join(",")}). "
"Reloading tags ${tagsToReload.join(",")}...",
className: runtimeType.toString(),
methodName: "updateDocument",
);
_labelRepository.findAllTags(tagsToReload);
}
}
Future<void> loadFieldSuggestions() async {
logger.fi(
"Loading suggestions for document ${state.document.id}...",
className: runtimeType.toString(),
methodName: "loadFieldSuggestions",
);
final suggestions = await _docsApi.findSuggestions(state.document);
logger.fi(
"Found ${suggestions.suggestionsCount} suggestions for document ${state.document.id}.",
className: runtimeType.toString(),
methodName: "loadFieldSuggestions",
);
emit(state.copyWith(suggestions: suggestions));
}

View File

@@ -183,8 +183,12 @@ class _DocumentEditPageState extends State<DocumentEditPage>
);
}
Padding _buildEditForm(BuildContext context, DocumentEditState state,
FieldSuggestions? filteredSuggestions, UserModel currentUser) {
Padding _buildEditForm(
BuildContext context,
DocumentEditState state,
FieldSuggestions? filteredSuggestions,
UserModel currentUser,
) {
final labelRepository = context.watch<LabelRepository>();
return Padding(

View File

@@ -105,14 +105,18 @@ class _DocumentViewState extends State<DocumentView> {
body: PdfView(
controller: _controller,
onDocumentLoaded: (document) {
if (mounted) {
setState(() {
_totalPages = document.pagesCount;
});
}
},
onPageChanged: (page) {
if (mounted) {
setState(() {
_currentPage = page;
});
}
},
),
);

View File

@@ -10,16 +10,16 @@ class LabelCubit extends Cubit<LabelState> {
final LabelRepository labelRepository;
LabelCubit(this.labelRepository) : super(const LabelState()) {
labelRepository.addListener(
() {
labelRepository.addListener(_updateStateListener);
}
void _updateStateListener() {
emit(state.copyWith(
correspondents: labelRepository.correspondents,
documentTypes: labelRepository.documentTypes,
storagePaths: labelRepository.storagePaths,
tags: labelRepository.tags,
));
},
);
}
Future<void> reload({
@@ -130,6 +130,7 @@ class LabelCubit extends Cubit<LabelState> {
@override
Future<void> close() {
labelRepository.removeListener(_updateStateListener);
return super.close();
}
}

View File

@@ -117,8 +117,12 @@ class TagsFormField extends StatelessWidget {
scrollDirection: Axis.horizontal,
itemCount: displayedSuggestions.length,
itemBuilder: (context, index) {
print(options);
final suggestion =
options[displayedSuggestions.elementAt(index)]!;
options[displayedSuggestions.elementAt(index)];
if (suggestion == null) {
return SizedBox.shrink();
}
return ColoredChipWrapper(
child: ActionChip(
label: Text(suggestion.name),