diff --git a/lib/common/apifunctions/get_graph_data.dart b/lib/common/apifunctions/get_graph_data.dart index 8012a29..e2d1bb1 100644 --- a/lib/common/apifunctions/get_graph_data.dart +++ b/lib/common/apifunctions/get_graph_data.dart @@ -86,12 +86,27 @@ class GraphData { ]; } - /// Graph types: + // TODO: Show available graph types based on whether or not the user is an Organisation or User + + /// Customer graph types: https://dev.peartrade.org/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.peartrade.org/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"} diff --git a/lib/common/functions/save_current_login.dart b/lib/common/functions/save_current_login.dart index 5800c10..44ec919 100644 --- a/lib/common/functions/save_current_login.dart +++ b/lib/common/functions/save_current_login.dart @@ -16,6 +16,9 @@ saveCurrentLogin(Map responseJson, loginEmail) async { var email = (loginEmail != null) ? loginEmail : ""; + var userType = (responseJson != null && responseJson.isNotEmpty) + ? LoginModel.fromJson(responseJson).userType + : ""; await preferences.setString( 'LastUser', (user != null && user.length > 0) ? user : ""); @@ -23,4 +26,6 @@ saveCurrentLogin(Map responseJson, loginEmail) async { 'LastToken', (token != null && token.length > 0) ? token : ""); await preferences.setString( 'LastEmail', (email != null && email.length > 0) ? email : ""); + await preferences.setString( + 'LastUserType', (userType != null && userType.length > 0) ? userType : ""); } diff --git a/lib/model/json/login_model.dart b/lib/model/json/login_model.dart index f47a15d..a87a810 100644 --- a/lib/model/json/login_model.dart +++ b/lib/model/json/login_model.dart @@ -1,15 +1,18 @@ class LoginModel { final String userName; final String token; + final String userType; - LoginModel(this.userName, this.token); + LoginModel(this.userName, this.token, this.userType); LoginModel.fromJson(Map json) : userName = json['display_name'], + userType = json['user_type'], token = json['session_key']; Map toJson() => { 'name': userName, + 'user_type': userType, 'token': token, }; } diff --git a/lib/pages/customerGraphs.dart b/lib/pages/customerGraphs.dart new file mode 100644 index 0000000..c581385 --- /dev/null +++ b/lib/pages/customerGraphs.dart @@ -0,0 +1,151 @@ +import 'package:flutter/material.dart'; +import 'package:local_spend/common/apifunctions/get_graph_data.dart'; +import 'package:charts_flutter/flutter.dart' as charts; + +class CustomerGraphs extends StatefulWidget { + CustomerGraphs({Key key}) : super(key: key); + + @override + _CustomerGraphsState createState() { + return _CustomerGraphsState(); + } +} + +class _CustomerGraphsState extends State { + GraphData totalLastWeekGraph = new GraphData("total_last_week"); + GraphData avgSpendLastWeekGraph = new GraphData("avg_spend_last_week"); + GraphData totalLastMonthGraph = new GraphData("total_last_month"); + GraphData avgSpendLastMonth = new GraphData("avg_spend_last_month"); + + @override + void initState() { + super.initState(); + } + + @override + void dispose() { + super.dispose(); + } + + @override + Widget build(BuildContext context) { + // Initializing graphs: + if (!totalLastWeekGraph.loaded) { + totalLastWeekGraph.setGraphData().then((_) { + setState(() {}); + }); + } + + if (!avgSpendLastWeekGraph.loaded) { + avgSpendLastWeekGraph.setGraphData().then((_) { + setState(() {}); + }); + } + + if (!totalLastMonthGraph.loaded) { + totalLastMonthGraph.setGraphData().then((_) { + setState(() {}); + }); + } + + if (!avgSpendLastMonth.loaded) { + avgSpendLastMonth.setGraphData().then((_) { + setState(() {}); + }); + } + + return ListView( + children: [ + + Container( + padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), + child : Text( + "Last Week's Total Spend", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + + Tooltip( + message: "Graph of total spend last week", + child : Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: totalLastWeekGraph.graph != null ? new charts.TimeSeriesChart(totalLastWeekGraph.graph) : Center(child: Text("Loading graph...")), //List> + ), + ), + + Container( + padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), + child : Text( + "Last Week's Average Spend", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + + Tooltip( + message: "Graph of average spend last week", + child : Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: avgSpendLastWeekGraph.graph != null ? new charts.TimeSeriesChart(avgSpendLastWeekGraph.graph) : Center(child: Text("Loading graph...")), //List> + ), + ), + + Container( + padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), + child : Text( + "Last Month's Total Spend", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + + Tooltip( + message: "Graph of total spend last month", + child : Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: totalLastMonthGraph.graph != null ? new charts.TimeSeriesChart(totalLastMonthGraph.graph) : Center(child: Text("Loading graph...")), //List> + ), + ), + + Container( + padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), + child : Text( + "Last Month's Average Spend", + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 22.0, + color: Colors.black, + fontWeight: FontWeight.bold, + ), + ), + ), + + Tooltip( + message: "Graph of average spend last month", + child : Container( + padding: EdgeInsets.symmetric(horizontal: 10), + height: 200, + child: avgSpendLastMonth.graph != null ? new charts.TimeSeriesChart(avgSpendLastMonth.graph) : Center(child: Text("Loading graph...")), //List> + ), + ), + + ], + ); + } +} \ No newline at end of file diff --git a/lib/pages/orgGraphs.dart b/lib/pages/orgGraphs.dart new file mode 100644 index 0000000..c016abf --- /dev/null +++ b/lib/pages/orgGraphs.dart @@ -0,0 +1,35 @@ +import 'package:flutter/material.dart'; +import 'package:local_spend/common/apifunctions/get_graph_data.dart'; +import 'package:charts_flutter/flutter.dart' as charts; + +class OrgGraphs extends StatefulWidget { + OrgGraphs({Key key}) : super(key: key); + + @override + _OrgGraphsState createState() { + return _OrgGraphsState(); + } +} + +class _OrgGraphsState extends State { + GraphData totalLastWeekGraph = new GraphData("total_last_week"); + + @override + void initState() { + super.initState(); + } + + @override + void dispose() { + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return ListView( + children: [ + + ], + ); + } +} \ No newline at end of file diff --git a/lib/pages/stats_page.dart b/lib/pages/stats_page.dart index 8b79872..a98f471 100644 --- a/lib/pages/stats_page.dart +++ b/lib/pages/stats_page.dart @@ -1,21 +1,8 @@ import 'package:flutter/material.dart'; import 'package:local_spend/common/platform/platform_scaffold.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:local_spend/common/functions/logout.dart'; -import 'package:url_launcher/url_launcher.dart'; -import 'package:local_spend/common/functions/customAbout.dart' as custom; -import 'package:local_spend/common/functions/showDialogTwoButtons.dart'; -import 'package:local_spend/common/widgets/charts/donut_chart.dart'; -import 'package:local_spend/common/widgets/charts/outside_label.dart'; -import 'package:local_spend/common/widgets/charts/auto_label.dart'; -import 'package:local_spend/common/widgets/charts/grouped_bar_chart.dart'; -import 'package:local_spend/common/widgets/charts/scatter_bucketingAxis_legend.dart'; -import 'package:local_spend/common/widgets/charts/numeric_line_bar_combo.dart'; -import 'package:local_spend/common/widgets/charts/series_legend_with_measures.dart'; -import 'package:local_spend/common/widgets/charts/time_series_simple.dart'; -import 'package:local_spend/common/apifunctions/get_graph_data.dart'; -import 'package:charts_flutter/flutter.dart' as charts; -import 'package:local_spend/common/widgets/charts/chart_builder.dart'; +import 'package:local_spend/pages/customerGraphs.dart'; +import 'package:local_spend/pages/orgGraphs.dart'; const URL = "https://flutter.io/"; const demonstration = false; @@ -28,17 +15,7 @@ class StatsPage extends StatefulWidget { } class StatsPageState extends State { - - /// Graph types: - /// - total_last_week - /// - avg_spend_last_week - /// - total_last_month - /// - avg_spend_last_month - - GraphData totalLastWeekGraph = new GraphData("total_last_week"); - GraphData avgSpendLastWeekGraph = new GraphData("avg_spend_last_week"); - GraphData totalLastMonthGraph = new GraphData("total_last_month"); - GraphData avgSpendLastMonth = new GraphData("avg_spend_last_month"); + String userType = "-"; @override void initState() { @@ -56,31 +33,17 @@ class StatsPageState extends State { await preferences.setString('LastPageRoute', lastRoute); } + Future _getUserType() async { + SharedPreferences preferences = await SharedPreferences.getInstance(); + return await preferences.get('LastUserType'); + } + @override Widget build(BuildContext context) { - - // Initializing graphs: - - if (!totalLastWeekGraph.loaded) { - totalLastWeekGraph.setGraphData().then((_) { - setState(() {}); - }); - } - - if (!avgSpendLastWeekGraph.loaded) { - avgSpendLastWeekGraph.setGraphData().then((_) { - setState(() {}); - }); - } - - if (!totalLastMonthGraph.loaded) { - totalLastMonthGraph.setGraphData().then((_) { - setState(() {}); - }); - } - - if (!avgSpendLastMonth.loaded) { - avgSpendLastMonth.setGraphData().then((_) { + if (userType == "-") { + _getUserType().then((value) { + print(value); + userType = '${value[0].toUpperCase()}${value.substring(1)}'; // capitalises first letter setState(() {}); }); } @@ -88,114 +51,38 @@ class StatsPageState extends State { return PlatformScaffold( appBar: AppBar( backgroundColor: Colors.blue[400], - title: Text( - "Statistics", - style: TextStyle( - fontSize: 20, - color: Colors.white, - ), + title: Row( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + "Statistics", + style: TextStyle( + fontSize: 20, + color: Colors.white, + ), + ), + Padding( + padding: EdgeInsets.symmetric(horizontal: 10) + ), + + Text( + "User type: " + userType, + style: TextStyle( + fontSize: 20, + color: Colors.white70, + ), + ), + ], + ), + centerTitle: true, + iconTheme: IconThemeData(color: Colors.black), ), -// leading: BackButton(), - centerTitle: true, - iconTheme: IconThemeData(color: Colors.black), - ), body : Container( padding: EdgeInsets.fromLTRB(0, 0, 0, 0), - child: ListView( - children: [ - - Container( - padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), - child : Text( - "Last Week's Total Spend", - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 22.0, - color: Colors.black, - fontWeight: FontWeight.bold, - ), - ), - ), - - Tooltip( - message: "Graph of total spend last week", - child : Container( - padding: EdgeInsets.symmetric(horizontal: 10), - height: 200, - child: totalLastWeekGraph.graph != null ? new charts.TimeSeriesChart(totalLastWeekGraph.graph) : Center(child: Text("Loading graph...")), //List> - ), - ), - - Container( - padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), - child : Text( - "Last Week's Average Spend", - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 22.0, - color: Colors.black, - fontWeight: FontWeight.bold, - ), - ), - ), - - Tooltip( - message: "Graph of average spend last week", - child : Container( - padding: EdgeInsets.symmetric(horizontal: 10), - height: 200, - child: avgSpendLastWeekGraph.graph != null ? new charts.TimeSeriesChart(avgSpendLastWeekGraph.graph) : Center(child: Text("Loading graph...")), //List> - ), - ), - - Container( - padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), - child : Text( - "Last Month's Total Spend", - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 22.0, - color: Colors.black, - fontWeight: FontWeight.bold, - ), - ), - ), - - Tooltip( - message: "Graph of total spend last month", - child : Container( - padding: EdgeInsets.symmetric(horizontal: 10), - height: 200, - child: totalLastMonthGraph.graph != null ? new charts.TimeSeriesChart(totalLastMonthGraph.graph) : Center(child: Text("Loading graph...")), //List> - ), - ), - - Container( - padding: EdgeInsets.fromLTRB(0.0,17,0.0,0.0), - child : Text( - "Last Month's Average Spend", - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 22.0, - color: Colors.black, - fontWeight: FontWeight.bold, - ), - ), - ), - - Tooltip( - message: "Graph of average spend last month", - child : Container( - padding: EdgeInsets.symmetric(horizontal: 10), - height: 200, - child: avgSpendLastMonth.graph != null ? new charts.TimeSeriesChart(avgSpendLastMonth.graph) : Center(child: Text("Loading graph...")), //List> - ), - ), - - ], - ), + child: (userType == "-" ? null : (userType.toLowerCase() == "customer" ? CustomerGraphs() : OrgGraphs())), ), ); }