diff --git a/lib/main.dart b/lib/main.dart index 9452bdc..75d6081 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,6 +3,7 @@ import 'package:provider/provider.dart'; import 'package:smartfit_app_mobile/modele/user.dart'; import 'package:smartfit_app_mobile/common/colo_extension.dart'; import 'package:smartfit_app_mobile/view/login/signup_view.dart'; +import 'package:smartfit_app_mobile/view/test/page_test.dart'; void main() { runApp(ChangeNotifierProvider( @@ -36,6 +37,7 @@ class MyApp extends StatelessWidget { primaryColor: TColor.primaryColor1, fontFamily: "Poppins"), home: const SignUpView(), + //home: const TestPage(), //home: const ProfileView(), ); } diff --git a/lib/modele/manager_file.dart b/lib/modele/manager_file.dart index a4178d2..a48ecfd 100644 --- a/lib/modele/manager_file.dart +++ b/lib/modele/manager_file.dart @@ -1,7 +1,10 @@ import 'dart:convert'; +import 'dart:ffi'; import 'dart:typed_data'; import 'package:csv/csv.dart'; import 'package:fit_tool/fit_tool.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:tuple/tuple.dart'; class ManagerFile { // -- Field @@ -41,23 +44,42 @@ class ManagerFile { ]; } - List> convertBytesFitFileIntoCSVList(Uint8List bytes) { + Tuple3>> convertBytesFitFileIntoCSVList( + Uint8List bytes) { FitFile fitFile = FitFile.fromBytes(bytes); // ----------- Lire le fit et extarire les données qu'on choisi ----------- // List>> dataResult = List.empty(growable: true); + String startTime = "2000-01-01"; + String category = "Generic"; for (Record element in fitFile.records) { List listeField = element.toRow(); Map> ligneDataResult = {}; bool skip = true; + bool sesssionLigne = false; if (listeField[0] != "Data") { continue; } for (int i = 0; i < listeField.length;) { + // -- Check si c'est une ligne session --// + if (i == 0 && listeField[0] == "Data" && listeField[2] == "session") { + sesssionLigne = true; + } + // -- Si ligne session && starttime -- // + if (sesssionLigne && listeField[i] == "start_time") { + startTime = + DateTime.fromMillisecondsSinceEpoch(listeField[i + 1] as int) + .toIso8601String(); + } + // -- Si ligne session && sport -- // + if (sesssionLigne && listeField[i] == "sport") { + category = _getCategoryById(listeField[i + 1] as int); + } + if (allowedFieldWalking.contains(listeField[i])) { Map tmp = {}; tmp["Value"] = listeField[i + 1].toString(); @@ -99,7 +121,9 @@ class ManagerFile { } csvData.insert(0, enteteCSV); // ------- FIN --------------- // - return csvData; + // -- Extraire la catégorie + date début -- // + + return Tuple3(category, startTime, csvData); } // -- Read the byte of file CSV -- // @@ -107,12 +131,48 @@ class ManagerFile { return const CsvToListConverter().convert(utf8.decode(bytes)); } - /* + // -- Retourne la catégorie et la date d'une activité -- // + Tuple2 getCategoryAndDate(List> contentFile) { + String startTime = "2000-01-01"; + String category = "Generic"; + + // On regarde que les 8 derniers ligne !! + for (int i = contentFile.length - 1; i != contentFile.length - 8; i--) { + if (contentFile[i][0] == "Data" && contentFile[i][0] == "session") { + for (int colonne = 6; colonne < contentFile[i].length; colonne++) { + if (contentFile[i][colonne] == "start_time") { + // Convertir la date timestamp !!! + startTime = DateTime.fromMillisecondsSinceEpoch( + contentFile[i][colonne + 1] as int) + .toIso8601String(); + } + if (contentFile[i][colonne] == "sport") { + category = _getCategoryById(contentFile[i][colonne + 1] as int); + } + } + } + } + return Tuple2(category, startTime); + } + + String _getCategoryById(int id) { + switch (id) { + case 0: + return "generic"; + case 2: + return "cycling"; + case 11: + return "walking"; + default: + return "generic"; + } + } + // ------------- Get The path of application --- // Future get localPath async { final directory = await getApplicationDocumentsDirectory(); return directory.path; - }*/ + } /* // ----- Read csv File ------- // diff --git a/lib/modele/manager_selected_activity.dart b/lib/modele/manager_selected_activity.dart index 280570e..0919bb3 100644 --- a/lib/modele/manager_selected_activity.dart +++ b/lib/modele/manager_selected_activity.dart @@ -54,7 +54,6 @@ class ManagerSelectedActivity { List getXWithTime(String field) { List result = List.empty(growable: true); - int firstTimestamp = 0; for (int c = 0; c < activitySelected.length; c++) { diff --git a/lib/modele/utile/list_activity/list_activity_utile.dart b/lib/modele/utile/list_activity/list_activity_utile.dart index 025b580..aad1884 100644 --- a/lib/modele/utile/list_activity/list_activity_utile.dart +++ b/lib/modele/utile/list_activity/list_activity_utile.dart @@ -63,16 +63,17 @@ class ListActivityUtile { Future> addFile( Uint8List bytes, String filename, String token) async { // -- Transormer le fit en CSV - List> csv = _managerFile.convertBytesFitFileIntoCSVList(bytes); - String csvString = const ListToCsvConverter().convert(csv); + Tuple3>> categorieStarttimeCsv = + _managerFile.convertBytesFitFileIntoCSVList(bytes); + + String csvString = + const ListToCsvConverter().convert(categorieStarttimeCsv.item3); Uint8List byteCSV = Uint8List.fromList(utf8.encode(csvString)); // --- Save Local // --- Api - String categoryActivity = filename.split("_").first.toLowerCase(); - String dateActivity = filename.split("_")[1].split("T").first; - Tuple2 result = await _strategy.uploadFileByte( - token, byteCSV, filename, categoryActivity, dateActivity); + Tuple2 result = await _strategy.uploadFileByte(token, byteCSV, + filename, categorieStarttimeCsv.item1, categorieStarttimeCsv.item2); if (result.item1 == false) { return Tuple2(false, result.item2); } diff --git a/lib/view/test/page_test.dart b/lib/view/test/page_test.dart index 0ccb04c..c5b6ec6 100644 --- a/lib/view/test/page_test.dart +++ b/lib/view/test/page_test.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:crypto/crypto.dart'; +import 'package:csv/csv.dart'; import 'package:fit_tool/fit_tool.dart'; import 'package:flutter/foundation.dart' show kIsWeb; import 'package:flutter/material.dart'; @@ -8,6 +9,7 @@ import 'package:file_picker/file_picker.dart'; import 'package:provider/provider.dart'; import 'dart:io'; import 'package:smartfit_app_mobile/modele/api/i_data_strategy.dart'; +import 'package:smartfit_app_mobile/modele/manager_file.dart'; import 'package:smartfit_app_mobile/modele/user.dart'; import 'package:smartfit_app_mobile/modele/api/request_api.dart'; import 'package:tuple/tuple.dart'; @@ -82,6 +84,8 @@ class _TestPage extends State { //late File x = File(file.path); Future readFile() async { + ManagerFile _managerFile = ManagerFile(); + PlatformFile t = result!.files.single; String? y = t.path; if (t.path == null) { @@ -91,15 +95,13 @@ class _TestPage extends State { final content = await file.readAsBytes(); FitFile fitFile = FitFile.fromBytes(content); //print(fitFile.toRows()); - print("--------------"); - print("--------------"); - print("--------------"); //print("${await _managerFile.localPath}\\test.csv"); - //final outFile = File("${await _managerFile.localPath}\\test.csv"); - //final csv = const ListToCsvConverter().convert(fitFile.toRows()); - //await outFile.writeAsString(csv); + final outFile = File("${await _managerFile.localPath}\\test.csv"); + final csv = const ListToCsvConverter().convert(fitFile.toRows()); + await outFile.writeAsString(csv); + /* // ----------- Lire le fit et extarire les données qu'on choisi ----------- // List liste = fitFile.records; List allowedField = [ @@ -180,7 +182,7 @@ class _TestPage extends State { //print(x.getDistanceWithTime(ActivityOfUser(result))); //print(x.getDistance(ActivityOfUser(result))); //print(x.getAltitudeWithTime(ActivityOfUser(result))); - //print(x.getSpeedWithTime(ActivityOfUser(result))); + //print(x.getSpeedWithTime(ActivityOfUser(result)));*/ } }