Skip to content

Commit

Permalink
support loading tags from server. closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
qcasey committed Jan 13, 2022
1 parent 527642f commit 6589f20
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 22 deletions.
4 changes: 3 additions & 1 deletion lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 3 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
171 changes: 151 additions & 20 deletions lib/share.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -88,13 +90,48 @@ class _SharePageState extends State<SharePage> {
});
}

Future<Map<int, String>> getAvailableTagsList() async {
final _auth = Provider.of<AuthModel>(context, listen: false);
var response = await Dio().get(_auth.user.formatRoute('api/tags/'),
options: Options(headers: <String, String>{
'authorization': _auth.user.formatBasicAuth()
}));
Map<int, String> tags = Map<int, String>();
for (var availableTag in response.data["results"]) {
tags[availableTag["id"]] = availableTag["name"];
}

return tags;
}

void uploadFileToPaperless(String path) async {
final _auth = Provider.of<AuthModel>(context, listen: false);
print("Uploading " + path + " to " + _auth.user.server);

// Check that tags still exist
List<int> postTags = List<int>.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(
Expand All @@ -105,23 +142,18 @@ class _SharePageState extends State<SharePage> {
}));
} 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');
}
Expand All @@ -138,7 +170,7 @@ class _SharePageState extends State<SharePage> {
return Scaffold(
appBar: AppBar(
title: Text("Paperless Share"),
actions: [_logoutButton()],
actions: _isActivelySharing ? [] : [_setTagButton(), _logoutButton()],
),
body: Center(
child: FractionallySizedBox(child: _bodyContent()),
Expand Down Expand Up @@ -169,10 +201,6 @@ class _SharePageState extends State<SharePage> {
Widget _logoutButton() {
final _auth = Provider.of<AuthModel>(context, listen: true);

if (_isActivelySharing) {
return new Container();
}

return // action button
IconButton(
icon: Icon(Icons.logout),
Expand Down Expand Up @@ -203,6 +231,23 @@ class _SharePageState extends State<SharePage> {
);
}

Widget _setTagButton() {
final _auth = Provider.of<AuthModel>(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),
Expand Down Expand Up @@ -232,3 +277,89 @@ class _SharePageState extends State<SharePage> {
));
}
}

class TagAlertDialog extends StatefulWidget {
final Function getAvailableTagsList;

const TagAlertDialog({Key key, this.getAvailableTagsList}) : super(key: key);

@override
_TagAlertDialogState createState() => _TagAlertDialogState();
}

class _TagAlertDialogState extends State<TagAlertDialog> {
Set<int> selectedTags = Set<int>();
Map<int, String> _availableTags = Map<int, String>();

@override
void initState() {
super.initState();
widget.getAvailableTagsList().then((availableTags) => {
setState(() {
_availableTags = availableTags;
})
});
EncryptedSharedPreferences prefs = EncryptedSharedPreferences();
prefs.getString("use_document_tags").then((value) {
Set<int> newTags = new Set<int>();

if (value != ";")
value.split(';').forEach((element) {
newTags.add(int.parse(element));
});
setState(() {
selectedTags = newTags;
});
});
}

@override
Widget build(BuildContext context) {
List<Widget> checkboxes = List<Widget>.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<int>.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();
},
),
],
);
}
}

0 comments on commit 6589f20

Please sign in to comment.