mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-14 22:12:18 -06:00
Removed suggestions from inbox, added translations, added paging to inbox, visual updates, changed default matching algorithm to auto
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hydrated_bloc/hydrated_bloc.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/core/repository/label_repository.dart';
|
||||
@@ -9,8 +7,9 @@ import 'package:paperless_mobile/core/repository/state/impl/correspondent_reposi
|
||||
import 'package:paperless_mobile/core/repository/state/impl/document_type_repository_state.dart';
|
||||
import 'package:paperless_mobile/core/repository/state/impl/tag_repository_state.dart';
|
||||
import 'package:paperless_mobile/features/inbox/bloc/state/inbox_state.dart';
|
||||
import 'package:paperless_mobile/features/paged_document_view/documents_paging_mixin.dart';
|
||||
|
||||
class InboxCubit extends HydratedCubit<InboxState> {
|
||||
class InboxCubit extends HydratedCubit<InboxState> with DocumentsPagingMixin {
|
||||
final LabelRepository<Tag, TagRepositoryState> _tagsRepository;
|
||||
final LabelRepository<Correspondent, CorrespondentRepositoryState>
|
||||
_correspondentRepository;
|
||||
@@ -21,6 +20,9 @@ class InboxCubit extends HydratedCubit<InboxState> {
|
||||
|
||||
final List<StreamSubscription> _subscriptions = [];
|
||||
|
||||
@override
|
||||
PaperlessDocumentsApi get api => _documentsApi;
|
||||
|
||||
InboxCubit(
|
||||
this._tagsRepository,
|
||||
this._documentsApi,
|
||||
@@ -67,105 +69,83 @@ class InboxCubit extends HydratedCubit<InboxState> {
|
||||
final inboxTags = await _tagsRepository.findAll().then(
|
||||
(tags) => tags.where((t) => t.isInboxTag ?? false).map((t) => t.id!),
|
||||
);
|
||||
|
||||
if (inboxTags.isEmpty) {
|
||||
// no inbox tags = no inbox items.
|
||||
return emit(
|
||||
state.copyWith(
|
||||
isLoaded: true,
|
||||
inboxItems: [],
|
||||
hasLoaded: true,
|
||||
value: [],
|
||||
inboxTags: [],
|
||||
),
|
||||
);
|
||||
}
|
||||
final inboxDocuments = await _documentsApi
|
||||
.findAll(DocumentFilter(
|
||||
tags: AnyAssignedTagsQuery(tagIds: inboxTags),
|
||||
sortField: SortField.added,
|
||||
))
|
||||
.then((psr) => psr.results);
|
||||
final newState = state.copyWith(
|
||||
isLoaded: true,
|
||||
inboxItems: inboxDocuments,
|
||||
inboxTags: inboxTags,
|
||||
return updateFilter(
|
||||
filter: DocumentFilter(
|
||||
sortField: SortField.added,
|
||||
tags: IdsTagsQuery.fromIds(inboxTags),
|
||||
),
|
||||
);
|
||||
emit(newState);
|
||||
}
|
||||
|
||||
///
|
||||
/// Updates the document with all inbox tags removed and removes the document
|
||||
/// from the currently loaded inbox documents.
|
||||
/// from the inbox.
|
||||
///
|
||||
Future<Iterable<int>> remove(DocumentModel document) async {
|
||||
Future<Iterable<int>> removeFromInbox(DocumentModel document) async {
|
||||
final tagsToRemove =
|
||||
document.tags.toSet().intersection(state.inboxTags.toSet());
|
||||
|
||||
final updatedTags = {...document.tags}..removeAll(tagsToRemove);
|
||||
|
||||
await _documentsApi.update(
|
||||
document.copyWith(
|
||||
tags: updatedTags,
|
||||
overwriteTags: true,
|
||||
),
|
||||
await api.update(
|
||||
document.copyWith(tags: updatedTags),
|
||||
);
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoaded: true,
|
||||
inboxItems: state.inboxItems.where((doc) => doc.id != document.id),
|
||||
),
|
||||
);
|
||||
|
||||
await remove(document);
|
||||
return tagsToRemove;
|
||||
}
|
||||
|
||||
///
|
||||
/// Adds the previously removed tags to the document and performs an update.
|
||||
///
|
||||
Future<void> undoRemove(
|
||||
Future<void> undoRemoveFromInbox(
|
||||
DocumentModel document,
|
||||
Iterable<int> removedTags,
|
||||
) async {
|
||||
final updatedDoc = document.copyWith(
|
||||
tags: {...document.tags, ...removedTags},
|
||||
overwriteTags: true,
|
||||
);
|
||||
await _documentsApi.update(updatedDoc);
|
||||
emit(state.copyWith(
|
||||
isLoaded: true,
|
||||
inboxItems: [...state.inboxItems, updatedDoc]
|
||||
..sort((d1, d2) => d2.added.compareTo(d1.added)),
|
||||
));
|
||||
return reload();
|
||||
}
|
||||
|
||||
///
|
||||
/// Removes inbox tags from all documents in the inbox.
|
||||
///
|
||||
Future<void> clearInbox() async {
|
||||
await _documentsApi.bulkAction(
|
||||
BulkModifyTagsAction.removeTags(
|
||||
state.inboxItems.map((e) => e.id),
|
||||
state.inboxTags,
|
||||
),
|
||||
);
|
||||
emit(state.copyWith(
|
||||
isLoaded: true,
|
||||
inboxItems: [],
|
||||
));
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
await _documentsApi.bulkAction(
|
||||
BulkModifyTagsAction.removeTags(
|
||||
state.documents.map((e) => e.id),
|
||||
state.inboxTags,
|
||||
),
|
||||
);
|
||||
emit(state.copyWith(
|
||||
hasLoaded: true,
|
||||
value: [],
|
||||
));
|
||||
} finally {
|
||||
emit(state.copyWith(isLoading: false));
|
||||
}
|
||||
}
|
||||
|
||||
void replaceUpdatedDocument(DocumentModel document) {
|
||||
if (document.tags.any((id) => state.inboxTags.contains(id))) {
|
||||
// If replaced document still has inbox tag assigned:
|
||||
emit(state.copyWith(
|
||||
inboxItems:
|
||||
state.inboxItems.map((e) => e.id == document.id ? document : e),
|
||||
));
|
||||
replace(document);
|
||||
} else {
|
||||
// Remove tag from inbox.
|
||||
emit(
|
||||
state.copyWith(
|
||||
inboxItems:
|
||||
state.inboxItems.where((element) => element.id != document.id)),
|
||||
);
|
||||
// Remove document from inbox.
|
||||
remove(document);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,48 +154,10 @@ class InboxCubit extends HydratedCubit<InboxState> {
|
||||
final int asn = await _documentsApi.findNextAsn();
|
||||
final updatedDocument = await _documentsApi
|
||||
.update(document.copyWith(archiveSerialNumber: asn));
|
||||
emit(
|
||||
state.copyWith(
|
||||
inboxItems: state.inboxItems
|
||||
.map((e) => e.id == document.id ? updatedDocument : e)),
|
||||
);
|
||||
replace(updatedDocument);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateDocument(DocumentModel document) async {
|
||||
final updatedDocument = await _documentsApi.update(document);
|
||||
emit(
|
||||
state.copyWith(
|
||||
inboxItems: state.inboxItems.map(
|
||||
(e) => e.id == document.id ? updatedDocument : e,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> deleteDocument(DocumentModel document) async {
|
||||
int deletedId = await _documentsApi.delete(document);
|
||||
emit(
|
||||
state.copyWith(
|
||||
inboxItems: state.inboxItems.where(
|
||||
(element) => element.id != deletedId,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void loadSuggestions() {
|
||||
state.inboxItems
|
||||
.whereNot((doc) => state.suggestions.containsKey(doc.id))
|
||||
.map((e) => _documentsApi.findSuggestions(e))
|
||||
.forEach((suggestion) async {
|
||||
final s = await suggestion;
|
||||
emit(state.copyWith(
|
||||
suggestions: {...state.suggestions, s.documentId!: s},
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
void acknowledgeHint() {
|
||||
emit(state.copyWith(isHintAcknowledged: true));
|
||||
}
|
||||
|
||||
@@ -1,57 +1,56 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:paperless_mobile/features/paged_document_view/model/documents_paged_state.dart';
|
||||
|
||||
part 'inbox_state.g.dart';
|
||||
|
||||
@JsonSerializable(
|
||||
ignoreUnannotated: true,
|
||||
)
|
||||
class InboxState with EquatableMixin {
|
||||
final bool isLoaded;
|
||||
|
||||
class InboxState extends DocumentsPagedState {
|
||||
final Iterable<int> inboxTags;
|
||||
|
||||
final Iterable<DocumentModel> inboxItems;
|
||||
|
||||
final Map<int, Tag> availableTags;
|
||||
|
||||
final Map<int, DocumentType> availableDocumentTypes;
|
||||
|
||||
final Map<int, Correspondent> availableCorrespondents;
|
||||
|
||||
final Map<int, FieldSuggestions> suggestions;
|
||||
@JsonKey()
|
||||
final bool isHintAcknowledged;
|
||||
|
||||
const InboxState({
|
||||
this.isLoaded = false,
|
||||
super.hasLoaded = false,
|
||||
super.isLoading = false,
|
||||
super.value = const [],
|
||||
super.filter = const DocumentFilter(),
|
||||
this.inboxTags = const [],
|
||||
this.inboxItems = const [],
|
||||
this.isHintAcknowledged = false,
|
||||
this.availableTags = const {},
|
||||
this.availableDocumentTypes = const {},
|
||||
this.availableCorrespondents = const {},
|
||||
this.suggestions = const {},
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
isLoaded,
|
||||
hasLoaded,
|
||||
isLoading,
|
||||
value,
|
||||
filter,
|
||||
inboxTags,
|
||||
inboxItems,
|
||||
documents,
|
||||
isHintAcknowledged,
|
||||
availableTags,
|
||||
availableDocumentTypes,
|
||||
availableCorrespondents,
|
||||
suggestions,
|
||||
];
|
||||
|
||||
InboxState copyWith({
|
||||
bool? isLoaded,
|
||||
bool? hasLoaded,
|
||||
bool? isLoading,
|
||||
Iterable<int>? inboxTags,
|
||||
Iterable<DocumentModel>? inboxItems,
|
||||
List<PagedSearchResult<DocumentModel>>? value,
|
||||
DocumentFilter? filter,
|
||||
bool? isHintAcknowledged,
|
||||
Map<int, Tag>? availableTags,
|
||||
Map<int, Correspondent>? availableCorrespondents,
|
||||
@@ -59,8 +58,9 @@ class InboxState with EquatableMixin {
|
||||
Map<int, FieldSuggestions>? suggestions,
|
||||
}) {
|
||||
return InboxState(
|
||||
isLoaded: isLoaded ?? this.isLoaded,
|
||||
inboxItems: inboxItems ?? this.inboxItems,
|
||||
hasLoaded: hasLoaded ?? super.hasLoaded,
|
||||
isLoading: isLoading ?? super.isLoading,
|
||||
value: value ?? super.value,
|
||||
inboxTags: inboxTags ?? this.inboxTags,
|
||||
isHintAcknowledged: isHintAcknowledged ?? this.isHintAcknowledged,
|
||||
availableCorrespondents:
|
||||
@@ -68,7 +68,7 @@ class InboxState with EquatableMixin {
|
||||
availableDocumentTypes:
|
||||
availableDocumentTypes ?? this.availableDocumentTypes,
|
||||
availableTags: availableTags ?? this.availableTags,
|
||||
suggestions: suggestions ?? this.suggestions,
|
||||
filter: filter ?? super.filter,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -76,4 +76,20 @@ class InboxState with EquatableMixin {
|
||||
_$InboxStateFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$InboxStateToJson(this);
|
||||
|
||||
@override
|
||||
InboxState copyWithPaged({
|
||||
bool? hasLoaded,
|
||||
bool? isLoading,
|
||||
List<PagedSearchResult<DocumentModel>>? value,
|
||||
DocumentFilter?
|
||||
filter, // Ignored as filter does not change while inbox is open
|
||||
}) {
|
||||
return copyWith(
|
||||
hasLoaded: hasLoaded,
|
||||
isLoading: isLoading,
|
||||
value: value,
|
||||
filter: filter,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user