43 lines
1.2 KiB
Dart
43 lines
1.2 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:csv/csv.dart';
|
|
import 'package:flutter_email_sender/flutter_email_sender.dart';
|
|
import 'package:eagletracker/class/geoPosition.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
Future<File> generateCsv(List<GeoPosition> positions) async {
|
|
List<List<dynamic>> csvData = [
|
|
['latitude', 'longitude', 'altitude', 'spped'] // Header
|
|
];
|
|
|
|
for (var position in positions) {
|
|
csvData.add([position.latitude, position.longitude, position.altitude]);
|
|
}
|
|
|
|
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
|
|
String appDocumentsPath = appDocumentsDirectory.path;
|
|
|
|
String csvFilePath = '$appDocumentsPath/positions.csv';
|
|
File csvFile = File(csvFilePath);
|
|
|
|
String csvContent = const ListToCsvConverter().convert(csvData);
|
|
await csvFile.writeAsString(csvContent);
|
|
|
|
return csvFile;
|
|
}
|
|
|
|
void sendEmailWithAttachment(File csvFile) async {
|
|
final Email email = Email(
|
|
subject: 'Eagle Tr@cker - Positions',
|
|
body: 'Veuillez trouver ci-joint le fichier CSV contenant les positions.',
|
|
recipients: [],
|
|
attachmentPaths: [csvFile.path],
|
|
);
|
|
|
|
try {
|
|
await FlutterEmailSender.send(email);
|
|
} catch (error) {
|
|
throw 'Failed to send email: $error';
|
|
}
|
|
}
|