diff --git a/Space_Mapper/lib/models/list_view.dart b/Space_Mapper/lib/models/list_view.dart index cfa9939a..0d8f2761 100644 --- a/Space_Mapper/lib/models/list_view.dart +++ b/Space_Mapper/lib/models/list_view.dart @@ -1,32 +1,41 @@ -//import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg; - +import 'package:geocoding/geocoding.dart'; import '../app_localizations.dart'; class CustomLocationsManager { - //static List customLocations = []; - - /*static List fetchAll({required bool sortByNewest}) { - if (sortByNewest) { - return customLocations; - } else { - return new List.from(customLocations.reversed); + static Future> getLocations(int maxElements) async { + List customLocations = []; + + // Make the current list to be empty. We want to fill it according to our custom parameters + //CustomLocationsManager.removeAllCustomLocations(); + + // Get a list of locations from the flutter_background_geolocation plugin database + List recordedLocations = await bg.BackgroundGeolocation.locations; + + // n is the minimum value of either the specified maximum amount of elements (maxElements), or the current size of recordedLocations + int n; + if (maxElements <= recordedLocations.length) + n = maxElements; + else + n = recordedLocations.length; + + // Fill the custom locations list, to display beautiful tiles instead of json data + for (int i = 0; i < n; ++i) { + // Check if there's already a location with the same UUID + for (int j = customLocations.length - 1; j >= 0; --j) { + if (recordedLocations[i]['uuid'] == customLocations[j].getUUID()) + continue; //CustomLocationsManager.customLocations[j].getUUID()) continue; + } + // Match not found, we add the location + CustomLocation newLocation = await CustomLocationsManager.createCustomLocation( + recordedLocations[i] + ); + customLocations.add(newLocation); } - }*/ - - /*static CustomLocation? fetchByUUID(String uuid) { - CustomLocation? ret = customLocations - .firstWhereOrNull((element) => element.getUUID() == uuid); - return ret; - }*/ - - /*static void removeAllCustomLocations() { - print("Removing " + customLocations.length.toString() + " customLocations"); - customLocations.clear(); - print("All customLocations removed"); - }*/ + return customLocations; + } /// Makes timestamp readable by a human static String formatTimestamp(String timestamp) { @@ -42,6 +51,51 @@ class CustomLocationsManager { } return result; } + + static Future createCustomLocation( + var recordedLocation) async { + CustomLocation location = new CustomLocation(); + + //Save data from flutter_background_geolocation library + location.setUUID(recordedLocation['uuid']); + location.setTimestamp(recordedLocation['timestamp']); + location.setActivity(recordedLocation['activity']['type']); + location.setSpeed(recordedLocation['coords']['speed'], + recordedLocation['coords']['speed_accuracy']); + location.setAltitude(recordedLocation['coords']['altitude'], + recordedLocation['coords']['altitude_accuracy']); + + Placemark? placemark = await getLocationData(recordedLocation['coords']['latitude'], + recordedLocation['coords']['longitude']); + + //Add our custom data + if (placemark != null) { + String? locality = placemark.locality; + String? subAdminArea = placemark.subAdministrativeArea; + String? street = placemark.street; + if (street != null) street += ", ${placemark.name}"; + // ignore: non_constant_identifier_names + String? ISO = placemark.isoCountryCode; + + location.setLocality(locality!); + location.setSubAdministrativeArea(subAdminArea!); + location.setStreet(street!); + location.setISOCountry(ISO!); + } + return location; + } + ///Get data such as city, province, postal code, street name, country... + static Future getLocationData(double lat, double long) async { + try { + List placemarks = await placemarkFromCoordinates( + lat, + long, + ); + return placemarks[0]; + } catch (err) { + return null; + } + } } class CustomLocation { @@ -174,4 +228,4 @@ class CustomLocation { num getAltitudeAcc() { return _altitude; } -} +} \ No newline at end of file diff --git a/Space_Mapper/lib/ui/list_view.dart b/Space_Mapper/lib/ui/list_view.dart index 85b4ad97..541b5fdc 100644 --- a/Space_Mapper/lib/ui/list_view.dart +++ b/Space_Mapper/lib/ui/list_view.dart @@ -1,55 +1,6 @@ import '../app_localizations.dart'; import '../models/list_view.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' - as bg; -import 'package:geocoding/geocoding.dart'; - -///Get data such as city, province, postal code, street name, country... -Future getLocationData(double lat, double long) async { - try { - List placemarks = await placemarkFromCoordinates( - lat, - long, - ); - return placemarks[0]; - } catch (err) { - return null; - } -} - -CustomLocation createCustomLocation( - var recordedLocation, Placemark? placemark) { - CustomLocation location = new CustomLocation(); - //Add location to list - //CustomLocationsManager.customLocations.add(location); - - //Save data from flutter_background_geolocation library - location.setUUID(recordedLocation['uuid']); - location.setTimestamp(recordedLocation['timestamp']); - location.setActivity(recordedLocation['activity']['type']); - location.setSpeed(recordedLocation['coords']['speed'], - recordedLocation['coords']['speed_accuracy']); - location.setAltitude(recordedLocation['coords']['altitude'], - recordedLocation['coords']['altitude_accuracy']); - - //Add our custom data - if (placemark != null) { - String? locality = placemark.locality; - String? subAdminArea = placemark.subAdministrativeArea; - String? street = placemark.street; - if (street != null) street += ", ${placemark.name}"; - // ignore: non_constant_identifier_names - String? ISO = placemark.isoCountryCode; - - location.setLocality(locality!); - location.setSubAdministrativeArea(subAdminArea!); - location.setStreet(street!); - location.setISOCountry(ISO!); - } - - return location; -} class STOListView extends StatefulWidget { const STOListView({Key? key}) : super(key: key); @@ -58,46 +9,11 @@ class STOListView extends StatefulWidget { _STOListViewState createState() => _STOListViewState(); } -class _STOListViewState extends State { - final int maxElements = 25; // Maximum amount of elements displayed on screen - - Future> recalculateLocations() async { - List customLocations = []; - - // Make the current list to be empty. We want to fill it according to our custom parameters - //CustomLocationsManager.removeAllCustomLocations(); - - // Get a list of locations from the flutter_background_geolocation plugin database - List recordedLocations = await bg.BackgroundGeolocation.locations; - - // n is the minimum value of either the specified maximum amount of elements (maxElements), or the current size of recordedLocations - int n; - if (maxElements <= recordedLocations.length) - n = maxElements; - else - n = recordedLocations.length; - - // Fill the custom locations list, to display beautiful tiles instead of json data - for (int i = 0; i < n; ++i) { - // Check if there's already a location with the same UUID - for (int j = customLocations.length - 1; j >= 0; --j) { - if (recordedLocations[i]['uuid'] == customLocations[j].getUUID()) - continue; //CustomLocationsManager.customLocations[j].getUUID()) continue; - } - // Match not found, we add the location - CustomLocation newLocation = createCustomLocation( - recordedLocations[i], - await getLocationData(recordedLocations[i]['coords']['latitude'], - recordedLocations[i]['coords']['longitude'])); - customLocations.add(newLocation); - } - return customLocations; - } +class _STOListViewState extends State { @override void initState() { super.initState(); - //recalculateLocations(); } @override @@ -107,7 +23,7 @@ class _STOListViewState extends State { title: Text( AppLocalizations.of(context)!.translate("locations_history"))), body: FutureBuilder>( - future: recalculateLocations(), + future: CustomLocationsManager.getLocations(25), builder: (context, snapshot) { if (!snapshot.hasData) { return Center( @@ -142,16 +58,6 @@ class _STOListViewState extends State { ScaffoldMessenger.of(context).showSnackBar( new SnackBar(content: new Text("Location removed"))) }, - /*confirmDismiss: (DismissDirection direction) async { - //if(direction == DismissDirection.) - // if (direction == left) - await thisLocation.deleteThisLocation(); - setState(() { - //recalculateLocations(); - //thisLocation = snapshot.data![index]; - }); - return true; - },*/ ); }); }, diff --git a/Space_Mapper/pubspec.yaml b/Space_Mapper/pubspec.yaml index 14adf45c..b21a09a0 100644 --- a/Space_Mapper/pubspec.yaml +++ b/Space_Mapper/pubspec.yaml @@ -11,7 +11,7 @@ description: Flutter version of Space Mapper with 2020 upgrades # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 3.0.2+18 +version: 3.1.0+19 environment: sdk: ">=2.12.0 <3.0.0"