Rework mirai core

This commit is contained in:
Vyn 2024-10-11 16:26:13 +02:00
parent f885d355cd
commit 36a2fe9220
62 changed files with 27689 additions and 765 deletions

View file

@ -1,167 +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
*/
#include "BaseSource.h"
#include "Day.h"
#include "TaskItem.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <memory>
#include <ostream>
namespace mirai
{
void BaseSource::setDirty(bool shouldBeDirty)
{
isDirty_ = shouldBeDirty;
}
bool BaseSource::isDirty() const
{
return isDirty_;
}
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 &&
day->getDate().year == date.year;
});
if (dayFound == days_.end()) {
auto newDay = std::make_unique<Day>(this, DayData{.date = date});
Day *dayPtr = newDay.get();
days_.push_back(std::move(newDay));
return dayPtr;
}
return dayFound->get();
}
std::vector<std::unique_ptr<Day>> *BaseSource::days()
{
return &days_;
}
std::vector<std::unique_ptr<TaskItem>> *BaseSource::unscheduledTasks()
{
return &unscheduledTasks_;
}
void BaseSource::addDay(const DayData &dayData)
{
days_.push_back(std::make_unique<Day>(this, dayData));
setDirty(true);
}
void BaseSource::addUnscheduledTask(const TaskItemData &taskData)
{
unscheduledTasks_.push_back(std::make_unique<TaskItem>(this, nullptr, taskData));
setDirty(true);
}
TaskItem *BaseSource::getTaskById(int taskId)
{
for (auto &day : days_) {
for (auto &task : *day->tasks()) {
if (task->id() == taskId) {
return task.get();
}
}
for (auto &event : *day->events()) {
for (auto &task : *event->tasks()) {
if (task->id() == taskId) {
return task.get();
}
}
}
}
for (auto &task : *unscheduledTasks()) {
if (task->id() == taskId) {
return task.get();
}
}
return nullptr;
}
void BaseSource::deleteTask(const TaskItem &taskToDelete)
{
for (auto &day : days_) {
for (auto &task : *day->tasks()) {
if (task->id() == taskToDelete.id()) {
day->deleteTask(taskToDelete);
return;
}
}
for (auto &event : *day->events()) {
for (auto &task : *event->tasks()) {
if (task->id() == taskToDelete.id()) {
event->deleteTask(taskToDelete);
return;
}
}
}
}
unscheduledTasks_.erase(
std::remove_if(
unscheduledTasks_.begin(), unscheduledTasks_.end(),
[&](const std::unique_ptr<TaskItem> &task) {
return task->id() == taskToDelete.id();
}
),
unscheduledTasks_.end()
);
}
Event *BaseSource::getEventById(int eventId)
{
for (auto &day : days_) {
for (auto &event : *day->events()) {
if (event->id() == eventId) {
return event.get();
}
}
}
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));*/
/*setDirty(true);*/
/*}*/
/*void BaseResource::removeTask(const TaskItem *taskToRemove)*/
/*{*/
/*auto findFunction = [&](const std::unique_ptr<TaskItem> &taskInFilter) {*/
/*return taskInFilter.get() == taskToRemove;*/
/*};*/
/*auto taskToDelete = std::remove_if(tasks_.begin(), tasks_.end(), findFunction);*/
/*if (taskToDelete == tasks_.end()) {*/
/*return;*/
/*}*/
/*tasks_.erase(taskToDelete);*/
/*setDirty(true);*/
/*}*/
int BaseSource::nextId_ = 0;
} // namespace mirai

View file

@ -82,6 +82,16 @@ bool Date::operator>(const Date &other) const
return !(*this < other) && !(*this == other);
}
bool Date::operator>=(const Date &other) const
{
return (*this > other) || (*this == other);
}
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;

View file

@ -5,100 +5,56 @@
*/
#include "Day.h"
#include "BaseSource.h"
#include "Task.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <ostream>
#include <vector>
#include <stdexcept>
namespace mirai
{
Day::Day(BaseSource *source, const DayData &data) : source_(source), data_(data)
int Day::id() const
{
return dayData_.id;
}
Event *Day::createEvent(const EventData &eventData)
int Day::sourceId() const
{
auto event = std::make_unique<Event>(source_, this, eventData);
mirai::Event *eventPtr = event.get();
events_.push_back(std::move(event));
onChange();
return eventPtr;
return data_->id;
}
std::vector<std::unique_ptr<Event>> *Day::events()
Date Day::date() const
{
return &events_;
return dayData_.date;
}
const Date &Day::getDate() const
std::vector<Task> Day::tasks()
{
return data_.date;
}
TaskItem *Day::createTask(const TaskItemData &taskData)
{
auto task = std::make_unique<TaskItem>(source_, this, taskData);
mirai::TaskItem *taskPtr = task.get();
tasks_.push_back(std::move(task));
onChange();
return taskPtr;
}
void Day::deleteTask(const TaskItem &taskToDelete)
{
int id = taskToDelete.id();
tasks_.erase(
std::remove_if(
tasks_.begin(), tasks_.end(),
[&](const std::unique_ptr<TaskItem> &task) {
return task->id() == id;
}
),
tasks_.end()
);
onChange();
}
void Day::deleteEvent(const Event &eventToDelete)
{
int id = eventToDelete.id();
events_.erase(
std::remove_if(
events_.begin(), events_.end(),
[&](const std::unique_ptr<Event> &event) {
return event->id() == id;
}
),
events_.end()
);
onChange();
}
void Day::onChange()
{
source()->setDirty(true);
}
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();
auto tasksData = data_->getTasksByDayId(id());
std::vector<Task> tasks;
std::transform(
tasksData.begin(), tasksData.end(), std::back_inserter(tasks),
[&](const TaskData &taskData) {
return Task{data_, taskData};
}
}
return nullptr;
);
return tasks;
}
BaseSource *Day::source()
std::vector<Event> Day::events()
{
return source_;
auto eventsData = data_->getEventsByDate(dayData_.date);
std::vector<Event> events;
std::transform(
eventsData.begin(), eventsData.end(), std::back_inserter(events),
[&](const EventData &eventData) {
return Event{data_, eventData};
}
);
return events;
}
void Day::mergeDay(const Day &otherDay)
{
throw std::runtime_error("Not implemented yet");
}
} // namespace mirai

View file

@ -5,112 +5,67 @@
*/
#include "Event.h"
#include "BaseSource.h"
#include "TaskItem.h"
#include "utils.h"
#include "Task.h"
#include <string>
namespace mirai
{
Event::Event(BaseSource *source, Day *parent, const EventData &data)
: source_(source), day_(parent), data(data)
int Event::id() const
{
return eventData_.id;
}
void Event::setText(const std::string &text)
int Event::sourceId() const
{
this->data.description = text;
onChange();
return data_->id;
}
const std::string &Event::getText() const
std::string Event::title() const
{
return data.description;
return eventData_.title;
}
const Date &Event::getDate() const
Time Event::startsAt() const
{
return data.date;
return eventData_.startsAt;
}
const Time &Event::getStartTime() const
Time Event::endsAt() const
{
return data.startTime;
return eventData_.endsAt;
}
const Time &Event::getEndTime() const
std::vector<Task> Event::queryTasks() const
{
return data.endTime;
}
const Tags &Event::getTags() const
{
return data.tags;
}
bool Event::hasTag(const std::string &tag) const
{
return vectorUtils::contains(data.tags, tag);
}
TaskItem *Event::createTask(const TaskItemData &taskData)
{
auto task = std::make_unique<TaskItem>(source_, day_, taskData);
mirai::TaskItem *taskPtr = task.get();
tasks_.push_back(std::move(task));
onChange();
return taskPtr;
}
void Event::deleteTask(const TaskItem &taskToDelete)
{
tasks_.erase(std::remove_if(
tasks_.begin(), tasks_.end(),
[&](const std::unique_ptr<TaskItem> &task) {
return task->id() == taskToDelete.id();
auto tasksData = data_->getTasksByEventId(eventData_.id);
std::vector<Task> tasks;
std::transform(
tasksData.begin(), tasksData.end(), std::back_inserter(tasks),
[&](const TaskData &taskData) {
return Task{data_, taskData};
}
));
);
return tasks;
}
std::vector<std::unique_ptr<TaskItem>> *Event::tasks()
void Event::setTitle(const std::string &newTitle)
{
return &tasks_;
data_->updateEvent(id(), {.title = newTitle});
}
void Event::setDay(const Day &day)
{
data_->updateEvent(id(), {.dayId = day.id()});
}
void Event::setStartTime(const Time &time)
{
data.startTime = time;
onChange();
data_->updateEvent(id(), {.startsAt = time});
}
void Event::setEndTime(const Time &time)
{
data.endTime = time;
onChange();
data_->updateEvent(id(), {.endsAt = time});
}
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

@ -4,39 +4,31 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/
#include "TodoMd.h"
#include "TaskItem.h"
#include "MarkdownDataProvider.h"
#include "cpp-utils/debug.h"
#include "utils.h"
#include <charconv>
#include <fstream>
#include <iostream>
#include <memory>
#include <ranges>
#include <stdexcept>
#include <string>
#include <string_view>
#include <regex>
namespace mirai
{
Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata)
{
Tags tags;
/*Tags TodoMdFormat::extractTagsFromMetadata(std::string metadata)*/
/*{*/
/*Tags tags;*/
std::regex regex("(#([a-zA-Z0-1]+))");
std::smatch matches;
/*std::regex regex("(#([a-zA-Z0-1]+))");*/
/*std::smatch matches;*/
while (std::regex_search(metadata, matches, regex)) {
if (!vectorUtils::contains(tags, matches[2].str())) {
tags.push_back(matches[2]);
}
metadata = matches.suffix();
}
return tags;
}
/*while (std::regex_search(metadata, matches, regex)) {*/
/*if (!vectorUtils::contains(tags, matches[2].str())) {*/
/*tags.push_back(matches[2]);*/
/*}*/
/*metadata = matches.suffix();*/
/*}*/
/*return tags;*/
/*}*/
std::string TodoMdFormat::fieldWithSpace(const std::string &field)
std::string fieldWithSpace(const std::string &field)
{
if (field.length() == 0) {
return "";
@ -44,7 +36,7 @@ std::string TodoMdFormat::fieldWithSpace(const std::string &field)
return (field + " ");
}
TaskItemData TodoMdFormat::stringToTask(const std::string &str, const std::string &date)
TaskData stringToTask(const std::string &str, const std::string &date)
{
std::smatch matches;
std::regex regex("- \\[(\\s|X)\\] (([0-9]{2}:[0-9]{2})-([0-9]{2}:[0-9]{2}) > )?(.*?)( -- (.*))?"
@ -63,10 +55,9 @@ TaskItemData TodoMdFormat::stringToTask(const std::string &str, const std::strin
std::string text = stringUtils::trim(matches[5]);
TaskItemData taskItem{
.text = text,
TaskData taskItem{
.title = text,
.state = str.substr(0, 5) == "- [X]" ? DONE : TODO,
.tags = extractTagsFromMetadata(matches[7])
};
return taskItem;
}
@ -86,7 +77,7 @@ Time stringToTime(const std::string &str)
return time;
}
EventData TodoMdFormat::stringToEvent(const std::string &str, const std::string &dateString)
EventData stringToEvent(const std::string &str, const std::string &dateString)
{
std::smatch matches;
std::regex regex("> (([0-9]{2}h[0-9]{2})-([0-9]{2}h[0-9]{2}) )(.*?)( -- (.*))?");
@ -110,73 +101,71 @@ EventData TodoMdFormat::stringToEvent(const std::string &str, const std::string
}
EventData eventData{
.description = text,
.date = date.value(),
.startTime = stringToTime(matches[2]),
.endTime = stringToTime(matches[3]),
.tags = extractTagsFromMetadata(matches[6])
.title = text, .startsAt = stringToTime(matches[2]), .endsAt = stringToTime(matches[3]),
//.tags = extractTagsFromMetadata(matches[6])
};
return eventData;
}
std::string TodoMdFormat::taskToString(const TaskItem &task)
std::string taskToString(const TaskData &task)
{
std::string str = task.getText();
if (task.getTags().size() > 0) {
str += " --";
for (const std::string &tag : task.getTags()) {
str += " #" + tag;
}
}
str = (task.getState() == DONE ? "- [X] " : "- [ ] ") + str;
std::string str = task.title;
/*if (task.getTags().size() > 0) {*/
/*str += " --";*/
/*for (const std::string &tag : task.getTags()) {*/
/*str += " #" + tag;*/
/*}*/
/*}*/
str = (task.state == DONE ? "- [X] " : "- [ ] ") + str;
return str;
}
std::string TodoMdFormat::stringify(BaseSource &source)
std::string MarkdownDataProvider::toMarkdown()
{
const std::string &name = source.getName();
const std::vector<std::unique_ptr<Day>> *days = source.days();
std::string result = "# " + name + "\n\n";
const std::vector<DayData> days = getDays();
std::string result = "# " + name() + "\n\n";
std::string currentDate = "";
for (const auto &day : *days) {
auto &date = day->getDate();
for (const auto &day : days) {
auto &date = day.date;
result +=
"## " + std::format("{:02d}-{:02d}-{:02d}", date.year, date.month, date.day) + "\n\n";
for (const auto &event : *(day->events())) {
auto &start = event->getStartTime();
auto &end = event->getEndTime();
for (auto event : getEventsByDate(date)) {
auto &start = event.startsAt;
auto &end = event.endsAt;
result += "> " +
std::format(
"{:02d}h{:02d}-{:02d}h{:02d} {}", start.hour, start.minute, end.hour,
end.minute, event->getText()
end.minute, event.title
) +
"\n";
for (const auto &task : *(event->tasks())) {
result += taskToString(*task) + '\n';
for (const auto &task : getTasksByEventId(event.id)) {
result += taskToString(task) + '\n';
}
result += '\n';
}
for (const auto &task : *(day->tasks())) {
result += taskToString(*task) + '\n';
for (const auto &task : getTasksByDayId(day.id)) {
result += taskToString(task) + '\n';
}
result += '\n';
}
if (source.unscheduledTasks()->size() > 0) {
auto unscheduledTasks = getTasksWithoutDate();
if (unscheduledTasks.size() > 0) {
result += "## Unscheduled\n\n";
for (const auto &task : *(source.unscheduledTasks())) {
result += taskToString(*task) + '\n';
for (const auto &task : unscheduledTasks) {
result += taskToString(task) + '\n';
}
result += '\n';
}
return result;
};
MiraiMarkdownFormatParseResult TodoMdFormat::parse(const std::string &content)
MarkdownData MarkdownDataProvider::parseMarkdown(const std::string &content)
{
cpputils::debug::Timer readMdFormatDuration;
std::vector<TaskItem> taskItems;
// std::vector<TaskItem> taskItems;
std::string line;
std::stringstream contentStream(content);
@ -185,13 +174,13 @@ MiraiMarkdownFormatParseResult TodoMdFormat::parse(const std::string &content)
std::cerr << "Couldn't find the task list name" << std::endl;
}
std::string name = line.substr(2);
data.name = line.substr(2);
std::string currentDateString = "";
std::vector<mirai::DayData> daysData;
std::vector<mirai::TaskItemData> unscheduledTasks;
mirai::DayData *currentDay = nullptr;
mirai::EventData *currentEvent = nullptr;
std::vector<mirai::TaskData> unscheduledTasks;
std::optional<Date> currentDate = std::nullopt;
std::optional<EventData> currentEvent = std::nullopt;
cpputils::debug::Timer stringToTaskDuration;
stringToTaskDuration.reset();
@ -200,40 +189,64 @@ MiraiMarkdownFormatParseResult TodoMdFormat::parse(const std::string &content)
if (std::string_view{line.data(), 3} == "## ") {
currentDateString = line.substr(3);
if (currentDateString == "Unscheduled") {
currentDay = nullptr;
currentDate = std::nullopt;
continue;
}
auto dateOpt = mirai::stringToDate(currentDateString);
if (!dateOpt.has_value()) {
throw std::runtime_error("Malformated date");
currentDate = mirai::stringToDate(currentDateString);
if (!currentDate.has_value()) {
throw std::runtime_error("Malformated date (1)");
}
daysData.push_back({.date = dateOpt.value()});
currentDay = &daysData.back();
insertDay({.id = generateUniqueId(), .date = currentDate.value()});
} else if (line.starts_with("> ")) {
auto event = stringToEvent(line, currentDateString);
if (currentDay) {
currentDay->events.push_back(event);
currentEvent = &currentDay->events.back();
if (currentDate.has_value()) {
auto day = getDayByDate(currentDate.value());
currentEvent = insertEvent(
{.id = generateUniqueId(),
.dayId = day->id,
.title = event.title,
.startsAt = event.startsAt,
.endsAt = event.endsAt}
);
}
} else if (line.starts_with("- [ ]") || line.starts_with("- [X]")) {
stringToTaskDuration.start();
TaskItemData taskItemData = stringToTask(line, currentDateString);
TaskData taskItemData = stringToTask(line, currentDateString);
stringToTaskDuration.stop();
if (currentEvent) {
currentEvent->tasks.push_back(taskItemData);
} else if (currentDay) {
currentDay->tasks.push_back(taskItemData);
if (currentEvent.has_value()) {
insertTask({
.id = generateUniqueId(),
.title = taskItemData.title,
.state = taskItemData.state,
.dayId = std::nullopt,
.eventId = currentEvent.value().id,
});
} else if (currentDate.has_value()) {
auto day = getDayByDate(currentDate.value());
insertTask({
.id = generateUniqueId(),
.title = taskItemData.title,
.state = taskItemData.state,
.dayId = day->id,
.eventId = std::nullopt,
});
} else {
unscheduledTasks.push_back(taskItemData);
insertTask({
.id = generateUniqueId(),
.title = taskItemData.title,
.state = taskItemData.state,
.dayId = std::nullopt,
.eventId = std::nullopt,
});
}
} else if (cpputils::string::trim(line) == "") {
currentEvent = nullptr;
currentEvent = std::nullopt;
}
}
gelinesDuration.printTimeElapsed("getlinesDuration");
stringToTaskDuration.printTimeElapsed("stringToTaskDuration");
readMdFormatDuration.printTimeElapsed("Reading MD File duration");
return {.name = name, .days = daysData, .unscheduledTasks = unscheduledTasks};
return data;
}
} // namespace mirai

View file

@ -0,0 +1,251 @@
/*
* 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 "MarkdownDataProvider.h"
#include "SourceDataProvider.h"
#include "cpp-utils/debug.h"
#include "utils.h"
#include <algorithm>
#include <cassert>
#include <functional>
#include <optional>
#include <regex>
#include <vector>
namespace mirai
{
std::string MarkdownDataProvider::name() const
{
return data.name;
}
void MarkdownDataProvider::save()
{
std::ofstream file(filePath_);
if (!file.is_open()) {
throw std::runtime_error("can't create " + filePath_);
}
const std::string content = toMarkdown();
file << content;
file.close();
setDirty(false);
};
void MarkdownDataProvider::load()
{
std::ifstream file(filePath_);
if (!file.is_open()) {
return;
}
std::string content = "";
std::string line;
while (std::getline(file, line)) {
content += line + "\n";
}
file.close();
data = parseMarkdown(content);
};
TaskData MarkdownDataProvider::insertTask(const TaskData &taskData)
{
data.tasks.push_back(taskData);
setDirty(true);
return taskData;
}
void MarkdownDataProvider::updateTask(int taskId, UpdatableTaskData updateData)
{
auto taskData = ptrFindFirst(data.tasks, [&](const TaskData &task) {
return task.id == taskId;
});
assert(taskData != nullptr); // Shouldn't be possible
if (updateData.title.has_value()) {
taskData->title = updateData.title.value();
}
if (updateData.state.has_value()) {
taskData->state = updateData.state.value();
}
if (updateData.dayId.has_value()) {
taskData->dayId = updateData.dayId.value();
}
if (updateData.eventId.has_value()) {
taskData->eventId = updateData.eventId.value();
}
setDirty(true);
}
std::optional<TaskData> MarkdownDataProvider::getTaskById(int taskId)
{
return findFirst(data.tasks, [&](const TaskData &task) {
return task.id == taskId;
});
}
std::vector<TaskData> MarkdownDataProvider::getTasksByEventId(int eventId)
{
return findAll(data.tasks, [&](const TaskData &task) {
return task.eventId == eventId;
});
}
std::vector<TaskData> MarkdownDataProvider::getTasksByDayId(int dayId)
{
return findAll(data.tasks, [&](const TaskData &task) {
return task.dayId == dayId;
});
}
std::vector<TaskData> MarkdownDataProvider::getTasksWithoutDate()
{
return findAll(data.tasks, [](const TaskData &t) {
return t.dayId == std::nullopt && t.eventId == std::nullopt;
});
}
std::vector<TaskData> MarkdownDataProvider::getTasksByDate(Date date)
{
auto day = getDayByDate(date);
if (!day.has_value()) {
return {};
}
return findAll(data.tasks, [&](const TaskData &t) {
return t.dayId == day.value().id;
});
}
std::vector<TaskData> MarkdownDataProvider::getTasks()
{
return data.tasks;
}
void MarkdownDataProvider::removeTaskById(int taskId)
{
data.tasks.erase(
std::remove_if(
data.tasks.begin(), data.tasks.end(),
[&](const TaskData &task) {
return task.id == taskId;
}
),
data.tasks.end()
);
setDirty(true);
}
DayData MarkdownDataProvider::insertDay(const DayData &dayData)
{
data.days.push_back(dayData);
setDirty(true);
return dayData;
}
void MarkdownDataProvider::updateDay(int dayId, UpdatableDayData updateData)
{
auto dayData = ptrFindFirst(data.days, [&](const DayData &day) {
return day.id == dayId;
});
assert(dayData != nullptr); // Shouldn't be possible
if (updateData.date.has_value()) {
dayData->date = updateData.date.value();
}
setDirty(true);
}
void MarkdownDataProvider::removeDayById(int dayId)
{
data.days.erase(
std::remove_if(
data.days.begin(), data.days.end(),
[&](const DayData &day) {
return day.id == dayId;
}
),
data.days.end()
);
setDirty(true);
}
std::vector<DayData> MarkdownDataProvider::getDays()
{
return data.days;
}
std::optional<DayData> MarkdownDataProvider::getDayByDate(const Date &date)
{
return findFirst(data.days, [&](const DayData &day) {
return day.date == date;
});
}
EventData MarkdownDataProvider::insertEvent(const EventData &eventData)
{
data.events.push_back(eventData);
setDirty(true);
return eventData;
}
void MarkdownDataProvider::updateEvent(int eventId, UpdatableEventData updateData)
{
auto eventData = ptrFindFirst(data.events, [&](const EventData &event) {
return event.id == eventId;
});
assert(eventData != nullptr); // Shouldn't be possible
if (updateData.title.has_value()) {
eventData->title = updateData.title.value();
}
if (updateData.dayId.has_value()) {
eventData->dayId = updateData.dayId.value();
}
if (updateData.startsAt.has_value()) {
eventData->startsAt = updateData.startsAt.value();
}
if (updateData.endsAt.has_value()) {
eventData->endsAt = updateData.endsAt.value();
}
setDirty(true);
}
void MarkdownDataProvider::removeEventById(int eventId)
{
data.events.erase(
std::remove_if(
data.events.begin(), data.events.end(),
[&](const EventData &event) {
return event.id == eventId;
}
),
data.events.end()
);
setDirty(true);
}
std::optional<EventData> MarkdownDataProvider::getEventById(int eventId)
{
return findFirst(data.events, [&](const EventData &event) {
return event.id == eventId;
});
}
std::vector<EventData> MarkdownDataProvider::getEventsByDate(Date date)
{
auto day = getDayByDate(date);
if (!day.has_value()) {
return {};
}
return findAll(data.events, [&](const EventData &event) {
return event.dayId == day.value().id;
});
}
std::vector<EventData> MarkdownDataProvider::getEvents()
{
return data.events;
}
} // namespace mirai

View file

@ -6,7 +6,8 @@
#include "Mirai.h"
#include "Config.h"
#include "TaskItem.h"
#include "Source.h"
#include "SourceDataProvider.h"
#include "cpp-utils/debug.h"
#include "utils.h"
#include <algorithm>
@ -19,11 +20,13 @@
namespace mirai
{
void Mirai::loadSource(std::unique_ptr<BaseSource> &&resource)
void Mirai::loadSource(std::unique_ptr<SourceDataProvider> &&resource)
{
resource->load();
sources_.push_back(std::move(resource));
reloadTags();
SourceDataProvider *sourceDataProvider = resource.release();
sourceDataProvider->load();
sources_.push_back(
std::make_unique<Source>(SourceConstructor{.sourceDataProvider = sourceDataProvider})
);
};
void Mirai::unloadAllSources()
@ -33,63 +36,19 @@ void Mirai::unloadAllSources()
void Mirai::save()
{
for (auto &resource : sources_) {
if (resource->isDirty()) {
resource->save();
resource->setDirty(false);
for (auto &source : sources_) {
if (source->isDirty()) {
source->save();
}
}
reloadTags();
}
void Mirai::deleteTask(const TaskItem &taskItem)
{
for (auto &resource : sources_) {
resource->deleteTask(taskItem);
}
}
std::vector<std::unique_ptr<BaseSource>> &Mirai::getSources()
std::vector<std::unique_ptr<Source>> &Mirai::getSources()
{
return sources_;
}
std::optional<std::reference_wrapper<BaseSource>> Mirai::getSourceByName(const std::string &name)
{
auto resourceIterator =
std::ranges::find_if(sources_, [&](const std::unique_ptr<BaseSource> &resource) {
return resource->getName() == name;
});
if (resourceIterator == sources_.end()) {
return std::nullopt;
}
return *(resourceIterator->get());
}
const std::vector<std::string> &Mirai::getTags()
{
return tags_;
}
void Mirai::reloadTags()
{
// 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)
Source *Mirai::getSourceById(int id)
{
if (id >= sources_.size()) {
return nullptr;

147
external/mirai-core/src/Source.cpp vendored Normal file
View file

@ -0,0 +1,147 @@
/*
* 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 "Source.h"
#include "SourceDataProvider.h"
#include "utils.h"
#include <algorithm>
#include <iterator>
#include <optional>
#include <vector>
namespace mirai
{
void Source::save()
{
data->save();
}
void Source::createTask(const createTaskParams &task)
{
std::optional<int> dayId = std::nullopt;
std::optional<int> eventId = std::nullopt;
if (!task.event.has_value() && task.date.has_value()) {
auto day = data->getDayByDate(task.date.value());
if (!day.has_value() && task.date.has_value()) {
day = data->insertDay({.id = generateUniqueId(), .date = task.date.value()});
dayId = day->id;
}
}
if (task.event.has_value() && task.event->id()) {
eventId = task.event.value().id();
}
data->insertTask({
.id = generateUniqueId(),
.title = task.title,
.state = TODO,
.dayId = dayId,
.eventId = eventId,
});
};
std::vector<Day> Source::getDays()
{
auto daysData = data->getDays();
std::vector<Day> days;
std::transform(
daysData.begin(), daysData.end(), std::back_inserter(days),
[&](const DayData &dayData) {
return Day{data, dayData};
}
);
return days;
}
std::optional<Day> Source::getDayByDate(Date date)
{
auto day = data->getDayByDate(date);
if (!day.has_value()) {
return std::nullopt;
}
return Day{data, day.value()};
}
void Source::createEvent(const createEventParams &eventToCreate)
{
std::optional<int> dayId = std::nullopt;
auto day = data->getDayByDate(eventToCreate.date);
if (!day.has_value()) {
day = data->insertDay({.id = generateUniqueId(), .date = eventToCreate.date});
dayId = day->id;
}
data->insertEvent({
.id = generateUniqueId(),
.dayId = day.value().id,
.title = eventToCreate.title,
});
};
std::optional<Event> Source::getEventById(int evendId)
{
auto event = data->getEventById(evendId);
if (!event.has_value()) {
return std::nullopt;
}
return Event{data, event.value()};
}
void Source::removeEvent(const Event &event)
{
data->removeEventById(event.id());
}
void Source::removeTask(const Task &task)
{
data->removeTaskById(task.id());
}
std::optional<Task> Source::getTaskById(int taskId)
{
auto taskData = data->getTaskById(taskId);
if (!taskData.has_value()) {
return std::nullopt;
}
Task task{data, taskData.value()};
return task;
}
std::vector<Task> Source::getTasks()
{
auto tasksData = data->getTasks();
std::vector<Task> tasks;
std::transform(
tasksData.begin(), tasksData.end(), std::back_inserter(tasks),
[&](const TaskData &taskData) {
return Task{data, taskData};
}
);
return tasks;
}
std::vector<Task> Source::getUnscheduledTasks()
{
auto tasksData = data->getTasksWithoutDate();
std::vector<Task> tasks;
std::transform(
tasksData.begin(), tasksData.end(), std::back_inserter(tasks),
[&](const TaskData &taskData) {
return Task{data, taskData};
}
);
return tasks;
}
std::string Source::name() const
{
return data->name();
}
} // namespace mirai

View file

@ -1,61 +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
*/
#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(*this);
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);
}
}
for (const auto &taskData : result.unscheduledTasks) {
addUnscheduledTask(taskData);
}
setName(result.name);
};
} // namespace mirai

63
external/mirai-core/src/Task.cpp vendored Normal file
View file

@ -0,0 +1,63 @@
/*
* 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 "Task.h"
#include "Day.h"
#include "SourceDataProvider.h"
#include <optional>
#include <string>
namespace mirai
{
int Task::id() const
{
return task_.id;
}
int Task::sourceId() const
{
return data_->id;
}
std::string Task::title() const
{
return task_.title;
}
mirai::TaskState Task::state() const
{
return task_.state;
}
bool Task::checked() const
{
return task_.state == mirai::DONE;
}
void Task::setTitle(const std::string &newTitle)
{
data_->updateTask(id(), {.title = newTitle});
}
void Task::setDay(const Day &day)
{
auto emptyEventId = std::optional<std::optional<int>>(std::optional<int>(std::nullopt));
data_->updateTask(id(), {.dayId = day.id(), .eventId = emptyEventId});
}
void Task::setEvent(const Event &event)
{
auto emptyDayId = std::optional<std::optional<int>>(std::optional<int>(std::nullopt));
data_->updateTask(id(), {.dayId = emptyDayId, .eventId = event.id()});
}
void Task::setChecked(bool checked)
{
data_->updateTask(id(), {.state = checked ? DONE : TODO});
}
} // namespace mirai

View file

@ -1,87 +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
*/
#include "TaskItem.h"
#include "BaseSource.h"
#include "utils.h"
#include <iostream>
namespace mirai
{
int TaskItem::nextId = 0;
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)
{
}
void TaskItem::markAsDone()
{
data.state = DONE;
onChange();
}
void TaskItem::markAsUndone()
{
data.state = TODO;
onChange();
}
void TaskItem::setText(const std::string &text)
{
this->data.text = text;
onChange();
}
const std::string &TaskItem::getText() const
{
return data.text;
}
const TaskItemState &TaskItem::getState() const
{
return data.state;
}
const Tags &TaskItem::getTags() const
{
return data.tags;
}
bool TaskItem::hasTag(const std::string &tag) const
{
return vectorUtils::contains(data.tags, tag);
}
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

@ -1,212 +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
*/
#include "TasksView.h"
#include "Mirai.h"
#include "TaskItem.h"
#include "utils.h"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <ctime>
#include <format>
#include <iostream>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
namespace mirai
{
TasksView::TasksView(Mirai *miraiInstance) : mirai(miraiInstance)
{
if (!miraiInstance) {
throw std::runtime_error("NULL pointer passed in TasksView");
}
update();
}
FilteredDay &TasksView::operator[](int index)
{
return filteredDays.at(index);
}
std::vector<TaskItem *> &TasksView::filteredUnscheduledTasks()
{
return filteredUnscheduledTasks_;
}
size_t TasksView::count() const
{
return filteredDays.size();
}
void TasksView::update()
{
auto todayDate = std::format("{:%Y-%m-%d}", std::chrono::system_clock::now());
filteredDays.clear();
filteredUnscheduledTasks_.clear();
for (auto &file : mirai->getSources()) {
if (resourcesFilter.size() > 0 &&
!vectorUtils::contains(resourcesFilter, file->getName())) {
continue;
}
for (auto &task : *file->unscheduledTasks()) {
if (shouldHideCompletedTasks() && task->getState() == DONE) {
continue;
}
if (tagsFilter.size() > 0 && !vectorUtils::containsAll(tagsFilter, task->getTags())) {
continue;
}
filteredUnscheduledTasks_.push_back(task.get());
}
for (auto &day : *file->days()) {
FilteredDay filteredDay{.day = day.get()};
for (auto &task : *day->tasks()) {
auto taskDate = std::format(
"{:04d}-{:02d}-{:02d}", day->getDate().year, day->getDate().month,
day->getDate().day
);
if (shouldHideCompletedTasks() && task->getState() == DONE &&
taskDate < todayDate) {
continue;
}
if (tagsFilter.size() > 0 &&
!vectorUtils::containsAll(tagsFilter, task->getTags())) {
continue;
}
filteredDay.filteredTasks.push_back(task.get());
}
for (auto &event : *day->events()) {
FilteredEvent filteredEvent{.event = event.get()};
auto eventDate = std::format(
"{:04d}-{:02d}-{:02d}", day->getDate().year, day->getDate().month,
day->getDate().day
);
for (auto &task : *event->tasks()) {
if (shouldHideCompletedTasks() && task->getState() == DONE &&
eventDate < todayDate) {
continue;
}
if (tagsFilter.size() > 0 &&
!vectorUtils::containsAll(tagsFilter, task->getTags())) {
continue;
}
filteredEvent.filteredTasks.push_back(task.get());
}
if (!filteredEvent.filteredTasks.empty() || !shouldHideCompletedTasks() ||
eventDate >= todayDate) {
filteredDay.filteredEvents.push_back(filteredEvent);
}
}
if (!filteredDay.filteredEvents.empty() || !filteredDay.filteredTasks.empty()) {
auto existingDay = std::ranges::find_if(filteredDays, [&](const FilteredDay &date) {
return date.day->getDate() == filteredDay.day->getDate();
});
if (existingDay == filteredDays.end()) {
filteredDays.push_back(filteredDay);
} else {
existingDay->filteredTasks.insert(
existingDay->filteredTasks.end(), filteredDay.filteredTasks.begin(),
filteredDay.filteredTasks.end()
);
existingDay->filteredEvents.insert(
existingDay->filteredEvents.end(), filteredDay.filteredEvents.begin(),
filteredDay.filteredEvents.end()
);
}
}
}
for (auto &filteredDay : filteredDays) {
std::ranges::sort(
filteredDay.filteredEvents,
[](const FilteredEvent &t1, const FilteredEvent &t2) {
return t1.event->getStartTime().hour < t2.event->getStartTime().hour;
}
);
}
std::ranges::sort(filteredDays, [](const FilteredDay &t1, const FilteredDay &t2) {
return t1.day->getDate() < t2.day->getDate();
});
}
}
void TasksView::addTagFilter(const std::string &tag)
{
tagsFilter.push_back(tag);
update();
}
void TasksView::removeTagFilter(const std::string &tag)
{
tagsFilter
.erase(std::remove_if(tagsFilter.begin(), tagsFilter.end(), [&](const auto &tagInFilter) {
return tag == tagInFilter;
}));
update();
}
void TasksView::addSourceFilter(const std::string &fileName)
{
resourcesFilter.push_back(fileName);
update();
}
void TasksView::removeSourceFilter(const std::string &fileName)
{
resourcesFilter.erase(std::remove_if(
resourcesFilter.begin(), resourcesFilter.end(),
[&](const auto &fileInFilter) {
return fileName == fileInFilter;
}
));
update();
}
void TasksView::removeFilters()
{
tagsFilter.clear();
resourcesFilter.clear();
update();
}
const std::vector<std::string> &TasksView::getActiveTagsFilter()
{
return tagsFilter;
}
const std::vector<std::string> &TasksView::getActiveFilesFilter()
{
return resourcesFilter;
}
bool TasksView::isSourceFilterActive(const std::string &sourceName)
{
if (std::ranges::find(getActiveFilesFilter(), sourceName) == getActiveFilesFilter().end()) {
return false;
}
return true;
}
void TasksView::hideCompletedTasks(bool hide)
{
shouldHideCompletedTasks_ = hide;
}
bool TasksView::shouldHideCompletedTasks() const
{
return shouldHideCompletedTasks_;
}
} // namespace mirai

150
external/mirai-core/src/View.cpp vendored Normal file
View file

@ -0,0 +1,150 @@
/*
* 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 "View.h"
#include "utils.h"
#include <iostream>
#include <iterator>
#include <ranges>
namespace mirai
{
View::View(Mirai *mirai) : mirai_(mirai)
{
}
void View::hideCompletedTasks(bool hide)
{
shouldHideCompletedTasks_ = hide;
}
bool View::shouldHideCompletedTasks() const
{
return shouldHideCompletedTasks_;
}
std::vector<Date> View::getDates()
{
std::vector<Date> datesVector;
for (auto &date : dates) {
datesVector.push_back(date.first);
}
return datesVector;
}
std::vector<Task> View::getTasksForDate(const Date &date)
{
return dates.at(date).tasks;
}
std::vector<Event> View::getEventsForDate(const Date &date)
{
return dates.at(date).events;
}
std::vector<Task> View::getUnscheduledTasks()
{
return unscheduledTasks_;
}
void View::update()
{
dates.clear();
unscheduledTasks_.clear();
auto todayDate = Date(std::chrono::system_clock::now());
for (int sourceId : sourcesIds_) {
Source *source = mirai_->getSourceById(sourceId);
for (auto day : source->getDays()) {
// day's tasks
auto sourceTasks = day.tasks() | std::ranges::views::filter([&](const Task &task) {
if (shouldHideCompletedTasks()) {
return task.checked() == false;
}
return true;
});
if (std::ranges::distance(sourceTasks) > 0) {
if (!dates.contains(day.date())) {
dates.insert_or_assign(day.date(), DateView{});
}
auto &tasks = dates.at(day.date()).tasks;
tasks.insert(tasks.end(), sourceTasks.begin(), sourceTasks.end());
}
// day's events
auto sourceEvents = day.events() | std::ranges::views::filter([&](const Event &event) {
return (day.date() >= todayDate) ||
findFirst(event.queryTasks(), [&](const Task &task) {
return task.checked() == false;
}).has_value();
});
if (std::ranges::distance(sourceEvents) > 0) {
if (!dates.contains(day.date())) {
dates.insert_or_assign(day.date(), DateView{});
}
auto &events = dates.at(day.date()).events;
events.insert(events.end(), sourceEvents.begin(), sourceEvents.end());
}
}
// unscheduled tasks
auto tasks = source->getUnscheduledTasks();
unscheduledTasks_.insert(unscheduledTasks_.end(), tasks.begin(), tasks.end());
}
}
void View::removeSources()
{
sourcesIds_.clear();
}
void View::addSource(const Source &source)
{
sourcesIds_.push_back(source.id);
}
void View::setSources(const std::vector<Source> &sources)
{
sourcesIds_.clear();
std::transform(
sources.begin(), sources.end(), std::back_inserter(sourcesIds_),
[](const Source &source) {
return source.id;
}
);
}
void View::setAllSources()
{
const auto &sources = mirai_->getSources();
sourcesIds_.clear();
std::transform(
sources.begin(), sources.end(), std::back_inserter(sourcesIds_),
[](const std::unique_ptr<Source> &source) {
return source->id;
}
);
}
bool View::isSourceSelected(const Source &source) const
{
for (auto &sourceId : sourcesIds_) {
if (sourceId == source.id) {
return true;
}
}
return false;
}
size_t View::activeSourceCount() const
{
return sourcesIds_.size();
}
} // namespace mirai

View file

@ -6,9 +6,12 @@
#include "utils.h"
#include <cctype>
#include <functional>
#include <optional>
namespace mirai
{
bool isDate(const std::string &dateStr)
{
// std regex are really slow
@ -35,4 +38,10 @@ bool isDate(const std::string &dateStr)
}
return true;
}
int generateUniqueId()
{
static int nextId = 0;
return nextId++;
}
} // namespace mirai