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
47 changes: 30 additions & 17 deletions Space_Mapper/lib/models/list_view.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:collection/collection.dart';
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart'
as bg;

class CustomLocationsManager {
static List<CustomLocation> customLocations = [];
Expand All @@ -16,24 +18,9 @@ class CustomLocationsManager {
.firstWhereOrNull((element) => element.getUUID() == uuid);
return ret;
}
}

class CustomLocation {
late final String _uuid;
late String _locality = "";
late String _subAdministrativeArea = "";
late String _street = "";
// ignore: non_constant_identifier_names
late String _ISOCountry = ""; // 2 letter code
late String _timestamp = ""; // ex: 2021-10-25T21:25:08.210Z
late String _activity = "";
late num _speed = -1; //in meters / second
late num _speedAccuracy = -1; //in meters / second
late num _altitude = -1; //in meters
late num _altitudeAccuracy = -1; // in meters

/// Makes timestamp readable by a human
String formatTimestamp(String timestamp) {
static String formatTimestamp(String timestamp) {
//2021-10-25T21:25:08.210Z <- This is the original format
//2021-10-25 | 21:25:08 <- This is the result
String result = "";
Expand All @@ -46,6 +33,22 @@ class CustomLocation {
}
return result;
}
}

class CustomLocation {
late final String _uuid;
late String _locality = "";
late String _subAdministrativeArea = "";
late String _street = "";
// ignore: non_constant_identifier_names
late String _ISOCountry = ""; // 2 letter code
late String _timestamp = "";
late String _activity = "";
late num _speed = -1; //in meters / second
late num _speedAccuracy = -1; //in meters / second
late num _altitude = -1; //in meters
late num _altitudeAccuracy = -1; // in meters
late bool _toDelete = false;

/// Checks if data is valid and then displays 3 lines with: Activity, Speed and Altitude
String displayCustomText(num maxSpeedAccuracy, num maxAltitudeAccuracy) {
Expand All @@ -64,6 +67,16 @@ class CustomLocation {
return ret;
}

Future<void> deleteThisLocation() async {
if (_toDelete == false) {
/// 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());
}
}

// Variable setters
void setUUID(String uuid) {
_uuid = uuid;
Expand All @@ -86,7 +99,7 @@ class CustomLocation {
}

void setTimestamp(String timestamp) {
_timestamp = formatTimestamp(timestamp);
_timestamp = CustomLocationsManager.formatTimestamp(timestamp);
}

void setActivity(String activity) {
Expand Down
6 changes: 2 additions & 4 deletions Space_Mapper/lib/ui/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,11 @@ class HomeViewState extends State<HomeView>
},
),
actions: <Widget>[
/*
IconButton(
icon: Icon(Icons.gps_fixed),
color: Colors.yellow,
onPressed:
_onClickGetCurrentPosition,
),*/
onPressed: _onClickGetCurrentPosition,
),
Switch(
value: _enabled,
onChanged: _onClickEnable,
Expand Down
163 changes: 59 additions & 104 deletions Space_Mapper/lib/ui/list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,114 +105,69 @@ class _STOListViewState extends State<STOListView> {
itemCount: customLocations.length,
itemBuilder: (context, index) {
CustomLocation thisLocation = customLocations[index];
return _tile(
thisLocation.getLocality() +
", " +
thisLocation.getSubAdministrativeArea() +
", " +
thisLocation.getISOCountryCode(),
thisLocation.getTimestamp() + "\n" + thisLocation.getStreet(),
thisLocation.displayCustomText(10.0, 10.0),
Icons.gps_fixed);
},
));
}

ListTile _tile(String title, String subtitle, String text, IconData icon) =>
ListTile(
title: Text(title,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20,
)),
subtitle: new RichText(
text: new TextSpan(
// Note: Styles for TextSpans must be explicitly defined.
// Child text spans will inherit styles from parent
style: new TextStyle(
fontSize: 14.0,
color: Colors.black,
),
children: <TextSpan>[
new TextSpan(
text: subtitle,
style: new TextStyle(fontWeight: FontWeight.bold)),
new TextSpan(text: text),
],
),
),
leading: Icon(
icon,
color: Colors.blue[500],
),
);
}

/*class STOListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Locations History")),
body: FutureBuilder<List>(
future: buildLocationsList(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List? data = snapshot.data;
return _jobsListView(data);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return Center(
child: CircularProgressIndicator(),
return Dismissible(
child: _tile(thisLocation),
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];
});
return true;
},
);
},
));
}

ListView _jobsListView(data) {
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
CustomLocation thisLocation = data[index];
return _tile(
thisLocation.getUUID().toString() +
thisLocation.getLocality() +
", " +
thisLocation.getSubAdministrativeArea() +
", " +
thisLocation.getISOCountryCode(),
thisLocation.getTimestamp(),
thisLocation.displayCustomText(10.0, 10.0),
Icons.gps_fixed);
});
}

ListTile _tile(String title, String subtitle, String text, IconData icon) =>
ListTile(
title: Text(title,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20,
)),
subtitle: new RichText(
text: new TextSpan(
// Note: Styles for TextSpans must be explicitly defined.
// Child text spans will inherit styles from parent
style: new TextStyle(
fontSize: 14.0,
color: Colors.black,
),
children: <TextSpan>[
new TextSpan(
text: subtitle,
style: new TextStyle(fontWeight: FontWeight.bold)),
new TextSpan(text: text),
],
ListTile _tile(CustomLocation thisLocation) {
String title = thisLocation.getLocality() +
", " +
thisLocation.getSubAdministrativeArea() +
", " +
thisLocation.getISOCountryCode();
String subtitle =
thisLocation.getTimestamp() + "\n" + thisLocation.getStreet();
String text = thisLocation.displayCustomText(10.0, 10.0);
return ListTile(
title: Text(title,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 20,
)),
subtitle: new RichText(
text: new TextSpan(
// Note: Styles for TextSpans must be explicitly defined.
// Child text spans will inherit styles from parent
style: new TextStyle(
fontSize: 14.0,
color: Colors.black,
),
children: <TextSpan>[
new TextSpan(
text: subtitle,
style: new TextStyle(fontWeight: FontWeight.bold)),
new TextSpan(text: text),
],
),
leading: Icon(
icon,
color: Colors.blue[500],
),
);
}*/
),
leading: Icon(
Icons.gps_fixed,
color: Colors.blue[500],
),
);
}
}
37 changes: 35 additions & 2 deletions Space_Mapper/lib/ui/side_drawer.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:asm/models/list_view.dart';
import 'package:asm/ui/list_view.dart';
import 'package:asm/ui/report_an_issue.dart';
import 'package:asm/ui/web_view.dart';
Expand All @@ -7,12 +9,43 @@ import 'package:share/share.dart';
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart'
as bg;

class ShareLocation {
late final String _timestamp;
final double _lat;
final double _long;

ShareLocation(timestamp, this._lat, this._long) {
_timestamp = CustomLocationsManager.formatTimestamp(timestamp);
}

Map<String, dynamic> toJson() => {
'timestamp': _timestamp,
'coords': {
'latitude': _lat,
'longitude': _long,
}
};
}

class SpaceMapperSideDrawer extends StatelessWidget {
_shareLocations() async {
var now = new DateTime.now();
List allLocations = await bg.BackgroundGeolocation.locations;
Share.share(allLocations.toString(),
subject: "space_mapper_trajectory_" + now.toIso8601String() + ".json");
List<ShareLocation> customLocation = [];

// We get only timestamp and coordinates into our custom class
for (var thisLocation in allLocations) {
ShareLocation _loc = new ShareLocation(
bg.Location(thisLocation).timestamp,
bg.Location(thisLocation).coords.latitude,
bg.Location(thisLocation).coords.longitude);
customLocation.add(_loc);
}

String prettyString = JsonEncoder.withIndent(' ').convert(customLocation);
String subject =
"space_mapper_trajectory_" + now.toIso8601String() + ".json";
Share.share(prettyString, subject: subject);
}

_launchProjectURL() async {
Expand Down
3 changes: 2 additions & 1 deletion Space_Mapper/test/unit/list_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ void main() {
String timestamp = "2021-10-25T21:25:08.210Z";
CustomLocation dL = new CustomLocation();
dL.setTimestamp(timestamp);
String ret = dL.formatTimestamp(dL.getTimestamp());
String ret =
CustomLocationsManager.formatTimestamp(dL.getTimestamp());
expect(ret, "2021-10-25 | 21:25:08");
});
});
Expand Down