Fix tags list not up to date after adding or removing a tag

This commit is contained in:
Vyn 2024-04-15 09:37:28 +02:00
parent dc4f0795c7
commit 9ced5c422d
3 changed files with 27 additions and 10 deletions

View file

@ -22,16 +22,7 @@ void Mirai::loadFile(const std::string &path)
{
auto tasksFile = TodoMdFormat::readFile(path);
files->push_back(std::move(tasksFile));
tags.clear();
for (auto &file : *files) {
for (auto &task : file->getTasks()) {
for (auto &tag : task->getTags()) {
if (!vectorUtils::contains(tags, tag)) {
tags.push_back(tag);
}
}
}
}
reloadTags();
}
void Mirai::save()
@ -42,6 +33,7 @@ void Mirai::save()
file->setDirty(false);
}
}
reloadTags();
}
void Mirai::addTask(TaskItemData taskItem)
@ -103,4 +95,18 @@ const std::vector<std::string> &Mirai::getTags()
return tags;
}
void Mirai::reloadTags()
{
tags.clear();
for (auto &file : *files) {
for (auto &task : file->getTasks()) {
for (auto &tag : task->getTags()) {
if (!vectorUtils::contains(tags, tag)) {
tags.push_back(tag);
}
}
}
}
}
} // namespace mirai

View file

@ -36,6 +36,8 @@ class Mirai
std::weak_ptr<TasksView> getTasks();
const std::vector<std::string> &getTags();
void reloadTags();
private:
// The `TasksFile`s are shared to the views, their lifetime can outlive
// this (Mirai) object

View file

@ -45,6 +45,15 @@ void TasksView::update()
tasksToShow.push_back(task.get());
}
}
std::ranges::sort(tasksToShow, [](const TaskItem *t1, const TaskItem *t2) {
if (t1->hasDate() && !t2->hasDate()) {
return true;
} else if (!t1->hasDate() && t2->hasDate()) {
return false;
}
return t1->getDate() < t2->getDate();
});
}
void TasksView::addTagFilter(const std::string &tag)