diff --git a/lib/common/apifunctions/categories.dart b/lib/common/apifunctions/categories.dart index dc537ab..b00cc75 100644 --- a/lib/common/apifunctions/categories.dart +++ b/lib/common/apifunctions/categories.dart @@ -4,35 +4,7 @@ import 'package:http/http.dart' as http; import 'package:local_spend/common/functions/get_token.dart'; import 'package:flutter/material.dart'; -class Category { - String name; - String index; - - Category({ - this.name, - this.index, - }); -} - -Future>> getCategoriesList() async { - //TODO: Return a list of [String, String] where {1} is categoryName and {2} is categoryValue for request - var categoriesList = List(); - - var categories = await getCategories(); - - categories.forEach((thisCategory) { - var thisMap = new DropdownMenuItem( - child: new Text(thisCategory.name), - value: thisCategory.index, - ); - - categoriesList.add(thisMap); - }); - - return categoriesList; -} - -Future> getCategories() async { // confusing name +Future> getCategories() async { const url = "https://dev.peartrade.org/api/search/category"; var token; @@ -53,7 +25,7 @@ Future> getCategories() async { // confusing name if (response.statusCode == 200) { //request successful - List categories = new List(); + List categories = new List(); Map responseMap = json.decode(response.body); var categoriesJSON = responseMap['categories']; @@ -70,8 +42,7 @@ Future> getCategories() async { // confusing name if (categoriesJSON[i.toString()] != null) { // print("Iteration " + i.toString()); // print(categoriesJSON[i.toString()]); - categories.add(new Category( - name: categoriesJSON[i.toString()], index: i.toString())); + categories.add(categoriesJSON[i.toString()]); // print(categories.last); i++; } else { diff --git a/lib/common/apifunctions/submit_receipt_api.dart b/lib/common/apifunctions/submit_receipt_api.dart index 9093cbc..9dbb338 100644 --- a/lib/common/apifunctions/submit_receipt_api.dart +++ b/lib/common/apifunctions/submit_receipt_api.dart @@ -31,8 +31,7 @@ Future submitReceiptAPI( 'transaction_type' : "3", 'transaction_value': receipt.amount, 'purchase_time': receipt.time, -// 'category': receipt.category, -// 'category': receipt.category, + 'category': receipt.category, 'essential': receipt.essential, 'organisation_name': receipt.organisationName, 'recurring': receipt.recurring, diff --git a/lib/common/widgets/future_dropdownmenu.dart b/lib/common/widgets/future_dropdownmenu.dart deleted file mode 100644 index 85573fb..0000000 --- a/lib/common/widgets/future_dropdownmenu.dart +++ /dev/null @@ -1,25 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:local_spend/common/apifunctions/categories.dart'; -import 'dart:async'; - -Future> getDropDownItems(String type) async { - List> items = new List>(); - -// if (type == "categories") { - var categories = await getCategories(); - - categories.forEach((thisValue) { - items.add( - new DropdownMenuItem( - child: new Text(thisValue.name), - value: thisValue.index, - key: Key(thisValue.index), - ), - ); - }); - - print("oof"); - - return items; -// } -} diff --git a/lib/pages/receipt_page_2.dart b/lib/pages/receipt_page_2.dart index 67de152..f63b8ab 100644 --- a/lib/pages/receipt_page_2.dart +++ b/lib/pages/receipt_page_2.dart @@ -1,10 +1,12 @@ import 'package:flutter/material.dart'; import 'package:local_spend/common/platform/platform_scaffold.dart'; import 'package:intl/intl.dart'; +import 'dart:core'; import 'package:flutter/cupertino.dart'; import 'package:local_spend/common/apifunctions/find_organisations.dart'; import 'package:local_spend/common/widgets/organisations_dialog.dart'; import 'package:local_spend/common/apifunctions/submit_receipt_api.dart'; +import 'package:local_spend/common/apifunctions/categories.dart'; class Transaction { DateTime date; @@ -40,6 +42,10 @@ class ReceiptPage2State extends State { false, "Uncategorised", ); + + Future> getCats() async { + return await getCategories(); + } _submitReceipt(Transaction transaction) { DateTime dt = new DateTime.now(); @@ -64,6 +70,7 @@ class ReceiptPage2State extends State { receipt.recurring = ""; } + print("Category: " + transaction.category); receipt.category = transaction.category; receipt.amount = transaction.amount.text.toString(); receipt.time = DateFormat("yyyy-MM-dd'T'hh:mm':00.000+01:00'").format(transaction.date).toString(); @@ -73,10 +80,20 @@ class ReceiptPage2State extends State { } List _sampleRecurringOptions = new List(7); - List _sampleCategories = new List(4); @override Widget build(BuildContext context) { + + Future> _futureCats = getCats(); + List _categories = new List(); + _categories.add("Fetching categories..."); + + _futureCats.then((value) { + _categories = null; + _categories = value; + setState(() {}); + }); + _sampleRecurringOptions[0] = "None"; _sampleRecurringOptions[1] = "Daily"; _sampleRecurringOptions[2] = "Weekly"; @@ -85,11 +102,6 @@ class ReceiptPage2State extends State { _sampleRecurringOptions[5] = "Quarterly"; _sampleRecurringOptions[6] = "Yearly"; // these will be difficult to fetch from server as they are coded into the site's rather than fetched - _sampleCategories[0] = "Uncategorised"; - _sampleCategories[1] = "Cheese"; - _sampleCategories[2] = "Fish"; - _sampleCategories[3] = "Music"; - return PlatformScaffold( appBar: AppBar( @@ -313,9 +325,9 @@ class ReceiptPage2State extends State { height: MediaQuery.of(context).copyWith().size.height / 3, child: CupertinoPicker( backgroundColor: Colors.white, - children: _sampleCategories.map((thisOption) => Text(thisOption)).toList(), + children: _categories.map((thisOption) => Text(thisOption)).toList(), onSelectedItemChanged: ((newValue) { - transaction.category = _sampleCategories[newValue]; + transaction.category = _categories[newValue]; setState(() {}); }), itemExtent: 32,