Started removing tight coupling

This commit is contained in:
Anton Stubenbord
2022-11-24 22:51:42 +01:00
parent eb5025e8ca
commit 5edbdabf26
27 changed files with 845 additions and 693 deletions

View File

@@ -6,12 +6,16 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_mobile/core/model/error_message.dart';
import 'package:injectable/injectable.dart';
import 'package:paperless_mobile/features/documents/model/document.model.dart';
import 'package:paperless_mobile/features/documents/repository/document_repository.dart';
@singleton
@injectable
class DocumentScannerCubit extends Cubit<List<File>> {
final DocumentRepository documentRepository;
static List<File> initialState = [];
DocumentScannerCubit() : super(initialState);
DocumentScannerCubit(this.documentRepository) : super(initialState);
void addScan(File file) => emit([...state, file]);
@@ -40,4 +44,30 @@ class DocumentScannerCubit extends Cubit<List<File>> {
throw const ErrorMessage(ErrorCode.scanRemoveFailed);
}
}
Future<void> uploadDocument(
Uint8List bytes,
String fileName, {
required String title,
required void Function(DocumentModel document)? onConsumptionFinished,
int? documentType,
int? correspondent,
Iterable<int> tags = const [],
DateTime? createdAt,
}) async {
await documentRepository.create(
bytes,
fileName,
title: title,
documentType: documentType,
correspondent: correspondent,
tags: tags,
createdAt: createdAt,
);
if (onConsumptionFinished != null) {
documentRepository
.waitForConsumptionFinished(fileName, title)
.then((value) => onConsumptionFinished(value));
}
}
}

View File

@@ -35,6 +35,7 @@ class DocumentUploadPage extends StatefulWidget {
final String? title;
final String? filename;
final void Function()? afterUpload;
final void Function(DocumentModel)? onSuccessfullyConsumed;
const DocumentUploadPage({
Key? key,
@@ -42,6 +43,7 @@ class DocumentUploadPage extends StatefulWidget {
this.afterUpload,
this.title,
this.filename,
this.onSuccessfullyConsumed,
}) : super(key: key);
@override
@@ -229,6 +231,7 @@ class _DocumentUploadPageState extends State<DocumentUploadPage> {
void _onSubmit() async {
if (_formKey.currentState?.saveAndValidate() ?? false) {
final cubit = BlocProvider.of<DocumentScannerCubit>(context);
try {
setState(() => _isUploadLoading = true);
@@ -240,17 +243,19 @@ class _DocumentUploadPageState extends State<DocumentUploadPage> {
final tags = fv[DocumentModel.tagsKey] as IdsTagsQuery;
final correspondent =
fv[DocumentModel.correspondentKey] as IdQueryParameter;
await BlocProvider.of<DocumentsCubit>(context).addDocument(
await cubit.uploadDocument(
widget.fileBytes,
_padWithPdfExtension(_formKey.currentState?.value[fkFileName]),
onConsumptionFinished: _onConsumptionFinished,
onConsumptionFinished: widget.onSuccessfullyConsumed,
title: title,
documentType: docType.id,
correspondent: correspondent.id,
tags: tags.ids,
createdAt: createdAt,
);
getIt<DocumentScannerCubit>().reset(); //TODO: Access via provider
cubit.reset(); //TODO: Access via provider
showSnackBar(context, S.of(context).documentUploadSuccessText);
Navigator.pop(context);
widget.afterUpload?.call();
@@ -275,24 +280,4 @@ class _DocumentUploadPageState extends State<DocumentUploadPage> {
String _formatFilename(String source) {
return source.replaceAll(RegExp(r"[\W_]"), "_");
}
void _onConsumptionFinished(DocumentModel document) {
// ScaffoldMessenger.of(rootScaffoldKey.currentContext!).showSnackBar(
// SnackBar(
// action: SnackBarAction(
// onPressed: () async {
// try {
// getIt<DocumentsCubit>().reloadDocuments();
// } on ErrorMessage catch (error, stackTrace) {
// showErrorMessage(context, error, stackTrace);
// }
// },
// label:
// S.of(context).documentUploadProcessingSuccessfulReloadActionText,
// ),
// content: Text(S.of(context).documentUploadProcessingSuccessfulText),
// ),
// );
getIt<PaperlessStatisticsCubit>().incrementInboxCount();
}
}

View File

@@ -8,7 +8,8 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mime/mime.dart';
import 'package:paperless_mobile/features/labels/bloc/label_bloc_provider.dart';
import 'package:paperless_mobile/core/bloc/paperless_statistics_cubit.dart';
import 'package:paperless_mobile/features/labels/bloc/global_state_bloc_provider.dart';
import 'package:paperless_mobile/core/global/constants.dart';
import 'package:paperless_mobile/core/model/error_message.dart';
import 'package:paperless_mobile/core/service/file_service.dart';
@@ -127,12 +128,17 @@ class _ScannerPageState extends State<ScannerPage>
final bytes = await doc.save();
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: getIt<DocumentsCubit>(),
child: LabelBlocProvider(
child: DocumentUploadPage(
fileBytes: bytes,
builder: (_) => GlobalStateBlocProvider(
additionalProviders: [
BlocProvider<DocumentScannerCubit>.value(
value: BlocProvider.of<DocumentScannerCubit>(context),
),
],
child: DocumentUploadPage(
fileBytes: bytes,
onSuccessfullyConsumed: (_) =>
BlocProvider.of<PaperlessStatisticsCubit>(context)
.updateStatistics(),
),
),
),
@@ -242,17 +248,20 @@ class _ScannerPageState extends State<ScannerPage>
// pdf
fileBytes = file.readAsBytesSync();
}
Navigator.push(
context,
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: getIt<DocumentsCubit>(),
child: LabelBlocProvider(
child: DocumentUploadPage(
filename: filename,
fileBytes: fileBytes,
builder: (_) => GlobalStateBlocProvider(
additionalProviders: [
BlocProvider<DocumentScannerCubit>.value(
value: BlocProvider.of<DocumentScannerCubit>(context),
),
],
child: DocumentUploadPage(
filename: filename,
fileBytes: fileBytes,
onSuccessfullyConsumed: (_) =>
BlocProvider.of<PaperlessStatisticsCubit>(context)
.updateStatistics(),
),
),
),