preparing for 'categories'

This commit is contained in:
Felix 2019-07-16 16:55:00 +01:00
parent c5cf66a210
commit adae182b1a
No known key found for this signature in database
GPG key ID: 130EF6DC43E4DD07
2 changed files with 353 additions and 220 deletions

View file

@ -0,0 +1,78 @@
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:local_spend/common/functions/get_token.dart';
class Category {
String name;
String index;
Category({
this.name,
this.index,
});
}
Future<List<Category>> getCategories() async {
const url = "https://dev.peartrade.org/api/search/category";
var token;
await getToken().then((result) {
token = result;
});
Map<String, String> body = {
"session_key":token,
};
final response = await http.post (
url,
body: json.encode(body),
);
// print(response.body);
if (response.statusCode == 200) {
//request successful
List<Category> categories = new List<Category>();
Map responseMap = json.decode(response.body);
var categoriesJSON = responseMap['categories'];
//nice.
// so the response needs to be decoded iteratively, like
// categories.add(new Category(name: categoriesJSON[i.toString()], index: i.toString()));
// print(categoriesJSON['11']); // prints "Banana"
int i = 1; // starts on 1. that was annoying to debug!
while (true) {
if (categoriesJSON[i.toString()] != null) {
// print("Iteration " + i.toString());
// print(categoriesJSON[i.toString()]);
categories.add(new Category(
name: categoriesJSON[i.toString()], index: i.toString()));
// print(categories.last);
i++;
} else {
// print("Number of categories: " + (i - 1).toString());
// print("categoriesJSON[" + i.toString() + ".toString()] == null");
break;
}
} // does this until error, which then tells it that there is no more JSON left
// print(categories[11].name);
// decodedResponse.forEach((key, value) {
//// print(key + ": " + value);
// categories.add(new Category(name: value, index: key));
// });
// print(categories[10].name.toString()); // prints "Banana"
return categories;
} else {
// not successful
return null;
}
}

View file

@ -11,6 +11,7 @@ import 'package:intl/intl.dart';
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart'; import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
import 'package:local_spend/common/apifunctions/find_organisations.dart'; import 'package:local_spend/common/apifunctions/find_organisations.dart';
import 'package:local_spend/common/widgets/popupListView.dart'; import 'package:local_spend/common/widgets/popupListView.dart';
import 'package:local_spend/common/apifunctions/categories.dart';
const URL = "https://flutter.io/"; const URL = "https://flutter.io/";
const demonstration = false; const demonstration = false;
@ -30,6 +31,7 @@ class ReceiptPageState extends State<ReceiptPage> {
final TextEditingController _categoryController = TextEditingController(); final TextEditingController _categoryController = TextEditingController();
final TextEditingController _orgController = TextEditingController(); final TextEditingController _orgController = TextEditingController();
final OrganizationController _organizationController = OrganizationController(); final OrganizationController _organizationController = OrganizationController();
List<String> _categoryDropDownItems = List<String>();
FocusNode focusNode; FocusNode focusNode;
@ -49,6 +51,10 @@ class ReceiptPageState extends State<ReceiptPage> {
@override @override
void initState() { void initState() {
getCategoriesStrings().then((value) {
_categoryDropDownItems = value;
});
super.initState(); super.initState();
_saveCurrentRoute("/ReceiptPageState"); _saveCurrentRoute("/ReceiptPageState");
@ -56,6 +62,9 @@ class ReceiptPageState extends State<ReceiptPage> {
_recurringController.text = "None"; _recurringController.text = "None";
_categoryController.text = ""; _categoryController.text = "";
// getCategoriesStrings().then((value) {
// _categoryDropDownItems = value;
// });
} }
@override @override
@ -72,7 +81,7 @@ class ReceiptPageState extends State<ReceiptPage> {
// this file is getting really messy sorry everyone // this file is getting really messy sorry everyone
void submitReceipt(String amount, String time, Organisation organisation, String recurring, String category) async { void submitReceipt(String amount, String time, Organisation organisation, String recurring, String category, String essential) async {
SystemChannels.textInput.invokeMethod('TextInput.hide'); SystemChannels.textInput.invokeMethod('TextInput.hide');
if (organisation == null) { if (organisation == null) {
@ -157,12 +166,7 @@ class ReceiptPageState extends State<ReceiptPage> {
receipt.recurring = recurring; receipt.recurring = recurring;
receipt.category = category; receipt.category = category;
// receipt.essential = convertBoolToString(toConvert) receipt.essential = essential;
// TODO: Categories
// receipt.category = category;
// receipt.etc = etc;
submitReceiptAPI(context, receipt); submitReceiptAPI(context, receipt);
Navigator.of(context).pushReplacementNamed("/HomePage"); Navigator.of(context).pushReplacementNamed("/HomePage");
@ -180,7 +184,25 @@ class ReceiptPageState extends State<ReceiptPage> {
return "false"; return "false";
} }
List<String> getOptions() { Future<List<String>> getCategoriesStrings() async {
var categories = getCategories(); //future<list<cat>>
var categoriesStrings = List<String>();
categories.then((val) {
val.forEach((thisCategory) {
// print(thisCategory.name);
categoriesStrings.add(thisCategory.name);
});
// print(categoriesStrings[10]); // prints 'Banana'
// print(categoriesStrings.toString());
_categoryDropDownItems = categoriesStrings;
return categoriesStrings;
});
}
List<String> getRecurringOptions() {
var options = new List<String>(7); var options = new List<String>(7);
options[0] = "None"; options[0] = "None";
options[1] = "Daily"; options[1] = "Daily";
@ -277,279 +299,312 @@ class ReceiptPageState extends State<ReceiptPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return WillPopScope( return PlatformScaffold(
onWillPop: () {
if (Navigator.canPop(context)) {
Navigator.of(context).pushNamedAndRemoveUntil(
'/LoginPage', (Route<dynamic> route) => false);
} else {
Navigator.of(context).pushReplacementNamed('/LoginPage');
}
},
child: PlatformScaffold(
appBar: AppBar( appBar: AppBar(
backgroundColor: Colors.blue[400], backgroundColor: Colors.blue[400],
title: Text( title: Text(
"Submit Receipt", "Submit Receipt",
style: TextStyle( style: TextStyle(
fontSize: 20, fontSize: 20,
color: Colors.white, color: Colors.white,
),
), ),
// leading: BackButton(),
centerTitle: true,
iconTheme: IconThemeData(color: Colors.black),
), ),
// leading: BackButton(),
centerTitle: true,
iconTheme: IconThemeData(color: Colors.black),
),
body: Container( body: Container(
padding: EdgeInsets.fromLTRB(30.0, 0.0, 30.0, 0.0), padding: EdgeInsets.fromLTRB(30.0, 0.0, 30.0, 0.0),
child: ListView( child: ListView(
children: <Widget>[ children: <Widget>[
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0), padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0),
child : Text( child : Text(
"Time of Transaction", "Time of Transaction",
style: TextStyle( style: TextStyle(
fontSize: 18.0, fontSize: 18.0,
color: Colors.black, color: Colors.black,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
),
), ),
), ),
DateTimePickerFormField( ),
inputType: InputType.both, DateTimePickerFormField(
format: DateFormat("dd/MM/yyyy 'at' hh:mm"), inputType: InputType.both,
editable: true, format: DateFormat("dd/MM/yyyy 'at' hh:mm"),
controller: _timeController, editable: true,
controller: _timeController,
decoration: InputDecoration(
labelText: 'Date/Time of Transaction', hasFloatingPlaceholder: false),
onChanged: (dt) => setState(() => date = dt),
),
Padding(
padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0),
child: Text(
"Amount",
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
child: TextField(
controller: _amountController,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Date/Time of Transaction', hasFloatingPlaceholder: false), hintText: 'Value in £',
onChanged: (dt) => setState(() => date = dt),
),
Padding(
padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0),
child: Text(
"Amount",
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.bold,
),
), ),
),
Padding(
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
child: TextField(
controller: _amountController,
decoration: InputDecoration(
hintText: 'Value in £',
),
// obscureText: true, // obscureText: true,
autocorrect: false, autocorrect: false,
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
style: TextStyle( style: TextStyle(
fontSize: 18.0, fontSize: 18.0,
color: Colors.grey[800], color: Colors.grey[800],
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
), ),
onSubmitted: (_) { onSubmitted: (_) {
FocusScope.of(context).requestFocus(focusNode); FocusScope.of(context).requestFocus(focusNode);
// submitReceipt(_amountController.text, _timeController.text); // submitReceipt(_amountController.text, _timeController.text);
}, },
),
), ),
),
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0), padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0),
child : Container ( child : Container (
height: 22, // this should be the same height as text height: 22, // this should be the same height as text
child : ListView( child : ListView(
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
children: <Widget>[ children: <Widget>[
Container( Container(
child: Text( child: Text(
"Organization Name", "Organization Name",
style: TextStyle( style: TextStyle(
fontSize: 18.0, fontSize: 18.0,
color: Colors.black, color: Colors.black,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
),
), ),
), ),
),
Container( Container(
child : Padding( child : Padding(
padding: EdgeInsets.fromLTRB(5,0,0,4), // sorry about hardcoded constraints padding: EdgeInsets.fromLTRB(5,0,0,4), // sorry about hardcoded constraints
child: FlatButton( child: FlatButton(
onPressed: () { onPressed: () {
_findOrganizationsDialog(context); _findOrganizationsDialog(context);
}, },
child: Text("Find", child: Text("Find",
style: TextStyle(color: Colors.blue, fontSize: 18.0) style: TextStyle(color: Colors.blue, fontSize: 18.0)
), ),
), ),
), ),
) )
], ],
),
), ),
), ),
),
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0), padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
child: TextField( child: TextField(
controller: _orgController, controller: _orgController,
focusNode: focusNode, focusNode: focusNode,
decoration: InputDecoration( decoration: InputDecoration(
hintText: 'Eg. Pear Trading', hintText: 'Eg. Pear Trading',
), ),
// obscureText: true, // obscureText: true,
autocorrect: true, autocorrect: true,
style: TextStyle( style: TextStyle(
fontSize: 18.0, fontSize: 18.0,
color: Colors.grey[800], color: Colors.grey[800],
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
),
onSubmitted: (_) {
submitReceipt(_amountController.text,
_timeController.text, _organizationController.organisation, _recurringController.text, _categoryController.text);
// TODO: make sure organisation is valid
// TODO: Add 'find organisation' button which displays a dialog to, well, find the organisation's address or manual entry
},
), ),
onSubmitted: (_) {
submitReceipt(_amountController.text,
_timeController.text, _organizationController.organisation, _recurringController.text, _categoryController.text, _essentialController.text);
},
), ),
),
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0), padding: EdgeInsets.fromLTRB(0.0,25,0.0,0.0),
child : Container ( child : Container (
height: 18, height: 18,
child : ListView( child : ListView(
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
children: <Widget>[ children: <Widget>[
Container( Container(
child: Text( child: Text(
"Essential", "Essential",
style: TextStyle( style: TextStyle(
fontSize: 18.0, fontSize: 18.0,
color: Colors.black, color: Colors.black,
),
), ),
), ),
),
Container( Container(
child : Padding( child : Padding(
padding: EdgeInsets.fromLTRB(20.0, 0.0, 0, 0), padding: EdgeInsets.fromLTRB(20.0, 0.0, 0, 0),
child: Checkbox(value: child: Checkbox(value:
_essentialController.text.toLowerCase() == 'true', _essentialController.text.toLowerCase() == 'true',
onChanged: (bool newValue) { onChanged: (bool newValue) {
setState(() { setState(() {
_essentialController.text = _essentialController.text =
convertBoolToString(newValue); convertBoolToString(newValue);
}); });
}), }),
),
), ),
),
], ],
),
), ),
), ),
),
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0,18,0.0,0.0), padding: EdgeInsets.fromLTRB(0.0,18,0.0,0.0),
child : Container ( child : Container (
height: 35, height: 35,
// width: 400, // width: 400,
child : ListView( child : ListView(
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
children: <Widget>[ children: <Widget>[
Container( Container(
padding: const EdgeInsets.fromLTRB(0, 7, 0, 8), padding: const EdgeInsets.fromLTRB(0, 7, 0, 8),
child: Text( child: Text(
"Recurring", "Recurring",
style: TextStyle( style: TextStyle(
fontSize: 18.0, fontSize: 18.0,
color: Colors.black, color: Colors.black,
),
), ),
), ),
),
Container( Container(
padding: const EdgeInsets.fromLTRB(29, 0, 0, 0),
child: DropdownButton<String>(
value: _recurringController.text,
onChanged: (String newValue) {
setState(() {
_recurringController.text = newValue;
});
},
items: getRecurringOptions().map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
)
),
],
),
),
),
Padding(
padding: EdgeInsets.fromLTRB(0.0,18,0.0,0.0),
child : Container (
height: 35,
// width: 400,
child : ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Container(
padding: const EdgeInsets.fromLTRB(0, 7, 0, 8),
child: Text(
"Category",
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
),
),
),
Container(
padding: const EdgeInsets.fromLTRB(29, 0, 0, 0), padding: const EdgeInsets.fromLTRB(29, 0, 0, 0),
child: DropdownButton<String>( child: DropdownButton<String>(
value: _recurringController.text, value: _categoryController.text,
onChanged: (String newValue) { onChanged: (String newValue) {
setState(() { setState(() {
_recurringController.text = newValue; _categoryController.text = newValue;
}); });
}, },
items: getOptions().map<DropdownMenuItem<String>>((String value) { items: _categoryDropDownItems.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>( return DropdownMenuItem<String>(
value: value, value: value,
child: Text(value), child: Text(value),
); );
}) }).toList(),
.toList(),
) )
), ),
], ],
),
), ),
), ),
),
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 0.0), padding: EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 0.0),
child: Container( child: Container(
height: 65.0, height: 65.0,
child: RaisedButton( child: RaisedButton(
onPressed: () { onPressed: () {
try { try {
submitReceipt( submitReceipt(
_amountController.text, _timeController.text, _amountController.text, _timeController.text,
_organizationController.organisation, _recurringController.text, _categoryController.text); _organizationController.organisation, _recurringController.text, _categoryController.text, _essentialController.text);
} }
catch (_) { catch (_) {
showDialog( showDialog(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
// return object of type Dialog // return object of type Dialog
return AlertDialog( return AlertDialog(
title: new Text("Invalid data"), title: new Text("Invalid data"),
content: new Text( content: new Text(
"We couldn't process your request because some of the data entered is invalid."), "We couldn't process your request because some of the data entered is invalid."),
actions: <Widget>[ actions: <Widget>[
new FlatButton( new FlatButton(
child: new Text("OK"), child: new Text("OK"),
onPressed: () { onPressed: () {
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
), ),
], ],
); );
}, },
); );
} }
}, },
child: Text("GO", child: Text("GO",
style: style:
TextStyle(color: Colors.white, fontSize: 22.0)), TextStyle(color: Colors.white, fontSize: 22.0)),
color: Colors.blue, color: Colors.blue,
),
), ),
), ),
], ),
), ],
), ),
), ),
); );