/* * 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 "AppWindowBackend.h" #include "AppWindow.h" #include "SeleniteSetup.h" #include "Utils.h" #include "mirai-core/DataProvider.h" #include "mirai-core/DateTime.h" #include "mirai-core/Day.h" #include "mirai-core/MarkdownDataProvider.h" #include "mirai-core/Mirai.h" #include "slint.h" #include "slint_string.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include AppWindowBackend::AppWindowBackend(mirai::Mirai *miraiInstance) : miraiInstance_(miraiInstance), view_(miraiInstance) { sources_ = std::make_shared>(); days_ = std::make_shared>(); calendar_ = std::make_shared>(); unscheduledTasks_ = std::make_shared>(); tags_ = std::make_shared>(); auto sourcesNames = std::make_shared>( sources_, [&](const ui::Source &a) { return a.name; } ); const auto palettePath = std::string(getenv("HOME")) + "/.config/evalyte/theme.json"; const auto palette = selenite::parseJson(palettePath); if (palette.has_value()) { std::println(std::cerr, "Warning, no {} found", palettePath); setSelenitePalette(mainWindow_->global(), palette.value()); setSelenitePalette(settingsWindow_->global(), palette.value()); setSelenitePalette(addSourceWindow_->global(), palette.value()); } mainWindow_->global().set_sources(sourcesNames); settingsWindow_->global().set_sources(sourcesNames); mainWindow_->global().set_sources_selected(sources_); settingsWindow_->global().set_sources_selected(sources_); mainWindow_->global().set_tags(tags_); mainWindow_->global().set_days(days_); mainWindow_->global().set_calendar(calendar_); view_.setAllSources(); view_.update(); reloadSources(); reloadTasks(); setupCallbacks(); } std::optional 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 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 ui::Date{.year = year, .month = month, .day = day}; } void AppWindowBackend::setupUtilsCallbacks() { mainWindow_->global().on_format_date([&](const ui::Date &date) { std::chrono::year_month_day chronoDate{ std::chrono::year(date.year), std::chrono::month(date.month), std::chrono::day(date.day), }; return std::format("{:%B %d}", chronoDate); }); mainWindow_->global().on_format_date_relative([&](const ui::Date &date) { return formatDateRelative(date); }); } void AppWindowBackend::setupCallbacks() { mainWindow_->global().on_settings_clicked([&]() { settingsWindow_->show(); }); mainWindow_->global().on_add_source_clicked([&]() { addSourceWindow_->show(); }); mainWindow_->global().on_edit_source_clicked([&](int sourceId) { auto source = miraiInstance_->getSourceById(sourceId); assert(source); auto markdownSource = dynamic_cast(source->dataProvider()); editSourceWindow_->set_id(source->id); editSourceWindow_->set_name(slint::SharedString(source->name())); editSourceWindow_->set_path(slint::SharedString(markdownSource->path())); editSourceWindow_->show(); }); addSourceWindow_->global().on_add_source([&](ui::AddSourceParam params) { std::unique_ptr file = std::make_unique(std::string(params.path)); miraiInstance_->addSource( std::string(params.name), std::string(params.type), std::move(file) ); addSourceWindow_->hide(); reloadSources(); reloadTasks(); }); editSourceWindow_->global().on_modify_source([&](ui::ModifySourceParam params) { miraiInstance_->editSource(params.id, std::string(params.name), std::string(params.path)); editSourceWindow_->hide(); reloadSources(); reloadTasks(); }); editSourceWindow_->global().on_delete_source([&](int sourceId) { miraiInstance_->deleteSource(sourceId); editSourceWindow_->hide(); reloadSources(); reloadTasks(); }); mainWindow_->global().on_task_clicked([&](int sourceId, int taskId) { auto source = miraiInstance_->getSourceById(sourceId); assert(source); auto task = source->getTaskById(taskId); assert(task); if (!task->checked() && !task->hasDate() && !task->hasEvent()) { task->setDate(mirai::Date(mirai::Date(std::chrono::system_clock::now()))); } task->setChecked(!task->checked()); miraiInstance_->save(); view_.update(); reloadTasks(); }); mainWindow_->global().on_source_clicked([&](int index) { // index with value -1 is equal to no selection, aka "All" if (index == -1) { view_.setAllSources(); } else { view_.removeSources(); const mirai::Source *source = miraiInstance_->getSourceById(index); view_.addSource(*source); } mainWindow_->global().set_default_source_index(index == -1 ? 0 : index); view_.update(); reloadSources(); reloadTasks(); }); mainWindow_->global().on_delete_task_clicked([&](int sourceId, int taskId) { auto source = miraiInstance_->getSourceById(sourceId); assert(source); auto task = source->getTaskById(taskId); assert(task); source->removeTask(*task); miraiInstance_->save(); view_.update(); reloadTasks(); }); mainWindow_->global().on_toggle_show_completed_tasks([&] { view_.hideCompletedTasks(!view_.shouldHideCompletedTasks()); view_.update(); reloadTasks(); }); mainWindow_->global().on_save_task([&](ui::SaveTaskData newTaskData) { auto source = miraiInstance_->getSourceById(newTaskData.sourceId); assert(source); auto task = source->getTaskById(newTaskData.id); assert(task.has_value()); const mirai::Date &date = SlintDateToMiraiDate(newTaskData.date); const auto dayOpt = source->getDayByDate(date); task->setTitle(std::string(newTaskData.title)); if (!task.value().hasEvent() && date.year != 0) { task->setDate(date); } if (!task.value().hasEvent() && date.year == 0) { task->unschedule(); } miraiInstance_->save(); view_.update(); reloadTasks(); }); mainWindow_->global().on_create_task([&](ui::NewTaskData newTaskData) { std::optional date = std::nullopt; if (newTaskData.date.year != 0) { date = SlintDateToMiraiDate(newTaskData.date); } auto source = miraiInstance_->getSourceById(newTaskData.sourceId); std::optional event = std::nullopt; if (newTaskData.eventId >= 0) { event = source->getEventById(newTaskData.eventId); } source->createTask({ .title = std::string(newTaskData.title), .event = event, .date = date, }); miraiInstance_->save(); view_.update(); reloadTasks(); }); mainWindow_->global().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(); view_.update(); reloadTasks(); }); mainWindow_->global().on_create_event([&](ui::NewEventParams newEventParams) { const ui::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().on_save_event([&](ui::SaveEventParams newEventParams) { const ui::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. miraiInstance_->save(); view_.update(); reloadTasks(); }); setupUtilsCallbacks(); } std::shared_ptr> stdToSlintStringVector(const std::vector &stdVector) { auto slintVector = std::make_shared>(); for (const auto &item : stdVector) { slintVector->push_back(slint::SharedString(item)); } return slintVector; } void AppWindowBackend::reloadTasks() { 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>(); for (int dayIndex = 0; dayIndex < dates.size(); ++dayIndex) { auto ¤tDate = dates.at(dayIndex); auto slintEvents = std::make_shared>(); auto slintDayTasks = std::make_shared>(); auto relativeDaysDiff = std::chrono::duration_cast( 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(relativeDaysDiff) }); // Day's tasks const std::vector 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 eventsForDate = view_.getEventsForDate(currentDate); for (int eventIndex = 0; eventIndex < eventsForDate.size(); ++eventIndex) { auto ¤tEvent = eventsForDate.at(eventIndex); auto slintTasks = std::make_shared>(); slintEvents->push_back(ui::Event{ .sourceId = currentEvent.sourceId(), .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; mainWindow_->global().set_days(days_); auto unscheduledTasksView = view_.getUnscheduledTasks(); unscheduledTasks_->clear(); for (int taskIndex = 0; taskIndex < unscheduledTasksView.size(); ++taskIndex) { auto &task = unscheduledTasksView.at(taskIndex); unscheduledTasks_->push_back({ .sourceId = task.sourceId(), .eventId = -1, .id = task.id(), .title = slint::SharedString(task.title()), .checked = task.checked(), }); } mainWindow_->global().set_unscheduled_tasks(unscheduledTasks_); calendar_->clear(); for (int dayIndex = 0; dayIndex < 7; ++dayIndex) { std::chrono::year_month_day nextDate = std::chrono::floor(std::chrono::system_clock::now()) + std::chrono::days{dayIndex}; auto currentDate = mirai::Date{nextDate}; auto events = view_.getEventsForDate(currentDate); auto slintEvents = std::make_shared>(); auto relativeDaysDiff = std::chrono::duration_cast( std::chrono::sys_days(currentDate.toStdChrono()) - std::chrono::sys_days(todayDate.toStdChrono()) ) .count(); if (relativeDaysDiff < 0 || relativeDaysDiff >= 3) { continue; } const std::vector eventsForDate = view_.getEventsForDate(currentDate); for (int eventIndex = 0; eventIndex < eventsForDate.size(); ++eventIndex) { auto ¤tEvent = eventsForDate.at(eventIndex); slintEvents->push_back(ui::CalendarDayEvent{ .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); } } void AppWindowBackend::reloadSources() { sources_->clear(); bool noSourceSelected = miraiInstance_->getSources().size() == view_.activeSourceCount(); for (const auto &source : miraiInstance_->getSources()) { bool isSourceSelected = view_.isSourceSelected(*source); mirai::MarkdownDataProvider *sourceProvider = dynamic_cast(source->dataProvider()); sources_->push_back( {.id = source->id, .name = slint::SharedString(source->name()), .selected = isSourceSelected && !noSourceSelected, .path = slint::SharedString(sourceProvider->path())} ); } mainWindow_->global().set_no_source_selected(noSourceSelected); } void AppWindowBackend::run() { mainWindow_->run(); }