diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index d3d2cdb..9103fe4 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -12,5 +12,7 @@ "fileUploaded": "Datei hochgeladen.", "readyToUpload": "Bereit zum Hochladen.", "shareInstructions": "Wählen Sie im Menü \"Freigeben\" die Option \"Paperless Share\", um Bilder und Dokumente hochzuladen.", - "serverInstructions": "Ihr Server wird versuchen, es automatisch zu verarbeiten. Es sollte in wenigen Augenblicken erscheinen." + "serverInstructions": "Ihr Server wird versuchen, es automatisch zu verarbeiten. Es sollte in wenigen Augenblicken erscheinen.", + "tagIntro": "Wählen Sie die Tags aus, die für alle hochgeladenen Dokumente verwendet werden sollen.", + "tagConfirm": "Tags bestätigen" } \ No newline at end of file diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 72b03b2..b4d344c 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -12,5 +12,7 @@ "fileUploaded": "File uploaded.", "readyToUpload": "Ready to upload!", "shareInstructions": "Select Paperless Share in the Share Menu to upload pictures and documents", - "serverInstructions": "Your server will attempt to process it automatically. It should appear within a few moments." + "serverInstructions": "Your server will attempt to process it automatically. It should appear within a few moments.", + "tagIntro": "Select the tags to use on all uploaded documents.", + "tagConfirm": "Set Tags" } \ No newline at end of file diff --git a/lib/share.dart b/lib/share.dart index 2effb04..184bf55 100644 --- a/lib/share.dart +++ b/lib/share.dart @@ -1,6 +1,8 @@ import 'dart:async'; +import 'dart:convert'; import 'package:dio/dio.dart'; +import 'package:encrypted_shared_preferences/encrypted_shared_preferences.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; @@ -88,13 +90,48 @@ class _SharePageState extends State { }); } + Future> getAvailableTagsList() async { + final _auth = Provider.of(context, listen: false); + var response = await Dio().get(_auth.user.formatRoute('api/tags/'), + options: Options(headers: { + 'authorization': _auth.user.formatBasicAuth() + })); + Map tags = Map(); + for (var availableTag in response.data["results"]) { + tags[availableTag["id"]] = availableTag["name"]; + } + + return tags; + } + void uploadFileToPaperless(String path) async { final _auth = Provider.of(context, listen: false); print("Uploading " + path + " to " + _auth.user.server); + // Check that tags still exist + List postTags = List.empty(growable: true); + try { + EncryptedSharedPreferences prefs = EncryptedSharedPreferences(); + final tags = await prefs.getString("use_document_tags"); + final tagList = tags != ";" ? tags.split(';') : []; + final availableTags = await getAvailableTagsList(); + + for (var tag in tagList) { + for (var availableTagID in availableTags.keys) { + if (int.parse(tag) == availableTagID) { + postTags.add(availableTagID); + } + } + } + } catch (e) { + print(e); + } + var formData = FormData.fromMap({ + "tags": postTags, "document": await MultipartFile.fromFile(path), }); + Response response; try { response = await Dio().post( @@ -105,23 +142,18 @@ class _SharePageState extends State { })); } on DioError catch (e) { response = e.response; + print(e); + print(response.data.toString()); } - if (response.statusCode == 200) { - Fluttertoast.showToast( - msg: AppLocalizations.of(context).fileUploaded, - toastLength: Toast.LENGTH_SHORT, - gravity: ToastGravity.BOTTOM, - timeInSecForIosWeb: 1, - ); - } else { - Fluttertoast.showToast( - msg: response.data.toString(), - toastLength: Toast.LENGTH_SHORT, - gravity: ToastGravity.BOTTOM, - timeInSecForIosWeb: 1, - ); - } + Fluttertoast.showToast( + msg: response.statusCode == 200 + ? AppLocalizations.of(context).fileUploaded + : response.data.toString(), + toastLength: Toast.LENGTH_SHORT, + gravity: ToastGravity.BOTTOM, + timeInSecForIosWeb: 1, + ); SystemChannels.platform.invokeMethod('SystemNavigator.pop'); } @@ -138,7 +170,7 @@ class _SharePageState extends State { return Scaffold( appBar: AppBar( title: Text("Paperless Share"), - actions: [_logoutButton()], + actions: _isActivelySharing ? [] : [_setTagButton(), _logoutButton()], ), body: Center( child: FractionallySizedBox(child: _bodyContent()), @@ -169,10 +201,6 @@ class _SharePageState extends State { Widget _logoutButton() { final _auth = Provider.of(context, listen: true); - if (_isActivelySharing) { - return new Container(); - } - return // action button IconButton( icon: Icon(Icons.logout), @@ -203,6 +231,23 @@ class _SharePageState extends State { ); } + Widget _setTagButton() { + final _auth = Provider.of(context, listen: true); + + return // action button + IconButton( + icon: Icon(Icons.local_offer_outlined), + tooltip: "Set Tag", + onPressed: () { + showDialog( + context: context, + builder: (BuildContext context) { + return TagAlertDialog(getAvailableTagsList: getAvailableTagsList); + }); + }, + ); + } + Widget _welcomeText() { return new Container( padding: EdgeInsets.symmetric(vertical: 50), @@ -232,3 +277,89 @@ class _SharePageState extends State { )); } } + +class TagAlertDialog extends StatefulWidget { + final Function getAvailableTagsList; + + const TagAlertDialog({Key key, this.getAvailableTagsList}) : super(key: key); + + @override + _TagAlertDialogState createState() => _TagAlertDialogState(); +} + +class _TagAlertDialogState extends State { + Set selectedTags = Set(); + Map _availableTags = Map(); + + @override + void initState() { + super.initState(); + widget.getAvailableTagsList().then((availableTags) => { + setState(() { + _availableTags = availableTags; + }) + }); + EncryptedSharedPreferences prefs = EncryptedSharedPreferences(); + prefs.getString("use_document_tags").then((value) { + Set newTags = new Set(); + + if (value != ";") + value.split(';').forEach((element) { + newTags.add(int.parse(element)); + }); + setState(() { + selectedTags = newTags; + }); + }); + } + + @override + Widget build(BuildContext context) { + List checkboxes = List.empty(growable: true); + for (var tagID in _availableTags.keys) { + final tagName = _availableTags[tagID]; + checkboxes.add(CheckboxListTile( + secondary: Icon(Icons.local_offer_outlined), + dense: true, + visualDensity: VisualDensity(vertical: -2, horizontal: -4), + title: Text(tagName), + // subtitle: Text(tagID.toString()), + value: this.selectedTags.contains(tagID), + onChanged: (isSelected) { + var newTags = new Set.from(selectedTags); + if (isSelected) + newTags.add(tagID); + else + newTags.remove(tagID); + setState(() { + selectedTags = newTags; + }); + })); + } + + return AlertDialog( + title: new Text("Paperless Tag"), + content: Column(mainAxisSize: MainAxisSize.min, children: [ + Text(AppLocalizations.of(context).tagIntro), + Container( + padding: EdgeInsets.symmetric(vertical: 15), + child: Column(children: checkboxes)) + ]), + actionsPadding: EdgeInsets.symmetric(horizontal: 10), + actions: [ + new TextButton( + child: new Text(AppLocalizations.of(context).tagConfirm), + onPressed: () async { + EncryptedSharedPreferences prefs = EncryptedSharedPreferences(); + var ret = await prefs.setString( + "use_document_tags", + this.selectedTags.length > 0 + ? this.selectedTags.join(';') + : ";"); + Navigator.of(context).pop(); + }, + ), + ], + ); + } +}