preparing for 'categories'
This commit is contained in:
parent
c5cf66a210
commit
adae182b1a
2 changed files with 353 additions and 220 deletions
78
lib/common/apifunctions/categories.dart
Normal file
78
lib/common/apifunctions/categories.dart
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,16 +299,7 @@ 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],
|
||||||
|
@ -416,9 +429,7 @@ class ReceiptPageState extends State<ReceiptPage> {
|
||||||
),
|
),
|
||||||
onSubmitted: (_) {
|
onSubmitted: (_) {
|
||||||
submitReceipt(_amountController.text,
|
submitReceipt(_amountController.text,
|
||||||
_timeController.text, _organizationController.organisation, _recurringController.text, _categoryController.text);
|
_timeController.text, _organizationController.organisation, _recurringController.text, _categoryController.text, _essentialController.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
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -494,7 +505,7 @@ class ReceiptPageState extends State<ReceiptPage> {
|
||||||
_recurringController.text = newValue;
|
_recurringController.text = newValue;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
items: getOptions().map<DropdownMenuItem<String>>((String value) {
|
items: getRecurringOptions().map<DropdownMenuItem<String>>((String value) {
|
||||||
return DropdownMenuItem<String>(
|
return DropdownMenuItem<String>(
|
||||||
value: value,
|
value: value,
|
||||||
child: Text(value),
|
child: Text(value),
|
||||||
|
@ -508,6 +519,51 @@ class ReceiptPageState extends State<ReceiptPage> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
|
|
||||||
|
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),
|
||||||
|
child: DropdownButton<String>(
|
||||||
|
value: _categoryController.text,
|
||||||
|
onChanged: (String newValue) {
|
||||||
|
setState(() {
|
||||||
|
_categoryController.text = newValue;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
items: _categoryDropDownItems.map<DropdownMenuItem<String>>((String value) {
|
||||||
|
return DropdownMenuItem<String>(
|
||||||
|
value: value,
|
||||||
|
child: Text(value),
|
||||||
|
);
|
||||||
|
}).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(
|
||||||
|
@ -517,7 +573,7 @@ class ReceiptPageState extends State<ReceiptPage> {
|
||||||
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(
|
||||||
|
@ -551,7 +607,6 @@ class ReceiptPageState extends State<ReceiptPage> {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue