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
10 changes: 8 additions & 2 deletions Space_Mapper/lib/models/list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class CustomLocationsManager {
return ret;
}

static void removeAllCustomLocations() {
print("Removing " + customLocations.length.toString() + " customLocations");
customLocations.clear();
print("All customLocations removed");
}

/// Makes timestamp readable by a human
static String formatTimestamp(String timestamp) {
//2021-10-25T21:25:08.210Z <- This is the original format
Expand Down Expand Up @@ -80,8 +86,8 @@ class CustomLocation {
/// We need this checker to ensure that the user doesn't send the delete request twice, causing an exception
_toDelete = true;
await bg.BackgroundGeolocation.destroyLocation(this.getUUID());
CustomLocationsManager.customLocations
.removeWhere((element) => element.getUUID() == this.getUUID());
//CustomLocationsManager.customLocations
// .removeWhere((element) => element.getUUID() == this.getUUID());
}
}

Expand Down
109 changes: 50 additions & 59 deletions Space_Mapper/lib/ui/list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ Future<Placemark?> getLocationData(double lat, double long) async {
}
}

void createCustomLocation(var recordedLocation, Placemark? placemark) {
CustomLocation createCustomLocation(
var recordedLocation, Placemark? placemark) {
CustomLocation location = new CustomLocation();
//Add location to list
CustomLocationsManager.customLocations.add(location);
//CustomLocationsManager.customLocations.add(location);

//Save data from flutter_background_geolocation library
location.setUUID(recordedLocation['uuid']);
Expand All @@ -46,6 +47,8 @@ void createCustomLocation(var recordedLocation, Placemark? placemark) {
location.setStreet(street!);
location.setISOCountry(ISO!);
}

return location;
}

class STOListView extends StatefulWidget {
Expand All @@ -56,46 +59,45 @@ class STOListView extends StatefulWidget {
}

class _STOListViewState extends State<STOListView> {
late List<CustomLocation> customLocations =
CustomLocationsManager.fetchAll(sortByNewest: true);
final int maxElements = 25; // Maximum amount of elements displayed on screen

Future<List<CustomLocation>> recalculateLocations() async {
List<CustomLocation> customLocations = [];

Future<void> recalculateLocations() async {
// 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;

/// We check if there are new location entries that we haven't saved in our list
if (recordedLocations.length != customLocations.length) {
for (int i = recordedLocations.length - 1; i >= 0; --i) {
for (int j = customLocations.length - 1; j >= 0; --j) {
if (recordedLocations[i]['uuid'] ==
CustomLocationsManager.customLocations[j].getUUID()) continue;
}
//Match not found, we add the location
createCustomLocation(
recordedLocations[i],
await getLocationData(recordedLocations[i]['coords']['latitude'],
recordedLocations[i]['coords']['longitude']));

//We update the state to display the new locations
if (this
.mounted) // We check if this screen is active. If we do 'setState' while it's not active, it'll crash (throw exception)
{
setState(() {
customLocations =
CustomLocationsManager.fetchAll(sortByNewest: true);
print("Loading positions: " +
customLocations.length.toString() +
" out of " +
recordedLocations.length.toString());
});
}
// 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;
}

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

@override
Expand All @@ -104,34 +106,23 @@ class _STOListViewState extends State<STOListView> {
appBar: AppBar(
title: Text(
AppLocalizations.of(context)!.translate("locations_history"))),
body: ListView.builder(
itemCount: customLocations.length,
itemBuilder: (context, index) {
CustomLocation thisLocation = customLocations[index];
return Dismissible(
child: _tile(thisLocation, context),
background: Container(
child: Container(
margin: EdgeInsets.only(right: 10.0),
alignment: Alignment.centerRight,
child: new LayoutBuilder(builder: (context, constraint) {
return new Icon(Icons.delete_forever,
size: constraint.biggest.height * 0.5);
}),
),
color: Colors.red,
),
key: ValueKey<CustomLocation>(customLocations[index]),
confirmDismiss: (DismissDirection direction) async {
await thisLocation.deleteThisLocation();
setState(() {
customLocations =
CustomLocationsManager.fetchAll(sortByNewest: true);
thisLocation = customLocations[index];
body: FutureBuilder<List<CustomLocation>>(
future: recalculateLocations(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.hasError) {
return Container();
}
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
CustomLocation thisLocation = snapshot.data![index];
return _tile(thisLocation, context);
});
return true;
},
);
},
));
}
Expand Down