Remove unused code, move logic from .h to their .cpp, clean some things

This commit is contained in:
Vyn 2024-09-02 11:52:06 +02:00
parent 924e35ecc4
commit cb6c663833
41 changed files with 492 additions and 978 deletions

View file

@ -14,12 +14,12 @@ add_library(mirai-core
src/Event.cpp
src/DateTime.cpp
src/EventEmitter.cpp
src/BaseResource.cpp
src/BaseSource.cpp
src/StdFileSource.cpp
src/TasksView.cpp
src/TodoMd.cpp
src/utils.cpp
)
target_include_directories(mirai-core PRIVATE "external")
target_include_directories(mirai-core PRIVATE "include/mirai-core")

View file

@ -4,29 +4,27 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_BASE_FILE_RESOURCE_H
#define MIRAI_BASE_FILE_RESOURCE_H
#pragma once
#include "BaseResource.h"
#include "BaseSource.h"
#include <string>
namespace mirai
{
struct BaseFileResourceConstructor {
struct BaseFileSourceConstructor {
std::string name;
std::string path;
};
class BaseFileResource : public BaseResource
class BaseFileSource : public BaseSource
{
public:
BaseFileResource(BaseFileResourceConstructor data)
: BaseResource(data.name), path(data.path) {};
BaseFileSource(BaseFileSourceConstructor data) : BaseSource(data.name), path(data.path) {};
BaseFileResource(BaseFileResource &) = delete;
BaseFileResource(BaseFileResource &&) = delete;
~BaseFileResource() override = default;
BaseFileSource(BaseFileSource &) = delete;
BaseFileSource(BaseFileSource &&) = delete;
~BaseFileSource() override = default;
const std::string &getPath() const
{
@ -37,4 +35,3 @@ class BaseFileResource : public BaseResource
std::string path;
};
} // namespace mirai
#endif

View file

@ -4,8 +4,7 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_BASE_RESOURCE_H
#define MIRAI_BASE_RESOURCE_H
#pragma once
#include "DateTime.h"
#include "Day.h"
@ -19,7 +18,7 @@
namespace mirai
{
class BaseResource
class BaseSource
{
public:
@ -28,23 +27,16 @@ class BaseResource
Tags tags;
};
BaseResource(std::string name) : name_(name) {};
BaseResource(BaseResource &) = delete;
BaseResource(BaseResource &&) = delete;
BaseSource(std::string name) : name_(name) {};
BaseSource(BaseSource &) = delete;
BaseSource(BaseSource &&) = delete;
virtual ~BaseResource() = default;
virtual ~BaseSource() = default;
virtual void save() = 0;
virtual void load() = 0;
void setName(std::string name)
{
this->name_ = name;
}
const std::string &getName() const
{
return name_;
}
void setName(std::string name);
const std::string &getName() const;
void addDay(const DayData &dayData);
Day *day(const Date &date);
@ -56,10 +48,7 @@ class BaseResource
void setDirty(bool shouldBeDirty);
bool isDirty() const;
int id() const
{
return id_;
}
int id() const;
private:
static int nextId_;
@ -70,4 +59,3 @@ class BaseResource
};
} // namespace mirai
#endif

View file

@ -16,78 +16,28 @@ namespace mirai
{
struct Date {
explicit Date(int year, unsigned month, unsigned day) : year(year), month(month), day(day)
{
}
explicit Date(int year, unsigned month, unsigned day);
explicit Date(std::chrono::time_point<std::chrono::system_clock> tp)
{
auto chronoDate = std::chrono::year_month_day(std::chrono::floor<std::chrono::days>(tp));
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
explicit Date(std::chrono::time_point<std::chrono::system_clock> tp);
explicit Date(std::chrono::year_month_day chronoDate);
explicit Date(std::chrono::year_month_day chronoDate)
{
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
bool operator==(const Date &other) const;
bool operator<(const Date &other) const;
bool operator>(const Date &other) const;
int year;
unsigned month;
unsigned day;
bool operator==(const Date &other) const
{
return other.year == year && other.month == month && other.day == day;
}
bool operator<(const Date &other) const
{
if (year < other.year) {
return true;
}
if (year == other.year && month < other.month) {
return true;
}
if (year == other.year && month == other.month && day < other.day) {
return true;
}
return false;
}
bool operator>(const Date &other) const
{
return !(*this < other) && !(*this == other);
}
};
struct Time {
bool operator==(const Time &other) const;
bool operator<(const Time &other) const;
bool operator>(const Time &other) const;
int hour;
int minute;
bool operator==(const Time &other) const
{
return other.hour == hour && other.minute == minute;
}
bool operator<(const Time &other) const
{
if (hour < other.hour) {
return true;
}
if (hour == other.hour && minute < other.minute) {
return true;
}
return false;
}
bool operator>(const Time &other) const
{
return !(*this < other) && !(*this == other);
}
};
std::optional<mirai::Date> stringToDate(const std::string &dateStr);

View file

@ -17,7 +17,7 @@
namespace mirai
{
class BaseResource;
class BaseSource;
struct DayData {
Date date;
@ -28,7 +28,7 @@ struct DayData {
class Day
{
public:
Day(BaseResource *source, const DayData &data);
Day(BaseSource *source, const DayData &data);
void setDate(const Date &date);
Event *createEvent(const EventData &eventData);
@ -40,25 +40,13 @@ class Day
const Date &getDate() const;
Event *getEventById(int eventId)
{
for (auto &event : events_) {
if (event->id() == eventId) {
return event.get();
}
}
return nullptr;
}
BaseResource *source()
{
return source_;
}
Event *getEventById(int eventId);
BaseSource *source();
private:
void onChange();
BaseResource *source_;
BaseSource *source_;
std::vector<std::unique_ptr<Event>> events_;
std::vector<std::unique_ptr<TaskItem>> tasks_;
DayData data_;

View file

@ -16,7 +16,7 @@
namespace mirai
{
class BaseResource;
class BaseSource;
struct EventData {
std::string description;
@ -33,14 +33,9 @@ class Event
{
private:
public:
Event(BaseResource *source, Day *parent, const EventData &data);
Event(BaseSource *source, Day *parent, const EventData &data);
Event &operator=(const EventData &newData)
{
data = newData;
onChange();
return *this;
};
Event &operator=(const EventData &newData);
void setText(const std::string &text);
void setStartTime(const Time &time);
@ -57,15 +52,8 @@ class Event
const Tags &getTags() const;
bool hasTag(const std::string &tag) const;
int id() const
{
return id_;
}
Day *day()
{
return day_;
}
int id() const;
Day *day();
private:
void onChange();
@ -75,6 +63,6 @@ class Event
Day *day_;
EventData data;
std::vector<std::unique_ptr<TaskItem>> tasks_;
BaseResource *source_;
BaseSource *source_;
};
} // namespace mirai

View file

@ -4,11 +4,10 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_MIRAI_H
#define MIRAI_MIRAI_H
#pragma once
#include "BaseFileResource.h"
#include "BaseResource.h"
#include "BaseFileSource.h"
#include "BaseSource.h"
#include "TaskItem.h"
#include "TodoMd.h"
#include <algorithm>
@ -25,31 +24,22 @@ class Mirai
{
public:
void loadResource(std::unique_ptr<BaseResource> &&resource);
void unloadAllResources();
void loadSource(std::unique_ptr<BaseSource> &&resource);
void unloadAllSources();
void save();
void removeTask(const TaskItem *taskItem);
std::optional<std::reference_wrapper<BaseResource>> getResourceByName(const std::string &name);
void deleteTask(const TaskItem &taskItem);
std::optional<std::reference_wrapper<BaseSource>> getSourceByName(const std::string &name);
std::vector<std::unique_ptr<BaseResource>> &getResources();
std::vector<std::unique_ptr<BaseSource>> &getSources();
const std::vector<std::string> &getTags();
void reloadTags();
// new stuff
// Returns a non owning pointer to the requested resource or nullptr if not found.
BaseResource *getResourceById(int id)
{
if (id >= resources_.size()) {
return nullptr;
}
return resources_.at(id).get();
}
BaseSource *getSourceById(int id);
private:
std::vector<std::unique_ptr<BaseResource>> resources_;
std::vector<std::unique_ptr<BaseSource>> sources_;
std::vector<std::string> tags_;
};
} // namespace mirai
#endif

View file

@ -1,74 +0,0 @@
/*
* 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
*/
#ifndef MIRAI_STD_FILE_RESOURCE_H
#define MIRAI_STD_FILE_RESOURCE_H
#include "BaseFileResource.h"
#include "TodoMd.h"
#include <fstream>
#include <iostream>
#include <ostream>
#include <string>
namespace mirai
{
class StdFileResource : public BaseFileResource
{
public:
StdFileResource(BaseFileResourceConstructor data) : BaseFileResource(data) {};
StdFileResource(StdFileResource &) = delete;
StdFileResource(StdFileResource &&) = delete;
StdFileResource operator=(StdFileResource &) = delete;
StdFileResource operator=(StdFileResource &&) = delete;
~StdFileResource() override = default;
void save() override
{
std::ofstream file(getPath());
if (!file.is_open()) {
throw std::runtime_error("can't create " + getPath());
}
const std::string content = TodoMdFormat::stringify(getName(), *days());
file << content;
file.close();
};
void load() override
{
std::ifstream file(getPath());
if (!file.is_open()) {
return;
}
std::string content = "";
std::string line;
while (std::getline(file, line)) {
content += line + "\n";
}
file.close();
auto result = TodoMdFormat::parse(content);
for (const auto &dayData : result.days) {
Day *newDay = day(dayData.date);
for (const auto &eventData : dayData.events) {
Event *newEvent = newDay->createEvent(eventData);
for (const auto &taskData : eventData.tasks) {
TaskItem *newTask = newEvent->createTask(taskData);
}
}
for (const auto &taskData : dayData.tasks) {
TaskItem *newTask = newDay->createTask(taskData);
}
}
setName(result.name);
};
};
} // namespace mirai
#endif

View file

@ -0,0 +1,29 @@
/*
* 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
*/
#pragma once
#include "BaseFileSource.h"
namespace mirai
{
class StdFileSource : public BaseFileSource
{
public:
StdFileSource(BaseFileSourceConstructor data) : BaseFileSource(data) {};
StdFileSource(StdFileSource &) = delete;
StdFileSource(StdFileSource &&) = delete;
StdFileSource operator=(StdFileSource &) = delete;
StdFileSource operator=(StdFileSource &&) = delete;
~StdFileSource() override = default;
void save() override;
void load() override;
;
};
} // namespace mirai

View file

@ -4,8 +4,7 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_TASKITEM_H
#define MIRAI_TASKITEM_H
#pragma once
#include "using.h"
#include <functional>
@ -14,7 +13,7 @@
namespace mirai
{
class BaseResource;
class BaseSource;
class Day;
enum TaskItemState { TODO, DONE };
@ -28,20 +27,13 @@ struct TaskItemData {
class TaskItem
{
public:
TaskItem(BaseResource *source, Day *day, const TaskItemData data);
TaskItem(BaseSource *source, Day *day, const TaskItemData data);
TaskItem &operator=(const TaskItemData &newData)
{
data = newData;
onChange();
return *this;
};
TaskItem &operator=(const TaskItemData &newData);
void markAsDone();
void markAsUndone();
void setDate(const std::string &date);
void setText(const std::string &text);
const std::string &getLine() const;
@ -50,20 +42,9 @@ class TaskItem
const Tags &getTags() const;
bool hasTag(const std::string &tag) const;
int id() const
{
return id_;
}
BaseResource *source()
{
return source_;
}
Day *day()
{
return day_;
}
int id() const;
BaseSource *source();
Day *day();
private:
void onChange();
@ -72,10 +53,8 @@ class TaskItem
int id_ = nextId++;
TaskItemData data;
BaseResource *source_;
BaseSource *source_;
Day *day_;
};
} // namespace mirai
#endif

View file

@ -4,8 +4,7 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_TASKSVIEW_H
#define MIRAI_TASKSVIEW_H
#pragma once
#include "Mirai.h"
#include "TaskItem.h"
#include "using.h"
@ -39,41 +38,14 @@ class TasksView
void update();
void addTagFilter(const std::string &tag);
void removeTagFilter(const std::string &tag);
void addResourceFilter(const std::string &fileName);
void removeResourceFilter(const std::string &fileName);
void addSourceFilter(const std::string &fileName);
void removeSourceFilter(const std::string &fileName);
void removeFilters();
const Tags &getActiveTagsFilter();
const std::vector<std::string> &getActiveFilesFilter();
void hideCompletedTasks(bool hide)
{
shouldHideCompletedTasks_ = hide;
}
bool shouldHideCompletedTasks() const
{
return shouldHideCompletedTasks_;
}
/*auto begin()*/ // TODO REWORK
/*{*/
/*return tasksToShow.begin();*/
/*}*/
/*auto begin() const*/
/*{*/
/*return tasksToShow.cbegin();*/
/*}*/
/*auto end() const*/
/*{*/
/*return tasksToShow.cend();*/
/*}*/
/*auto end()*/
/*{*/
/*return tasksToShow.end();*/
/*}*/
void hideCompletedTasks(bool hide);
bool shouldHideCompletedTasks() const;
private:
std::vector<FilteredDay> filteredDays;
@ -84,4 +56,3 @@ class TasksView
bool shouldHideCompletedTasks_ = true;
};
} // namespace mirai
#endif

View file

@ -4,8 +4,7 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_TODOMD_H
#define MIRAI_TODOMD_H
#pragma once
#include "DateTime.h"
#include "Day.h"
@ -44,5 +43,3 @@ class TodoMdFormat
static Tags extractTagsFromMetadata(std::string metadata);
};
} // namespace mirai
#endif

View file

@ -4,8 +4,7 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_TODOTXT_H
#define MIRAI_TODOTXT_H
#pragma once
/*#include "TaskItem.h"*/
/*#include "utils.h"*/
@ -90,5 +89,3 @@
/*}*/
/*};*/
/*}*/
#endif

View file

@ -4,8 +4,7 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_USING_H
#define MIRAI_USING_H
#pragma once
#include <memory>
#include <string>
@ -15,5 +14,3 @@ namespace mirai
{
using Tags = std::vector<std::string>;
}
#endif

View file

@ -4,8 +4,7 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#ifndef MIRAI_UTILS_H
#define MIRAI_UTILS_H
#pragma once
#include <algorithm>
#include <cctype>
@ -21,5 +20,3 @@ namespace vectorUtils = cpputils::vector;
bool isDate(const std::string &dateStr);
} // namespace mirai
#endif

View file

@ -4,7 +4,7 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "BaseResource.h"
#include "BaseSource.h"
#include "Day.h"
#include "TaskItem.h"
#include <algorithm>
@ -16,17 +16,17 @@
namespace mirai
{
void BaseResource::setDirty(bool shouldBeDirty)
void BaseSource::setDirty(bool shouldBeDirty)
{
isDirty_ = shouldBeDirty;
}
bool BaseResource::isDirty() const
bool BaseSource::isDirty() const
{
return isDirty_;
}
Day *BaseResource::day(const Date &date)
Day *BaseSource::day(const Date &date)
{
auto dayFound = std::find_if(days_.begin(), days_.end(), [&](std::unique_ptr<Day> &day) {
return day->getDate().day == date.day && day->getDate().month == date.month &&
@ -41,18 +41,18 @@ Day *BaseResource::day(const Date &date)
return dayFound->get();
}
std::vector<std::unique_ptr<Day>> *BaseResource::days()
std::vector<std::unique_ptr<Day>> *BaseSource::days()
{
return &days_;
}
void BaseResource::addDay(const DayData &dayData)
void BaseSource::addDay(const DayData &dayData)
{
days_.push_back(std::make_unique<Day>(this, dayData));
setDirty(true);
}
TaskItem *BaseResource::getTaskById(int taskId)
TaskItem *BaseSource::getTaskById(int taskId)
{
for (auto &day : days_) {
for (auto &task : *day->tasks()) {
@ -71,7 +71,7 @@ TaskItem *BaseResource::getTaskById(int taskId)
return nullptr;
}
void BaseResource::deleteTask(const TaskItem &taskToDelete)
void BaseSource::deleteTask(const TaskItem &taskToDelete)
{
for (auto &day : days_) {
for (auto &task : *day->tasks()) {
@ -91,7 +91,7 @@ void BaseResource::deleteTask(const TaskItem &taskToDelete)
}
}
Event *BaseResource::getEventById(int eventId)
Event *BaseSource::getEventById(int eventId)
{
for (auto &day : days_) {
for (auto &event : *day->events()) {
@ -103,6 +103,21 @@ Event *BaseResource::getEventById(int eventId)
return nullptr;
}
void BaseSource::setName(std::string name)
{
this->name_ = name;
}
const std::string &BaseSource::getName() const
{
return name_;
}
int BaseSource::id() const
{
return id_;
}
/*void BaseResource::addTask(TaskItemData taskItem)*/
/*{*/
/*tasks.push_back(std::make_unique<TaskItem>(this, taskItem));*/
@ -122,6 +137,6 @@ Event *BaseResource::getEventById(int eventId)
/*setDirty(true);*/
/*}*/
int BaseResource::nextId_ = 0;
int BaseSource::nextId_ = 0;
} // namespace mirai

View file

@ -34,4 +34,72 @@ std::optional<mirai::Date> stringToDate(const std::string &dateStr)
auto day = dateSplit[2];
return mirai::Date(year, static_cast<unsigned>(month), static_cast<unsigned>(day));
}
Date::Date(int year, unsigned month, unsigned day) : year(year), month(month), day(day)
{
}
Date::Date(std::chrono::time_point<std::chrono::system_clock> tp)
{
auto chronoDate = std::chrono::year_month_day(std::chrono::floor<std::chrono::days>(tp));
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
Date::Date(std::chrono::year_month_day chronoDate)
{
year = static_cast<int>(chronoDate.year());
month = static_cast<unsigned>(chronoDate.month());
day = static_cast<unsigned>(chronoDate.day());
}
int year;
unsigned month;
unsigned day;
bool Date::operator==(const Date &other) const
{
return other.year == year && other.month == month && other.day == day;
}
bool Date::operator<(const Date &other) const
{
if (year < other.year) {
return true;
}
if (year == other.year && month < other.month) {
return true;
}
if (year == other.year && month == other.month && day < other.day) {
return true;
}
return false;
}
bool Date::operator>(const Date &other) const
{
return !(*this < other) && !(*this == other);
}
bool Time::operator==(const Time &other) const
{
return other.hour == hour && other.minute == minute;
}
bool Time::operator<(const Time &other) const
{
if (hour < other.hour) {
return true;
}
if (hour == other.hour && minute < other.minute) {
return true;
}
return false;
}
bool Time::operator>(const Time &other) const
{
return !(*this < other) && !(*this == other);
}
} // namespace mirai

View file

@ -5,7 +5,7 @@
*/
#include "Day.h"
#include "BaseResource.h"
#include "BaseSource.h"
#include <algorithm>
#include <iostream>
#include <memory>
@ -15,7 +15,7 @@
namespace mirai
{
Day::Day(BaseResource *source, const DayData &data) : source_(source), data_(data)
Day::Day(BaseSource *source, const DayData &data) : source_(source), data_(data)
{
}
@ -86,4 +86,19 @@ std::vector<std::unique_ptr<TaskItem>> *Day::tasks()
{
return &tasks_;
}
Event *Day::getEventById(int eventId)
{
for (auto &event : events_) {
if (event->id() == eventId) {
return event.get();
}
}
return nullptr;
}
BaseSource *Day::source()
{
return source_;
}
} // namespace mirai

View file

@ -5,14 +5,14 @@
*/
#include "Event.h"
#include "BaseResource.h"
#include "BaseSource.h"
#include "TaskItem.h"
#include "utils.h"
namespace mirai
{
Event::Event(BaseResource *source, Day *parent, const EventData &data)
Event::Event(BaseSource *source, Day *parent, const EventData &data)
: source_(source), day_(parent), data(data)
{
}
@ -94,6 +94,23 @@ void Event::onChange()
source_->setDirty(true);
}
Event &Event::operator=(const EventData &newData)
{
data = newData;
onChange();
return *this;
};
int Event::id() const
{
return id_;
}
Day *Event::day()
{
return day_;
}
int Event::nextId = 0;
} // namespace mirai

View file

@ -19,21 +19,21 @@
namespace mirai
{
void Mirai::loadResource(std::unique_ptr<BaseResource> &&resource)
void Mirai::loadSource(std::unique_ptr<BaseSource> &&resource)
{
resource->load();
resources_.push_back(std::move(resource));
sources_.push_back(std::move(resource));
reloadTags();
};
void Mirai::unloadAllResources()
void Mirai::unloadAllSources()
{
resources_.clear();
sources_.clear();
}
void Mirai::save()
{
for (auto &resource : resources_) {
for (auto &resource : sources_) {
if (resource->isDirty()) {
resource->save();
resource->setDirty(false);
@ -42,27 +42,26 @@ void Mirai::save()
reloadTags();
}
void Mirai::removeTask(const TaskItem *taskItem)
void Mirai::deleteTask(const TaskItem &taskItem)
{
for (auto &resource : resources_) {
// resource->removeTask(taskItem); // TODO REWORK
for (auto &resource : sources_) {
resource->deleteTask(taskItem);
}
}
std::vector<std::unique_ptr<BaseResource>> &Mirai::getResources()
std::vector<std::unique_ptr<BaseSource>> &Mirai::getSources()
{
return resources_;
return sources_;
}
std::optional<std::reference_wrapper<BaseResource>> Mirai::getResourceByName(const std::string &name
)
std::optional<std::reference_wrapper<BaseSource>> Mirai::getSourceByName(const std::string &name)
{
auto resourceIterator =
std::ranges::find_if(resources_, [&](const std::unique_ptr<BaseResource> &resource) {
std::ranges::find_if(sources_, [&](const std::unique_ptr<BaseSource> &resource) {
return resource->getName() == name;
});
if (resourceIterator == resources_.end()) {
if (resourceIterator == sources_.end()) {
return std::nullopt;
}
return *(resourceIterator->get());
@ -75,18 +74,27 @@ const std::vector<std::string> &Mirai::getTags()
void Mirai::reloadTags()
{
/*cpputils::debug::Timer reloadingTagsDuration;*/ // TODO REWORK
/*tags.clear();*/
/*for (auto &resource : resources) {*/
/*for (auto &task : resource->getTasks()) {*/
/*for (auto &tag : task->getTags()) {*/
/*if (!vectorUtils::contains(tags, tag)) {*/
/*tags.push_back(tag);*/
/*}*/
/*}*/
/*}*/
/*}*/
// TODO TAGS
/*cpputils::debug::Timer reloadingTagsDuration;*/
/*tags.clear();*/
/*for (auto &resource : resources) {*/
/*for (auto &task : resource->getTasks()) {*/
/*for (auto &tag : task->getTags()) {*/
/*if (!vectorUtils::contains(tags, tag)) {*/
/*tags.push_back(tag);*/
/*}*/
/*}*/
/*}*/
/*}*/
/*reloadingTagsDuration.printTimeElapsed("ReloadingTags");*/
}
BaseSource *Mirai::getSourceById(int id)
{
if (id >= sources_.size()) {
return nullptr;
}
return sources_.at(id).get();
}
} // namespace mirai

View file

@ -0,0 +1,58 @@
/*
* 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 "StdFileSource.h"
#include "TodoMd.h"
#include <fstream>
#include <iostream>
#include <ostream>
#include <string>
namespace mirai
{
void StdFileSource::save()
{
std::ofstream file(getPath());
if (!file.is_open()) {
throw std::runtime_error("can't create " + getPath());
}
const std::string content = TodoMdFormat::stringify(getName(), *days());
file << content;
file.close();
};
void StdFileSource::load()
{
std::ifstream file(getPath());
if (!file.is_open()) {
return;
}
std::string content = "";
std::string line;
while (std::getline(file, line)) {
content += line + "\n";
}
file.close();
auto result = TodoMdFormat::parse(content);
for (const auto &dayData : result.days) {
Day *newDay = day(dayData.date);
for (const auto &eventData : dayData.events) {
Event *newEvent = newDay->createEvent(eventData);
for (const auto &taskData : eventData.tasks) {
TaskItem *newTask = newEvent->createTask(taskData);
}
}
for (const auto &taskData : dayData.tasks) {
TaskItem *newTask = newDay->createTask(taskData);
}
}
setName(result.name);
};
} // namespace mirai

View file

@ -5,7 +5,7 @@
*/
#include "TaskItem.h"
#include "BaseResource.h"
#include "BaseSource.h"
#include "utils.h"
#include <iostream>
@ -14,7 +14,14 @@ namespace mirai
int TaskItem::nextId = 0;
TaskItem::TaskItem(BaseResource *source, Day *day, TaskItemData data)
TaskItem &TaskItem::operator=(const TaskItemData &newData)
{
data = newData;
onChange();
return *this;
};
TaskItem::TaskItem(BaseSource *source, Day *day, TaskItemData data)
: source_(source), day_(day), data(data)
{
}
@ -62,4 +69,19 @@ void TaskItem::onChange()
source()->setDirty(true);
}
int TaskItem::id() const
{
return id_;
}
BaseSource *TaskItem::source()
{
return source_;
}
Day *TaskItem::day()
{
return day_;
}
} // namespace mirai

View file

@ -46,7 +46,7 @@ void TasksView::update()
auto todayDate = std::format("{:%Y-%m-%d}", std::chrono::system_clock::now());
filteredDays.clear();
for (auto &file : mirai->getResources()) {
for (auto &file : mirai->getSources()) {
if (resourcesFilter.size() > 0 &&
!vectorUtils::contains(resourcesFilter, file->getName())) {
continue;
@ -55,9 +55,8 @@ void TasksView::update()
FilteredDay filteredDay{.day = day.get()};
for (auto &task : *day->tasks()) {
auto taskDate = std::format(
"{}-0{}-{}", day->getDate().year, // TODO REWORK REMOVE 0{}
day->getDate().month,
day->getDate().day // TODO REWORK CLEAN THIS MESS
"{:04d}-{:02d}-{:02d}", day->getDate().year, day->getDate().month,
day->getDate().day
);
if (shouldHideCompletedTasks() && task->getState() == DONE &&
taskDate < todayDate) {
@ -72,8 +71,8 @@ void TasksView::update()
for (auto &event : *day->events()) {
FilteredEvent filteredEvent{.event = event.get()};
auto eventDate = std::format(
// TODO REWORK
"{}-0{}-{}", day->getDate().year, day->getDate().month, day->getDate().day
"{:04d}-{:02d}-{:02d}", day->getDate().year, day->getDate().month,
day->getDate().day
);
for (auto &task : *event->tasks()) {
@ -141,13 +140,13 @@ void TasksView::removeTagFilter(const std::string &tag)
update();
}
void TasksView::addResourceFilter(const std::string &fileName)
void TasksView::addSourceFilter(const std::string &fileName)
{
resourcesFilter.push_back(fileName);
update();
}
void TasksView::removeResourceFilter(const std::string &fileName)
void TasksView::removeSourceFilter(const std::string &fileName)
{
resourcesFilter.erase(std::remove_if(
resourcesFilter.begin(), resourcesFilter.end(),
@ -173,4 +172,14 @@ const std::vector<std::string> &TasksView::getActiveFilesFilter()
{
return resourcesFilter;
}
void TasksView::hideCompletedTasks(bool hide)
{
shouldHideCompletedTasks_ = hide;
}
bool TasksView::shouldHideCompletedTasks() const
{
return shouldHideCompletedTasks_;
}
} // namespace mirai