diff --git a/lib/service/steps_service.dart b/lib/service/steps_service.dart new file mode 100644 index 0000000..05bf63e --- /dev/null +++ b/lib/service/steps_service.dart @@ -0,0 +1,28 @@ +import 'dart:convert'; +import 'package:http/http.dart' as http; +import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; + +class StepsService { + Future getStepsToday(deviceId) async { + final response = await http.get( + Uri.parse(Endpoints.getStepsToday), + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + 'deviceid': deviceId, + }, + ); + return jsonDecode(response.body)['cumulative_steps_today']; + } + + Future> getStepsLastFiveDays(deviceId) async { + final response = await http.get( + Uri.parse(Endpoints.getStepsLastFiveDays), + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + 'deviceid': deviceId, + }, + ); + print(jsonDecode(response.body)['daily_steps'].runtimeType); + return jsonDecode(response.body)['daily_steps']; + } +} \ No newline at end of file diff --git a/lib/utils/endpoints.dart b/lib/utils/endpoints.dart index 932f9ec..8211a4d 100644 --- a/lib/utils/endpoints.dart +++ b/lib/utils/endpoints.dart @@ -1,5 +1,6 @@ const String home = "https://leg-barkr.nw.r.appspot.com/"; const String register = "https://leg-barkr.nw.r.appspot.com/authentication/register"; const String verify = "https://leg-barkr.nw.r.appspot.com/authentication/verify"; -const String getLastLocation = "https://leg-barkr.nw.r.appspot.com/readings/last/location"; - +const String getLastLocation = "https://leg-barkr.nw.r.appspot.com/readings/location/last"; +const String getStepsToday = "https://leg-barkr.nw.r.appspot.com/readings/steps/today"; +const String getStepsLastFiveDays = "https://leg-barkr.nw.r.appspot.com/readings/steps/last-five-days"; diff --git a/lib/view/steps/steps_page.dart b/lib/view/steps/steps_page.dart index 5806796..948cc56 100644 --- a/lib/view/steps/steps_page.dart +++ b/lib/view/steps/steps_page.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:leg_barkr_app/model/steps_series.dart'; +import 'package:leg_barkr_app/service/steps_service.dart'; import 'package:leg_barkr_app/view/steps/steps_chart.dart'; import 'package:leg_barkr_app/view/steps/steps_today.dart'; @@ -11,28 +12,42 @@ class StepsPage extends StatefulWidget { } class _StepsPageState extends State { - // Dummy metrics - final List data = [ - StepsSeries(DateTime.utc(2022, 2, 9), 9867), - StepsSeries(DateTime.utc(2022, 2, 8), 8123), - StepsSeries(DateTime.utc(2022, 2, 7), 10234), - StepsSeries(DateTime.utc(2022, 2, 6), 6521), - StepsSeries(DateTime.utc(2022, 2, 5), 1021), - StepsSeries(DateTime.utc(2022, 2, 4), 10567), - StepsSeries(DateTime.utc(2022, 2, 3), 7500) - ]; + Future> onStepsRetrieved() async{ + List res = await StepsService().getStepsLastFiveDays("132-567-001"); + List steps = []; + for (int i = 0; i < res.length; i++){ + steps.add(res[i]); + } + return steps; + } @override Widget build(BuildContext context) { return Padding( padding: EdgeInsets.fromLTRB(0.0, 50.0, 10.0, 0.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - StepsToday(5123), - new Expanded(child: StepsChart(data)) - ], + child: FutureBuilder( + future: onStepsRetrieved(), + builder: (BuildContext context, AsyncSnapshot snapshot) { + int stepsToday = 0; + List stepsSeries = []; + if(snapshot.hasData) { + List stepsLastFiveDays = snapshot.data; + stepsToday = stepsLastFiveDays[0]; + for(int i = 0; i < stepsLastFiveDays.length; i++){ + DateTime now = DateTime.now(); + stepsSeries.add(StepsSeries(DateTime(now.year, now.month, now.day-i), stepsLastFiveDays[i])); + } + stepsSeries = List.from(stepsSeries.reversed); + } + return Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + StepsToday(stepsToday), + new Expanded(child: StepsChart(stepsSeries)) + ], + ); + }, ) ); }