feat: Improve notifications, add donation button, improved asn form field

This commit is contained in:
Anton Stubenbord
2023-03-06 22:21:14 +01:00
parent bd891a8658
commit 1172e54199
37 changed files with 985 additions and 305 deletions

View File

@@ -3,10 +3,13 @@ import 'dart:io';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:open_filex/open_filex.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/notifier/document_changed_notifier.dart';
import 'package:paperless_mobile/core/service/file_description.dart';
import 'package:paperless_mobile/core/service/file_service.dart';
import 'package:paperless_mobile/features/notifications/services/local_notification_service.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
@@ -15,15 +18,18 @@ part 'document_details_state.dart';
class DocumentDetailsCubit extends Cubit<DocumentDetailsState> {
final PaperlessDocumentsApi _api;
final DocumentChangedNotifier _notifier;
final LocalNotificationService _notificationService;
final List<StreamSubscription> _subscriptions = [];
DocumentDetailsCubit(
this._api,
this._notifier, {
this._notifier,
this._notificationService, {
required DocumentModel initialDocument,
}) : super(DocumentDetailsState(document: initialDocument)) {
_notifier.subscribe(this, onUpdated: replace);
loadSuggestions();
loadMetaData();
}
Future<void> delete(DocumentModel document) async {
@@ -36,6 +42,11 @@ class DocumentDetailsCubit extends Cubit<DocumentDetailsState> {
emit(state.copyWith(suggestions: suggestions));
}
Future<void> loadMetaData() async {
final metaData = await _api.getMetaData(state.document);
emit(state.copyWith(metaData: metaData));
}
Future<void> loadFullContent() async {
final doc = await _api.find(state.document.id);
if (doc == null) {
@@ -47,11 +58,20 @@ class DocumentDetailsCubit extends Cubit<DocumentDetailsState> {
));
}
Future<void> assignAsn(DocumentModel document) async {
if (document.archiveSerialNumber == null) {
final int asn = await _api.findNextAsn();
final updatedDocument =
await _api.update(document.copyWith(archiveSerialNumber: asn));
Future<void> assignAsn(
DocumentModel document, {
int? asn,
bool autoAssign = false,
}) async {
if (!autoAssign) {
final updatedDocument = await _api.update(
document.copyWith(archiveSerialNumber: () => asn),
);
_notifier.notifyUpdated(updatedDocument);
} else {
final int autoAsn = await _api.findNextAsn();
final updatedDocument = await _api
.update(document.copyWith(archiveSerialNumber: () => autoAsn));
_notifier.notifyUpdated(updatedDocument);
}
}
@@ -59,14 +79,19 @@ class DocumentDetailsCubit extends Cubit<DocumentDetailsState> {
Future<ResultType> openDocumentInSystemViewer() async {
final cacheDir = await FileService.temporaryDirectory;
final metaData = await _api.getMetaData(state.document);
final bytes = await _api.download(state.document);
if (state.metaData == null) {
await loadMetaData();
}
final file = File('${cacheDir.path}/${metaData.mediaFilename}')
..createSync(recursive: true)
..writeAsBytesSync(bytes);
await _api.downloadToFile(
state.document,
'${cacheDir.path}/${state.metaData!.mediaFilename}',
);
return OpenFilex.open(file.path, type: "application/pdf").then(
return OpenFilex.open(
'${cacheDir.path}/${state.metaData!.mediaFilename}',
type: "application/pdf",
).then(
(value) => value.type,
);
}
@@ -75,15 +100,60 @@ class DocumentDetailsCubit extends Cubit<DocumentDetailsState> {
emit(state.copyWith(document: document));
}
Future<void> shareDocument() async {
final documentBytes = await _api.download(state.document);
final dir = await getTemporaryDirectory();
final String path = "${dir.path}/${state.document.originalFileName}";
await File(path).writeAsBytes(documentBytes);
Future<void> downloadDocument({
bool downloadOriginal = false,
required String locale,
}) async {
if (state.metaData == null) {
await loadMetaData();
}
String filePath = _buildDownloadFilePath(
downloadOriginal,
await FileService.downloadsDirectory,
);
final desc = FileDescription.fromPath(
state.metaData!.mediaFilename
.replaceAll("/", " "), // Flatten directory structure
);
await _notificationService.notifyFileDownload(
document: state.document,
filename: "${desc.filename}.${desc.extension}",
filePath: filePath,
finished: false,
locale: locale,
);
await _api.downloadToFile(
state.document,
filePath,
original: downloadOriginal,
);
await _notificationService.notifyFileDownload(
document: state.document,
filename: "${desc.filename}.${desc.extension}",
filePath: filePath,
finished: true,
locale: locale,
);
debugPrint("Downloaded file to $filePath");
}
Future<void> shareDocument({bool shareOriginal = false}) async {
if (state.metaData == null) {
await loadMetaData();
}
String filePath = _buildDownloadFilePath(
shareOriginal,
await FileService.temporaryDirectory,
);
await _api.downloadToFile(
state.document,
filePath,
original: shareOriginal,
);
Share.shareXFiles(
[
XFile(
path,
filePath,
name: state.document.originalFileName,
mimeType: "application/pdf",
lastModified: state.document.modified,
@@ -93,12 +163,21 @@ class DocumentDetailsCubit extends Cubit<DocumentDetailsState> {
);
}
String _buildDownloadFilePath(bool original, Directory dir) {
final description = FileDescription.fromPath(
state.metaData!.mediaFilename
.replaceAll("/", " "), // Flatten directory structure
);
final extension = original ? description.extension : 'pdf';
return "${dir.path}/${description.filename}.$extension";
}
@override
Future<void> close() {
Future<void> close() async {
for (final element in _subscriptions) {
element.cancel();
await element.cancel();
}
_notifier.unsubscribe(this);
return super.close();
await super.close();
}
}