mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-03 18:23:19 +00:00
Support multiple files
This commit is contained in:
parent
f8f49233dc
commit
689eea07a7
22 changed files with 528 additions and 131 deletions
|
@ -5,8 +5,15 @@
|
|||
*/
|
||||
|
||||
#include "Backend.h"
|
||||
#include "TaskItem.h"
|
||||
#include "core/TaskItem.h"
|
||||
#include "core/TodoMd.h"
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <qjsonarray.h>
|
||||
#include <qjsonvalue.h>
|
||||
#include <qlogging.h>
|
||||
#include <qvariant.h>
|
||||
|
||||
Backend::Backend()
|
||||
{
|
||||
|
@ -33,8 +40,9 @@ Backend::Backend()
|
|||
qWarning() << "config.json should contains a 'files' string array";
|
||||
exit(1);
|
||||
}
|
||||
QString filePath = jsonFilesPath.toArray()[0].toString();
|
||||
mirai.loadFile(filePath.toStdString());
|
||||
for (const QJsonValueRef &filePath : jsonFilesPath.toArray()) {
|
||||
mirai.loadFile(filePath.toString().toStdString());
|
||||
}
|
||||
view = mirai.getTasks();
|
||||
rebuildQMLTasksList();
|
||||
}
|
||||
|
@ -47,10 +55,18 @@ void Backend::addTodo(QString newTodo, QString date)
|
|||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::addTodoFromRawFormat(QString text, QString date)
|
||||
void Backend::addTodoFromRawFormat(QString filePath, QString text, QString date)
|
||||
{
|
||||
mirai.addTask(mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString()));
|
||||
auto file = mirai.getFileByPath(filePath.toStdString());
|
||||
if (!file.has_value()) {
|
||||
qWarning() << "File path '" << filePath << "' doesn't exist" << Qt::endl;
|
||||
return;
|
||||
}
|
||||
file.value().get().addTask(
|
||||
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString())
|
||||
);
|
||||
mirai.save();
|
||||
view.lock()->update();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
@ -69,6 +85,20 @@ void Backend::removeTagFilter(QString tag)
|
|||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::addFileFilter(QString fileName)
|
||||
{
|
||||
view.lock()->addFileFilter(fileName.toStdString());
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::removeFileFilter(QString fileName)
|
||||
{
|
||||
view.lock()->removeFileFilter(fileName.toStdString());
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::removeFilters()
|
||||
{
|
||||
view.lock()->removeFilters();
|
||||
|
@ -79,6 +109,7 @@ void Backend::removeFilters()
|
|||
void Backend::updateTodoFromRawFormat(int todoIndex, QString text, QString date)
|
||||
{
|
||||
QMLTaskItem &taskItem = QMLTasks[todoIndex];
|
||||
taskItem.taskItem->setText(taskItem.taskItem->getText());
|
||||
*(taskItem.taskItem) =
|
||||
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString());
|
||||
mirai.save();
|
||||
|
@ -89,7 +120,6 @@ void Backend::updateTodoFromRawFormat(int todoIndex, QString text, QString date)
|
|||
void Backend::updateTodo(int todoIndex, QString state, QString text, QString date)
|
||||
{
|
||||
QMLTaskItem &taskItem = QMLTasks[todoIndex];
|
||||
taskItem.taskItem->state = state == "TODO" ? mirai::TODO : mirai::DONE;
|
||||
if (state == "DONE") {
|
||||
taskItem.taskItem->markAsDone();
|
||||
} else if (state == "TODO") {
|
||||
|
@ -120,6 +150,12 @@ void Backend::hideCompletedTasks(bool shouldHide)
|
|||
|
||||
void Backend::rebuildQMLTasksList()
|
||||
{
|
||||
// TODO MOVE TO ANOTHER FILE
|
||||
QMLTasksFiles.clear();
|
||||
for (auto &file : mirai.getFiles()) {
|
||||
QMLTasksFiles.push_back({.tasksFile = file.get()});
|
||||
}
|
||||
// ----
|
||||
QMLTasks.clear();
|
||||
std::string lastDate = "";
|
||||
std::time_t t = std::time(nullptr);
|
||||
|
@ -133,8 +169,8 @@ void Backend::rebuildQMLTasksList()
|
|||
continue;
|
||||
}
|
||||
bool shouldShowDate = false;
|
||||
if (lastDate != task.date) {
|
||||
lastDate = task.date;
|
||||
if (lastDate != task.getDate()) {
|
||||
lastDate = task.getDate();
|
||||
shouldShowDate = true;
|
||||
}
|
||||
QList<QString> qStringTags;
|
||||
|
@ -142,7 +178,8 @@ void Backend::rebuildQMLTasksList()
|
|||
qStringTags.push_back(QString::fromStdString(tag));
|
||||
}
|
||||
QMLTasks.push_back(
|
||||
{.taskItem = &task, .shouldShowDate = shouldShowDate, .tags = qStringTags});
|
||||
{.taskItem = &task, .shouldShowDate = shouldShowDate, .tags = qStringTags}
|
||||
);
|
||||
}
|
||||
|
||||
QMLTags.clear();
|
||||
|
@ -154,6 +191,11 @@ void Backend::rebuildQMLTasksList()
|
|||
for (auto &activeTagFilter : view.lock()->getActiveTagsFilter()) {
|
||||
QMLActiveTagsFilter.push_back(QString::fromStdString(activeTagFilter));
|
||||
}
|
||||
|
||||
QMLActiveFilesFilter.clear();
|
||||
for (auto &activeFileFilter : view.lock()->getActiveFilesFilter()) {
|
||||
QMLActiveFilesFilter.push_back(QString::fromStdString(activeFileFilter));
|
||||
}
|
||||
}
|
||||
|
||||
QVariant Backend::getTasks()
|
||||
|
@ -171,7 +213,17 @@ QVariant Backend::getActiveTagsFilter()
|
|||
return QVariant::fromValue(QMLActiveTagsFilter);
|
||||
}
|
||||
|
||||
QVariant Backend::getActiveFilesFilter()
|
||||
{
|
||||
return QVariant::fromValue(QMLActiveFilesFilter);
|
||||
}
|
||||
|
||||
bool Backend::shouldHideCompletedTasks()
|
||||
{
|
||||
return shouldHideCompletedTasks_;
|
||||
}
|
||||
|
||||
QVariant Backend::getFiles()
|
||||
{
|
||||
return QVariant::fromValue(QMLTasksFiles);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue