Skip to content
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

made load tasks from csv files feature #91 #127

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions lib/enums/taskoptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ enum TaskOption {
launchMeditationScreen,
toggleTipVisibility,
exportToCSV,
loadFromCSV,
}
72 changes: 72 additions & 0 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
Expand Down Expand Up @@ -190,6 +191,15 @@ class _HomeScreenState extends State<HomeScreen> {
} else if (option == TaskOption.exportToCSV) {
exportToCSV(tasks);
}
else if (option == TaskOption.loadFromCSV) {
importFromCSV(tasks).then((newTasks) {
setState(() {
tasks = newTasks;
});
TaskStorage.saveTasks(tasks);
});
}

});
void _editTask(int index) async {
final newTask = await Navigator.push<Task>(
Expand Down Expand Up @@ -283,6 +293,64 @@ class _HomeScreenState extends State<HomeScreen> {
print("File saved at: $path");
}

Future<List<Task>> importFromCSV(List<Task> existingTasks) async {
try {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['csv'],
);
if (result == null) {
print("Import canceled.");
return existingTasks;
}
final file = File(result.files.single.path!);
final content = await file.readAsString();
List<List<dynamic>> rows = const CsvToListConverter().convert(content);
if (rows.isEmpty) {
Fluttertoast.showToast(msg: "CSV file is empty");
return existingTasks;
}
List<String> expectedHeader = ["Title", "Description", "Is Completed", "Has Deadline", "Deadline"];
List<String> actualHeader = rows[0].map((e) => e.toString()).toList();
if (!listEquals(expectedHeader, actualHeader)) {
Fluttertoast.showToast(msg: "Invalid CSV format. Please use the correct template");
return existingTasks;
}
List<Task> importedTasks = [];
for (int i = 1; i < rows.length; i++) {
try {
var row = rows[i];
DateTime? deadline;
if (row[4].toString().isNotEmpty) {
List<String> dateParts = row[4].toString().split('/');
deadline = DateTime(
int.parse(dateParts[2]),
int.parse(dateParts[1]),
int.parse(dateParts[0]),
);
}
Task task = Task(
title: row[0].toString(),
description: row[1].toString(),
isCompleted: row[2].toString().toLowerCase() == 'true',
hasDeadline: row[3].toString().toLowerCase() == 'true',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sanat-Jha This is no longer a field inside class Task. Please update accordingly

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Akshit517 Updated, please review.

deadline: deadline ?? DateTime.now(),
);
importedTasks.add(task);
} catch (e) {
print("Error parsing row $i: $e");
Fluttertoast.showToast(msg: "Error parsing some tasks. Some entries might be skipped.");
}
}
existingTasks.addAll(importedTasks);
Fluttertoast.showToast(msg: "Successfully imported ${importedTasks.length} tasks");
return existingTasks;
} catch (e) {
print("Import error: $e");
Fluttertoast.showToast(msg: "Error importing CSV file");
return existingTasks;
}
}
void _loadKudos() async {
Kudos loadedKudos = await KudosStorage.loadKudos();
setState(() {
Expand Down Expand Up @@ -345,6 +413,10 @@ class _HomeScreenState extends State<HomeScreen> {
value: TaskOption.launchMeditationScreen,
child: Text("Meditate"),
),
const PopupMenuItem(
value: TaskOption.loadFromCSV,
child: Text("Load Tasks from csv file."),
),
];
},
),
Expand Down