-
Notifications
You must be signed in to change notification settings - Fork 816
feat: added csv functionalities #2785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yugesh-Kumar-S
wants to merge
7
commits into
fossasia:flutter
Choose a base branch
from
Yugesh-Kumar-S:data-logging
base: flutter
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4facc32
csv functionalities
Yugesh-Kumar-S f4ba1ac
Merge branch 'flutter' of https://github.com/Yugesh-Kumar-S/pslab-and…
Yugesh-Kumar-S e62922a
fixes
Yugesh-Kumar-S c94ca79
fixes
Yugesh-Kumar-S 62875c6
fixes
Yugesh-Kumar-S 4b87b9c
improved chart
Yugesh-Kumar-S dc6bb3e
sourcery suggestion
Yugesh-Kumar-S File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
import 'package:csv/csv.dart'; | ||
import 'package:file_picker/file_picker.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:permission_handler/permission_handler.dart'; | ||
import 'package:pslab/others/logger_service.dart'; | ||
import 'package:pslab/view/about_us_screen.dart'; | ||
import 'package:share_plus/share_plus.dart'; | ||
import 'package:intl/intl.dart'; | ||
|
||
class CsvService { | ||
static const String csvDirectory = 'PSLab'; | ||
|
||
Future<Directory> getInstrumentDirectory(String instrumentName) async { | ||
if (Platform.isAndroid) { | ||
await requestStoragePermission(); | ||
final directory = | ||
Directory('/storage/emulated/0/Android/media/PSLab/$instrumentName'); | ||
if (!await directory.exists()) { | ||
await directory.create(recursive: true); | ||
} | ||
return directory; | ||
} else if (Platform.isIOS || | ||
Platform.isWindows || | ||
Platform.isMacOS || | ||
Platform.isLinux) { | ||
final dir = await getApplicationDocumentsDirectory(); | ||
final directory = Directory('${dir.path}/PSLab/$instrumentName'); | ||
if (!await directory.exists()) { | ||
await directory.create(recursive: true); | ||
} | ||
return directory; | ||
} else { | ||
throw UnsupportedError('Unsupported platform'); | ||
} | ||
} | ||
|
||
Future<void> requestStoragePermission() async { | ||
if (Platform.isAndroid) { | ||
final status = await Permission.manageExternalStorage.request(); | ||
if (!status.isGranted) { | ||
await openAppSettings(); | ||
} | ||
} | ||
} | ||
|
||
Future<File?> saveCsvFile( | ||
String instrumentName, String fileName, List<List<dynamic>> data) async { | ||
try { | ||
if (data.length <= 1) { | ||
logger.w('No data recorded to save for $fileName'); | ||
return null; | ||
} | ||
final directory = await getInstrumentDirectory(instrumentName); | ||
|
||
String finalFileName; | ||
if (fileName.isEmpty) { | ||
finalFileName = | ||
'${DateFormat('yyyy-MM-dd_HH-mm-ss').format(DateTime.now())}.csv'; | ||
} else { | ||
finalFileName = fileName.endsWith('.csv') ? fileName : '$fileName.csv'; | ||
} | ||
|
||
final file = File('${directory.path}/$finalFileName'); | ||
|
||
String csvData = const ListToCsvConverter().convert(data); | ||
await file.writeAsString(csvData); | ||
logger.i('CSV file saved at: ${file.path}'); | ||
return file; | ||
} catch (e) { | ||
logger.e('Error saving CSV file: $e'); | ||
return null; | ||
} | ||
} | ||
|
||
Future<List<FileSystemEntity>> getSavedFiles(String instrumentName) async { | ||
try { | ||
final directory = await getInstrumentDirectory(instrumentName); | ||
final files = directory | ||
.listSync() | ||
.where((item) => item.path.endsWith('.csv')) | ||
.toList(); | ||
files.sort( | ||
(a, b) => b.statSync().modified.compareTo(a.statSync().modified)); | ||
return files; | ||
} catch (e) { | ||
logger.e('Error getting saved files: $e'); | ||
return []; | ||
} | ||
} | ||
|
||
Future<void> deleteFile(String filePath) async { | ||
try { | ||
final file = File(filePath); | ||
if (await file.exists()) { | ||
await file.delete(); | ||
logger.i('File deleted: $filePath'); | ||
} | ||
} catch (e) { | ||
logger.e('Error deleting file: $e'); | ||
} | ||
} | ||
|
||
Future<void> deleteAllFiles(String instrumentName) async { | ||
try { | ||
final directory = await getInstrumentDirectory(instrumentName); | ||
if (await directory.exists()) { | ||
await directory.delete(recursive: true); | ||
logger.i('All files for $instrumentName deleted.'); | ||
} | ||
} catch (e) { | ||
logger.e('Error deleting all files for $instrumentName: $e'); | ||
} | ||
} | ||
|
||
Future<void> shareFile(String filePath) async { | ||
try { | ||
final xFile = XFile(filePath); | ||
await SharePlus.instance.share( | ||
ShareParams(files: [xFile], text: appLocalizations.sharingMessage)); | ||
} catch (e) { | ||
logger.e('Error sharing file: $e'); | ||
} | ||
} | ||
|
||
Future<List<List<dynamic>>?> pickAndReadCsvFile() async { | ||
try { | ||
final result = await FilePicker.platform.pickFiles( | ||
type: FileType.custom, | ||
allowedExtensions: ['csv'], | ||
); | ||
|
||
if (result != null && result.files.single.path != null) { | ||
final file = File(result.files.single.path!); | ||
return await readCsvFromFile(file); | ||
} | ||
} catch (e) { | ||
logger.e('Error picking or reading CSV file: $e'); | ||
} | ||
return null; | ||
} | ||
|
||
Future<List<List<dynamic>>> readCsvFromFile(File file) async { | ||
try { | ||
final input = file.openRead(); | ||
final fields = await input | ||
.transform(utf8.decoder) | ||
.transform(const CsvToListConverter(shouldParseNumbers: true)) | ||
.toList(); | ||
return fields; | ||
} catch (e) { | ||
logger.e('Error reading CSV from file: $e'); | ||
return []; | ||
} | ||
} | ||
|
||
void writeMetaData(String instrumentName, List<List<dynamic>> data) { | ||
if (data.isNotEmpty && data[0].isNotEmpty && data[0][0] == instrumentName) { | ||
return; | ||
} | ||
|
||
final now = DateTime.now(); | ||
final sdf = DateFormat('yyyy-MM-dd HH:mm:ss'); | ||
final metaDataTime = sdf.format(now); | ||
final metaData = [ | ||
instrumentName, | ||
metaDataTime.split(' ')[0], | ||
metaDataTime.split(' ')[1] | ||
]; | ||
data.insert(0, metaData); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.