/* * Mirai. Copyright (C) 2024 Vyn * This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only) * The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt */ #include "TasksFile.h" #include "TaskItem.h" #include #include #include #include #include namespace mirai { TasksFile::TasksFile(TasksFileConstructor params) : name(params.name), path(params.path) { for (const auto &task : params.tasks) { auto taskPtr = std::make_unique(task); tasks.push_back(std::move(taskPtr)); } sortByDate(); } void TasksFile::onTaskChanged(TaskItem &taskItem) { std::cout << "THIS PTR: " << this << std::endl; setDirty(true); } void TasksFile::setDirty(bool shouldBeDirty) { isDirty_ = shouldBeDirty; } bool TasksFile::isDirty() const { return isDirty_; } const std::string &TasksFile::getName() const { return name; } const std::string &TasksFile::getPath() const { return path; } TasksPtrs &TasksFile::getTasks() { return tasks; } void TasksFile::addTask(TaskItemData taskItem) { tasks.push_back(std::make_unique(this, taskItem)); setDirty(true); } void TasksFile::addTask(std::string text, std::string date) { auto newTask = std::make_unique( TaskItem{this, {.text = text, .state = TODO, .date = date == "" ? "No date" : date}} ); tasks.push_back(std::move(newTask)); } void TasksFile::removeTask(const TaskItem *taskToRemove) { auto findFunction = [&](const std::unique_ptr &taskInFilter) { return taskInFilter.get() == taskToRemove; }; auto taskToDelete = std::remove_if(tasks.begin(), tasks.end(), findFunction); if (taskToDelete == tasks.end()) { return; } tasks.erase(taskToDelete); setDirty(true); } void TasksFile::sortByDate() { std::sort( tasks.begin(), tasks.end(), [](const std::unique_ptr &t1, const std::unique_ptr &t2) { if (t1->hasDate() && !t2->hasDate()) { return true; } else if (!t1->hasDate() && t2->hasDate()) { return false; } return t1->getDate() < t2->getDate(); } ); } } // namespace mirai