mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-10 12:07:58 -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user