Small refactorings to inbox

This commit is contained in:
Anton Stubenbord
2023-02-02 19:52:44 +01:00
parent 748cd21713
commit ba5a1fcbc7
6 changed files with 347 additions and 329 deletions

View File

@@ -18,16 +18,20 @@ class InboxCubit extends HydratedCubit<InboxState> with PagedDocumentsMixin {
final PaperlessDocumentsApi _documentsApi;
final PaperlessServerStatsApi _statsApi;
final List<StreamSubscription> _subscriptions = [];
@override
PaperlessDocumentsApi get api => _documentsApi;
Timer? _taskTimer;
InboxCubit(
this._tagsRepository,
this._documentsApi,
this._correspondentRepository,
this._documentTypeRepository,
this._statsApi,
) : super(
InboxState(
availableCorrespondents:
@@ -60,6 +64,15 @@ class InboxCubit extends HydratedCubit<InboxState> with PagedDocumentsMixin {
}
}),
);
//TODO: Do this properly in a background task.
_taskTimer = Timer.periodic(const Duration(seconds: 5), (timer) {
refreshItemsInInboxCount();
});
}
void refreshItemsInInboxCount() async {
final stats = await _statsApi.getServerStatistics();
emit(state.copyWith(itemsInInboxCount: stats.documentsInInbox));
}
///
@@ -175,9 +188,10 @@ class InboxCubit extends HydratedCubit<InboxState> with PagedDocumentsMixin {
@override
Future<void> close() {
_subscriptions.forEach((element) {
element.cancel();
});
_taskTimer?.cancel();
for (var sub in _subscriptions) {
sub.cancel();
}
return super.close();
}
}

View File

@@ -16,6 +16,8 @@ class InboxState extends PagedDocumentsState {
final Map<int, Correspondent> availableCorrespondents;
final int itemsInInboxCount;
@JsonKey()
final bool isHintAcknowledged;
@@ -29,6 +31,7 @@ class InboxState extends PagedDocumentsState {
this.availableTags = const {},
this.availableDocumentTypes = const {},
this.availableCorrespondents = const {},
this.itemsInInboxCount = 0,
});
@override
@@ -43,6 +46,7 @@ class InboxState extends PagedDocumentsState {
availableTags,
availableDocumentTypes,
availableCorrespondents,
itemsInInboxCount,
];
InboxState copyWith({
@@ -56,6 +60,7 @@ class InboxState extends PagedDocumentsState {
Map<int, Correspondent>? availableCorrespondents,
Map<int, DocumentType>? availableDocumentTypes,
Map<int, FieldSuggestions>? suggestions,
int? itemsInInboxCount,
}) {
return InboxState(
hasLoaded: hasLoaded ?? super.hasLoaded,
@@ -69,6 +74,7 @@ class InboxState extends PagedDocumentsState {
availableDocumentTypes ?? this.availableDocumentTypes,
availableTags: availableTags ?? this.availableTags,
filter: filter ?? super.filter,
itemsInInboxCount: itemsInInboxCount ?? this.itemsInInboxCount,
);
}