Add 'remove source' feature

This commit is contained in:
Vyn 2025-07-03 10:57:14 +02:00
parent 0046cb9bbb
commit e608a4afcc
Signed by: vyn
GPG key ID: E1B2BE34E7A971E7
11 changed files with 126 additions and 286 deletions

View file

@ -10,7 +10,7 @@ add_library(mirai-core
src/task.cpp src/task.cpp
src/event.cpp src/event.cpp
src/date.cpp src/date.cpp
src/EventEmitter.cpp src/event_emitter.cpp
src/source.cpp src/source.cpp
src/markdown_data_provider.cpp src/markdown_data_provider.cpp
src/markdown_data_provider.parser.cpp src/markdown_data_provider.parser.cpp

View file

@ -6,7 +6,7 @@
#pragma once #pragma once
#include "EventEmitter.h" #include "event_emitter.h"
#include "source.h" #include "source.h"
#include <functional> #include <functional>
#include <memory> #include <memory>
@ -21,34 +21,34 @@ class core
public: public:
core(const std::string &configFilePath); core(const std::string &configFilePath);
void addSource( void add_source(
const std::string &name, const std::string &type, std::unique_ptr<data_provider> &&source const std::string &name, const std::string &color, const std::string &type,
std::unique_ptr<data_provider> &&source
); );
void void
editSource(int id, const std::string &name, const std::string &color, const std::string &path); edit_source(int id, const std::string &name, const std::string &color, const std::string &path);
void deleteSource(int id); void remove_source(int id);
void unloadAllSources(); void unload_all_sources();
void save(); void save();
std::vector<std::unique_ptr<source>> &getSources(); std::vector<std::unique_ptr<source>> &get_sources();
const std::vector<std::string> &getTags();
// Returns a non owning pointer to the requested resource or nullptr if not found. // Returns a non owning pointer to the requested resource or nullptr if not found.
source *getSourceById(int id); source *get_source_by_id(int id);
source *getSourceByName(const std::string &name); source *get_source_by_name(const std::string &name);
void onSourceAdded(std::function<void(source *)> f); void on_source_added(std::function<void(source *)> f);
void onSourceEdited(std::function<void(source *)> f); void on_source_edited(std::function<void(source *)> f);
void onSourceDeleted(std::function<void(int)> f); void on_source_removed(std::function<void(int)> f);
private: private:
void loadConfig(const std::string &path); void load_config(const std::string &path);
void saveConfig(); void save_config();
std::vector<std::unique_ptr<source>> sources_; std::vector<std::unique_ptr<source>> _sources;
std::string configPath_; std::string _config_path;
EventEmitter<source *> sourceAdded; event_emitter<source *> source_added;
EventEmitter<source *> sourceEdited; event_emitter<source *> source_edited;
EventEmitter<int> sourceDeleted; event_emitter<int> source_removed;
}; };
} // namespace mirai } // namespace mirai

View file

@ -9,11 +9,11 @@
#include <functional> #include <functional>
#include <vector> #include <vector>
template <typename T> class EventEmitter template <typename T> class event_emitter
{ {
public: public:
void registerCallback(std::function<void(T)> func) void register_callback(std::function<void(T)> func)
{ {
callbacks.push_back(func); callbacks.push_back(func);
} }

View file

@ -22,7 +22,7 @@
namespace mirai namespace mirai
{ {
void core::loadConfig(const std::string &path) void core::load_config(const std::string &path)
{ {
std::ifstream file(path); std::ifstream file(path);
if (!file) { if (!file) {
@ -46,7 +46,7 @@ void core::loadConfig(const std::string &path)
std::unique_ptr<data_provider> file = std::make_unique<markdown_data_provider>(path); std::unique_ptr<data_provider> file = std::make_unique<markdown_data_provider>(path);
data_provider *sourceDataProvider = file.release(); data_provider *sourceDataProvider = file.release();
sourceDataProvider->load(); sourceDataProvider->load();
sources_.push_back( _sources.push_back(
std::make_unique<source>(source_constructor{ std::make_unique<source>(source_constructor{
.name = name, .color = color, .source_data_provider = sourceDataProvider .name = name, .color = color, .source_data_provider = sourceDataProvider
}) })
@ -54,17 +54,17 @@ void core::loadConfig(const std::string &path)
} }
} }
void core::saveConfig() void core::save_config()
{ {
std::ofstream file(configPath_); std::ofstream file(_config_path);
if (!file.is_open()) { if (!file.is_open()) {
std::print(std::cerr, "Can't save config to {}", configPath_); std::print(std::cerr, "Can't save config to {}", _config_path);
return; return;
} }
auto files = rei::json::JsonArray{}; auto files = rei::json::JsonArray{};
auto configJson = rei::json::JsonObject{}; auto configJson = rei::json::JsonObject{};
configJson.set("files", files); configJson.set("files", files);
for (auto &source : sources_) { for (auto &source : _sources) {
rei::json::JsonObject jsonSource; rei::json::JsonObject jsonSource;
jsonSource.set("name", source->name()); jsonSource.set("name", source->name());
@ -78,109 +78,110 @@ void core::saveConfig()
file.close(); file.close();
} }
core::core(const std::string &configFilePath) : configPath_(configFilePath) core::core(const std::string &configFilePath) : _config_path(configFilePath)
{ {
loadConfig(configFilePath); load_config(configFilePath);
} }
void core::addSource( void core::add_source(
const std::string &name, const std::string &type, std::unique_ptr<data_provider> &&new_source const std::string &name, const std::string &color, const std::string &type,
std::unique_ptr<data_provider> &&new_source
) )
{ {
data_provider *sourceDataProvider = new_source.release(); data_provider *sourceDataProvider = new_source.release();
sourceDataProvider->load(); sourceDataProvider->load();
sources_.push_back( _sources.push_back(
std::make_unique<source>( std::make_unique<source>(source_constructor{
source_constructor{.name = name, .source_data_provider = sourceDataProvider} .name = name, .color = color, .source_data_provider = sourceDataProvider
) })
); );
saveConfig(); save_config();
sourceAdded.emit(nullptr); source_added.emit(nullptr);
}; };
void core::editSource( void core::edit_source(
int id, const std::string &name, const std::string &color, const std::string &path int id, const std::string &name, const std::string &color, const std::string &path
) )
{ {
auto source = getSourceById(id); auto source = get_source_by_id(id);
source->set_name(name); source->set_name(name);
source->set_color(color); source->set_color(color);
data_provider *sourceDataProvider = source->data_provider(); data_provider *sourceDataProvider = source->data_provider();
saveConfig(); save_config();
sourceEdited.emit(nullptr); source_edited.emit(nullptr);
} }
void core::deleteSource(int id) void core::remove_source(int id)
{ {
sources_.erase( _sources.erase(
std::remove_if( std::remove_if(
sources_.begin(), sources_.end(), _sources.begin(), _sources.end(),
[&](const std::unique_ptr<source> &source) { return source->id == id; } [&](const std::unique_ptr<source> &source) { return source->id == id; }
), ),
sources_.end() _sources.end()
); );
saveConfig(); save_config();
sourceDeleted.emit(id); source_removed.emit(id);
} }
void core::unloadAllSources() void core::unload_all_sources()
{ {
sources_.clear(); _sources.clear();
} }
void core::save() void core::save()
{ {
for (auto &source : sources_) { for (auto &source : _sources) {
if (source->is_dirty()) { if (source->is_dirty()) {
source->save(); source->save();
} }
} }
} }
std::vector<std::unique_ptr<source>> &core::getSources() std::vector<std::unique_ptr<source>> &core::get_sources()
{ {
return sources_; return _sources;
} }
source *core::getSourceById(int id) source *core::get_source_by_id(int id)
{ {
auto source_found = std::ranges::find_if(sources_, [&](const std::unique_ptr<source> &source) { auto source_found = std::ranges::find_if(_sources, [&](const std::unique_ptr<source> &source) {
return source->id == id; return source->id == id;
}); });
// assert(source_found != sources_.end()); // This should not happen // assert(source_found != sources_.end()); // This should not happen
if (source_found == sources_.end()) { if (source_found == _sources.end()) {
return nullptr; return nullptr;
} }
return source_found->get(); return source_found->get();
} }
source *core::getSourceByName(const std::string &name) source *core::get_source_by_name(const std::string &name)
{ {
auto source_found = std::ranges::find_if(sources_, [&](const std::unique_ptr<source> &source) { auto source_found = std::ranges::find_if(_sources, [&](const std::unique_ptr<source> &source) {
return source->name() == name; return source->name() == name;
}); });
assert(source_found != sources_.end()); // This should not happen assert(source_found != _sources.end()); // This should not happen
if (source_found == sources_.end()) { if (source_found == _sources.end()) {
return nullptr; return nullptr;
} }
return source_found->get(); return source_found->get();
} }
void core::onSourceAdded(std::function<void(source *)> f) void core::on_source_added(std::function<void(source *)> f)
{ {
sourceAdded.registerCallback(f); source_added.register_callback(f);
} }
void core::onSourceEdited(std::function<void(source *)> f) void core::on_source_edited(std::function<void(source *)> f)
{ {
sourceEdited.registerCallback(f); source_edited.register_callback(f);
} }
void core::onSourceDeleted(std::function<void(int)> f) void core::on_source_removed(std::function<void(int)> f)
{ {
sourceDeleted.registerCallback(f); source_removed.register_callback(f);
} }
} // namespace mirai } // namespace mirai

View file

@ -4,4 +4,4 @@
* The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt * The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
*/ */
#include "EventEmitter.h" #include "event_emitter.h"

View file

@ -37,11 +37,6 @@ app_logic::app_logic(mirai::core *miraiInstance) : _mirai_core(miraiInstance)
_sidebar_view = ui::SidebarView(); _sidebar_view = ui::SidebarView();
_calendar_view = ui::CalendarView(); _calendar_view = ui::CalendarView();
_tasks_view = ui::TasksView(); _tasks_view = ui::TasksView();
/*auto sourcesNames = std::make_shared<slint::MapModel<ui::Source, slint::SharedString>>(*/
/*sources_, [&](const ui::Source &a) {*/
/*return a.name;*/
/*}*/
/*);*/
const auto palettePath = std::string(getenv("HOME")) + "/.config/evalyte/theme.json"; const auto palettePath = std::string(getenv("HOME")) + "/.config/evalyte/theme.json";
const auto palette = selenite::parseJson(palettePath); const auto palette = selenite::parseJson(palettePath);
@ -60,13 +55,8 @@ app_logic::app_logic(mirai::core *miraiInstance) : _mirai_core(miraiInstance)
show_all_sources(); show_all_sources();
update_views(); update_views();
/*view_.setAllSources();*/
/*view_.update();*/
reloadSources(); setup_callbacks();
reloadTasks();
setupCallbacks();
} }
std::optional<ui::Date> stringToDate(const std::string &dateStr) std::optional<ui::Date> stringToDate(const std::string &dateStr)
@ -90,59 +80,57 @@ std::optional<ui::Date> stringToDate(const std::string &dateStr)
return ui::Date{.year = year, .month = month, .day = day}; return ui::Date{.year = year, .month = month, .day = day};
} }
void app_logic::setupCallbacks() void app_logic::setup_callbacks()
{ {
models().on_get_source_id_from_name([&](slint::SharedString name) { models().on_get_source_id_from_name([&](slint::SharedString name) {
auto source = _mirai_core->getSourceByName(std::string(name)); auto source = _mirai_core->get_source_by_name(std::string(name));
assert(source); assert(source);
return source->id; return source->id;
}); });
models().on_get_source_name_from_id([&](int sourceId) { models().on_get_source_name_from_id([&](int sourceId) {
auto source = _mirai_core->getSourceById(sourceId); auto source = _mirai_core->get_source_by_id(sourceId);
assert(source); assert(source);
return slint::SharedString(source->name()); return slint::SharedString(source->name());
}); });
models().on_get_source_color_from_id_as_hex([&](int sourceId) { models().on_get_source_color_from_id_as_hex([&](int sourceId) {
auto source = _mirai_core->getSourceById(sourceId); auto source = _mirai_core->get_source_by_id(sourceId);
assert(source); assert(source);
return slint::SharedString(source->color()); return slint::SharedString(source->color());
}); });
models().on_get_source_color_from_id_as_color([&](int sourceId) { models().on_get_source_color_from_id_as_color([&](int sourceId) {
auto source = _mirai_core->getSourceById(sourceId); auto source = _mirai_core->get_source_by_id(sourceId);
assert(source); assert(source);
auto color = selenite::hexStringToColor(source->color()); auto color = selenite::hexStringToColor(source->color());
return slint::Color::from_rgb_uint8(color.r, color.g, color.b); return slint::Color::from_rgb_uint8(color.r, color.g, color.b);
}); });
models().on_get_source_path_from_id([&](int sourceId) { models().on_get_source_path_from_id([&](int sourceId) {
auto source = _mirai_core->getSourceById(sourceId); auto source = _mirai_core->get_source_by_id(sourceId);
assert(source); assert(source);
mirai::markdown_data_provider *sourceProvider = mirai::markdown_data_provider *sourceProvider =
dynamic_cast<mirai::markdown_data_provider *>(source->data_provider()); dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());
return slint::SharedString(sourceProvider->path()); return slint::SharedString(sourceProvider->path());
}); });
_mirai_core->onSourceAdded([&](mirai::source *source) { refreshModels(); }); _mirai_core->on_source_added([&](mirai::source *source) { update_views(); });
_mirai_core->onSourceEdited([&](mirai::source *source) { refreshModels(); }); _mirai_core->on_source_edited([&](mirai::source *source) { update_views(); });
_mirai_core->onSourceDeleted([&](int id) { refreshModels(); }); _mirai_core->on_source_removed([&](int id) { update_views(); });
actions().on_task_clicked([&](int sourceId, int taskId) { actions().on_task_clicked([&](int sourceId, int taskId) {
auto source = _mirai_core->getSourceById(sourceId); auto source = _mirai_core->get_source_by_id(sourceId);
assert(source); assert(source);
auto task = source->get_task_by_id(taskId); auto task = source->get_task_by_id(taskId);
assert(task); assert(task);
task->set_checked(!task->checked()); task->set_checked(!task->checked());
_mirai_core->save(); _mirai_core->save();
update_views(); update_views();
// view_.update();
reloadTasks();
}); });
actions().on_source_clicked([&](int index) { actions().on_source_clicked([&](int index) {
const mirai::source *const source = _mirai_core->getSourceById(index); const mirai::source *const source = _mirai_core->get_source_by_id(index);
assert(source != nullptr); assert(source != nullptr);
if (should_show_source(*source)) { if (should_show_source(*source)) {
hide_source(*source); hide_source(*source);
@ -153,43 +141,45 @@ void app_logic::setupCallbacks()
update_views(); update_views();
}); });
actions().on_add_source([&](slint::SharedString name, slint::SharedString path) { actions().on_add_source([&](slint::SharedString name, slint::SharedString color,
slint::SharedString path) {
std::unique_ptr<mirai::data_provider> file = std::unique_ptr<mirai::data_provider> file =
std::make_unique<mirai::markdown_data_provider>(std::string(path)); std::make_unique<mirai::markdown_data_provider>(std::string(path));
_mirai_core->addSource(std::string(name), "FileSystemMarkdown", std::move(file)); _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, actions().on_edit_source([&](int sourceId, slint::SharedString name, slint::SharedString color,
slint::SharedString path) { slint::SharedString path) {
_mirai_core->editSource(sourceId, std::string(name), std::string(color), std::string(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) { actions().on_delete_task_clicked([&](int sourceId, int taskId) {
auto source = _mirai_core->getSourceById(sourceId); auto source = _mirai_core->get_source_by_id(sourceId);
assert(source); assert(source);
auto task = source->get_task_by_id(taskId); auto task = source->get_task_by_id(taskId);
assert(task); assert(task);
source->remove_task(*task); source->remove_task(*task);
_mirai_core->save(); _mirai_core->save();
// view_.update();
update_views(); update_views();
reloadTasks();
}); });
actions().on_toggle_show_completed_tasks([&] { actions().on_toggle_show_completed_tasks([&] {
// view_.hideCompletedTasks(!view_.shouldHideCompletedTasks());
if (should_hide_completed_tasks()) { if (should_hide_completed_tasks()) {
show_completed_tasks(); show_completed_tasks();
} else { } else {
hide_completed_tasks(); hide_completed_tasks();
} }
// view_.update();
update_views(); update_views();
reloadTasks();
}); });
actions().on_save_task([&](ui::SaveTaskData newTaskData) { actions().on_save_task([&](ui::SaveTaskData newTaskData) {
auto source = _mirai_core->getSourceById(newTaskData.sourceId); auto source = _mirai_core->get_source_by_id(newTaskData.sourceId);
assert(source); assert(source);
auto task = source->get_task_by_id(newTaskData.id); auto task = source->get_task_by_id(newTaskData.id);
assert(task.has_value()); assert(task.has_value());
@ -198,9 +188,7 @@ void app_logic::setupCallbacks()
task->set_title(std::string(newTaskData.title)); task->set_title(std::string(newTaskData.title));
_mirai_core->save(); _mirai_core->save();
// view_.update();
update_views(); update_views();
reloadTasks();
}); });
actions().on_create_task([&](ui::NewTaskData newTaskData) { actions().on_create_task([&](ui::NewTaskData newTaskData) {
@ -208,7 +196,7 @@ void app_logic::setupCallbacks()
if (newTaskData.date.year != 0) { if (newTaskData.date.year != 0) {
date = slint_date_to_mirai_date(newTaskData.date); date = slint_date_to_mirai_date(newTaskData.date);
} }
auto source = _mirai_core->getSourceById(newTaskData.sourceId); auto source = _mirai_core->get_source_by_id(newTaskData.sourceId);
std::optional<mirai::event> event = std::nullopt; std::optional<mirai::event> event = std::nullopt;
if (newTaskData.eventId >= 0) { if (newTaskData.eventId >= 0) {
@ -221,13 +209,11 @@ void app_logic::setupCallbacks()
}); });
_mirai_core->save(); _mirai_core->save();
// view_.update();
update_views(); update_views();
reloadTasks();
}); });
actions().on_delete_event([&](int sourceId, int eventId) { actions().on_delete_event([&](int sourceId, int eventId) {
auto source = _mirai_core->getSourceById(sourceId); auto source = _mirai_core->get_source_by_id(sourceId);
assert(source); assert(source);
auto event = source->get_event_by_id(eventId); auto event = source->get_event_by_id(eventId);
assert(event.has_value()); assert(event.has_value());
@ -235,13 +221,12 @@ void app_logic::setupCallbacks()
_mirai_core->save(); _mirai_core->save();
// view_.update(); // view_.update();
update_views(); update_views();
reloadTasks();
}); });
actions().on_create_event([&](ui::CreateEventParams newEventParams) { actions().on_create_event([&](ui::CreateEventParams newEventParams) {
const ui::Date &date = newEventParams.date; const ui::Date &date = newEventParams.date;
const std::string dateStr = slint_date_to_std_string(date); const std::string dateStr = slint_date_to_std_string(date);
auto source = _mirai_core->getSourceById(newEventParams.source_id); auto source = _mirai_core->get_source_by_id(newEventParams.source_id);
source->create_event({ source->create_event({
.title = std::string(newEventParams.title), .title = std::string(newEventParams.title),
@ -264,159 +249,6 @@ stdToSlintStringVector(const std::vector<std::string> &stdVector)
return slintVector; return slintVector;
} }
void app_logic::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<slint::VectorModel<ui::Day>>();*/
/*for (int dayIndex = 0; dayIndex < dates.size(); ++dayIndex) {*/
/*auto &currentDate = 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 &currentEvent = 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 &currentEvent = 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);*/
/*}*/
}
void app_logic::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 app_logic::refreshModels()
{
show_all_sources();
update_views();
}
void app_logic::update_views() void app_logic::update_views()
{ {
update_sidebar_view(); update_sidebar_view();
@ -427,14 +259,14 @@ void app_logic::update_views()
void app_logic::update_available_sources() void app_logic::update_available_sources()
{ {
const auto &sources = _mirai_core->getSources(); const auto &sources = _mirai_core->get_sources();
auto available_sources = std::make_shared<slint::VectorModel<ui::AvailableSource>>(); auto available_sources = std::make_shared<slint::VectorModel<ui::AvailableSource>>();
auto available_sources_strings = std::make_shared<slint::VectorModel<slint::SharedString>>(); auto available_sources_strings = std::make_shared<slint::VectorModel<slint::SharedString>>();
for (auto &source : sources) { for (auto &source : sources) {
mirai::markdown_data_provider *sourceProvider = mirai::markdown_data_provider *sourceProvider =
dynamic_cast<mirai::markdown_data_provider *>(source->data_provider()); dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());
const auto color = const auto color =
selenite::hexStringToColor(_mirai_core->getSourceById(source->id)->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); auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b);
available_sources->push_back( available_sources->push_back(
@ -449,14 +281,14 @@ void app_logic::update_available_sources()
void app_logic::update_sidebar_view() void app_logic::update_sidebar_view()
{ {
const auto &sources = _mirai_core->getSources(); const auto &sources = _mirai_core->get_sources();
auto new_sources = std::make_shared<slint::VectorModel<ui::SidebarViewSources>>(); auto new_sources = std::make_shared<slint::VectorModel<ui::SidebarViewSources>>();
for (auto &source : sources) { for (auto &source : sources) {
mirai::markdown_data_provider *sourceProvider = mirai::markdown_data_provider *sourceProvider =
dynamic_cast<mirai::markdown_data_provider *>(source->data_provider()); dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());
const auto color = const auto color =
selenite::hexStringToColor(_mirai_core->getSourceById(source->id)->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); auto source_color = slint::Color::from_rgb_uint8(color.r, color.g, color.b);
new_sources->push_back({ new_sources->push_back({
.id = source->id, .id = source->id,
@ -497,7 +329,7 @@ void app_logic::update_calendar_view()
auto today = mirai::date(std::chrono::system_clock::now()); auto today = mirai::date(std::chrono::system_clock::now());
auto new_slint_dates = std::make_shared<slint::VectorModel<ui::CalendarViewDate>>(); auto new_slint_dates = std::make_shared<slint::VectorModel<ui::CalendarViewDate>>();
auto &sources = _mirai_core->getSources(); auto &sources = _mirai_core->get_sources();
auto all_events = merge_all_events_from_sources(sources); auto all_events = merge_all_events_from_sources(sources);
for (int day_index = 0; day_index < 7; ++day_index) { for (int day_index = 0; day_index < 7; ++day_index) {
@ -518,7 +350,7 @@ void app_logic::update_calendar_view()
for (int event_index = 0; event_index < events_for_date.size(); ++event_index) { for (int event_index = 0; event_index < events_for_date.size(); ++event_index) {
auto &current_event = events_for_date.at(event_index); auto &current_event = events_for_date.at(event_index);
// TODO directly remove the source instead of this workaround after data layer refacto // TODO directly remove the source instead of this workaround after data layer refacto
auto source = _mirai_core->getSourceById(current_event.source_id()); auto source = _mirai_core->get_source_by_id(current_event.source_id());
if (!should_show_source(*source)) { if (!should_show_source(*source)) {
continue; continue;
} }
@ -618,7 +450,7 @@ void app_logic::update_tasks_view()
auto today = mirai::date(std::chrono::system_clock::now()); auto today = mirai::date(std::chrono::system_clock::now());
auto new_slint_dates = std::make_shared<slint::VectorModel<ui::TasksViewDate>>(); auto new_slint_dates = std::make_shared<slint::VectorModel<ui::TasksViewDate>>();
auto &sources = _mirai_core->getSources(); auto &sources = _mirai_core->get_sources();
auto all_tasks_dates = group_tasks_by_date(get_all_tasks_from_sources(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) { for (int date_index = 0; date_index < all_tasks_dates.size(); ++date_index) {
@ -637,7 +469,7 @@ void app_logic::update_tasks_view()
for (auto &source_group : current_date_group.sources) { for (auto &source_group : current_date_group.sources) {
auto new_slint_source = ui::TasksViewSource(); auto new_slint_source = ui::TasksViewSource();
auto new_slint_tasks = std::make_shared<slint::VectorModel<ui::TasksViewTask>>(); auto new_slint_tasks = std::make_shared<slint::VectorModel<ui::TasksViewTask>>();
auto source = _mirai_core->getSourceById(source_group.id); auto source = _mirai_core->get_source_by_id(source_group.id);
if (!should_show_source(*source)) { if (!should_show_source(*source)) {
continue; continue;
} }
@ -655,7 +487,7 @@ void app_logic::update_tasks_view()
new_slint_source.tasks = new_slint_tasks; new_slint_source.tasks = new_slint_tasks;
new_slint_source.id = source_group.id; new_slint_source.id = source_group.id;
new_slint_source.name = new_slint_source.name =
slint::SharedString(_mirai_core->getSourceById(source_group.id)->name()); slint::SharedString(_mirai_core->get_source_by_id(source_group.id)->name());
const auto color = selenite::hexStringToColor(source->color()); const auto color = selenite::hexStringToColor(source->color());
new_slint_source.color = slint::Color::from_rgb_uint8(color.r, color.g, color.b); new_slint_source.color = slint::Color::from_rgb_uint8(color.r, color.g, color.b);
if (new_slint_source.tasks->row_count() > 0) { if (new_slint_source.tasks->row_count() > 0) {
@ -699,7 +531,7 @@ bool app_logic::should_show_source(const mirai::source &source) const
void app_logic::show_all_sources() void app_logic::show_all_sources()
{ {
auto &sources = _mirai_core->getSources(); auto &sources = _mirai_core->get_sources();
for (auto &source : sources) { for (auto &source : sources) {
show_source(*source); show_source(*source);
} }

View file

@ -21,10 +21,6 @@ class app_logic
void run(); void run();
void reloadSources();
void reloadTasks();
void refreshModels();
void update_views(); void update_views();
void update_sidebar_view(); void update_sidebar_view();
void update_calendar_view(); void update_calendar_view();
@ -42,7 +38,7 @@ class app_logic
bool should_hide_completed_tasks() const; bool should_hide_completed_tasks() const;
private: private:
void setupCallbacks(); void setup_callbacks();
const ui::AppModels &models(); const ui::AppModels &models();
const ui::AppActions &actions(); const ui::AppActions &actions();

View file

@ -32,8 +32,9 @@ export struct CreateEventParams {
export global AppActions { export global AppActions {
callback task-clicked(int, int); callback task-clicked(int, int);
callback source-clicked(int); callback source-clicked(int);
callback add-source(name: string, path: string); callback add-source(name: string, color: string, path: string);
callback edit-source(sourceId: int, name: string, color: string, path: string); callback edit-source(sourceId: int, name: string, color: string, path: string);
callback remove-source(sourceId: int);
callback toggle-show-completed-tasks(); callback toggle-show-completed-tasks();
callback delete-task-clicked(int, int); callback delete-task-clicked(int, int);

View file

@ -9,6 +9,7 @@ export component AddSourceModal inherits Modal {
private property <int> source-id-to-edit: -1; private property <int> source-id-to-edit: -1;
private property <string> name: ""; private property <string> name: "";
private property <string> path: ""; private property <string> path: "";
private property <string> color_: "#ffffff";
public function open() { public function open() {
root.show(); root.show();
@ -21,6 +22,10 @@ export component AddSourceModal inherits Modal {
label: "Name"; label: "Name";
text <=> root.name; text <=> root.name;
} }
colorInput := VTextInput {
label: "Color";
text <=> root.color_;
}
pathInput := VTextInput { pathInput := VTextInput {
label: "Path"; label: "Path";
text <=> root.path; text <=> root.path;
@ -28,7 +33,7 @@ export component AddSourceModal inherits Modal {
VButton { VButton {
text: "Add"; text: "Add";
clicked => { clicked => {
AppActions.add-source(name, path); AppActions.add-source(name, color_, path);
root.close(); root.close();
} }
} }

View file

@ -30,10 +30,10 @@ export component EditSourceModal inherits Modal {
label: "Color"; label: "Color";
text <=> root.color_; text <=> root.color_;
} }
pathInput := VTextInput { //pathInput := VTextInput {
label: "Path"; //label: "Path";
text <=> root.path; //text <=> root.path;
} //}
VButton { VButton {
text: "Save"; text: "Save";
clicked => { clicked => {
@ -45,7 +45,8 @@ export component EditSourceModal inherits Modal {
text: "Delete"; text: "Delete";
background-color: Palette.red; background-color: Palette.red;
double-clicked => { double-clicked => {
//root.delete-source(root.id) AppActions.remove-source(source-id-to-edit);
root.close();
} }
} }
} }

View file

@ -66,7 +66,11 @@ export component SideBar inherits Rectangle {
TouchArea { TouchArea {
clicked => { AppActions.source-clicked(source.id) } clicked => { AppActions.source-clicked(source.id) }
//right-clicked => { editSourcePopup.edit(source.id) } pointer-event(e) => {
if (e.button == PointerEventButton.right && e.kind == PointerEventKind.up) {
editSourcePopup.edit(source.id)
}
}
} }
} }
} }