organisations working sorta
This commit is contained in:
parent
aa6de5559a
commit
1e40d78dcd
3 changed files with 168 additions and 117 deletions
|
@ -4,7 +4,6 @@ import 'package:http/http.dart' as http;
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:local_spend/common/functions/get_token.dart';
|
import 'package:local_spend/common/functions/get_token.dart';
|
||||||
|
|
||||||
|
|
||||||
class Organisation {
|
class Organisation {
|
||||||
var id = 0;
|
var id = 0;
|
||||||
var name = "";
|
var name = "";
|
||||||
|
@ -13,67 +12,83 @@ class Organisation {
|
||||||
var town = "";
|
var town = "";
|
||||||
|
|
||||||
Organisation(
|
Organisation(
|
||||||
this.id,
|
this.id,
|
||||||
this.name,
|
this.name,
|
||||||
this.postcode,
|
this.postcode,
|
||||||
this.streetName,
|
this.streetName,
|
||||||
this.town,
|
this.town,
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Organisation> jsonToOrganisations(String json) {
|
class Organisations {
|
||||||
Map decoded = jsonDecode(json);
|
|
||||||
List<dynamic> validated = decoded['validated'];
|
|
||||||
List<Map> organisationsMaps = new List<Map>();
|
|
||||||
validated.forEach((element) => organisationsMaps.add(element));
|
|
||||||
List<Organisation> organisations = new List<Organisation>();
|
|
||||||
|
|
||||||
for (var i = 0; i < organisationsMaps.length; i++) {
|
List<Organisation> getTestData() {
|
||||||
final params = organisationsMaps[i].values.toList();
|
var numItems = 200;
|
||||||
|
var itemsList = new List<Organisation>();
|
||||||
|
|
||||||
var newOrganisation = new Organisation(
|
for (int i = 0; i < numItems; i++) {
|
||||||
params[0].toInt(),
|
itemsList.add(new Organisation(
|
||||||
params[1].toString(),
|
i,
|
||||||
params[2].toString(), // oof
|
"Payee " + (i + 1).toString(),
|
||||||
params[3].toString(), // this could be improved...
|
"eee eee",
|
||||||
params[4].toString(),
|
"yeet street",
|
||||||
|
"Robloxia"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return itemsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Organisation> _jsonToOrganisations(String json) {
|
||||||
|
Map decoded = jsonDecode(json);
|
||||||
|
List<dynamic> validated = decoded['validated'];
|
||||||
|
List<Map> organisationsMaps = new List<Map>();
|
||||||
|
validated.forEach((element) => organisationsMaps.add(element));
|
||||||
|
List<Organisation> organisations = new List<Organisation>();
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return organisations;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Organisation>> findOrganisations(String search) async {
|
||||||
|
final url = "https://dev.peartrade.org/api/search";
|
||||||
|
var token;
|
||||||
|
|
||||||
|
await getToken().then((result) {
|
||||||
|
token = result;
|
||||||
|
});
|
||||||
|
|
||||||
|
Map<String, String> body = {
|
||||||
|
"search_name":search,
|
||||||
|
"session_key":token,
|
||||||
|
};
|
||||||
|
|
||||||
|
final response = await http.post (
|
||||||
|
url,
|
||||||
|
body: json.encode(body),
|
||||||
);
|
);
|
||||||
|
|
||||||
organisations.add(newOrganisation);
|
if (response.statusCode == 200) {
|
||||||
|
//request successful
|
||||||
|
return _jsonToOrganisations(response.body);
|
||||||
|
} else {
|
||||||
|
// not successful
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return organisations;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<List<Organisation>> findOrganisations(String search) async {
|
|
||||||
final url = "https://dev.peartrade.org/api/search";
|
|
||||||
var token;
|
|
||||||
|
|
||||||
await getToken().then((result) {
|
|
||||||
token = result;
|
|
||||||
});
|
|
||||||
|
|
||||||
Map<String, String> body = {
|
|
||||||
"search_name":search,
|
|
||||||
"session_key":token,
|
|
||||||
};
|
|
||||||
|
|
||||||
final response = await http.post (
|
|
||||||
url,
|
|
||||||
body: json.encode(body),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
|
||||||
//request successful
|
|
||||||
return jsonToOrganisations(response.body);
|
|
||||||
} else {
|
|
||||||
// not successful
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class OrganizationController extends TextEditingController {
|
|
||||||
Organisation organisation;
|
|
||||||
}
|
}
|
|
@ -15,75 +15,117 @@ class FindOrganisations {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Text> getFavourites() {
|
|
||||||
var numItems = 200;
|
|
||||||
var itemsList = new List<Text>();
|
|
||||||
|
|
||||||
for (int i = 0; i < numItems; i++) {
|
|
||||||
itemsList.add(Text(
|
|
||||||
"Payee " + (i + 1).toString(),
|
|
||||||
style: new TextStyle(fontSize: 18),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
return itemsList;
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo: get all organisations, favourites and all data from one 'organisations' class or similar
|
// todo: get all organisations, favourites and all data from one 'organisations' class or similar
|
||||||
// eg items: organisations.getFavourites().orderBy(name),
|
// eg items: organisations.getFavourites().orderBy(name),
|
||||||
|
|
||||||
Future<Organisation> dialog(context) {
|
Future<Organisation> dialog(context) {
|
||||||
var searchBar = getSearchBar(null, "Payee Name");
|
TextEditingController searchBarText = new TextEditingController();
|
||||||
var favourites = getFavourites();
|
var organisations = new Organisations();
|
||||||
|
var listTitle = "All Organisations";
|
||||||
|
var organisationsList = organisations.getTestData();
|
||||||
|
|
||||||
|
void _submitSearch(String search) {
|
||||||
|
listTitle = "Results for \'" + search + "\'";
|
||||||
|
|
||||||
|
var futureOrgs = organisations.findOrganisations(search);
|
||||||
|
futureOrgs.then((val) {
|
||||||
|
debugPrint("There are " + val.length.toString() +
|
||||||
|
" payees matching the query \'" + search + "\'.");
|
||||||
|
organisationsList = val;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return showDialog<Organisation>(
|
return showDialog<Organisation>(
|
||||||
context: context,
|
context: context,
|
||||||
barrierDismissible: true,
|
barrierDismissible: true,
|
||||||
|
|
||||||
builder: (BuildContext context) {
|
builder: (BuildContext context) {
|
||||||
return SimpleDialog(
|
return StatefulBuilder(
|
||||||
children: <Widget>[
|
builder: (context, setState) {
|
||||||
Padding(
|
return SimpleDialog(
|
||||||
padding: EdgeInsets.symmetric(horizontal: 10),
|
children: <Widget>[
|
||||||
child: searchBar,
|
Padding(
|
||||||
),
|
padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
width: 200,
|
||||||
|
height: 50,
|
||||||
|
child: TextField(
|
||||||
|
controller: searchBarText,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
hintText: "Payee Name",
|
||||||
|
),
|
||||||
|
onSubmitted: (_) {
|
||||||
|
_submitSearch(searchBarText.text);
|
||||||
|
setState(() => {});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
Container(
|
Container(
|
||||||
padding: EdgeInsets.fromLTRB(20, 20, 0, 0),
|
width: 80,
|
||||||
child: Text(
|
padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
|
||||||
"Favourites",
|
child: RaisedButton(
|
||||||
style: new TextStyle(fontSize: 23, fontWeight: FontWeight.bold),
|
onPressed: () {
|
||||||
),
|
_submitSearch(searchBarText.text);
|
||||||
),
|
setState(() => {});
|
||||||
|
},
|
||||||
Container(
|
child: Icon(Icons.search, color: Colors.white),
|
||||||
padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
|
color: Colors.blue,
|
||||||
width: MediaQuery.of(context).size.width * 0.7,
|
// make inactive when search in progress as activity indicator
|
||||||
height: MediaQuery.of(context).size.height * 0.67,
|
),
|
||||||
|
),
|
||||||
child: Material(
|
],
|
||||||
shadowColor: Colors.transparent,
|
),
|
||||||
color: Colors.transparent,
|
),
|
||||||
child: ListView.builder(
|
|
||||||
itemCount: favourites.length,
|
Container(
|
||||||
itemBuilder: (context, index) {
|
padding: EdgeInsets.fromLTRB(20, 20, 20, 0),
|
||||||
return Card(
|
child: Text(
|
||||||
child: ListTile(
|
listTitle,
|
||||||
leading: Icon(Icons.person),
|
style: new TextStyle(
|
||||||
title: favourites[index],
|
fontSize: 23, fontWeight: FontWeight.bold),
|
||||||
trailing: Icon(Icons.arrow_forward_ios),
|
),
|
||||||
onTap: () {},
|
),
|
||||||
)
|
|
||||||
);
|
Container(
|
||||||
},
|
padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
|
||||||
|
width: MediaQuery
|
||||||
|
.of(context)
|
||||||
|
.size
|
||||||
|
.width * 0.7,
|
||||||
|
height: MediaQuery
|
||||||
|
.of(context)
|
||||||
|
.size
|
||||||
|
.height * 0.67,
|
||||||
|
|
||||||
|
child: Material(
|
||||||
|
shadowColor: Colors.transparent,
|
||||||
|
color: Colors.transparent,
|
||||||
|
child: ListView.builder(
|
||||||
|
itemCount: organisationsList.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
return Card(
|
||||||
|
child: ListTile(
|
||||||
|
leading: Icon(Icons.person),
|
||||||
|
title: Text(organisationsList[index].name, style: new TextStyle(fontSize: 18)),
|
||||||
|
trailing: Icon(Icons.arrow_forward_ios),
|
||||||
|
onTap: () {},
|
||||||
|
)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
),
|
|
||||||
|
|
||||||
|
|
||||||
// help button for if org not listed
|
// help button for if org not listed
|
||||||
// cancel and ok buttons
|
// cancel and ok buttons
|
||||||
|
|
||||||
],
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -17,12 +17,6 @@ class Transaction {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find Organisations
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// end Find Organisations
|
|
||||||
|
|
||||||
class ReceiptPage2 extends StatefulWidget {
|
class ReceiptPage2 extends StatefulWidget {
|
||||||
@override
|
@override
|
||||||
State<StatefulWidget> createState() {
|
State<StatefulWidget> createState() {
|
||||||
|
|
Reference in a new issue