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
|
|
|
|
*/
|
|
|
|
|
2024-10-29 15:02:46 +01:00
|
|
|
#include "AppWindow.h"
|
2024-11-04 14:45:27 +01:00
|
|
|
#include "../../SeleniteSetup.h"
|
|
|
|
#include "../../shared/Utils.h"
|
2024-10-29 15:02:46 +01:00
|
|
|
#include "mirai-core/DateTime.h"
|
2025-06-29 20:07:11 +02:00
|
|
|
#include "mirai-core/Event.h"
|
2024-10-29 15:02:46 +01:00
|
|
|
#include "mirai-core/MarkdownDataProvider.h"
|
2024-08-16 21:35:12 +02:00
|
|
|
#include "mirai-core/Mirai.h"
|
2025-06-29 20:07:11 +02:00
|
|
|
#include "mirai-core/Source.h"
|
2024-11-04 14:45:27 +01:00
|
|
|
#include "selenite/palette.h"
|
2025-06-26 10:12:23 +02:00
|
|
|
#include "slint_color.h"
|
2025-06-29 20:07:11 +02:00
|
|
|
#include "slint_models.h"
|
2024-08-16 21:35:12 +02:00
|
|
|
#include "slint_string.h"
|
2024-11-04 14:45:27 +01:00
|
|
|
#include "ui.h"
|
2025-06-29 20:07:11 +02:00
|
|
|
#include <algorithm>
|
2024-08-16 21:35:12 +02:00
|
|
|
#include <bits/chrono.h>
|
|
|
|
#include <cassert>
|
|
|
|
#include <chrono>
|
2024-10-27 22:11:11 +01:00
|
|
|
#include <cstdlib>
|
2024-08-16 21:35:12 +02:00
|
|
|
#include <iostream>
|
2025-06-29 20:07:11 +02:00
|
|
|
#include <iterator>
|
2024-08-16 21:35:12 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
2024-11-03 18:27:36 +01:00
|
|
|
#include <print>
|
2025-06-29 20:07:11 +02:00
|
|
|
#include <ranges>
|
2024-08-16 21:35:12 +02:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
2025-06-29 20:07:11 +02:00
|
|
|
AppWindow::AppWindow(mirai::Mirai *miraiInstance) : miraiInstance_(miraiInstance)
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
2025-06-29 20:07:11 +02:00
|
|
|
_sidebar_view = ui::SidebarView();
|
|
|
|
_calendar_view = ui::CalendarView();
|
|
|
|
_tasks_view = ui::TasksView();
|
|
|
|
/*auto sourcesNames = std::make_shared<slint::MapModel<ui::Source, slint::SharedString>>(*/
|
|
|
|
/*sources_, [&](const ui::Source &a) {*/
|
|
|
|
/*return a.name;*/
|
|
|
|
/*}*/
|
|
|
|
/*);*/
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2024-10-29 15:02:46 +01:00
|
|
|
const auto palettePath = std::string(getenv("HOME")) + "/.config/evalyte/theme.json";
|
|
|
|
const auto palette = selenite::parseJson(palettePath);
|
2024-10-27 22:11:11 +01:00
|
|
|
if (palette.has_value()) {
|
2024-11-03 18:27:36 +01:00
|
|
|
std::println(std::cerr, "Warning, no {} found", palettePath);
|
2024-10-29 15:02:46 +01:00
|
|
|
setSelenitePalette(mainWindow_->global<ui::Palette>(), palette.value());
|
2024-10-27 22:11:11 +01:00
|
|
|
}
|
|
|
|
|
2024-11-04 14:45:27 +01:00
|
|
|
bindSlintUtils(mainWindow_->global<ui::Utils>());
|
2024-10-29 15:02:46 +01:00
|
|
|
|
2025-06-29 20:07:11 +02:00
|
|
|
models().set_sidebar_view(_sidebar_view);
|
|
|
|
models().set_calendar_view(_calendar_view);
|
|
|
|
models().set_tasks_view(_tasks_view);
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2025-06-29 20:07:11 +02:00
|
|
|
show_all_sources();
|
|
|
|
update_views();
|
|
|
|
/*view_.setAllSources();*/
|
|
|
|
/*view_.update();*/
|
2024-08-16 21:35:12 +02:00
|
|
|
|
2024-09-02 11:52:06 +02:00
|
|
|
reloadSources();
|
2024-08-16 21:35:12 +02:00
|
|
|
reloadTasks();
|
|
|
|
|
|
|
|
setupCallbacks();
|
|
|
|
}
|
|
|
|
|
2024-10-15 16:51:05 +02:00
|
|
|
std::optional<ui::Date> stringToDate(const std::string &dateStr)
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
|
|
|
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];
|
2024-10-15 16:51:05 +02:00
|
|
|
return ui::Date{.year = year, .month = month, .day = day};
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
|
|
|
|
2024-11-04 14:45:27 +01:00
|
|
|
void AppWindow::setupCallbacks()
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
2024-11-05 16:55:13 +01:00
|
|
|
models().on_get_source_id_from_name([&](slint::SharedString name) {
|
|
|
|
auto source = miraiInstance_->getSourceByName(std::string(name));
|
|
|
|
assert(source);
|
|
|
|
return source->id;
|
|
|
|
});
|
2025-02-02 11:42:28 +01:00
|
|
|
|
|
|
|
models().on_get_source_name_from_id([&](int sourceId) {
|
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
|
|
|
return slint::SharedString(source->name());
|
|
|
|
});
|
|
|
|
|
2025-06-26 10:12:23 +02:00
|
|
|
models().on_get_source_color_from_id_as_hex([&](int sourceId) {
|
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
|
|
|
return slint::SharedString(source->color());
|
|
|
|
});
|
|
|
|
|
|
|
|
models().on_get_source_color_from_id_as_color([&](int sourceId) {
|
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
|
|
|
auto color = selenite::hexStringToColor(source->color());
|
|
|
|
return slint::Color::from_rgb_uint8(color.r, color.g, color.b);
|
|
|
|
});
|
|
|
|
|
2025-06-24 08:35:38 +02:00
|
|
|
models().on_get_source_path_from_id([&](int sourceId) {
|
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
2025-06-27 10:55:56 +02:00
|
|
|
mirai::markdown_data_provider *sourceProvider =
|
|
|
|
dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());
|
2025-06-24 08:35:38 +02:00
|
|
|
return slint::SharedString(sourceProvider->path());
|
|
|
|
});
|
|
|
|
|
2025-06-29 20:07:11 +02:00
|
|
|
miraiInstance_->onSourceAdded([&](mirai::source *source) { refreshModels(); });
|
|
|
|
miraiInstance_->onSourceEdited([&](mirai::source *source) { refreshModels(); });
|
|
|
|
miraiInstance_->onSourceDeleted([&](int id) { refreshModels(); });
|
2024-11-04 14:45:27 +01:00
|
|
|
|
|
|
|
actions().on_task_clicked([&](int sourceId, int taskId) {
|
2024-09-02 11:52:06 +02:00
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
2025-06-27 10:55:56 +02:00
|
|
|
auto task = source->get_task_by_id(taskId);
|
2024-08-16 21:35:12 +02:00
|
|
|
assert(task);
|
2025-06-29 20:07:11 +02:00
|
|
|
task->set_checked(!task->checked());
|
2024-08-16 21:35:12 +02:00
|
|
|
miraiInstance_->save();
|
2025-06-29 20:07:11 +02:00
|
|
|
update_views();
|
|
|
|
// view_.update();
|
2024-10-16 11:04:30 +02:00
|
|
|
reloadTasks();
|
2024-08-16 21:35:12 +02:00
|
|
|
});
|
|
|
|
|
2024-11-04 14:45:27 +01:00
|
|
|
actions().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) {
|
2025-06-29 20:07:11 +02:00
|
|
|
show_all_sources();
|
|
|
|
// view_.setAllSources();
|
2024-08-16 21:35:12 +02:00
|
|
|
} else {
|
2025-06-29 20:07:11 +02:00
|
|
|
hide_all_sources();
|
|
|
|
// view_.removeSources();
|
2025-06-27 10:55:56 +02:00
|
|
|
const mirai::source *source = miraiInstance_->getSourceById(index);
|
2025-06-29 20:07:11 +02:00
|
|
|
show_source(*source);
|
|
|
|
// view_.addSource(*source);
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
2025-06-29 20:07:11 +02:00
|
|
|
// models().set_default_source_index(index == -1 ? 0 : index);
|
|
|
|
update_views();
|
|
|
|
// view_.update();
|
2024-10-09 17:07:17 +02:00
|
|
|
reloadSources();
|
2024-08-16 21:35:12 +02:00
|
|
|
reloadTasks();
|
|
|
|
});
|
|
|
|
|
2025-06-24 09:31:34 +02:00
|
|
|
actions().on_add_source([&](slint::SharedString name, slint::SharedString path) {
|
2025-06-27 10:55:56 +02:00
|
|
|
std::unique_ptr<mirai::data_provider> file =
|
|
|
|
std::make_unique<mirai::markdown_data_provider>(std::string(path));
|
2025-06-24 09:31:34 +02:00
|
|
|
miraiInstance_->addSource(std::string(name), "FileSystemMarkdown", std::move(file));
|
|
|
|
});
|
|
|
|
|
2025-06-26 10:12:23 +02:00
|
|
|
actions().on_edit_source([&](int sourceId, slint::SharedString name, slint::SharedString color,
|
|
|
|
slint::SharedString path) {
|
|
|
|
miraiInstance_->editSource(
|
|
|
|
sourceId, std::string(name), std::string(color), std::string(path)
|
|
|
|
);
|
2025-06-24 08:35:38 +02:00
|
|
|
});
|
|
|
|
|
2024-11-04 14:45:27 +01:00
|
|
|
actions().on_delete_task_clicked([&](int sourceId, int taskId) {
|
2024-09-02 11:52:06 +02:00
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
2025-06-27 10:55:56 +02:00
|
|
|
auto task = source->get_task_by_id(taskId);
|
2024-08-16 21:35:12 +02:00
|
|
|
assert(task);
|
2025-06-27 10:55:56 +02:00
|
|
|
source->remove_task(*task);
|
2024-08-16 21:35:12 +02:00
|
|
|
miraiInstance_->save();
|
2025-06-29 20:07:11 +02:00
|
|
|
// view_.update();
|
|
|
|
update_views();
|
2024-08-16 21:35:12 +02:00
|
|
|
reloadTasks();
|
|
|
|
});
|
|
|
|
|
2024-11-04 14:45:27 +01:00
|
|
|
actions().on_toggle_show_completed_tasks([&] {
|
2025-06-29 20:07:11 +02:00
|
|
|
// view_.hideCompletedTasks(!view_.shouldHideCompletedTasks());
|
|
|
|
if (should_hide_completed_tasks()) {
|
|
|
|
show_completed_tasks();
|
|
|
|
} else {
|
|
|
|
hide_completed_tasks();
|
|
|
|
}
|
|
|
|
// view_.update();
|
|
|
|
update_views();
|
2024-10-11 16:26:13 +02:00
|
|
|
reloadTasks();
|
|
|
|
});
|
|
|
|
|
2024-11-04 14:45:27 +01:00
|
|
|
actions().on_save_task([&](ui::SaveTaskData newTaskData) {
|
2024-10-11 16:26:13 +02:00
|
|
|
auto source = miraiInstance_->getSourceById(newTaskData.sourceId);
|
2024-09-02 11:52:06 +02:00
|
|
|
assert(source);
|
2025-06-27 10:55:56 +02:00
|
|
|
auto task = source->get_task_by_id(newTaskData.id);
|
2024-10-15 11:55:39 +02:00
|
|
|
assert(task.has_value());
|
2024-10-11 16:26:13 +02:00
|
|
|
const mirai::Date &date = SlintDateToMiraiDate(newTaskData.date);
|
2025-06-29 20:07:11 +02:00
|
|
|
// const auto dayOpt = source->get_day_by_date(date);
|
|
|
|
task->set_title(std::string(newTaskData.title));
|
2024-10-17 14:38:55 +02:00
|
|
|
|
2024-10-11 16:26:13 +02:00
|
|
|
miraiInstance_->save();
|
2025-06-29 20:07:11 +02:00
|
|
|
// view_.update();
|
|
|
|
update_views();
|
2024-10-11 16:26:13 +02:00
|
|
|
reloadTasks();
|
2024-08-16 21:35:12 +02:00
|
|
|
});
|
|
|
|
|
2024-11-04 14:45:27 +01:00
|
|
|
actions().on_create_task([&](ui::NewTaskData newTaskData) {
|
2024-10-11 16:26:13 +02:00
|
|
|
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) {
|
2025-06-27 10:55:56 +02:00
|
|
|
event = source->get_event_by_id(newTaskData.eventId);
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
|
|
|
|
2025-06-27 10:55:56 +02:00
|
|
|
source->create_task({
|
2024-10-11 16:26:13 +02:00
|
|
|
.title = std::string(newTaskData.title),
|
2025-06-29 20:07:11 +02:00
|
|
|
.due_date = date,
|
2024-10-11 16:26:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
miraiInstance_->save();
|
2025-06-29 20:07:11 +02:00
|
|
|
// view_.update();
|
|
|
|
update_views();
|
2024-10-11 16:26:13 +02:00
|
|
|
reloadTasks();
|
2024-08-16 21:35:12 +02:00
|
|
|
});
|
|
|
|
|
2025-06-17 17:03:07 +02:00
|
|
|
actions().on_delete_event([&](int sourceId, int eventId) {
|
2024-10-11 16:26:13 +02:00
|
|
|
auto source = miraiInstance_->getSourceById(sourceId);
|
|
|
|
assert(source);
|
2025-06-27 10:55:56 +02:00
|
|
|
auto event = source->get_event_by_id(eventId);
|
2024-10-11 16:26:13 +02:00
|
|
|
assert(event.has_value());
|
2025-06-27 10:55:56 +02:00
|
|
|
source->remove_event(event.value());
|
2024-10-11 16:26:13 +02:00
|
|
|
miraiInstance_->save();
|
2025-06-29 20:07:11 +02:00
|
|
|
// view_.update();
|
|
|
|
update_views();
|
2024-08-16 21:35:12 +02:00
|
|
|
reloadTasks();
|
|
|
|
});
|
|
|
|
|
2025-06-17 17:03:07 +02:00
|
|
|
actions().on_create_event([&](ui::CreateEventParams newEventParams) {
|
2024-10-15 16:51:05 +02:00
|
|
|
const ui::Date &date = newEventParams.date;
|
2024-10-11 16:26:13 +02:00
|
|
|
const std::string dateStr = SlintDateToStdString(date);
|
2025-06-29 20:07:11 +02:00
|
|
|
// const auto sourceId = models().get_default_source_index();
|
|
|
|
// auto source = miraiInstance_->getSourceById(sourceId == -1 ? 0 : sourceId);
|
|
|
|
|
|
|
|
/* source->create_event({*/
|
|
|
|
/*.title = std::string(newEventParams.title),*/
|
|
|
|
/*.date = SlintDateToMiraiDate(newEventParams.date),*/
|
|
|
|
/*.starts_at = SlintTimeToMiraiTime(newEventParams.startsAt),*/
|
|
|
|
/*.ends_at = SlintTimeToMiraiTime(newEventParams.endsAt),*/
|
|
|
|
/*});*/
|
2024-10-11 16:26:13 +02:00
|
|
|
miraiInstance_->save();
|
2025-06-29 20:07:11 +02:00
|
|
|
// view_.update();
|
|
|
|
update_views();
|
2024-10-11 16:26:13 +02:00
|
|
|
reloadTasks();
|
|
|
|
});
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-11-04 14:45:27 +01:00
|
|
|
void AppWindow::reloadTasks()
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
2025-06-29 20:07:11 +02:00
|
|
|
/* days_->clear();*/
|
|
|
|
/*if (miraiInstance_->getSources().size() == 0) {*/
|
|
|
|
/*return;*/
|
|
|
|
/*}*/
|
|
|
|
/*auto todayDate = mirai::Date(std::chrono::system_clock::now());*/
|
|
|
|
/*auto dates = view_.getDates();*/
|
|
|
|
/*auto slintDays = std::make_shared<slint::VectorModel<ui::Day>>();*/
|
|
|
|
/*for (int dayIndex = 0; dayIndex < dates.size(); ++dayIndex) {*/
|
|
|
|
/*auto ¤tDate = dates.at(dayIndex);*/
|
|
|
|
/*auto slintEvents = std::make_shared<slint::VectorModel<ui::Event>>();*/
|
|
|
|
/*auto slintDayTasks = std::make_shared<slint::VectorModel<ui::TaskData>>();*/
|
|
|
|
/*auto relativeDaysDiff = std::chrono::duration_cast<std::chrono::days>(*/
|
|
|
|
/*std::chrono::sys_days(currentDate.toStdChrono()) -*/
|
|
|
|
/*std::chrono::sys_days(todayDate.toStdChrono())*/
|
|
|
|
/*)*/
|
|
|
|
/*.count();*/
|
|
|
|
/*slintDays->push_back(*/
|
|
|
|
/*ui::Day{*/
|
|
|
|
/*.date = MiraiDateToSlintDate(currentDate),*/
|
|
|
|
/*.events = slintEvents,*/
|
|
|
|
/*.tasks = slintDayTasks,*/
|
|
|
|
/*.isLate = currentDate < todayDate,*/
|
|
|
|
/*.isToday = currentDate == todayDate,*/
|
|
|
|
/*.relativeDaysDiff = static_cast<int>(relativeDaysDiff)*/
|
|
|
|
/*}*/
|
|
|
|
/*);*/
|
|
|
|
/*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);*/
|
|
|
|
/*slintDayTasks->push_back({*/
|
|
|
|
/*.sourceId = task.sourceId(),*/
|
|
|
|
/*.eventId = -1,*/
|
|
|
|
/*.id = task.id(),*/
|
|
|
|
/*.title = slint::SharedString(task.title()),*/
|
|
|
|
/*.checked = task.checked(),*/
|
|
|
|
/*});*/
|
|
|
|
/*}*/
|
|
|
|
|
|
|
|
/*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);*/
|
|
|
|
/*auto slintTasks = std::make_shared<slint::VectorModel<ui::TaskData>>();*/
|
|
|
|
/*slintEvents->push_back(*/
|
|
|
|
/*ui::Event{*/
|
|
|
|
/*.sourceId = currentEvent.source_id(),*/
|
|
|
|
/*.id = currentEvent.id(),*/
|
|
|
|
/*.title = slint::SharedString(currentEvent.title()),*/
|
|
|
|
/*.startsAt = MiraiTimeToSlintTime(currentEvent.startsAt()),*/
|
|
|
|
/*.endsAt = MiraiTimeToSlintTime(currentEvent.endsAt()),*/
|
|
|
|
/*.tasks = slintTasks,*/
|
|
|
|
/*}*/
|
|
|
|
/*);*/
|
|
|
|
/*auto eventTasks = currentEvent.queryTasks();*/
|
|
|
|
/*for (int taskIndex = 0; taskIndex < eventTasks.size(); ++taskIndex) {*/
|
|
|
|
/*auto &task = eventTasks.at(taskIndex);*/
|
|
|
|
/*slintTasks->push_back({*/
|
|
|
|
/*.sourceId = task.sourceId(),*/
|
|
|
|
/*.eventId = currentEvent.id(),*/
|
|
|
|
/*.id = task.id(),*/
|
|
|
|
/*.title = slint::SharedString(task.title()),*/
|
|
|
|
/*.checked = task.checked(),*/
|
|
|
|
/*});*/
|
|
|
|
/*}*/
|
|
|
|
/*}*/
|
|
|
|
/*}*/
|
|
|
|
/*days_ = slintDays;*/
|
|
|
|
/*models().set_days(days_);*/
|
|
|
|
|
|
|
|
/*auto unscheduledTasksView = view_.getUnscheduledTasks();*/
|
|
|
|
/*unscheduledTasks_->clear();*/
|
|
|
|
/*for (int taskIndex = 0; taskIndex < unscheduledTasksView.size(); ++taskIndex) {*/
|
|
|
|
/*auto &task = unscheduledTasksView.at(taskIndex);*/
|
|
|
|
/*const auto &source = miraiInstance_->getSourceById(task.sourceId());*/
|
|
|
|
/*std::println("request name for source id {} : {}", task.sourceId(), source->name());*/
|
|
|
|
/*unscheduledTasks_->push_back({*/
|
|
|
|
/*.sourceId = task.sourceId(),*/
|
|
|
|
/*.eventId = -1,*/
|
|
|
|
/*.id = task.id(),*/
|
|
|
|
/*.title = slint::SharedString(task.title()),*/
|
|
|
|
/*.checked = task.checked(),*/
|
|
|
|
/*});*/
|
|
|
|
/*}*/
|
|
|
|
/*models().set_unscheduled_tasks(unscheduledTasks_);*/
|
|
|
|
|
|
|
|
/*calendar_->clear();*/
|
|
|
|
/*for (int dayIndex = 0; dayIndex < 7; ++dayIndex) {*/
|
|
|
|
/*std::chrono::year_month_day nextDate =*/
|
|
|
|
/*std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now()) +*/
|
|
|
|
/*std::chrono::days{dayIndex};*/
|
|
|
|
/*auto currentDate = mirai::Date{nextDate};*/
|
|
|
|
/*auto events = view_.getEventsForDate(currentDate);*/
|
|
|
|
/*auto slintEvents = std::make_shared<slint::VectorModel<ui::CalendarDayEvent>>();*/
|
|
|
|
/*auto relativeDaysDiff = std::chrono::duration_cast<std::chrono::days>(*/
|
|
|
|
/*std::chrono::sys_days(currentDate.toStdChrono()) -*/
|
|
|
|
/*std::chrono::sys_days(todayDate.toStdChrono())*/
|
|
|
|
/*)*/
|
|
|
|
/*.count();*/
|
|
|
|
/*if (relativeDaysDiff < 0 || relativeDaysDiff >= 7) {*/
|
|
|
|
/*continue;*/
|
|
|
|
/*}*/
|
|
|
|
/*const std::vector<mirai::Event> eventsForDate = view_.getEventsForDate(currentDate);*/
|
|
|
|
/*for (int eventIndex = 0; eventIndex < eventsForDate.size(); ++eventIndex) {*/
|
|
|
|
/*auto ¤tEvent = eventsForDate.at(eventIndex);*/
|
|
|
|
/*slintEvents->push_back(*/
|
|
|
|
/*ui::CalendarDayEvent{*/
|
|
|
|
/*.sourceId = currentEvent.source_id(),*/
|
|
|
|
/*.id = currentEvent.id(),*/
|
|
|
|
/*.title = slint::SharedString(currentEvent.title()),*/
|
|
|
|
/*.startsAt = MiraiTimeToSlintTime(currentEvent.startsAt()),*/
|
|
|
|
/*.endsAt = MiraiTimeToSlintTime(currentEvent.endsAt()),*/
|
|
|
|
/*}*/
|
|
|
|
/*);*/
|
|
|
|
/*}*/
|
|
|
|
|
|
|
|
/*auto calendarDay = ui::CalendarDay{*/
|
|
|
|
/*.events = slintEvents,*/
|
|
|
|
/*.date = MiraiDateToSlintDate(currentDate),*/
|
|
|
|
/*.header = slint::SharedString(*/
|
|
|
|
/*capitalize(formatDateRelative(MiraiDateToSlintDate(currentDate)))*/
|
|
|
|
/*)*/
|
|
|
|
/*};*/
|
|
|
|
/*calendar_->push_back(calendarDay);*/
|
|
|
|
/*}*/
|
|
|
|
}
|
2024-10-11 16:26:13 +02:00
|
|
|
|
2025-06-29 20:07:11 +02:00
|
|
|
void AppWindow::reloadSources()
|
|
|
|
{
|
|
|
|
/*sources_->clear();*/
|
|
|
|
/*bool noSourceSelected = miraiInstance_->getSources().size() == view_.activeSourceCount();*/
|
|
|
|
/*for (const auto &source : miraiInstance_->getSources()) {*/
|
|
|
|
/*bool isSourceSelected = view_.isSourceSelected(*source);*/
|
|
|
|
/*mirai::markdown_data_provider *sourceProvider =*/
|
|
|
|
/*dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());*/
|
|
|
|
/*sources_->push_back(*/
|
|
|
|
/*{.id = source->id,*/
|
|
|
|
/*.name = slint::SharedString(source->name()),*/
|
|
|
|
/*.selected = isSourceSelected && !noSourceSelected,*/
|
|
|
|
/*.path = slint::SharedString(sourceProvider->path())}*/
|
|
|
|
/*);*/
|
|
|
|
/*}*/
|
|
|
|
/*models().set_no_source_selected(noSourceSelected);*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::refreshModels()
|
|
|
|
{
|
|
|
|
show_all_sources();
|
|
|
|
update_views();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::update_views()
|
|
|
|
{
|
|
|
|
update_sidebar_view();
|
|
|
|
update_calendar_view();
|
|
|
|
update_tasks_view();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::update_sidebar_view()
|
|
|
|
{
|
|
|
|
const auto &sources = miraiInstance_->getSources();
|
|
|
|
|
|
|
|
auto new_sources = std::make_shared<slint::VectorModel<ui::SidebarViewSources>>();
|
|
|
|
std::println("sources");
|
|
|
|
for (auto &source : sources) {
|
|
|
|
std::println("source: {}", source->name());
|
|
|
|
mirai::markdown_data_provider *sourceProvider =
|
|
|
|
dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());
|
|
|
|
|
|
|
|
new_sources->push_back(
|
|
|
|
{.id = source->id, .name = slint::SharedString(source->name()), .active = false}
|
|
|
|
);
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
2025-06-29 20:07:11 +02:00
|
|
|
|
|
|
|
_sidebar_view.sources = new_sources;
|
|
|
|
_sidebar_view.all_active = true; // TESTING
|
|
|
|
_sidebar_view.active_view = ui::SidebarViewActiveTab::Calendar;
|
|
|
|
models().set_sidebar_view(_sidebar_view);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<mirai::Event>
|
|
|
|
get_all_events_from_sources(std::vector<std::unique_ptr<mirai::source>> &sources)
|
|
|
|
{
|
|
|
|
std::vector<mirai::Event> all_events;
|
|
|
|
for (auto &source : sources) {
|
|
|
|
const auto &events = source->get_events();
|
|
|
|
all_events.insert(all_events.end(), events.begin(), events.end());
|
2024-10-08 16:36:01 +02:00
|
|
|
}
|
2025-06-29 20:07:11 +02:00
|
|
|
return all_events;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<mirai::Event>
|
|
|
|
get_events_for_date(const std::vector<mirai::Event> &events, const mirai::Date &date)
|
|
|
|
{
|
|
|
|
auto filtered_events = events | std::ranges::views::filter([&](const mirai::Event &event) {
|
|
|
|
return event.date() == date;
|
|
|
|
});
|
|
|
|
return std::ranges::to<std::vector>(filtered_events);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::update_calendar_view()
|
|
|
|
{
|
|
|
|
auto today = mirai::Date(std::chrono::system_clock::now());
|
|
|
|
auto new_slint_dates = std::make_shared<slint::VectorModel<ui::CalendarViewDate>>();
|
|
|
|
|
|
|
|
auto &sources = miraiInstance_->getSources();
|
|
|
|
auto all_events = get_all_events_from_sources(sources);
|
|
|
|
|
|
|
|
for (int day_index = 0; day_index < 7; ++day_index) {
|
|
|
|
std::chrono::year_month_day next_date =
|
|
|
|
std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now())
|
|
|
|
+ std::chrono::days{day_index};
|
|
|
|
auto current_date = mirai::Date{next_date};
|
|
|
|
auto new_slint_events = std::make_shared<slint::VectorModel<ui::CalendarViewEvent>>();
|
|
|
|
auto relative_days_diff = std::chrono::duration_cast<std::chrono::days>(
|
|
|
|
std::chrono::sys_days(current_date.toStdChrono())
|
|
|
|
- std::chrono::sys_days(today.toStdChrono())
|
2024-10-29 15:02:46 +01:00
|
|
|
)
|
2025-06-29 20:07:11 +02:00
|
|
|
.count();
|
|
|
|
if (relative_days_diff < 0 || relative_days_diff >= 7) {
|
2024-10-29 15:02:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
2025-06-29 20:07:11 +02:00
|
|
|
auto events_for_date = get_events_for_date(all_events, current_date);
|
|
|
|
for (int event_index = 0; event_index < events_for_date.size(); ++event_index) {
|
|
|
|
auto ¤t_event = events_for_date.at(event_index);
|
|
|
|
new_slint_events->push_back(
|
|
|
|
ui::CalendarViewEvent{
|
|
|
|
.source_id = current_event.source_id(),
|
|
|
|
.id = current_event.id(),
|
|
|
|
.title = slint::SharedString(current_event.title()),
|
|
|
|
.starts_at = MiraiTimeToSlintTime(current_event.starts_at()),
|
|
|
|
.ends_at = MiraiTimeToSlintTime(current_event.ends_at()),
|
2025-06-17 17:03:07 +02:00
|
|
|
}
|
|
|
|
);
|
2024-10-29 15:02:46 +01:00
|
|
|
}
|
|
|
|
|
2025-06-29 20:07:11 +02:00
|
|
|
auto new_slint_date = ui::CalendarViewDate{
|
|
|
|
.events = new_slint_events,
|
|
|
|
.date = MiraiDateToSlintDate(current_date),
|
2025-06-17 17:03:07 +02:00
|
|
|
.header = slint::SharedString(
|
2025-06-29 20:07:11 +02:00
|
|
|
capitalize(formatDateRelative(MiraiDateToSlintDate(current_date)))
|
2025-06-17 17:03:07 +02:00
|
|
|
)
|
2024-10-29 15:02:46 +01:00
|
|
|
};
|
2025-06-29 20:07:11 +02:00
|
|
|
new_slint_dates->push_back(new_slint_date);
|
2024-10-29 15:02:46 +01:00
|
|
|
}
|
2025-06-29 20:07:11 +02:00
|
|
|
_calendar_view.dates = new_slint_dates;
|
|
|
|
models().set_calendar_view(_calendar_view);
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
|
|
|
|
2025-06-29 20:07:11 +02:00
|
|
|
std::vector<mirai::Task>
|
|
|
|
get_all_tasks_from_sources(std::vector<std::unique_ptr<mirai::source>> &sources)
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
2025-06-29 20:07:11 +02:00
|
|
|
std::vector<mirai::Task> all_tasks;
|
|
|
|
for (auto &source : sources) {
|
|
|
|
const auto &tasks = source->get_tasks();
|
|
|
|
all_tasks.insert(all_tasks.end(), tasks.begin(), tasks.end());
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
2025-06-29 20:07:11 +02:00
|
|
|
std::sort(all_tasks.begin(), all_tasks.end(), [](const mirai::Task &t1, const mirai::Task &t2) {
|
|
|
|
if (!t1.has_due_date() && !t2.has_due_date()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (t1.has_due_date() && !t2.has_due_date()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!t1.has_due_date() && t2.has_due_date()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return t1.due_date() < t2.due_date();
|
|
|
|
});
|
|
|
|
return all_tasks;
|
2024-08-16 21:35:12 +02:00
|
|
|
}
|
|
|
|
|
2025-06-29 20:07:11 +02:00
|
|
|
struct tasks_source_group {
|
|
|
|
int id;
|
|
|
|
std::vector<mirai::Task> tasks;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tasks_date_group {
|
|
|
|
std::optional<mirai::Date> date;
|
|
|
|
std::vector<tasks_source_group> sources;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<tasks_date_group> group_tasks_by_date(const std::vector<mirai::Task> &tasks)
|
2024-11-04 14:45:27 +01:00
|
|
|
{
|
2025-06-29 20:07:11 +02:00
|
|
|
if (tasks.size() == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<tasks_date_group> dates_group;
|
|
|
|
int next_task_index = 0;
|
|
|
|
|
|
|
|
mirai::Task current_task = tasks.at(next_task_index);
|
|
|
|
|
|
|
|
while (next_task_index < tasks.size()) {
|
|
|
|
tasks_date_group date_group{.date = current_task.due_date()};
|
|
|
|
|
|
|
|
while (next_task_index < tasks.size() && current_task.due_date() == date_group.date) {
|
|
|
|
tasks_source_group source_group{.id = current_task.source_id()};
|
|
|
|
|
|
|
|
while (next_task_index < tasks.size() && current_task.due_date() == date_group.date
|
|
|
|
&& current_task.source_id() == source_group.id) {
|
|
|
|
source_group.tasks.push_back(current_task);
|
|
|
|
next_task_index++;
|
|
|
|
if (next_task_index >= tasks.size()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_task = tasks.at(next_task_index);
|
|
|
|
}
|
|
|
|
date_group.sources.push_back(source_group);
|
|
|
|
}
|
|
|
|
dates_group.push_back(date_group);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dates_group;
|
|
|
|
};
|
|
|
|
|
|
|
|
void AppWindow::update_tasks_view()
|
|
|
|
{
|
|
|
|
auto today = mirai::Date(std::chrono::system_clock::now());
|
|
|
|
auto new_slint_dates = std::make_shared<slint::VectorModel<ui::TasksViewDate>>();
|
|
|
|
|
|
|
|
auto &sources = miraiInstance_->getSources();
|
|
|
|
auto all_tasks_dates = group_tasks_by_date(get_all_tasks_from_sources(sources));
|
|
|
|
|
|
|
|
for (int date_index = 0; date_index < all_tasks_dates.size(); ++date_index) {
|
|
|
|
auto ¤t_date_group = all_tasks_dates.at(date_index);
|
|
|
|
auto ¤t_date = current_date_group.date;
|
|
|
|
std::optional<int> relative_days_diff = std::nullopt;
|
|
|
|
if (current_date.has_value()) {
|
|
|
|
relative_days_diff = std::chrono::duration_cast<std::chrono::days>(
|
|
|
|
std::chrono::sys_days(current_date.value().toStdChrono())
|
|
|
|
- std::chrono::sys_days(today.toStdChrono())
|
|
|
|
)
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto new_slint_sources = std::make_shared<slint::VectorModel<ui::TasksViewSource>>();
|
|
|
|
for (auto &source : current_date_group.sources) {
|
|
|
|
auto new_slint_source = ui::TasksViewSource();
|
|
|
|
auto new_slint_tasks = std::make_shared<slint::VectorModel<ui::TasksViewTask>>();
|
|
|
|
for (auto &task : source.tasks) {
|
|
|
|
if (task.has_due_date() && task.due_date() < today) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
new_slint_tasks->push_back(
|
|
|
|
{.id = task.id(),
|
|
|
|
.source_id = source.id,
|
|
|
|
.title = slint::SharedString(task.title()),
|
|
|
|
.checked = task.checked()}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
new_slint_source.tasks = new_slint_tasks;
|
|
|
|
new_slint_source.id = source.id;
|
|
|
|
new_slint_source.name =
|
|
|
|
slint::SharedString(miraiInstance_->getSourceById(source.id)->name());
|
|
|
|
const auto color =
|
|
|
|
selenite::hexStringToColor(miraiInstance_->getSourceById(source.id)->color());
|
|
|
|
new_slint_source.color = slint::Color::from_rgb_uint8(color.r, color.g, color.b);
|
|
|
|
if (new_slint_source.tasks->row_count() > 0) {
|
|
|
|
new_slint_sources->push_back(new_slint_source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto new_slint_date = ui::TasksViewDate{
|
|
|
|
.sources = new_slint_sources,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (current_date.has_value()) {
|
|
|
|
new_slint_date.due_date = MiraiDateToSlintDate(current_date.value());
|
|
|
|
new_slint_date.is_late = current_date.value() < today;
|
|
|
|
} else {
|
|
|
|
new_slint_date.no_date = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_slint_date.sources->row_count() > 0) {
|
|
|
|
new_slint_dates->push_back(new_slint_date);
|
|
|
|
}
|
|
|
|
_tasks_view.dates = new_slint_dates;
|
|
|
|
models().set_tasks_view(_tasks_view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::show_source(const mirai::source &source)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::show_all_sources()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::hide_all_sources()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::show_completed_tasks()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::hide_completed_tasks()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AppWindow::should_hide_completed_tasks() const
|
|
|
|
{
|
|
|
|
return true;
|
2024-11-04 14:45:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppWindow::run()
|
2024-08-16 21:35:12 +02:00
|
|
|
{
|
|
|
|
mainWindow_->run();
|
|
|
|
}
|
2024-11-04 14:45:27 +01:00
|
|
|
|
2025-06-24 12:04:54 +02:00
|
|
|
const ui::AppModels &AppWindow::models()
|
2024-11-04 14:45:27 +01:00
|
|
|
{
|
2025-06-24 12:04:54 +02:00
|
|
|
return mainWindow_->global<ui::AppModels>();
|
2024-11-04 14:45:27 +01:00
|
|
|
}
|
|
|
|
|
2025-06-24 12:04:54 +02:00
|
|
|
const ui::AppActions &AppWindow::actions()
|
2024-11-04 14:45:27 +01:00
|
|
|
{
|
2025-06-24 12:04:54 +02:00
|
|
|
return mainWindow_->global<ui::AppActions>();
|
2024-11-04 14:45:27 +01:00
|
|
|
}
|