diff --git a/lib/service/auth_service.dart b/lib/service/auth_service.dart new file mode 100644 index 0000000..8d066bc --- /dev/null +++ b/lib/service/auth_service.dart @@ -0,0 +1,21 @@ +import 'dart:convert'; +import 'package:http/http.dart' as http; +import 'package:leg_barkr_app/utils/endpoints.dart' as Endpoints; + +class AuthService{ + Future> getUserDevices(sessionToken) async { + final response = await http.get( + Uri.parse(Endpoints.getUserDevices), + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + 'token': sessionToken, + }, + ); + List list = jsonDecode(response.body)['devices']; + List res = []; + for (final l in list) { + res.add(l.toString()); + } + return res; + } +} \ No newline at end of file diff --git a/lib/service/steps_service.dart b/lib/service/steps_service.dart index 05bf63e..561959a 100644 --- a/lib/service/steps_service.dart +++ b/lib/service/steps_service.dart @@ -14,7 +14,7 @@ class StepsService { return jsonDecode(response.body)['cumulative_steps_today']; } - Future> getStepsLastFiveDays(deviceId) async { + Future> getStepsLastFiveDays(deviceId) async { final response = await http.get( Uri.parse(Endpoints.getStepsLastFiveDays), headers: { @@ -22,7 +22,11 @@ class StepsService { 'deviceid': deviceId, }, ); - print(jsonDecode(response.body)['daily_steps'].runtimeType); - return jsonDecode(response.body)['daily_steps']; + List list = jsonDecode(response.body)['daily_steps']; + List steps = []; + for (final l in list){ + steps.add(l); + } + return steps; } } \ No newline at end of file diff --git a/lib/utils/endpoints.dart b/lib/utils/endpoints.dart index b1f966a..0167263 100644 --- a/lib/utils/endpoints.dart +++ b/lib/utils/endpoints.dart @@ -1,7 +1,8 @@ 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 getUserDevices = "https://leg-barkr.nw.r.appspot.com/authentication/get-user-devices"; 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"; -const String getMetricsSummary = "https://leg-barkr.nw.r.appspot.com/readings/metrics-summary"; \ No newline at end of file +const String getMetricsSummary = "https://leg-barkr.nw.r.appspot.com/readings/metrics-summary"; diff --git a/lib/view/home.dart b/lib/view/home.dart index 376205c..3950402 100644 --- a/lib/view/home.dart +++ b/lib/view/home.dart @@ -1,9 +1,11 @@ import 'package:flutter/material.dart'; import 'package:firebase_auth/firebase_auth.dart'; +import 'package:leg_barkr_app/service/auth_service.dart'; import 'package:leg_barkr_app/view/metrics/metrics_page.dart'; import 'package:leg_barkr_app/view/steps/steps_page.dart'; import 'package:leg_barkr_app/view/map/map_page.dart'; import 'package:leg_barkr_app/view/settings/settings_page.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class HomeScreen extends StatefulWidget { const HomeScreen({Key? key}) : super(key: key); @@ -16,13 +18,19 @@ class _HomeScreenState extends State { int _page = 0; PageController _pageController = PageController(); - _HomeScreenState(){ + _HomeScreenState() { FirebaseAuth.instance .authStateChanges() - .listen((User? user) { - if (user == null) { - Navigator.pushNamed(context, "/login"); - } + .listen((User? user) async { + if (user == null) { + Navigator.pushNamed(context, "/login"); + } else { + final prefs = await SharedPreferences.getInstance(); + final String token = await user.getIdToken(); + final List userDevices = await AuthService().getUserDevices(token); + prefs.setStringList("devices", userDevices); + prefs.setString("current_device", userDevices[0]); + } }); } @@ -58,7 +66,7 @@ class _HomeScreenState extends State { BottomNavigationBarItem(icon: Icon(Icons.location_on_outlined), label: 'Location'), BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'), ], - currentIndex: _page, + currentIndex: _page, selectedItemColor: Colors.green, unselectedItemColor: Colors.black, showSelectedLabels: true, diff --git a/lib/view/map/map_page.dart b/lib/view/map/map_page.dart index b305560..64ac8c4 100644 --- a/lib/view/map/map_page.dart +++ b/lib/view/map/map_page.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'package:leg_barkr_app/service/map_service.dart'; +import 'package:shared_preferences/shared_preferences.dart'; class MapPage extends StatefulWidget { const MapPage({ Key? key }) : super(key: key); @@ -15,8 +16,11 @@ class _MapPageState extends State { Future _onMapCreated(GoogleMapController controller) async { _mapController = controller; - final lastLocation = await MapService().getPetLastLocation("132-567-001"); // change this. + final prefs = await SharedPreferences.getInstance(); + final String deviceId = prefs.getString("current_device") ?? ""; + final lastLocation = await MapService().getPetLastLocation(deviceId); final myLocation = await MapService().getMyLocation(); + setState(() { _markers.clear(); final petMarker = Marker( diff --git a/lib/view/metrics/metrics_page.dart b/lib/view/metrics/metrics_page.dart index 3b1bae5..1d33ef2 100644 --- a/lib/view/metrics/metrics_page.dart +++ b/lib/view/metrics/metrics_page.dart @@ -3,10 +3,10 @@ import 'package:leg_barkr_app/model/metrics_data.dart'; import 'package:leg_barkr_app/model/metrics_response.dart'; import 'package:leg_barkr_app/model/temp_series.dart'; import 'package:leg_barkr_app/service/metrics_service.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'metrics_row.dart'; -import 'package:leg_barkr_app/utils/constants.dart' as Constants; - import 'temp_chart.dart'; +import 'package:leg_barkr_app/utils/constants.dart' as Constants; class MetricsPage extends StatefulWidget { const MetricsPage({ Key? key }) : super(key: key); @@ -29,10 +29,11 @@ class _MetricsPageState extends State { */ Future onMetricsReceived() async{ - return await MetricsService().getMetricsSummary("132-567-001"); + final prefs = await SharedPreferences.getInstance(); + final String deviceId = prefs.getString("current_device") ?? ""; + return await MetricsService().getMetricsSummary(deviceId); } - @override Widget build(BuildContext context) { return Padding( @@ -52,7 +53,6 @@ class _MetricsPageState extends State { children: [ Text("Today's summary", textAlign: TextAlign.center, style: TextStyle(color: Colors.black, fontSize: 36, fontWeight: FontWeight.bold)), - // Dummy data MetricsRow(new MetricsData(metricsResponse.lastSkinTemp, metricsResponse.minSkinTemp, metricsResponse.maxSkinTemp, Constants.MIN_SKIN_TEMP, Constants.MAX_SKIN_TEMP, Constants.LOW_SKIN_TEMP_DOG, Constants.HIGH_SKIN_TEMP_DOG, "Skin temperature", "°C"), Colors.white, Colors.green, true), MetricsRow(new MetricsData(metricsResponse.lastHumidity, metricsResponse.minHumidity, metricsResponse.maxHumidity, Constants.MIN_HUMIDITY, Constants.MAX_HUMIDITY, Constants.LOW_HUMIDITY_DOG, Constants.HIGH_HUMIDITY_DOG, "Humidity", "%"), Colors.green, Colors.black, false), MetricsRow(new MetricsData(metricsResponse.lastAirTemp, metricsResponse.minAirTemp, metricsResponse.maxAirTemp, Constants.MIN_AIR_TEMP, Constants.MAX_AIR_TEMP, Constants.LOW_AIR_TEMP_DOG, Constants.HIGH_AIR_TEMP_DOG, "Air temperature", "°C"), Colors.white, Colors.green, true), diff --git a/lib/view/steps/steps_page.dart b/lib/view/steps/steps_page.dart index 948cc56..742c3c4 100644 --- a/lib/view/steps/steps_page.dart +++ b/lib/view/steps/steps_page.dart @@ -3,6 +3,7 @@ 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'; +import 'package:shared_preferences/shared_preferences.dart'; class StepsPage extends StatefulWidget { const StepsPage({ Key? key }) : super(key: key); @@ -14,12 +15,9 @@ class StepsPage extends StatefulWidget { class _StepsPageState extends State { 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; + final prefs = await SharedPreferences.getInstance(); + final String deviceId = prefs.getString("current_device") ?? ""; + return await StepsService().getStepsLastFiveDays(deviceId); } @override diff --git a/pubspec.lock b/pubspec.lock index 1384262..a6ad33b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -148,6 +148,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.0" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.2" file: dependency: transitive description: @@ -422,6 +429,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.0" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.5" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.3" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.5" pedantic: dependency: transitive description: @@ -429,6 +457,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.11.1" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" plugin_platform_interface: dependency: transitive description: @@ -436,6 +471,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.2" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.4" pub_semver: dependency: transitive description: @@ -457,6 +499,62 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.0" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.13" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.11" + shared_preferences_ios: + dependency: transitive + description: + name: shared_preferences_ios + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.10" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + shared_preferences_macos: + dependency: transitive + description: + name: shared_preferences_macos + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.3" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.3" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" sky_engine: dependency: transitive description: flutter @@ -560,6 +658,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.1" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "2.4.0" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0+1" yaml: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index eb22923..afb9505 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -44,6 +44,7 @@ dependencies: json_serializable: ^6.1.4 geocoding: ^2.0.2 geolocator: ^8.2.0 + shared_preferences: ^2.0.13 dev_dependencies: