mirror of
https://codeberg.org/vyn/mirai.git
synced 2025-07-02 09:23:18 +00:00
Refactor TasksView, Mirai instance doesn't depend on it anymore
This commit is contained in:
parent
d1a4858504
commit
841c87752f
7 changed files with 39 additions and 57 deletions
|
@ -7,6 +7,7 @@
|
|||
#include "Backend.h"
|
||||
#include "TaskItem.h"
|
||||
#include "core/TaskItem.h"
|
||||
#include "core/TasksView.h"
|
||||
#include "core/TodoMd.h"
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
@ -15,7 +16,7 @@
|
|||
#include <qlogging.h>
|
||||
#include <qvariant.h>
|
||||
|
||||
Backend::Backend()
|
||||
Backend::Backend() : view(&mirai)
|
||||
{
|
||||
std::cout << "Backend created" << std::endl;
|
||||
QDir().mkdir(QDir::homePath() + "/.config/mirai");
|
||||
|
@ -51,8 +52,7 @@ Backend::Backend()
|
|||
jsonTagsConfig.toObject()[jsonTagConfigKey].toObject()["color"].toString();
|
||||
}
|
||||
}
|
||||
|
||||
view = mirai.getTasks();
|
||||
view.update();
|
||||
rebuildQMLTasksList();
|
||||
}
|
||||
|
||||
|
@ -60,6 +60,7 @@ void Backend::addTodo(QString newTodo, QString date)
|
|||
{
|
||||
mirai.addTask(newTodo.toStdString(), date.toStdString());
|
||||
mirai.save();
|
||||
view.update();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
@ -75,42 +76,42 @@ void Backend::addTodoFromRawFormat(QString filePath, QString text, QString date)
|
|||
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString())
|
||||
);
|
||||
mirai.save();
|
||||
view.lock()->update();
|
||||
view.update();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::addTagFilter(QString tag)
|
||||
{
|
||||
view.lock()->addTagFilter(tag.toStdString());
|
||||
view.addTagFilter(tag.toStdString());
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::removeTagFilter(QString tag)
|
||||
{
|
||||
view.lock()->removeTagFilter(tag.toStdString());
|
||||
view.removeTagFilter(tag.toStdString());
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::addFileFilter(QString fileName)
|
||||
{
|
||||
view.lock()->addFileFilter(fileName.toStdString());
|
||||
view.addFileFilter(fileName.toStdString());
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::removeFileFilter(QString fileName)
|
||||
{
|
||||
view.lock()->removeFileFilter(fileName.toStdString());
|
||||
view.removeFileFilter(fileName.toStdString());
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
||||
void Backend::removeFilters()
|
||||
{
|
||||
view.lock()->removeFilters();
|
||||
view.removeFilters();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
@ -122,7 +123,7 @@ void Backend::updateTodoFromRawFormat(int todoIndex, QString text, QString date)
|
|||
*(taskItem.taskItem) =
|
||||
mirai::TodoMdFormat::StringToTask(text.toStdString(), date.toStdString());
|
||||
mirai.save();
|
||||
view.lock()->update();
|
||||
view.update();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
@ -138,7 +139,7 @@ void Backend::updateTodo(int todoIndex, QString state, QString text, QString dat
|
|||
taskItem.taskItem->setText(text.toStdString());
|
||||
taskItem.taskItem->setDate(date.toStdString());
|
||||
mirai.save();
|
||||
view.lock()->update();
|
||||
view.update();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
@ -148,6 +149,7 @@ void Backend::removeTodo(int todoIndex)
|
|||
QMLTaskItem &taskItem = QMLTasks[todoIndex];
|
||||
mirai.removeTask(taskItem.taskItem);
|
||||
mirai.save();
|
||||
view.update();
|
||||
rebuildQMLTasksList();
|
||||
emit tasksChanged();
|
||||
}
|
||||
|
@ -173,8 +175,8 @@ void Backend::rebuildQMLTasksList()
|
|||
std::tm tm = *std::localtime(&t);
|
||||
std::stringstream currentDate;
|
||||
currentDate << std::put_time(&tm, "%Y-%m-%d");
|
||||
for (int i = 0; i < view.lock()->count(); ++i) {
|
||||
mirai::TaskItem &task = (*view.lock())[i];
|
||||
for (int i = 0; i < view.count(); ++i) {
|
||||
mirai::TaskItem &task = view[i];
|
||||
if (shouldHideCompletedTasks_ && task.getState() == mirai::DONE && task.hasDate() &&
|
||||
task.getDate() < currentDate.str()) {
|
||||
continue;
|
||||
|
@ -199,12 +201,12 @@ void Backend::rebuildQMLTasksList()
|
|||
}
|
||||
|
||||
QMLActiveTagsFilter.clear();
|
||||
for (auto &activeTagFilter : view.lock()->getActiveTagsFilter()) {
|
||||
for (auto &activeTagFilter : view.getActiveTagsFilter()) {
|
||||
QMLActiveTagsFilter.push_back(QString::fromStdString(activeTagFilter));
|
||||
}
|
||||
|
||||
QMLActiveFilesFilter.clear();
|
||||
for (auto &activeFileFilter : view.lock()->getActiveFilesFilter()) {
|
||||
for (auto &activeFileFilter : view.getActiveFilesFilter()) {
|
||||
QMLActiveFilesFilter.push_back(QString::fromStdString(activeFileFilter));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ class Backend : public QObject
|
|||
bool shouldHideCompletedTasks();
|
||||
|
||||
mirai::Mirai mirai;
|
||||
std::weak_ptr<mirai::TasksView> view;
|
||||
mirai::TasksView view;
|
||||
|
||||
QList<QMLTaskItem> QMLTasks;
|
||||
QList<QMLTasksFile> QMLTasksFiles;
|
||||
|
|
|
@ -21,13 +21,13 @@ namespace mirai
|
|||
void Mirai::loadFile(const std::string &path)
|
||||
{
|
||||
auto tasksFile = TodoMdFormat::readFile(path);
|
||||
files->push_back(std::move(tasksFile));
|
||||
files.push_back(std::move(tasksFile));
|
||||
reloadTags();
|
||||
}
|
||||
|
||||
void Mirai::save()
|
||||
{
|
||||
for (auto &file : *files) {
|
||||
for (auto &file : files) {
|
||||
if (file->isDirty()) {
|
||||
TodoMdFormat::writeFile(*file);
|
||||
file->setDirty(false);
|
||||
|
@ -38,10 +38,7 @@ void Mirai::save()
|
|||
|
||||
void Mirai::addTask(TaskItemData taskItem)
|
||||
{
|
||||
(*files)[0]->addTask(taskItem);
|
||||
for (auto &view : views) {
|
||||
view->update();
|
||||
}
|
||||
files[0]->addTask(taskItem);
|
||||
}
|
||||
|
||||
void Mirai::addTask(std::string text, std::string date)
|
||||
|
@ -50,46 +47,33 @@ void Mirai::addTask(std::string text, std::string date)
|
|||
/*std::tm tm = *std::localtime(&t);*/
|
||||
/*std::stringstream ssCreationDate;*/
|
||||
/*ssCreationDate << std::put_time(&tm, "%Y-%m-%d");*/
|
||||
(*files)[0]->addTask(text, date);
|
||||
for (auto &view : views) {
|
||||
view->update();
|
||||
}
|
||||
files[0]->addTask(text, date);
|
||||
}
|
||||
|
||||
void Mirai::removeTask(const TaskItem *taskItem)
|
||||
{
|
||||
for (auto &file : *files) {
|
||||
for (auto &file : files) {
|
||||
file->removeTask(taskItem);
|
||||
}
|
||||
for (auto &view : views) {
|
||||
view->update();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<TasksFile>> &Mirai::getFiles()
|
||||
{
|
||||
return *files.get();
|
||||
return files;
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<TasksFile>> Mirai::getFileByPath(const std::string &path)
|
||||
{
|
||||
auto fileIterator = std::ranges::find_if(*files, [&](const std::unique_ptr<TasksFile> &file) {
|
||||
auto fileIterator = std::ranges::find_if(files, [&](const std::unique_ptr<TasksFile> &file) {
|
||||
return file->getPath() == path;
|
||||
});
|
||||
|
||||
if (fileIterator == files->end()) {
|
||||
if (fileIterator == files.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return *(fileIterator->get());
|
||||
}
|
||||
|
||||
std::weak_ptr<TasksView> Mirai::getTasks()
|
||||
{
|
||||
auto view = std::make_shared<TasksView>(files);
|
||||
views.push_back(view);
|
||||
return view;
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Mirai::getTags()
|
||||
{
|
||||
return tags;
|
||||
|
@ -98,7 +82,7 @@ const std::vector<std::string> &Mirai::getTags()
|
|||
void Mirai::reloadTags()
|
||||
{
|
||||
tags.clear();
|
||||
for (auto &file : *files) {
|
||||
for (auto &file : files) {
|
||||
for (auto &task : file->getTasks()) {
|
||||
for (auto &tag : task->getTags()) {
|
||||
if (!vectorUtils::contains(tags, tag)) {
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
#include "TaskItem.h"
|
||||
#include "TasksFile.h"
|
||||
#include "TasksView.h"
|
||||
#include "TodoMd.h"
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
@ -33,22 +32,12 @@ class Mirai
|
|||
std::optional<std::reference_wrapper<TasksFile>> getFileByPath(const std::string &path);
|
||||
|
||||
std::vector<std::unique_ptr<TasksFile>> &getFiles();
|
||||
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
|
||||
// because we can't control if the caller will keep the main object alive.
|
||||
std::shared_ptr<std::vector<std::unique_ptr<TasksFile>>> files =
|
||||
std::make_shared<std::vector<std::unique_ptr<TasksFile>>>();
|
||||
|
||||
// We keep a vector of shared_ptr because we need the ref counting mechanism to know
|
||||
// if we have to remove the view (not used) or tell the view to update() on
|
||||
// some changes.
|
||||
std::vector<std::shared_ptr<TasksView>> views;
|
||||
std::vector<std::unique_ptr<TasksFile>> files = std::vector<std::unique_ptr<TasksFile>>();
|
||||
std::vector<std::string> tags;
|
||||
};
|
||||
} // namespace mirai
|
||||
|
|
|
@ -5,19 +5,24 @@
|
|||
*/
|
||||
|
||||
#include "TasksView.h"
|
||||
#include "core/Mirai.h"
|
||||
#include "utils.h"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace mirai
|
||||
{
|
||||
|
||||
TasksView::TasksView(std::shared_ptr<std::vector<std::unique_ptr<TasksFile>>> files) : files(files)
|
||||
TasksView::TasksView(Mirai *miraiInstance) : mirai(miraiInstance)
|
||||
{
|
||||
if (!miraiInstance) {
|
||||
throw std::runtime_error("NULL pointer passed in TasksView");
|
||||
}
|
||||
update();
|
||||
}
|
||||
|
||||
|
@ -34,7 +39,7 @@ size_t TasksView::count() const
|
|||
void TasksView::update()
|
||||
{
|
||||
tasksToShow.clear();
|
||||
for (auto &file : *files) {
|
||||
for (auto &file : mirai->getFiles()) {
|
||||
if (filesFilter.size() > 0 && !vectorUtils::contains(filesFilter, file->getName())) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#ifndef MIRAI_TASKSVIEW_H
|
||||
#define MIRAI_TASKSVIEW_H
|
||||
#include "TasksFile.h"
|
||||
#include "core/Mirai.h"
|
||||
#include "using.h"
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
@ -19,7 +20,7 @@ class TasksView
|
|||
{
|
||||
|
||||
public:
|
||||
TasksView(std::shared_ptr<std::vector<std::unique_ptr<TasksFile>>> files);
|
||||
TasksView(Mirai *mirai);
|
||||
|
||||
TaskItem &operator[](int index);
|
||||
|
||||
|
@ -34,7 +35,7 @@ class TasksView
|
|||
const std::vector<std::string> &getActiveFilesFilter();
|
||||
|
||||
private:
|
||||
std::shared_ptr<std::vector<std::unique_ptr<TasksFile>>> files;
|
||||
Mirai *mirai;
|
||||
std::vector<TaskItem *> tasksToShow;
|
||||
Tags tagsFilter;
|
||||
std::vector<std::string> filesFilter;
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
try {
|
||||
std::cout << "Mirai started" << std::endl;
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
qreal refDpi = 54.;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue