From 7553ce25f878d752b8dba12ab212e5070eabed16 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 21 Aug 2019 16:50:26 +0100 Subject: [PATCH] organisations graphs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit save login is now somehow broken 🧐 --- lib/common/apifunctions/get_graph_data.dart | 317 +++++++++----------- lib/pages/map_page.dart | 14 +- lib/pages/orgGraphs.dart | 148 ++++++++- lib/pages/spash_screen.dart | 5 +- 4 files changed, 293 insertions(+), 191 deletions(-) diff --git a/lib/common/apifunctions/get_graph_data.dart b/lib/common/apifunctions/get_graph_data.dart index f24ebdf..6513fb4 100644 --- a/lib/common/apifunctions/get_graph_data.dart +++ b/lib/common/apifunctions/get_graph_data.dart @@ -4,6 +4,132 @@ import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; import 'package:charts_flutter/flutter.dart' as charts; + +/// Customer graph types: https://dev.localspend.co.uk/api/v1/customer/graphs +/// - total_last_week +/// - avg_spend_last_week +/// - total_last_month +/// - avg_spend_last_month + + +/// Organisations' graphs types: to fetch, POST to https://dev.localspend.co.uk/api/stats/[name] as {"session_key":"[boop beep]"} +/// - organisations_all : organisation +/// - pies : organisation/pies +/// - snippets : organisation/snippets +/// - graphs : organisation/graphs +/// - {"graph":"customers_last_7_days","session_key":"[bleep]"} +/// - {"graph":"customers_last_30_days","session_key":"[blah]"} +/// - {"graph":"sales_last_7_days","session_key":"[bloop]"} +/// - {"graph":"sales_last_7_days","session_key":"[reee]"} +/// - {"graph":"purchases_last_7_days","session_key":"[yee]"} +/// - {"graph":"purchases_last_30_days","session_key":"[yah]"} +/// - {"graph":"purchases_all;","session_key":"[kappa]"} // I don't think this one works +/// +/// HTTP POST request sample: +/// {"graph":"total_last_week","session_key":"blahblahblah"} + + + +class OrganisationGraph { + OrganisationGraph( + this.chartType, + {this.graphsType = ""} + ); + + String graphsType = ""; // type of graph, eg customers_last_7_days, sales_last_30_days, purchases_last_30_days etc + String chartType; // type of chart, eg organisations_all, pies, snippets or graphs + + List> graph; + + List cachedData; + + bool loaded = false; + + Future getGraphData() async { + if (loaded) { + this.graph = [ + new charts.Series( + id: 'Chart', + colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, + domainFn: (TimeSeriesCustomersOrSales spend, _) => spend.time, + measureFn: (TimeSeriesCustomersOrSales spend, _) => + spend.numberOfStuff, + data: cachedData, + ) + ]; + return null; + } + + String url = "https://dev.localspend.co.uk/api/v1/organisation/"; + + if (!(this.chartType == "organisations_all")) { + url += this.chartType; + } + + SharedPreferences preferences = await SharedPreferences.getInstance(); + Map body; + + if (this.chartType == "graphs") { + body = { + 'graph': this.graphsType, + 'session_key': preferences.get('LastToken'), + }; + } else { + body = { + 'session_key': preferences.get('LastToken'), + }; + } + + print(url); + print(json.encode(body).toString()); + + final response = await http.post( + url, + body: json.encode(body), + ); + + try { + if (response.statusCode == 200) { + final responseJson = jsonDecode(response.body); + final List labels = responseJson['graph']['labels']; + final List data = responseJson['graph']['data']; + + List graphDataList = new List< + TimeSeriesCustomersOrSales>(); + + for (int i = 0; i < labels.length; i++) { + graphDataList.add( + new TimeSeriesCustomersOrSales( + data[i] * 1.00, DateTime.parse(labels[i]))); + } + + this.cachedData = graphDataList; + this.loaded = true; + + this.graph = [ + new charts.Series( + id: 'Chart', + colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, + domainFn: (TimeSeriesCustomersOrSales spend, _) => spend.time, + measureFn: (TimeSeriesCustomersOrSales spend, _) => + spend.numberOfStuff, + data: graphDataList, + ) + ]; + return this.graph; + } else { + final errorMessage = json.decode(response.body)['message']; + print("Error: " + errorMessage); + this.graph = null; + } + } catch (error) { + print(error.toString()); + } + + } + +} + class GraphData { GraphData( this.chartType, @@ -15,8 +141,8 @@ class GraphData { List cachedData; bool loaded = false; - Future setGraphData() async { - if (loaded == true) { + Future>> setGraphData() async { + if (loaded) { this.graph = [ new charts.Series( id: 'Spend', @@ -26,6 +152,7 @@ class GraphData { data: cachedData, ) ]; + return null; } final url = "https://dev.localspend.co.uk/api/v1/customer/graphs"; @@ -66,167 +193,16 @@ class GraphData { data: timeSeriesSpendList, ) ]; + return this.graph; + } else { final errorMessage = json.decode(response.body)['message']; print("Error: " + errorMessage); this.graph = null; - } - } - - Future>> getGraphData() async { - if (loaded == true) { - return [ - new charts.Series( - id: 'Spend', - colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, - domainFn: (TimeSeriesSpend spend, _) => spend.time, - measureFn: (TimeSeriesSpend spend, _) => spend.spend, - data: cachedData, - ) - ]; - } - - // TODO: Show available graph types based on whether or not the user is an Organisation or User - - /// Customer graph types: https://dev.localspend.co.uk/api/v1/customer/graphs - /// - total_last_week - /// - avg_spend_last_week - /// - total_last_month - /// - avg_spend_last_month - /// - /// Organisations' graphs types: to fetch, POST to https://dev.localspend.co.uk/api/stats/[name] as {"session_key":"[boop beep]"} - /// - organisations_all : organisation - /// - pies : organisation/pies - /// - snippets : organisation/snippets - /// - graphs : organisation/graphs - /// - {"graph":"customers_last_7_days","session_key":"[bleep]"} - /// - {"graph":"customers_last_30_days","session_key":"[blah]"} - /// - {"graph":"sales_last_7_days","session_key":"[bloop]"} - /// - {"graph":"sales_last_7_days","session_key":"[reee]"} - /// - {"graph":"purchases_last_7_days","session_key":"[yee]"} - /// - {"graph":"purchases_last_30_days","session_key":"[yah]"} - /// - {"graph":"purchases_all;","session_key":"[kappa]"} // I don't think this one works - /// - /// HTTP POST request sample: - /// {"graph":"total_last_week","session_key":"blahblahblah"} - - final url = "https://dev.localspend.co.uk/api/v1/customer/graphs"; - SharedPreferences preferences = await SharedPreferences.getInstance(); - - Map body = { - 'graph': this.chartType, - 'session_key': preferences.get('LastToken'), - }; - - final response = await http.post( - url, - body: json.encode(body), - ); - - if (response.statusCode == 200) { - final responseJson = jsonDecode(response.body); - final List labels = responseJson['graph']['labels']; - final List data = responseJson['graph']['data']; - - List timeSeriesSpendList = new List(); - - for (int i = 0; i < labels.length; i++) { - timeSeriesSpendList.add( - new TimeSeriesSpend(data[i] * 1.00, DateTime.parse(labels[i]))); -// print(timeSeriesSpendList[i].time.toString() + " : " + timeSeriesSpendList[i].spend.toString()); - } - - cachedData = timeSeriesSpendList; - loaded = true; - - return [ - new charts.Series( - id: 'Spend', - colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, - domainFn: (TimeSeriesSpend spend, _) => spend.time, - measureFn: (TimeSeriesSpend spend, _) => spend.spend, - data: timeSeriesSpendList, - ) - ]; - } else { - final errorMessage = json.decode(response.body)['message']; - print("Error: " + errorMessage); - return null; } } -} - -class OrgGraphData { - OrgGraphData( - this.chartType, - ); - - var chartType; - List> graph; - - List cachedData; - bool loaded = false; - - Future setGraphData() async { - if (loaded == true) { - this.graph = [ - new charts.Series( - id: 'Spend', - colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, - domainFn: (TimeSeriesSpend spend, _) => spend.time, - measureFn: (TimeSeriesSpend spend, _) => spend.spend, - data: cachedData, - ) - ]; - } - - final url = "https://dev.localspend.co.uk/api/v1/customer/graphs"; - SharedPreferences preferences = await SharedPreferences.getInstance(); - - Map body = { - 'graph': this.chartType, - 'session_key': preferences.get('LastToken'), - }; - - final response = await http.post( - url, - body: json.encode(body), - ); - - if (response.statusCode == 200) { - final responseJson = jsonDecode(response.body); - final List labels = responseJson['graph']['labels']; - final List data = responseJson['graph']['data']; - - List timeSeriesSpendList = new List(); - - for (int i = 0; i < labels.length; i++) { - timeSeriesSpendList.add( - new TimeSeriesSpend(data[i] * 1.00, DateTime.parse(labels[i]))); -// print(timeSeriesSpendList[i].time.toString() + " : " + timeSeriesSpendList[i].spend.toString()); - } - - cachedData = timeSeriesSpendList; - loaded = true; - - this.graph = [ - new charts.Series( - id: 'Spend', - colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, - domainFn: (TimeSeriesSpend spend, _) => spend.time, - measureFn: (TimeSeriesSpend spend, _) => spend.spend, - data: timeSeriesSpendList, - ) - ]; - } else { - final errorMessage = json.decode(response.body)['message']; - print("Error: " + errorMessage); - - this.graph = null; - } - } Future>> getGraphData() async { if (loaded == true) { @@ -241,30 +217,6 @@ class OrgGraphData { ]; } - // TODO: Show available graph types based on whether or not the user is an Organisation or User - - /// Customer graph types: https://dev.localspend.co.uk/api/v1/customer/graphs - /// - total_last_week - /// - avg_spend_last_week - /// - total_last_month - /// - avg_spend_last_month - /// - /// Organisations' graphs types: to fetch, POST to https://dev.localspend.co.uk/api/stats/[name] as {"session_key":"[boop beep]"} - /// - organisations_all : organisation - /// - pies : organisation/pies - /// - snippets : organisation/snippets - /// - graphs : organisation/graphs - /// - {"graph":"customers_last_7_days","session_key":"[bleep]"} - /// - {"graph":"customers_last_30_days","session_key":"[blah]"} - /// - {"graph":"sales_last_7_days","session_key":"[bloop]"} - /// - {"graph":"sales_last_7_days","session_key":"[reee]"} - /// - {"graph":"purchases_last_7_days","session_key":"[yee]"} - /// - {"graph":"purchases_last_30_days","session_key":"[yah]"} - /// - {"graph":"purchases_all;","session_key":"[kappa]"} // I don't think this one works - /// - /// HTTP POST request sample: - /// {"graph":"total_last_week","session_key":"blahblahblah"} - final url = "https://dev.localspend.co.uk/api/v1/customer/graphs"; SharedPreferences preferences = await SharedPreferences.getInstance(); @@ -318,3 +270,10 @@ class TimeSeriesSpend { final DateTime time; final double spend; } + +class TimeSeriesCustomersOrSales { + TimeSeriesCustomersOrSales(this.numberOfStuff, this.time); + + final DateTime time; + final double numberOfStuff; +} \ No newline at end of file diff --git a/lib/pages/map_page.dart b/lib/pages/map_page.dart index 1cdb36c..2b05ef5 100644 --- a/lib/pages/map_page.dart +++ b/lib/pages/map_page.dart @@ -49,13 +49,13 @@ class MapSampleState extends State { @override Widget build(BuildContext context) { return new Scaffold( - body: GoogleMap( - mapType: MapType.hybrid, - initialCameraPosition: _kGooglePlex, - onMapCreated: (GoogleMapController controller) { - _controller.complete(controller); - }, - ), +// body: GoogleMap( +// mapType: MapType.hybrid, +// initialCameraPosition: _kGooglePlex, +// onMapCreated: (GoogleMapController controller) { +// _controller.complete(controller); +// }, +// ), ); } } diff --git a/lib/pages/orgGraphs.dart b/lib/pages/orgGraphs.dart index 4b08e9a..057f68c 100644 --- a/lib/pages/orgGraphs.dart +++ b/lib/pages/orgGraphs.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:local_spend/common/apifunctions/get_graph_data.dart'; -//import 'package:charts_flutter/flutter.dart' as charts; +import 'package:charts_flutter/flutter.dart' as charts; class OrgGraphs extends StatefulWidget { OrgGraphs({Key key}) : super(key: key); @@ -12,7 +12,27 @@ class OrgGraphs extends StatefulWidget { } class _OrgGraphsState extends State { - GraphData totalLastWeekGraph = new GraphData("total_last_week"); + + /// Organisations' graphs types: to fetch, POST to https://dev.localspend.co.uk/api/stats/[graph_type] as {"session_key":"[boop beep]"} + /// - organisations_all : organisation + /// - pies : organisation/pies + /// - snippets : organisation/snippets + /// - graphs : organisation/graphs + /// - {"graph":"customers_last_7_days","session_key":"[bleep]"} + /// - {"graph":"customers_last_30_days","session_key":"[blah]"} + /// - {"graph":"sales_last_7_days","session_key":"[bloop]"} + /// - {"graph":"sales_last_7_days","session_key":"[reee]"} + /// - {"graph":"purchases_last_7_days","session_key":"[yee]"} + /// - {"graph":"purchases_last_30_days","session_key":"[yah]"} + /// - {"graph":"purchases_all;","session_key":"[kappa]"} // I don't think this one works + /// + /// HTTP POST request sample: + /// {"graph":"total_last_week","session_key":"blahblahblah"} + +// OrganisationGraph customersLastWeek = new OrganisationGraph("graphs", graphsType: "customers_last_7_days"); + OrganisationGraph customersLastMonth = new OrganisationGraph("graphs", graphsType: "customers_last_30_days"); + OrganisationGraph salesLastMonth = new OrganisationGraph("graphs", graphsType: "sales_last_30_days"); + OrganisationGraph purchasesLastMonth = new OrganisationGraph("graphs", graphsType: "purchases_last_30_days"); //purchases_last_30_days @override void initState() { @@ -26,8 +46,130 @@ class _OrgGraphsState extends State { @override Widget build(BuildContext context) { +// if (!customersLastWeek.loaded) { +// customersLastWeek.getGraphData().then((_) { +// setState(() {}); +// }); +// } + if (!customersLastMonth.loaded) { + customersLastMonth.getGraphData().then((_) { + setState(() {}); + }); + } + if (!salesLastMonth.loaded) { + salesLastMonth.getGraphData().then((_) { + setState(() {}); + }); + } + if (!purchasesLastMonth.loaded) { + purchasesLastMonth.getGraphData().then((_) { + setState(() {}); + }); + } + return ListView( - children: [], + children: [ +// Container( +// padding: EdgeInsets.fromLTRB(0.0, 17, 0.0, 0.0), +// child: Text( +// "Last Week's Customers", +// textAlign: TextAlign.center, +// style: TextStyle( +// fontSize: 22.0, +// color: Colors.black, +// fontWeight: FontWeight.bold, +// ), +// ), +// ), +// Tooltip( +// message: "Graph of customers last week", +// child: Container( +// padding: EdgeInsets.symmetric(horizontal: 10), +// height: 200, +// child: customersLastWeek.graph != null +// ? new charts.TimeSeriesChart(customersLastWeek.graph) +// : Center( +// child: Text( +// "Loading graph...")), //List> +// ), +// ), + + Container( + padding: EdgeInsets.fromLTRB(0.0, 17, 0.0, 0.0), + child: Text( + "This Month's Customers", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + Tooltip( + message: "Customers this month", // this needs to be better + child: Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: customersLastMonth.graph != null + ? new charts.TimeSeriesChart(customersLastMonth.graph) + : Center( + child: Text( + "Loading graph...")), //List> + ), + ), + + Container( + padding: EdgeInsets.fromLTRB(0.0, 17, 0.0, 0.0), + child: Text( + "This Month's Revenue", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + Tooltip( + message: "Revenue from sales this month", + child: Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: salesLastMonth.graph != null + ? new charts.TimeSeriesChart(salesLastMonth.graph) + : Center( + child: Text( + "Loading graph...")), //List> + ), + ), + + Container( + padding: EdgeInsets.fromLTRB(0.0, 17, 0.0, 0.0), + child: Text( + "This Month's Sales", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + Tooltip( + message: "Number of sales this month", + child: Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: purchasesLastMonth.graph != null + ? new charts.TimeSeriesChart(purchasesLastMonth.graph) + : Center( + child: Text( + "Loading graph...")), //List> + ), + ), + + ], ); } } diff --git a/lib/pages/spash_screen.dart b/lib/pages/spash_screen.dart index f887e09..bcfe751 100644 --- a/lib/pages/spash_screen.dart +++ b/lib/pages/spash_screen.dart @@ -41,14 +41,15 @@ class _SplashScreenState extends State { alignment: FractionalOffset(0.5, 0.3), decoration: BoxDecoration( image: DecorationImage( - image: AssetImage('assets/images/launch_image.png')), + image: AssetImage('assets/images/launch_image.png'), + ), ), ), ), Container( margin: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 30.0), child: Text( - "© Copyright Statement 2019", + "© Copyright Pear Trading 2019", style: TextStyle( fontSize: 16.0, color: Colors.black,