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

@ -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