Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 77 additions & 23 deletions Space_Mapper/lib/models/list_view.dart
Original file line number Diff line number Diff line change
@@ -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<CustomLocation> customLocations = [];

/*static List<CustomLocation> fetchAll({required bool sortByNewest}) {
if (sortByNewest) {
return customLocations;
} else {
return new List.from(customLocations.reversed);
static Future<List<CustomLocation>> getLocations(int maxElements) async {
List<CustomLocation> 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) {
Expand All @@ -42,6 +51,51 @@ class CustomLocationsManager {
}
return result;
}

static Future<CustomLocation> 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<Placemark?> getLocationData(double lat, double long) async {
try {
List<Placemark> placemarks = await placemarkFromCoordinates(
lat,
long,
);
return placemarks[0];
} catch (err) {
return null;
}
}
}

class CustomLocation {
Expand Down Expand Up @@ -174,4 +228,4 @@ class CustomLocation {
num getAltitudeAcc() {
return _altitude;
}
}
}
98 changes: 2 additions & 96 deletions Space_Mapper/lib/ui/list_view.dart
Original file line number Diff line number Diff line change
@@ -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<Placemark?> getLocationData(double lat, double long) async {
try {
List<Placemark> 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);
Expand All @@ -58,46 +9,11 @@ class STOListView extends StatefulWidget {
_STOListViewState createState() => _STOListViewState();
}

class _STOListViewState extends State<STOListView> {
final int maxElements = 25; // Maximum amount of elements displayed on screen

Future<List<CustomLocation>> recalculateLocations() async {
List<CustomLocation> 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<STOListView> {

@override
void initState() {
super.initState();
//recalculateLocations();
}

@override
Expand All @@ -107,7 +23,7 @@ class _STOListViewState extends State<STOListView> {
title: Text(
AppLocalizations.of(context)!.translate("locations_history"))),
body: FutureBuilder<List<CustomLocation>>(
future: recalculateLocations(),
future: CustomLocationsManager.getLocations(25),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
Expand Down Expand Up @@ -142,16 +58,6 @@ class _STOListViewState extends State<STOListView> {
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;
},*/
);
});
},
Expand Down
2 changes: 1 addition & 1 deletion Space_Mapper/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down