diff --git a/CMakeLists.txt b/CMakeLists.txt index 366e6f0..1688344 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set(CMAKE_BUILD_RPATH_USE_ORIGIN ON) add_executable(mirai src/main.cpp - src/windows/AppWindow/AppWindow.cpp + src/app_logic.cpp src/shared/Utils.cpp ) diff --git a/src/windows/AppWindow/AppWindow.cpp b/src/app_logic.cpp similarity index 86% rename from src/windows/AppWindow/AppWindow.cpp rename to src/app_logic.cpp index a6041dd..8715764 100644 --- a/src/windows/AppWindow/AppWindow.cpp +++ b/src/app_logic.cpp @@ -4,8 +4,7 @@ * The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt */ -#include "AppWindow.h" -#include "../../shared/Utils.h" +#include "app_logic.h" #include "mirai-core/core.h" #include "mirai-core/date.h" #include "mirai-core/event.h" @@ -13,6 +12,7 @@ #include "mirai-core/source.h" #include "selenite/palette.h" #include "selenite/selenite.h" +#include "shared/Utils.h" #include "slint_color.h" #include "slint_models.h" #include "slint_string.h" @@ -32,7 +32,7 @@ #include #include -AppWindow::AppWindow(mirai::core *miraiInstance) : miraiInstance_(miraiInstance) +app_logic::app_logic(mirai::core *miraiInstance) : _mirai_core(miraiInstance) { _sidebar_view = ui::SidebarView(); _calendar_view = ui::CalendarView(); @@ -48,11 +48,11 @@ AppWindow::AppWindow(mirai::core *miraiInstance) : miraiInstance_(miraiInstance) if (palette.has_value()) { std::println(std::cerr, "Warning, no {} found", palettePath); setSelenitePalette( - mainWindow_->global(), palette.value() + _main_window->global(), palette.value() ); } - bindSlintUtils(mainWindow_->global()); + bindSlintUtils(_main_window->global()); models().set_sidebar_view(_sidebar_view); models().set_calendar_view(_calendar_view); @@ -90,59 +90,59 @@ std::optional stringToDate(const std::string &dateStr) return ui::Date{.year = year, .month = month, .day = day}; } -void AppWindow::setupCallbacks() +void app_logic::setupCallbacks() { models().on_get_source_id_from_name([&](slint::SharedString name) { - auto source = miraiInstance_->getSourceByName(std::string(name)); + auto source = _mirai_core->getSourceByName(std::string(name)); assert(source); return source->id; }); models().on_get_source_name_from_id([&](int sourceId) { - auto source = miraiInstance_->getSourceById(sourceId); + auto source = _mirai_core->getSourceById(sourceId); assert(source); return slint::SharedString(source->name()); }); models().on_get_source_color_from_id_as_hex([&](int sourceId) { - auto source = miraiInstance_->getSourceById(sourceId); + auto source = _mirai_core->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); + auto source = _mirai_core->getSourceById(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 = miraiInstance_->getSourceById(sourceId); + auto source = _mirai_core->getSourceById(sourceId); assert(source); mirai::markdown_data_provider *sourceProvider = dynamic_cast(source->data_provider()); return slint::SharedString(sourceProvider->path()); }); - miraiInstance_->onSourceAdded([&](mirai::source *source) { refreshModels(); }); - miraiInstance_->onSourceEdited([&](mirai::source *source) { refreshModels(); }); - miraiInstance_->onSourceDeleted([&](int id) { refreshModels(); }); + _mirai_core->onSourceAdded([&](mirai::source *source) { refreshModels(); }); + _mirai_core->onSourceEdited([&](mirai::source *source) { refreshModels(); }); + _mirai_core->onSourceDeleted([&](int id) { refreshModels(); }); actions().on_task_clicked([&](int sourceId, int taskId) { - auto source = miraiInstance_->getSourceById(sourceId); + auto source = _mirai_core->getSourceById(sourceId); assert(source); auto task = source->get_task_by_id(taskId); assert(task); task->set_checked(!task->checked()); - miraiInstance_->save(); + _mirai_core->save(); update_views(); // view_.update(); reloadTasks(); }); actions().on_source_clicked([&](int index) { - const mirai::source *const source = miraiInstance_->getSourceById(index); + const mirai::source *const source = _mirai_core->getSourceById(index); assert(source != nullptr); if (should_show_source(*source)) { hide_source(*source); @@ -156,23 +156,21 @@ void AppWindow::setupCallbacks() actions().on_add_source([&](slint::SharedString name, slint::SharedString path) { std::unique_ptr file = std::make_unique(std::string(path)); - miraiInstance_->addSource(std::string(name), "FileSystemMarkdown", std::move(file)); + _mirai_core->addSource(std::string(name), "FileSystemMarkdown", std::move(file)); }); 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) - ); + _mirai_core->editSource(sourceId, std::string(name), std::string(color), std::string(path)); }); actions().on_delete_task_clicked([&](int sourceId, int taskId) { - auto source = miraiInstance_->getSourceById(sourceId); + auto source = _mirai_core->getSourceById(sourceId); assert(source); auto task = source->get_task_by_id(taskId); assert(task); source->remove_task(*task); - miraiInstance_->save(); + _mirai_core->save(); // view_.update(); update_views(); reloadTasks(); @@ -191,7 +189,7 @@ void AppWindow::setupCallbacks() }); actions().on_save_task([&](ui::SaveTaskData newTaskData) { - auto source = miraiInstance_->getSourceById(newTaskData.sourceId); + auto source = _mirai_core->getSourceById(newTaskData.sourceId); assert(source); auto task = source->get_task_by_id(newTaskData.id); assert(task.has_value()); @@ -199,7 +197,7 @@ void AppWindow::setupCallbacks() // const auto dayOpt = source->get_day_by_date(date); task->set_title(std::string(newTaskData.title)); - miraiInstance_->save(); + _mirai_core->save(); // view_.update(); update_views(); reloadTasks(); @@ -210,7 +208,7 @@ void AppWindow::setupCallbacks() if (newTaskData.date.year != 0) { date = SlintDateToMiraiDate(newTaskData.date); } - auto source = miraiInstance_->getSourceById(newTaskData.sourceId); + auto source = _mirai_core->getSourceById(newTaskData.sourceId); std::optional event = std::nullopt; if (newTaskData.eventId >= 0) { @@ -222,19 +220,19 @@ void AppWindow::setupCallbacks() .due_date = date, }); - miraiInstance_->save(); + _mirai_core->save(); // view_.update(); update_views(); reloadTasks(); }); actions().on_delete_event([&](int sourceId, int eventId) { - auto source = miraiInstance_->getSourceById(sourceId); + auto source = _mirai_core->getSourceById(sourceId); assert(source); auto event = source->get_event_by_id(eventId); assert(event.has_value()); source->remove_event(event.value()); - miraiInstance_->save(); + _mirai_core->save(); // view_.update(); update_views(); reloadTasks(); @@ -243,7 +241,7 @@ void AppWindow::setupCallbacks() actions().on_create_event([&](ui::CreateEventParams newEventParams) { const ui::Date &date = newEventParams.date; const std::string dateStr = SlintDateToStdString(date); - auto source = miraiInstance_->getSourceById(newEventParams.source_id); + auto source = _mirai_core->getSourceById(newEventParams.source_id); source->create_event({ .title = std::string(newEventParams.title), @@ -251,7 +249,7 @@ void AppWindow::setupCallbacks() .starts_at = SlintTimeToMiraiTime(newEventParams.starts_at), .ends_at = SlintTimeToMiraiTime(newEventParams.ends_at), }); - miraiInstance_->save(); + _mirai_core->save(); update_views(); }); } @@ -266,7 +264,7 @@ stdToSlintStringVector(const std::vector &stdVector) return slintVector; } -void AppWindow::reloadTasks() +void app_logic::reloadTasks() { /* days_->clear();*/ /*if (miraiInstance_->getSources().size() == 0) {*/ @@ -395,7 +393,7 @@ void AppWindow::reloadTasks() /*}*/ } -void AppWindow::reloadSources() +void app_logic::reloadSources() { /*sources_->clear();*/ /*bool noSourceSelected = miraiInstance_->getSources().size() == view_.activeSourceCount();*/ @@ -413,13 +411,13 @@ void AppWindow::reloadSources() /*models().set_no_source_selected(noSourceSelected);*/ } -void AppWindow::refreshModels() +void app_logic::refreshModels() { show_all_sources(); update_views(); } -void AppWindow::update_views() +void app_logic::update_views() { update_sidebar_view(); update_calendar_view(); @@ -427,16 +425,16 @@ void AppWindow::update_views() update_available_sources(); } -void AppWindow::update_available_sources() +void app_logic::update_available_sources() { - const auto &sources = miraiInstance_->getSources(); + const auto &sources = _mirai_core->getSources(); 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(miraiInstance_->getSourceById(source->id)->color()); + selenite::hexStringToColor(_mirai_core->getSourceById(source->id)->color()); auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b); available_sources->push_back( @@ -449,16 +447,16 @@ void AppWindow::update_available_sources() models().set_available_sources_strings(available_sources_strings); }; -void AppWindow::update_sidebar_view() +void app_logic::update_sidebar_view() { - const auto &sources = miraiInstance_->getSources(); + const auto &sources = _mirai_core->getSources(); 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(miraiInstance_->getSourceById(source->id)->color()); + selenite::hexStringToColor(_mirai_core->getSourceById(source->id)->color()); auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b); new_sources->push_back({ .id = source->id, @@ -494,12 +492,12 @@ get_events_for_date(const std::vector &events, const mirai::date & return std::ranges::to(filtered_events); } -void AppWindow::update_calendar_view() +void app_logic::update_calendar_view() { auto today = mirai::date(std::chrono::system_clock::now()); auto new_slint_dates = std::make_shared>(); - auto &sources = miraiInstance_->getSources(); + auto &sources = _mirai_core->getSources(); auto all_events = merge_all_events_from_sources(sources); for (int day_index = 0; day_index < 7; ++day_index) { @@ -520,7 +518,7 @@ void AppWindow::update_calendar_view() 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 = miraiInstance_->getSourceById(current_event.source_id()); + auto source = _mirai_core->getSourceById(current_event.source_id()); if (!should_show_source(*source)) { continue; } @@ -615,12 +613,12 @@ std::vector group_tasks_by_date(const std::vector return dates_group; }; -void AppWindow::update_tasks_view() +void app_logic::update_tasks_view() { auto today = mirai::date(std::chrono::system_clock::now()); auto new_slint_dates = std::make_shared>(); - auto &sources = miraiInstance_->getSources(); + auto &sources = _mirai_core->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) { @@ -639,7 +637,7 @@ void AppWindow::update_tasks_view() for (auto &source_group : current_date_group.sources) { auto new_slint_source = ui::TasksViewSource(); auto new_slint_tasks = std::make_shared>(); - auto source = miraiInstance_->getSourceById(source_group.id); + auto source = _mirai_core->getSourceById(source_group.id); if (!should_show_source(*source)) { continue; } @@ -657,7 +655,7 @@ void AppWindow::update_tasks_view() new_slint_source.tasks = new_slint_tasks; new_slint_source.id = source_group.id; new_slint_source.name = - slint::SharedString(miraiInstance_->getSourceById(source_group.id)->name()); + slint::SharedString(_mirai_core->getSourceById(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) { @@ -683,58 +681,58 @@ void AppWindow::update_tasks_view() } } -void AppWindow::show_source(const mirai::source &source) +void app_logic::show_source(const mirai::source &source) { _source_filters.insert_or_assign(source.id, true); } -void AppWindow::hide_source(const mirai::source &source) +void app_logic::hide_source(const mirai::source &source) { _source_filters.insert_or_assign(source.id, false); } -bool AppWindow::should_show_source(const mirai::source &source) const +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 AppWindow::show_all_sources() +void app_logic::show_all_sources() { - auto &sources = miraiInstance_->getSources(); + auto &sources = _mirai_core->getSources(); for (auto &source : sources) { show_source(*source); } } -void AppWindow::hide_all_sources() +void app_logic::hide_all_sources() { } -void AppWindow::show_completed_tasks() +void app_logic::show_completed_tasks() { } -void AppWindow::hide_completed_tasks() +void app_logic::hide_completed_tasks() { } -bool AppWindow::should_hide_completed_tasks() const +bool app_logic::should_hide_completed_tasks() const { return true; } -void AppWindow::run() +void app_logic::run() { - mainWindow_->run(); + _main_window->run(); } -const ui::AppModels &AppWindow::models() +const ui::AppModels &app_logic::models() { - return mainWindow_->global(); + return _main_window->global(); } -const ui::AppActions &AppWindow::actions() +const ui::AppActions &app_logic::actions() { - return mainWindow_->global(); + return _main_window->global(); } diff --git a/src/windows/AppWindow/AppWindow.h b/src/app_logic.h similarity index 88% rename from src/windows/AppWindow/AppWindow.h rename to src/app_logic.h index 2f34ba7..2e62a3d 100644 --- a/src/windows/AppWindow/AppWindow.h +++ b/src/app_logic.h @@ -13,11 +13,11 @@ #include #include -class AppWindow +class app_logic { public: - AppWindow(mirai::core *mirai); + app_logic(mirai::core *mirai); void run(); @@ -50,9 +50,9 @@ class AppWindow ui::TasksView _tasks_view; ui::CalendarView _calendar_view; - slint::ComponentHandle mainWindow_ = ui::AppWindow::create(); + slint::ComponentHandle _main_window = ui::AppWindow::create(); - mirai::core *miraiInstance_; + mirai::core *_mirai_core; std::unordered_map _source_filters; bool _should_hide_completed_tasks; diff --git a/src/main.cpp b/src/main.cpp index 6f2ab2c..d331280 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,10 +4,10 @@ * 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 "evalyte-cpp-common/evalyte.h" #include "mirai-core/core.h" #include "ui.h" -#include "windows/AppWindow/AppWindow.h" #include #include @@ -16,7 +16,7 @@ int main(int argc, char **argv) evalyte::create_required_directories("mirai"); const auto config_file_path = evalyte::config_directory_path("mirai") + "/config.json"; mirai::core mirai{config_file_path}; - AppWindow appWindow{&mirai}; - appWindow.run(); + app_logic app{&mirai}; + app.run(); return 0; }