Skip to content

Commit

Permalink
Merge pull request #99 from dreautall/attachduringcreate
Browse files Browse the repository at this point in the history
allow attachment during transaction creation
  • Loading branch information
dreautall authored Jul 25, 2023
2 parents 48970c2 + 149c9f9 commit 58a4a32
Show file tree
Hide file tree
Showing 2 changed files with 383 additions and 251 deletions.
84 changes: 74 additions & 10 deletions lib/pages/transaction.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -805,6 +807,7 @@ class _TransactionPageState extends State<TransactionPage>
ScaffoldMessenger.of(context);
final FireflyIii api = context.read<FireflyService>().api;
final NavigatorState nav = Navigator.of(context);
final AuthUser? user = context.read<FireflyService>().user;

// Sanity checks
String? error;
Expand All @@ -815,6 +818,9 @@ class _TransactionPageState extends State<TransactionPage>
if (_titleTextController.text.isEmpty) {
error = S.of(context).transactionErrorTitle;
}
if (user == null) {
error = S.of(context).errorAPIUnavailable;
}
if (error != null) {
msg.showSnackBar(SnackBar(
content: Text(error),
Expand Down Expand Up @@ -997,6 +1003,72 @@ class _TransactionPageState extends State<TransactionPage>
return;
}

// Upload attachments if required
if ((_attachments?.isNotEmpty ?? false) &&
_transactionJournalIDs
.firstWhereOrNull((String? e) => e != null) ==
null) {
log.fine(() =>
"uploading ${_attachments!.length} attachments");
TransactionSplit? tx = resp
.body?.data.attributes.transactions
.firstWhereOrNull((TransactionSplit e) =>
e.transactionJournalId != null);
if (tx != null) {
String txId = tx.transactionJournalId!;
log.finest(() => "uploading to txId $txId");
for (AttachmentRead attachment in _attachments!) {
log.finest(() =>
"uploading attachment ${attachment.id}: ${attachment.attributes.filename}");
final Response<AttachmentSingle> respAttachment =
await api.v1AttachmentsPost(
body: AttachmentStore(
filename: attachment.attributes.filename,
attachableType: AttachableType.transactionjournal,
attachableId: txId,
),
);
if (!respAttachment.isSuccessful ||
respAttachment.body == null) {
log.warning(() => "error uploading attachment");
continue;
}
final AttachmentRead newAttachment =
respAttachment.body!.data;
log.finest(
() => "attachment id is ${newAttachment.id}");
final HttpClientRequest request =
await HttpClient().postUrl(
Uri.parse(newAttachment.attributes.uploadUrl!),
);
user!.headers().forEach(
(String key, String value) =>
request.headers.add(key, value),
);
request.headers.set(HttpHeaders.contentTypeHeader,
"application/octet-stream");
final Stream<List<int>> listenStream =
File(attachment.attributes.uploadUrl!)
.openRead()
.transform(
StreamTransformer<List<int>,
List<int>>.fromHandlers(
handleData: (List<int> data,
EventSink<List<int>> sink) {
sink.add(data);
},
handleDone: (EventSink<List<int>> sink) {
sink.close();
},
),
);
await request.addStream(listenStream);
await request.close();
log.fine(() => "done uploading attachment");
}
}
}

if (nav.canPop()) {
nav.pop(true);
} else {
Expand Down Expand Up @@ -1073,22 +1145,14 @@ class _TransactionPageState extends State<TransactionPage>
icon: Icons.attach_file,
tooltip: S.of(context).transactionAttachments,
onPressed: () async {
String? txId = _transactionJournalIDs
.firstWhereOrNull((String? element) => element != null);
if (txId == null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(S.of(context).transactionErrorSaveFirst),
behavior: SnackBarBehavior.floating,
));
return;
}
List<AttachmentRead> dialogAttachments =
_attachments ?? <AttachmentRead>[];
await showDialog<List<AttachmentRead>>(
context: context,
builder: (BuildContext context) => AttachmentDialog(
attachments: dialogAttachments,
transactionId: txId,
transactionId: _transactionJournalIDs
.firstWhereOrNull((String? element) => element != null),
),
);
setState(() {
Expand Down
Loading

0 comments on commit 58a4a32

Please sign in to comment.