mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-10 06:07:57 -06:00
Reworked inbox, added more information and allow suggestions to be directly clicked
This commit is contained in:
@@ -1,16 +1,64 @@
|
||||
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';
|
||||
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/tag_repository_state.dart';
|
||||
import 'package:paperless_mobile/features/inbox/bloc/state/inbox_state.dart';
|
||||
|
||||
class InboxCubit extends HydratedCubit<InboxState> {
|
||||
final LabelRepository<Tag, TagRepositoryState> _tagsRepository;
|
||||
final LabelRepository<Correspondent, CorrespondentRepositoryState>
|
||||
_correspondentRepository;
|
||||
final LabelRepository<DocumentType, DocumentTypeRepositoryState>
|
||||
_documentTypeRepository;
|
||||
|
||||
final PaperlessDocumentsApi _documentsApi;
|
||||
|
||||
InboxCubit(this._tagsRepository, this._documentsApi)
|
||||
: super(const InboxState());
|
||||
final List<StreamSubscription> _subscriptions = [];
|
||||
|
||||
InboxCubit(
|
||||
this._tagsRepository,
|
||||
this._documentsApi,
|
||||
this._correspondentRepository,
|
||||
this._documentTypeRepository,
|
||||
) : super(
|
||||
InboxState(
|
||||
availableCorrespondents:
|
||||
_correspondentRepository.current?.values ?? {},
|
||||
availableDocumentTypes:
|
||||
_documentTypeRepository.current?.values ?? {},
|
||||
availableTags: _tagsRepository.current?.values ?? {},
|
||||
),
|
||||
) {
|
||||
_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));
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
/// Fetches inbox tag ids and loads the inbox items (documents).
|
||||
@@ -135,6 +183,39 @@ class InboxCubit extends HydratedCubit<InboxState> {
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
Future.wait(state.inboxItems
|
||||
.whereNot((doc) => state.suggestions.containsKey(doc.id))
|
||||
.map((e) => _documentsApi.findSuggestions(e))).then((results) {
|
||||
emit(state.copyWith(suggestions: {
|
||||
...state.suggestions,
|
||||
for (var r in results) r.documentId!: r
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
void acknowledgeHint() {
|
||||
emit(state.copyWith(isHintAcknowledged: true));
|
||||
}
|
||||
@@ -148,4 +229,12 @@ class InboxCubit extends HydratedCubit<InboxState> {
|
||||
Map<String, dynamic> toJson(InboxState state) {
|
||||
return state.toJson();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
_subscriptions.forEach((element) {
|
||||
element.cancel();
|
||||
});
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,17 +5,24 @@ import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'inbox_state.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
@JsonSerializable(
|
||||
ignoreUnannotated: true,
|
||||
)
|
||||
class InboxState with EquatableMixin {
|
||||
@JsonKey(ignore: true)
|
||||
final bool isLoaded;
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
final Iterable<int> inboxTags;
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
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({
|
||||
@@ -23,6 +30,10 @@ class InboxState with EquatableMixin {
|
||||
this.inboxTags = const [],
|
||||
this.inboxItems = const [],
|
||||
this.isHintAcknowledged = false,
|
||||
this.availableTags = const {},
|
||||
this.availableDocumentTypes = const {},
|
||||
this.availableCorrespondents = const {},
|
||||
this.suggestions = const {},
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -31,6 +42,10 @@ class InboxState with EquatableMixin {
|
||||
inboxTags,
|
||||
inboxItems,
|
||||
isHintAcknowledged,
|
||||
availableTags,
|
||||
availableDocumentTypes,
|
||||
availableCorrespondents,
|
||||
suggestions,
|
||||
];
|
||||
|
||||
InboxState copyWith({
|
||||
@@ -38,12 +53,22 @@ class InboxState with EquatableMixin {
|
||||
Iterable<int>? inboxTags,
|
||||
Iterable<DocumentModel>? inboxItems,
|
||||
bool? isHintAcknowledged,
|
||||
Map<int, Tag>? availableTags,
|
||||
Map<int, Correspondent>? availableCorrespondents,
|
||||
Map<int, DocumentType>? availableDocumentTypes,
|
||||
Map<int, FieldSuggestions>? suggestions,
|
||||
}) {
|
||||
return InboxState(
|
||||
isLoaded: isLoaded ?? this.isLoaded,
|
||||
inboxItems: inboxItems ?? this.inboxItems,
|
||||
inboxTags: inboxTags ?? this.inboxTags,
|
||||
isHintAcknowledged: isHintAcknowledged ?? this.isHintAcknowledged,
|
||||
availableCorrespondents:
|
||||
availableCorrespondents ?? this.availableCorrespondents,
|
||||
availableDocumentTypes:
|
||||
availableDocumentTypes ?? this.availableDocumentTypes,
|
||||
availableTags: availableTags ?? this.availableTags,
|
||||
suggestions: suggestions ?? this.suggestions,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user