diff --git a/lib/service/steps_service.dart b/lib/service/steps_service.dart index e14e6a9..05bf63e 100644 --- a/lib/service/steps_service.dart +++ b/lib/service/steps_service.dart @@ -11,7 +11,18 @@ class StepsService { 'deviceid': deviceId, }, ); - print(jsonDecode(response.body)['cumulative_steps_today']); 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/view/steps/steps_page.dart b/lib/view/steps/steps_page.dart index 563c67b..948cc56 100644 --- a/lib/view/steps/steps_page.dart +++ b/lib/view/steps/steps_page.dart @@ -12,36 +12,39 @@ 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{ - return await StepsService().getStepsToday("132-567-001"); + 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) { - onStepsRetrieved(); return Padding( padding: EdgeInsets.fromLTRB(0.0, 50.0, 10.0, 0.0), child: FutureBuilder( future: onStepsRetrieved(), builder: (BuildContext context, AsyncSnapshot snapshot) { - return Column( + 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(snapshot.data), - new Expanded(child: StepsChart(data)) + StepsToday(stepsToday), + new Expanded(child: StepsChart(stepsSeries)) ], ); },