/* * 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 "app_logic.h" #include "mirai-core/core.h" #include "mirai-core/date.h" #include "mirai-core/event.h" #include "mirai-core/markdown_data_provider.h" #include "mirai-core/source.h" #include "selenite/palette.h" #include "selenite/selenite.h" #include "slint_color.h" #include "slint_models.h" #include "slint_string.h" #include "ui.h" #include "ui/utils.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include app_logic::app_logic(mirai::core *miraiInstance) : _mirai_core(miraiInstance) { _sidebar_view = ui::SidebarView(); _calendar_view = ui::CalendarView(); _tasks_view = ui::TasksView(); 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( _main_window->global(), palette.value() ); } bind_slint_utils(_main_window->global()); models().set_sidebar_view(_sidebar_view); models().set_calendar_view(_calendar_view); models().set_tasks_view(_tasks_view); show_all_sources(); update_views(); setup_callbacks(); } 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 app_logic::setup_callbacks() { models().on_get_source_id_from_name([&](slint::SharedString name) { auto source = _mirai_core->get_source_by_name(std::string(name)); assert(source); return source->id; }); models().on_get_source_name_from_id([&](int sourceId) { auto source = _mirai_core->get_source_by_id(sourceId); assert(source); return slint::SharedString(source->name()); }); models().on_get_source_color_from_id_as_hex([&](int sourceId) { auto source = _mirai_core->get_source_by_id(sourceId); assert(source); return slint::SharedString(source->color()); }); models().on_get_source_color_from_id_as_color([&](int sourceId) { auto source = _mirai_core->get_source_by_id(sourceId); assert(source); auto color = selenite::hexStringToColor(source->color()); return slint::Color::from_rgb_uint8(color.r, color.g, color.b); }); models().on_get_source_path_from_id([&](int sourceId) { auto source = _mirai_core->get_source_by_id(sourceId); assert(source); mirai::markdown_data_provider *sourceProvider = dynamic_cast(source->data_provider()); return slint::SharedString(sourceProvider->path()); }); _mirai_core->on_source_added([&](mirai::source *source) { update_views(); }); _mirai_core->on_source_edited([&](mirai::source *source) { update_views(); }); _mirai_core->on_source_removed([&](int id) { update_views(); }); actions().on_task_clicked([&](int sourceId, int taskId) { auto source = _mirai_core->get_source_by_id(sourceId); assert(source); auto task = source->get_task_by_id(taskId); assert(task); task->set_checked(!task->checked()); _mirai_core->save(); update_views(); }); actions().on_source_clicked([&](int index) { const mirai::source *const source = _mirai_core->get_source_by_id(index); assert(source != nullptr); if (should_show_source(*source)) { hide_source(*source); } else { show_source(*source); } update_views(); }); actions().on_add_source([&](slint::SharedString name, slint::SharedString color, slint::SharedString path) { std::unique_ptr file = std::make_unique(std::string(path)); _mirai_core->add_source( std::string(name), std::string(color), "FileSystemMarkdown", std::move(file) ); }); actions().on_edit_source([&](int sourceId, slint::SharedString name, slint::SharedString color, slint::SharedString path) { _mirai_core->edit_source( sourceId, std::string(name), std::string(color), std::string(path) ); }); actions().on_remove_source([&](int source_id) { _mirai_core->remove_source(source_id); }); actions().on_delete_task_clicked([&](int sourceId, int taskId) { auto source = _mirai_core->get_source_by_id(sourceId); assert(source); auto task = source->get_task_by_id(taskId); assert(task); source->remove_task(*task); _mirai_core->save(); update_views(); }); actions().on_toggle_show_completed_tasks([&] { if (should_hide_completed_tasks()) { show_completed_tasks(); } else { hide_completed_tasks(); } update_views(); }); actions().on_save_task([&](ui::SaveTaskData newTaskData) { auto source = _mirai_core->get_source_by_id(newTaskData.sourceId); assert(source); auto task = source->get_task_by_id(newTaskData.id); assert(task.has_value()); const mirai::date &date = slint_date_to_mirai_date(newTaskData.date); // const auto dayOpt = source->get_day_by_date(date); task->set_title(std::string(newTaskData.title)); _mirai_core->save(); update_views(); }); actions().on_create_task([&](ui::NewTaskData newTaskData) { std::optional date = std::nullopt; if (newTaskData.date.year != 0) { date = slint_date_to_mirai_date(newTaskData.date); } auto source = _mirai_core->get_source_by_id(newTaskData.sourceId); std::optional event = std::nullopt; if (newTaskData.eventId >= 0) { event = source->get_event_by_id(newTaskData.eventId); } source->create_task({ .title = std::string(newTaskData.title), .due_date = date, }); _mirai_core->save(); update_views(); }); actions().on_delete_event([&](int sourceId, int eventId) { auto source = _mirai_core->get_source_by_id(sourceId); assert(source); auto event = source->get_event_by_id(eventId); assert(event.has_value()); source->remove_event(event.value()); _mirai_core->save(); // view_.update(); update_views(); }); actions().on_create_event([&](ui::CreateEventParams newEventParams) { const ui::Date &date = newEventParams.date; const std::string dateStr = slint_date_to_std_string(date); auto source = _mirai_core->get_source_by_id(newEventParams.source_id); source->create_event({ .title = std::string(newEventParams.title), .date = slint_date_to_mirai_date(newEventParams.date), .starts_at = slint_time_to_mirai_time(newEventParams.starts_at), .ends_at = slint_time_to_mirai_time(newEventParams.ends_at), }); _mirai_core->save(); update_views(); }); } 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 app_logic::update_views() { update_sidebar_view(); update_calendar_view(); update_tasks_view(); update_available_sources(); } void app_logic::update_available_sources() { const auto &sources = _mirai_core->get_sources(); auto available_sources = std::make_shared>(); auto available_sources_strings = std::make_shared>(); for (auto &source : sources) { mirai::markdown_data_provider *sourceProvider = dynamic_cast(source->data_provider()); const auto color = selenite::hexStringToColor(_mirai_core->get_source_by_id(source->id)->color()); auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b); available_sources->push_back( {.id = source->id, .name = slint::SharedString(source->name()), .color = source_color} ); available_sources_strings->push_back(slint::SharedString(source->name())); } models().set_available_sources(available_sources); models().set_available_sources_strings(available_sources_strings); }; void app_logic::update_sidebar_view() { const auto &sources = _mirai_core->get_sources(); auto new_sources = std::make_shared>(); for (auto &source : sources) { mirai::markdown_data_provider *sourceProvider = dynamic_cast(source->data_provider()); const auto color = selenite::hexStringToColor(_mirai_core->get_source_by_id(source->id)->color()); auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b); new_sources->push_back({ .id = source->id, .name = slint::SharedString(source->name()), .color = source_color, .active = _source_filters.contains(source->id) && _source_filters.at(source->id), }); } _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 merge_all_events_from_sources(std::vector> &sources) { std::vector all_events; for (auto &source : sources) { const auto &events = source->get_events(); all_events.insert(all_events.end(), events.begin(), events.end()); } return all_events; } std::vector get_events_for_date(const std::vector &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(filtered_events); } void app_logic::update_calendar_view() { auto today = mirai::date(std::chrono::system_clock::now()); auto new_slint_dates = std::make_shared>(); auto &sources = _mirai_core->get_sources(); auto all_events = merge_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::system_clock::now()) + std::chrono::days{day_index}; auto current_date = mirai::date{next_date}; auto new_slint_events = std::make_shared>(); auto relative_days_diff = std::chrono::duration_cast( std::chrono::sys_days(current_date.to_std_chrono()) - std::chrono::sys_days(today.to_std_chrono()) ) .count(); if (relative_days_diff < 0 || relative_days_diff >= 7) { continue; } 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); // TODO directly remove the source instead of this workaround after data layer refacto auto source = _mirai_core->get_source_by_id(current_event.source_id()); if (!should_show_source(*source)) { continue; } 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 = mirai_time_to_slint_time(current_event.starts_at()), .ends_at = mirai_time_to_slint_time(current_event.ends_at()), } ); } auto new_slint_date = ui::CalendarViewDate{ .events = new_slint_events, .date = mirai_date_to_slint_date(current_date), .header = slint::SharedString( capitalize(format_date_relative(mirai_date_to_slint_date(current_date))) ) }; new_slint_dates->push_back(new_slint_date); } _calendar_view.dates = new_slint_dates; models().set_calendar_view(_calendar_view); } std::vector get_all_tasks_from_sources(std::vector> &sources) { std::vector all_tasks; for (auto &source : sources) { const auto &tasks = source->get_tasks(); all_tasks.insert(all_tasks.end(), tasks.begin(), tasks.end()); } 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; } struct tasks_source_group { int id; std::vector tasks; }; struct tasks_date_group { std::optional date; std::vector sources; }; std::vector group_tasks_by_date(const std::vector &tasks) { if (tasks.size() == 0) { return {}; } std::vector 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 app_logic::update_tasks_view() { auto today = mirai::date(std::chrono::system_clock::now()); auto new_slint_dates = std::make_shared>(); auto &sources = _mirai_core->get_sources(); 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 relative_days_diff = std::nullopt; if (current_date.has_value()) { relative_days_diff = std::chrono::duration_cast( std::chrono::sys_days(current_date.value().to_std_chrono()) - std::chrono::sys_days(today.to_std_chrono()) ) .count(); } auto new_slint_sources = std::make_shared>(); for (auto &source_group : current_date_group.sources) { auto new_slint_source = ui::TasksViewSource(); auto new_slint_tasks = std::make_shared>(); auto source = _mirai_core->get_source_by_id(source_group.id); if (!should_show_source(*source)) { continue; } for (auto &task : source_group.tasks) { if (task.has_due_date() && task.due_date() < today) { continue; } new_slint_tasks->push_back( {.id = task.id(), .source_id = source_group.id, .title = slint::SharedString(task.title()), .checked = task.checked()} ); } new_slint_source.tasks = new_slint_tasks; new_slint_source.id = source_group.id; new_slint_source.name = slint::SharedString(_mirai_core->get_source_by_id(source_group.id)->name()); const auto color = selenite::hexStringToColor(source->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 = mirai_date_to_slint_date(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 app_logic::show_source(const mirai::source &source) { _source_filters.insert_or_assign(source.id, true); } void app_logic::hide_source(const mirai::source &source) { _source_filters.insert_or_assign(source.id, false); } bool app_logic::should_show_source(const mirai::source &source) const { const int source_id = source.id; return _source_filters.contains(source_id) && _source_filters.at(source_id); } void app_logic::show_all_sources() { auto &sources = _mirai_core->get_sources(); for (auto &source : sources) { show_source(*source); } } void app_logic::hide_all_sources() { } void app_logic::show_completed_tasks() { } void app_logic::hide_completed_tasks() { } bool app_logic::should_hide_completed_tasks() const { return true; } void app_logic::run() { _main_window->run(); } const ui::AppModels &app_logic::models() { return _main_window->global(); } const ui::AppActions &app_logic::actions() { return _main_window->global(); }