mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-22 21:45:48 +00:00
Data is now collected for the user's device; not hardcoded
This commit is contained in:
parent
3965eb2667
commit
f4d0527a3d
21
lib/service/auth_service.dart
Normal file
21
lib/service/auth_service.dart
Normal file
|
@ -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<List<String>> getUserDevices(sessionToken) async {
|
||||
final response = await http.get(
|
||||
Uri.parse(Endpoints.getUserDevices),
|
||||
headers: <String, String>{
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
'token': sessionToken,
|
||||
},
|
||||
);
|
||||
List<dynamic> list = jsonDecode(response.body)['devices'];
|
||||
List<String> res = [];
|
||||
for (final l in list) {
|
||||
res.add(l.toString());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@ class StepsService {
|
|||
return jsonDecode(response.body)['cumulative_steps_today'];
|
||||
}
|
||||
|
||||
Future<List<dynamic>> getStepsLastFiveDays(deviceId) async {
|
||||
Future<List<int>> getStepsLastFiveDays(deviceId) async {
|
||||
final response = await http.get(
|
||||
Uri.parse(Endpoints.getStepsLastFiveDays),
|
||||
headers: <String, String>{
|
||||
|
@ -22,7 +22,11 @@ class StepsService {
|
|||
'deviceid': deviceId,
|
||||
},
|
||||
);
|
||||
print(jsonDecode(response.body)['daily_steps'].runtimeType);
|
||||
return jsonDecode(response.body)['daily_steps'];
|
||||
List<dynamic> list = jsonDecode(response.body)['daily_steps'];
|
||||
List<int> steps = [];
|
||||
for (final l in list){
|
||||
steps.add(l);
|
||||
}
|
||||
return steps;
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
const String getMetricsSummary = "https://leg-barkr.nw.r.appspot.com/readings/metrics-summary";
|
||||
|
|
|
@ -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<HomeScreen> {
|
|||
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<String> userDevices = await AuthService().getUserDevices(token);
|
||||
prefs.setStringList("devices", userDevices);
|
||||
prefs.setString("current_device", userDevices[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -58,7 +66,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||
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,
|
||||
|
|
|
@ -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<MapPage> {
|
|||
|
||||
Future<void> _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(
|
||||
|
|
|
@ -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<MetricsPage> {
|
|||
*/
|
||||
|
||||
Future<MetricsResponse> 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<MetricsPage> {
|
|||
children: <Widget>[
|
||||
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),
|
||||
|
|
|
@ -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<StepsPage> {
|
||||
|
||||
Future<List<int>> onStepsRetrieved() async{
|
||||
List<dynamic> res = await StepsService().getStepsLastFiveDays("132-567-001");
|
||||
List<int> 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
|
||||
|
|
112
pubspec.lock
112
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:
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue