extract contact list sorting and refine sort

This commit is contained in:
Green Sky
2025-03-22 20:05:30 +01:00
parent 0f85bcc128
commit b87866cb0b
5 changed files with 199 additions and 56 deletions

View File

@@ -62,9 +62,6 @@ namespace Components {
int tm_min {0};
};
// empty contact comp that is sorted in the set for displaying
struct ContactSortTag {};
} // Components
namespace Context {
@@ -207,7 +204,8 @@ ChatGui4::ChatGui4(
_b_tc(_bil, tu),
_theme(theme),
_sip(tu),
_ivp(_msg_tc)
_ivp(_msg_tc),
_cls(cs)
{
_os_sr.subscribe(ObjectStore_Event::object_update);
}
@@ -258,57 +256,9 @@ float ChatGui4::render(float time_delta, bool window_hidden, bool window_focused
}
renderContactList();
// after vis check
if (_contact_list_sortable) {
// TODO: extract this; with timer and events to dirty
// !! events !!
auto& cr = _cs.registry();
// first: make sure every cantact we want to have in the list has the tag
// do we pass exclusion to the list widget, or update sort comp? - the later
// TODO: re do from sratch every time?
//cr.clear<Components::ContactSortTag>();
for (const auto cv : cr.view<Contact::Components::TagBig>()) {
(void)cr.get_or_emplace<Components::ContactSortTag>(cv);
}
// second: sort
cr.sort<Components::ContactSortTag>(
[&](const Contact4 lhs, const Contact4 rhs) -> bool {
// TODO: custom sort rules, order
// - groups (> privates)
if (cr.all_of<Contact::Components::TagGroup>(lhs) && !cr.all_of<Contact::Components::TagGroup>(rhs)) {
return true;
} else if (!cr.all_of<Contact::Components::TagGroup>(lhs) && cr.all_of<Contact::Components::TagGroup>(rhs)) {
return false;
}
// - activity (exists)
if (cr.all_of<Contact::Components::LastActivity>(lhs) && !cr.all_of<Contact::Components::LastActivity>(rhs)) {
return true;
} else if (!cr.all_of<Contact::Components::LastActivity>(lhs) && cr.all_of<Contact::Components::LastActivity>(rhs)) {
return false;
}
// else - we can assume both have or dont have LastActivity
// - activity new > old
if (cr.all_of<Contact::Components::LastActivity>(lhs)) {
const auto l = cr.get<Contact::Components::LastActivity>(lhs).ts;
const auto r = cr.get<Contact::Components::LastActivity>(rhs).ts;
if (l > r) {
return true;
} else if (l < r) {
return false;
}
}
// - first seen new > old
// TODO: implement
return false;
},
entt::insertion_sort{} // o(n) in 99% of cases
);
_cls.sort();
}
ImGui::SameLine();
@@ -1715,7 +1665,7 @@ void ChatGui4::renderContactList(void) {
_rmm,
_theme,
_contact_tc,
contact_const_runtime_view{}.iterate(cr.storage<Components::ContactSortTag>()),
contact_const_runtime_view{}.iterate(cr.storage<Contact::Components::ContactSortTag>()),
selected_contact
)) {
_selected_contact = selected_contact.entity();