2024-08-16 21:35:12 +02:00
|
|
|
/*
|
|
|
|
* 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 "UiState.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
#include "appwindow.h"
|
2024-10-11 16:26:13 +02:00
|
|
|
#include "mirai-core/Day.h"
|
2024-08-16 21:35:12 +02:00
|
|
|
#include "mirai-core/Mirai.h"
|
2024-10-11 16:26:13 +02:00
|
|
|
#include "mirai-core/SourceDataProvider.h"
|
2024-08-16 21:35:12 +02:00
|
|
|
#include "slint.h"
|
|
|
|
#include "slint_sharedvector.h"
|
|
|
|
#include "slint_string.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <bits/chrono.h>
|
|
|
|
#include <cassert>
|
|
|
|
#include <charconv>
|
|
|
|
#include <chrono>
|
|
|
|
#include <ctime>
|
|
|
|
#include <format>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
#include <ostream>
|
|
|
|
#include <ranges>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
UiState::UiState(mirai::Mirai *miraiInstance) : miraiInstance_(miraiInstance), view_(miraiInstance)
|
|
|
|
{
|
2024-10-09 17:07:17 +02:00
|
|
|
sources_ = std::make_shared<slint::VectorModel<Source>>();
|
2024-08-16 21:35:12 +02:00
|
|
|
days_ = std::make_shared<slint::VectorModel<Day>>();
|
2024-10-08 16:36:01 +02:00
|
|
|
unscheduledTasks_ = std::make_shared<slint::VectorModel<TaskData>>();
|
2024-08-16 21:35:12 +02:00
|
|
|
tags_ = std::make_shared<slint::VectorModel<slint::SharedString>>();
|
2024-10-09 17:07:17 +02:00
|
|
|
auto sourcesNames = std::make_shared<slint::MapModel<Source, slint::SharedString>>(
|
|
|
|
sources_,
|
|
|
|
[&](const Source &a) {
|
|
|
|
return a.name;
|
|
|
|
}
|
|
|
|
);
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2024-10-09 17:07:17 +02:00
|
|
|
mainWindow_->global<Backend>().set_sources(sourcesNames);
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
view_.setAllSources();
|
2024-08-16 21:35:12 +02:00
|
|
|
view_.update();
|
|
|
|
|
2024-09-02 11:52:06 +02:00
|
|
|
reloadSources();
|
2024-08-16 21:35:12 +02:00
|
|
|
reloadTasks();
|
|
|
|
|
|
|
|
setupCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Date> stringToDate(const std::string &dateStr)
|
|
|
|
{
|
|
|
|
using std::operator""sv;
|
|
|
|
|
|
|
|
auto dateSplitView =
|
|
|
|
dateStr | std::views::split("-"sv) | std::views::transform([](auto v) -> int {
|
|
|
|
int i = 0;
|
|
|
|
std::from_chars(v.data(), v.data() + v.size(), i);
|
|
|
|
return i;
|
|
|
|
});
|
|
|
|
std::vector<int> dateSplit{dateSplitView.begin(), dateSplitView.end()};
|
|
|
|
if (dateSplit.size() != 3) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto year = dateSplit[0];
|
|
|
|
auto month = dateSplit[1];
|
|
|
|
auto day = dateSplit[2];
|
|
|
|
return Date{.year = year, .month = month, .day = day};
|
|
|
|
}
|
|
|
|
|
|
|
|
void UiState::setupUtilsCallbacks()
|
|
|
|
{
|
|
|
|
mainWindow_->global<Backend>().on_formatDate([&](const Date &date) {
|
|
|
|
std::chrono::year_month_day chronoDate{
|
|
|
|
std::chrono::year(date.year),
|
|
|
|
std::chrono::month(date.month),
|
|
|
|
std::chrono::day(date.day),
|
|
|
|
};
|
2024-10-08 17:05:52 +02:00
|
|
|
return std::format("{:%B %d}", chronoDate);
|
2024-08-16 21:35:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void UiState::setupCallbacks()
|
|
|
|
{
|
|
|
|
mainWindow_->global<Backend>().on_task_clicked([&](int sourceId, int taskId) {
|
2024-09-02 11:52:06 +02:00
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
|
|
|
auto task = source->getTaskById(taskId);
|
2024-08-16 21:35:12 +02:00
|
|
|
assert(task);
|
2024-10-11 16:26:13 +02:00
|
|
|
task->setChecked(!task->checked());
|
|
|
|
// task->getState() == mirai::DONE ? task->markAsUndone() : task->markAsDone();
|
2024-08-16 21:35:12 +02:00
|
|
|
miraiInstance_->save();
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow_->global<Backend>().on_source_clicked([&](int index) {
|
2024-10-09 17:07:17 +02:00
|
|
|
// index with value -1 is equal to no selection, aka "All"
|
|
|
|
if (index == -1) {
|
2024-10-11 16:26:13 +02:00
|
|
|
view_.setAllSources();
|
2024-08-16 21:35:12 +02:00
|
|
|
} else {
|
2024-10-11 16:26:13 +02:00
|
|
|
view_.removeSources();
|
|
|
|
const mirai::Source *source = miraiInstance_->getSourceById(index);
|
|
|
|
view_.addSource(*source);
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
|
|
|
view_.update();
|
2024-10-09 17:07:17 +02:00
|
|
|
reloadSources();
|
2024-08-16 21:35:12 +02:00
|
|
|
reloadTasks();
|
|
|
|
});
|
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
/*mainWindow_->global<Backend>().on_tag_clicked([&](int index) {*/
|
|
|
|
/*const std::string &tag = miraiInstance_->getTags().at(index);*/
|
|
|
|
/*if (std::ranges::find(view_.getActiveTagsFilter(), tag) ==*/
|
|
|
|
/*view_.getActiveTagsFilter().end()) {*/
|
|
|
|
/*view_.addTagFilter(tag);*/
|
|
|
|
/*} else {*/
|
|
|
|
/*view_.removeTagFilter(tag);*/
|
|
|
|
/*}*/
|
|
|
|
/*view_.update();*/
|
|
|
|
/*reloadTasks();*/
|
|
|
|
/*});*/
|
2024-08-16 21:35:12 +02:00
|
|
|
|
|
|
|
mainWindow_->global<Backend>().on_delete_task_clicked([&](int sourceId, int taskId) {
|
2024-09-02 11:52:06 +02:00
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
|
|
|
auto task = source->getTaskById(taskId);
|
2024-08-16 21:35:12 +02:00
|
|
|
assert(task);
|
2024-10-11 16:26:13 +02:00
|
|
|
source->removeTask(*task);
|
2024-08-16 21:35:12 +02:00
|
|
|
miraiInstance_->save();
|
|
|
|
view_.update();
|
|
|
|
reloadTasks();
|
|
|
|
});
|
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
mainWindow_->global<Backend>().on_toggle_show_completed_tasks([&] {
|
|
|
|
view_.hideCompletedTasks(!view_.shouldHideCompletedTasks());
|
|
|
|
view_.update();
|
|
|
|
reloadTasks();
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow_->global<Backend>().set_sources_selected(sources_);
|
|
|
|
mainWindow_->global<Backend>().set_tags(tags_);
|
|
|
|
mainWindow_->global<Backend>().set_visible_tasks(days_);
|
|
|
|
|
|
|
|
mainWindow_->global<Backend>().on_saveTask([&](SaveTaskData newTaskData) {
|
|
|
|
auto source = miraiInstance_->getSourceById(newTaskData.sourceId);
|
2024-09-02 11:52:06 +02:00
|
|
|
assert(source);
|
2024-10-11 16:26:13 +02:00
|
|
|
auto task = source->getTaskById(newTaskData.id);
|
2024-08-16 21:35:12 +02:00
|
|
|
assert(task);
|
2024-10-11 16:26:13 +02:00
|
|
|
const mirai::Date &date = SlintDateToMiraiDate(newTaskData.date);
|
|
|
|
const auto dayOpt = source->getDayByDate(date);
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
if (!dayOpt.has_value()) {
|
|
|
|
throw std::runtime_error("Missing auto day creation implementation");
|
2024-10-08 16:36:01 +02:00
|
|
|
}
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
const auto day = dayOpt.value();
|
|
|
|
|
|
|
|
task->setTitle(std::string(newTaskData.title));
|
|
|
|
task->setDay(day);
|
|
|
|
|
|
|
|
miraiInstance_->save();
|
|
|
|
view_.update();
|
|
|
|
reloadTasks();
|
2024-08-16 21:35:12 +02:00
|
|
|
});
|
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
mainWindow_->global<Backend>().on_createTask([&](NewTaskData newTaskData) {
|
|
|
|
std::optional<mirai::Date> date = std::nullopt;
|
|
|
|
if (newTaskData.date.year != 0) {
|
|
|
|
date = SlintDateToMiraiDate(newTaskData.date);
|
|
|
|
}
|
|
|
|
auto source = miraiInstance_->getSourceById(newTaskData.sourceId);
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
std::optional<mirai::Event> event = std::nullopt;
|
|
|
|
if (newTaskData.eventId >= 0) {
|
|
|
|
event = source->getEventById(newTaskData.eventId);
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
source->createTask({
|
|
|
|
.title = std::string(newTaskData.title),
|
|
|
|
.event = event,
|
|
|
|
.date = date,
|
|
|
|
});
|
|
|
|
|
|
|
|
miraiInstance_->save();
|
|
|
|
view_.update();
|
|
|
|
reloadTasks();
|
2024-08-16 21:35:12 +02:00
|
|
|
});
|
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
mainWindow_->global<Backend>().on_delete_event_clicked([&](int sourceId, int eventId) {
|
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
|
|
|
auto event = source->getEventById(eventId);
|
|
|
|
assert(event.has_value());
|
|
|
|
source->removeEvent(event.value());
|
|
|
|
miraiInstance_->save();
|
2024-08-16 21:35:12 +02:00
|
|
|
view_.update();
|
|
|
|
reloadTasks();
|
|
|
|
});
|
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
mainWindow_->global<Backend>().on_createEvent([&](NewEventParams newEventParams) {
|
|
|
|
const Date &date = newEventParams.date;
|
|
|
|
const std::string dateStr = SlintDateToStdString(date);
|
|
|
|
auto source = miraiInstance_->getSourceById(newEventParams.sourceId);
|
|
|
|
|
|
|
|
source->createEvent({
|
|
|
|
.title = std::string(newEventParams.title),
|
|
|
|
.date = SlintDateToMiraiDate(newEventParams.date),
|
|
|
|
.startsAt = SlintTimeToMiraiTime(newEventParams.startsAt),
|
|
|
|
.endsAt = SlintTimeToMiraiTime(newEventParams.endsAt),
|
|
|
|
});
|
|
|
|
miraiInstance_->save();
|
|
|
|
view_.update();
|
|
|
|
reloadTasks();
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow_->global<Backend>().on_saveEvent([&](SaveEventParams newEventParams) {
|
|
|
|
const Date &date = newEventParams.date;
|
|
|
|
const std::string dateStr = SlintDateToStdString(date);
|
|
|
|
auto source = miraiInstance_->getSourceById(newEventParams.sourceId);
|
|
|
|
assert(source);
|
|
|
|
auto event = source->getEventById(newEventParams.id);
|
|
|
|
assert(event);
|
|
|
|
event->setTitle(std::string(newEventParams.title));
|
|
|
|
event->setStartTime(SlintTimeToMiraiTime(newEventParams.startsAt));
|
|
|
|
event->setEndTime(SlintTimeToMiraiTime(newEventParams.endsAt));
|
|
|
|
|
|
|
|
// TODO we can't change the date of the event for now.
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
miraiInstance_->save();
|
|
|
|
view_.update();
|
|
|
|
reloadTasks();
|
|
|
|
});
|
2024-08-16 21:35:12 +02:00
|
|
|
setupUtilsCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<slint::VectorModel<slint::SharedString>>
|
|
|
|
stdToSlintStringVector(const std::vector<std::string> &stdVector)
|
|
|
|
{
|
|
|
|
auto slintVector = std::make_shared<slint::VectorModel<slint::SharedString>>();
|
|
|
|
for (const auto &item : stdVector) {
|
|
|
|
slintVector->push_back(slint::SharedString(item));
|
|
|
|
}
|
|
|
|
return slintVector;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UiState::reloadTasks()
|
|
|
|
{
|
|
|
|
days_->clear();
|
2024-09-02 11:52:06 +02:00
|
|
|
if (miraiInstance_->getSources().size() == 0) {
|
2024-08-16 21:35:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto todayDate = mirai::Date(std::chrono::system_clock::now());
|
2024-10-11 16:26:13 +02:00
|
|
|
auto dates = view_.getDates();
|
2024-08-16 21:35:12 +02:00
|
|
|
auto slintDays = std::make_shared<slint::VectorModel<Day>>();
|
2024-10-11 16:26:13 +02:00
|
|
|
for (int dayIndex = 0; dayIndex < dates.size(); ++dayIndex) {
|
|
|
|
auto ¤tDate = dates.at(dayIndex);
|
2024-08-16 21:35:12 +02:00
|
|
|
auto slintEvents = std::make_shared<slint::VectorModel<Event>>();
|
|
|
|
auto slintDayTasks = std::make_shared<slint::VectorModel<TaskData>>();
|
2024-10-08 17:05:52 +02:00
|
|
|
auto relativeDaysDiff = std::chrono::duration_cast<std::chrono::days>(
|
2024-10-11 16:26:13 +02:00
|
|
|
std::chrono::sys_days(currentDate.toStdChrono()) -
|
2024-10-08 17:05:52 +02:00
|
|
|
std::chrono::sys_days(todayDate.toStdChrono())
|
|
|
|
)
|
|
|
|
.count();
|
|
|
|
slintDays->push_back(
|
2024-10-11 16:26:13 +02:00
|
|
|
Day{.date = MiraiDateToSlintDate(currentDate),
|
2024-10-08 17:05:52 +02:00
|
|
|
.events = slintEvents,
|
|
|
|
.tasks = slintDayTasks,
|
2024-10-11 16:26:13 +02:00
|
|
|
.isLate = currentDate < todayDate,
|
|
|
|
.isToday = currentDate == todayDate,
|
2024-10-08 17:05:52 +02:00
|
|
|
.relativeDaysDiff = static_cast<int>(relativeDaysDiff)}
|
|
|
|
);
|
2024-10-11 16:26:13 +02:00
|
|
|
// Day's tasks
|
|
|
|
const std::vector<mirai::Task> tasksForDate = view_.getTasksForDate(currentDate);
|
|
|
|
for (int taskIndex = 0; taskIndex < tasksForDate.size(); ++taskIndex) {
|
|
|
|
auto &task = tasksForDate.at(taskIndex);
|
2024-08-16 21:35:12 +02:00
|
|
|
slintDayTasks->push_back({
|
2024-10-11 16:26:13 +02:00
|
|
|
.sourceId = task.sourceId(),
|
|
|
|
.id = task.id(),
|
|
|
|
.title = slint::SharedString(task.title()),
|
|
|
|
.checked = task.checked(),
|
2024-08-16 21:35:12 +02:00
|
|
|
});
|
|
|
|
}
|
2024-10-11 16:26:13 +02:00
|
|
|
|
|
|
|
// Day's events
|
|
|
|
const std::vector<mirai::Event> eventsForDate = view_.getEventsForDate(currentDate);
|
|
|
|
for (int eventIndex = 0; eventIndex < eventsForDate.size(); ++eventIndex) {
|
|
|
|
auto ¤tEvent = eventsForDate.at(eventIndex);
|
2024-08-16 21:35:12 +02:00
|
|
|
auto slintTasks = std::make_shared<slint::VectorModel<TaskData>>();
|
|
|
|
slintEvents->push_back(Event{
|
2024-10-11 16:26:13 +02:00
|
|
|
.sourceId = currentEvent.sourceId(),
|
|
|
|
.id = currentEvent.id(),
|
|
|
|
.title = slint::SharedString(currentEvent.title()),
|
|
|
|
.startsAt = MiraiTimeToSlintTime(currentEvent.startsAt()),
|
|
|
|
.endsAt = MiraiTimeToSlintTime(currentEvent.endsAt()),
|
2024-08-16 21:35:12 +02:00
|
|
|
.tasks = slintTasks,
|
|
|
|
});
|
2024-10-11 16:26:13 +02:00
|
|
|
auto eventTasks = currentEvent.queryTasks();
|
|
|
|
for (int taskIndex = 0; taskIndex < eventTasks.size(); ++taskIndex) {
|
|
|
|
auto &task = eventTasks.at(taskIndex);
|
2024-08-16 21:35:12 +02:00
|
|
|
slintTasks->push_back({
|
2024-10-11 16:26:13 +02:00
|
|
|
.sourceId = task.sourceId(),
|
|
|
|
.id = task.id(),
|
|
|
|
.title = slint::SharedString(task.title()),
|
|
|
|
.checked = task.checked(),
|
2024-08-16 21:35:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
days_ = slintDays;
|
|
|
|
mainWindow_->global<Backend>().set_visible_tasks(days_);
|
2024-10-08 16:36:01 +02:00
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
auto unscheduledTasksView = view_.getUnscheduledTasks();
|
2024-10-08 16:36:01 +02:00
|
|
|
unscheduledTasks_->clear();
|
2024-10-11 16:26:13 +02:00
|
|
|
for (int taskIndex = 0; taskIndex < unscheduledTasksView.size(); ++taskIndex) {
|
|
|
|
auto &task = unscheduledTasksView.at(taskIndex);
|
2024-10-08 16:36:01 +02:00
|
|
|
unscheduledTasks_->push_back({
|
2024-10-11 16:26:13 +02:00
|
|
|
.sourceId = task.sourceId(),
|
|
|
|
.id = task.id(),
|
|
|
|
.title = slint::SharedString(task.title()),
|
|
|
|
.checked = task.checked(),
|
2024-10-08 16:36:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
mainWindow_->global<Backend>().set_unscheduled_tasks(unscheduledTasks_);
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
|
|
|
|
2024-09-02 11:52:06 +02:00
|
|
|
void UiState::reloadSources()
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
2024-09-02 11:52:06 +02:00
|
|
|
sources_->clear();
|
2024-10-11 16:26:13 +02:00
|
|
|
bool noSourceSelected = miraiInstance_->getSources().size() == view_.activeSourceCount();
|
2024-09-02 11:52:06 +02:00
|
|
|
for (const auto &source : miraiInstance_->getSources()) {
|
2024-10-11 16:26:13 +02:00
|
|
|
bool isSourceSelected = view_.isSourceSelected(*source);
|
2024-10-09 17:07:17 +02:00
|
|
|
sources_->push_back(
|
2024-10-11 16:26:13 +02:00
|
|
|
{.name = slint::SharedString(source->name()),
|
|
|
|
.selected = isSourceSelected && !noSourceSelected}
|
2024-10-09 17:07:17 +02:00
|
|
|
);
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
2024-10-09 17:07:17 +02:00
|
|
|
mainWindow_->global<Backend>().set_no_source_selected(noSourceSelected);
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UiState::run()
|
|
|
|
{
|
|
|
|
mainWindow_->run();
|
|
|
|
}
|