import 'dart:typed_data'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:paperless_mobile/features/documents/bloc/documents_state.dart'; import 'package:paperless_mobile/features/documents/model/document.model.dart'; import 'package:paperless_mobile/features/documents/model/document_filter.dart'; import 'package:paperless_mobile/features/documents/model/paged_search_result.dart'; import 'package:paperless_mobile/features/documents/repository/document_repository.dart'; import 'package:injectable/injectable.dart'; @singleton class DocumentsCubit extends Cubit { final DocumentRepository documentRepository; DocumentsCubit(this.documentRepository) : super(DocumentsState.initial); Future addDocument( Uint8List bytes, String fileName, { required String title, required void Function(DocumentModel document) onConsumptionFinished, int? documentType, int? correspondent, List? tags, DateTime? createdAt, }) async { await documentRepository.create( bytes, fileName, title: title, documentType: documentType, correspondent: correspondent, tags: tags, createdAt: createdAt, ); // documentRepository // .waitForConsumptionFinished(fileName, title) // .then((value) => onConsumptionFinished(value)); } Future removeDocument(DocumentModel document) async { await documentRepository.delete(document); return await reloadDocuments(); } Future bulkRemoveDocuments(List documents) async { await documentRepository.bulkDelete(documents); return await reloadDocuments(); } Future updateDocument(DocumentModel document) async { await documentRepository.update(document); await reloadDocuments(); } Future loadDocuments() async { final result = await documentRepository.find(state.filter); emit(DocumentsState( isLoaded: true, value: [...state.value, result], filter: state.filter, )); } Future reloadDocuments() async { if (state.currentPageNumber >= 5) { return _bulkReloadDocuments(); } var newPages = []; for (final page in state.value) { final result = await documentRepository.find(state.filter.copyWith(page: page.pageKey)); newPages.add(result); } emit(DocumentsState(isLoaded: true, value: newPages, filter: state.filter)); } Future _bulkReloadDocuments() async { final result = await documentRepository .find(state.filter.copyWith(page: 1, pageSize: state.documents.length)); emit(DocumentsState(isLoaded: true, value: [result], filter: state.filter)); } Future loadMore() async { if (state.isLastPageLoaded) { return; } final newFilter = state.filter.copyWith(page: state.filter.page + 1); final result = await documentRepository.find(newFilter); emit(DocumentsState(isLoaded: true, value: [...state.value, result], filter: newFilter)); } Future assignAsn(DocumentModel document) async { if (document.archiveSerialNumber == null) { final int asn = await documentRepository.findNextAsn(); updateDocument(document.copyWith(archiveSerialNumber: asn)); } } /// /// Update filter state and automatically reload documents. Always resets page to 1. /// Use [DocumentsCubit.loadMore] to load more data. Future updateFilter({ final DocumentFilter filter = DocumentFilter.initial, }) async { final result = await documentRepository.find(filter.copyWith(page: 1)); emit(DocumentsState(filter: filter, value: [result], isLoaded: true)); } /// /// Convenience method which allows to directly use [DocumentFilter.copyWith] on the current filter. /// Future updateCurrentFilter(final DocumentFilter Function(DocumentFilter) transformFn) { return updateFilter(filter: transformFn(state.filter)); } void toggleDocumentSelection(DocumentModel model) { if (state.selection.contains(model)) { emit( state.copyWith( selection: state.selection.where((element) => element.id != model.id).toList(), ), ); } else { emit( state.copyWith(selection: [...state.selection, model]), ); } } void resetSelection() { emit(state.copyWith(selection: [])); } void reset() { emit(DocumentsState.initial); } }