diff --git a/lib/common/apifunctions/find_organisations.dart b/lib/common/apifunctions/find_organisations.dart index c39afd7..4c6346a 100644 --- a/lib/common/apifunctions/find_organisations.dart +++ b/lib/common/apifunctions/find_organisations.dart @@ -7,22 +7,70 @@ class Organisation { var id = 0; var name = ""; var postcode = ""; - var street_name = ""; + var streetName = ""; //street_name var town = ""; + + Organisation(int id, String name, String postcode, String streetName, String town) { + this.id = id; + this.name = name; + this.postcode = postcode; + this.streetName = streetName; //street_name + this.town = town; + } + } List jsonToOrganisations(String json) { Map decoded = jsonDecode(json); - print(decoded); +// print(decoded); - List validated = decoded['validated']; + List validated = decoded['unvalidated']; // Map organisation = validated[0]; - List organisations = new List(); + print(""); + print("Response:"); + for (var i = 0; i < validated.length; i++) { + print(validated[i]); + } - validated.forEach((element) => organisations.add(element)); + List organisationsMaps = new List(); -// print(organisations); + validated.forEach((element) => organisationsMaps.add(element)); + +// print(""); +// print("organisationsMaps:"); +// print(organisationsMaps); + + List organisations = new List(); + +// organisationsMaps[0].forEach((k,v) => print('${k}: ${v}')); + + for (var i = 0; i < organisationsMaps.length; i++) { + final params = organisationsMaps[i].values.toList(); + + var newOrganisation = new Organisation( + params[0].toInt(), + params[1].toString(), + params[2].toString(), // oof + params[3].toString(), // this could be improved... + params[4].toString(), + ); + + organisations.add(newOrganisation); + } + + // the reason some organizations do not show up is because they are not all validated + // option to 'show unvalidated' should be added along with maybe a settings section + + print(""); + print("Local:"); + for (var i = 0; i < organisations.length; i++) + { + print(organisations[i].name); + } + print(""); + + return organisations; } Future> findOrganisations(String search) async { @@ -43,7 +91,7 @@ Future> findOrganisations(String search) async { body: json.encode(body), ); - print(response.body); +// print(response.body); if (response.statusCode == 200) { //request successful diff --git a/lib/common/widgets/popupListView.dart b/lib/common/widgets/popupListView.dart new file mode 100644 index 0000000..2ef18bc --- /dev/null +++ b/lib/common/widgets/popupListView.dart @@ -0,0 +1,52 @@ +import 'package:flutter/material.dart'; + +class PopupListView { + List options = new List(); + var context; + String listTitle; + List simpleDialogOptions = new List(); + String result; + + PopupListView(context, List options, String title) { + this.context = context; + this.options = options; + this.listTitle = title; + } + + + List getDialogOptions() { + var dialogOptionsList = new List(); + + for (var i = 0; i < options.length; i++) { + dialogOptionsList.add( + new SimpleDialogOption( + child: Text(options[i]), + onPressed: () { + Navigator.of(this.context).pop(); +// print("Chosen organisation is " + options[i]); + optionChosen(options[i]); + }, + ), + ); + } + + return dialogOptionsList; + } + + Widget dialog() { + return new SimpleDialog( + title: Text(listTitle), + children : getDialogOptions(), + ); + } + + void optionChosen(String option) { + // this works at least... + + + } +} + + + + diff --git a/lib/pages/login_page.dart b/lib/pages/login_page.dart index 1e01460..d102639 100644 --- a/lib/pages/login_page.dart +++ b/lib/pages/login_page.dart @@ -18,8 +18,8 @@ class LoginPage extends StatefulWidget { } class LoginPageState extends State { - final TextEditingController _emailController = TextEditingController(); - final TextEditingController _passwordController = TextEditingController(); + final TextEditingController _emailController = TextEditingController(text: 'test@example.com'); // remove + final TextEditingController _passwordController = TextEditingController(text: 'abc123'); // remove FocusNode focusNode; // added so focus can move automatically diff --git a/lib/pages/receipt_page.dart b/lib/pages/receipt_page.dart index 53f126e..fa145d1 100644 --- a/lib/pages/receipt_page.dart +++ b/lib/pages/receipt_page.dart @@ -11,6 +11,7 @@ import 'package:url_launcher/url_launcher.dart'; import 'package:intl/intl.dart'; import 'package:datetime_picker_formfield/datetime_picker_formfield.dart'; import 'package:local_spend/common/apifunctions/find_organisations.dart'; +import 'package:local_spend/common/widgets/popupListView.dart'; const URL = "https://flutter.io/"; const demonstration = false; @@ -146,6 +147,32 @@ class ReceiptPageState extends State { // know that before writing this and it's done now so I'm keeping it. } + String listOrganisations(List organisations, context) { + var optionsList = new List(); + + for (var i = 0; i < organisations.length; i++) { + optionsList.add(organisations[i].name); + } + + var popupListView = new PopupListView(context, optionsList, "Choose Organization"); + + popupListView.options = optionsList; + + var dialog = popupListView.dialog(); + +// print(dialog); + + showDialog( + context: context, + builder: (BuildContext context) { + return dialog; + }, + ); + + print(popupListView.result); + return popupListView.result; + } + @override Widget build(BuildContext context) { var drawer = Drawer(); @@ -264,10 +291,12 @@ class ReceiptPageState extends State { padding: EdgeInsets.fromLTRB(5,0,0,4), // sorry about hardcoded constraints child: FlatButton( onPressed: () { - debugPrint("TODO: 'find organisation' dialog"); - - findOrganisations(_orgController.text); + var organisations = findOrganisations(_orgController.text); + // some tasty async stuff here yum yum + // and a pretty little dialog too yay + var choice = organisations.then((data) => listOrganisations(data, context)); + // choice is a Future }, child: Text("Find", style: