[WIP] Fix everything

This commit is contained in:
Vyn 2025-06-30 20:26:54 +02:00
parent 15bd0f58a7
commit 686d86df6f
Signed by: vyn
GPG key ID: E1B2BE34E7A971E7
10 changed files with 212 additions and 125 deletions

View file

@ -140,22 +140,15 @@ void AppWindow::setupCallbacks()
});
actions().on_source_clicked([&](int index) {
// index with value -1 is equal to no selection, aka "All"
if (index == -1) {
show_all_sources();
// view_.setAllSources();
const mirai::source *const source = miraiInstance_->getSourceById(index);
assert(source != nullptr);
if (should_show_source(*source)) {
hide_source(*source);
} else {
hide_all_sources();
// view_.removeSources();
const mirai::source *source = miraiInstance_->getSourceById(index);
show_source(*source);
// view_.addSource(*source);
}
// models().set_default_source_index(index == -1 ? 0 : index);
update_views();
// view_.update();
reloadSources();
reloadTasks();
});
actions().on_add_source([&](slint::SharedString name, slint::SharedString path) {
@ -248,19 +241,16 @@ void AppWindow::setupCallbacks()
actions().on_create_event([&](ui::CreateEventParams newEventParams) {
const ui::Date &date = newEventParams.date;
const std::string dateStr = SlintDateToStdString(date);
// const auto sourceId = models().get_default_source_index();
// auto source = miraiInstance_->getSourceById(sourceId == -1 ? 0 : sourceId);
auto source = miraiInstance_->getSourceById(newEventParams.source_id);
/* source->create_event({*/
/*.title = std::string(newEventParams.title),*/
/*.date = SlintDateToMiraiDate(newEventParams.date),*/
/*.starts_at = SlintTimeToMiraiTime(newEventParams.startsAt),*/
/*.ends_at = SlintTimeToMiraiTime(newEventParams.endsAt),*/
/*});*/
source->create_event({
.title = std::string(newEventParams.title),
.date = SlintDateToMiraiDate(newEventParams.date),
.starts_at = SlintTimeToMiraiTime(newEventParams.starts_at),
.ends_at = SlintTimeToMiraiTime(newEventParams.ends_at),
});
miraiInstance_->save();
// view_.update();
update_views();
reloadTasks();
});
}
@ -432,22 +422,48 @@ void AppWindow::update_views()
update_sidebar_view();
update_calendar_view();
update_tasks_view();
update_available_sources();
}
void AppWindow::update_available_sources()
{
const auto &sources = miraiInstance_->getSources();
auto available_sources = std::make_shared<slint::VectorModel<ui::AvailableSource>>();
auto available_sources_strings = std::make_shared<slint::VectorModel<slint::SharedString>>();
for (auto &source : sources) {
mirai::markdown_data_provider *sourceProvider =
dynamic_cast<mirai::markdown_data_provider *>(source->data_provider());
const auto color =
selenite::hexStringToColor(miraiInstance_->getSourceById(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 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}
);
const auto color =
selenite::hexStringToColor(miraiInstance_->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,
.name = slint::SharedString(source->name()),
.color = source_color,
.active = _source_filters.contains(source->id) && _source_filters.at(source->id),
});
}
_sidebar_view.sources = new_sources;
@ -457,7 +473,7 @@ void AppWindow::update_sidebar_view()
}
std::vector<mirai::Event>
get_all_events_from_sources(std::vector<std::unique_ptr<mirai::source>> &sources)
merge_all_events_from_sources(std::vector<std::unique_ptr<mirai::source>> &sources)
{
std::vector<mirai::Event> all_events;
for (auto &source : sources) {
@ -482,7 +498,7 @@ void AppWindow::update_calendar_view()
auto new_slint_dates = std::make_shared<slint::VectorModel<ui::CalendarViewDate>>();
auto &sources = miraiInstance_->getSources();
auto all_events = get_all_events_from_sources(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 =
@ -501,6 +517,11 @@ void AppWindow::update_calendar_view()
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 &current_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());
if (!should_show_source(*source)) {
continue;
}
new_slint_events->push_back(
ui::CalendarViewEvent{
.source_id = current_event.source_id(),
@ -613,26 +634,29 @@ void AppWindow::update_tasks_view()
}
auto new_slint_sources = std::make_shared<slint::VectorModel<ui::TasksViewSource>>();
for (auto &source : current_date_group.sources) {
for (auto &source_group : 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) {
auto source = miraiInstance_->getSourceById(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.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.id;
new_slint_source.id = source_group.id;
new_slint_source.name =
slint::SharedString(miraiInstance_->getSourceById(source.id)->name());
const auto color =
selenite::hexStringToColor(miraiInstance_->getSourceById(source.id)->color());
slint::SharedString(miraiInstance_->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) {
new_slint_sources->push_back(new_slint_source);
@ -659,10 +683,26 @@ void AppWindow::update_tasks_view()
void AppWindow::show_source(const mirai::source &source)
{
_source_filters.insert_or_assign(source.id, true);
}
void AppWindow::hide_source(const mirai::source &source)
{
_source_filters.insert_or_assign(source.id, false);
}
bool AppWindow::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()
{
auto &sources = miraiInstance_->getSources();
for (auto &source : sources) {
show_source(*source);
}
}
void AppWindow::hide_all_sources()