aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-10-04 15:48:21 +0200
committerdec05eba <dec05eba@protonmail.com>2020-10-04 15:48:57 +0200
commit30d23a4fb9ad20bff7d7d31c914e9900b1bdf94c (patch)
tree6c4ad387ddfb182acc28268b0cc4648809850c03 /src
parentb64cb2c537d06d822a63f8139ca9bf2ffd80c21f (diff)
Matrix: sort messages by timestamp
Diffstat (limited to 'src')
-rw-r--r--src/Body.cpp25
-rw-r--r--src/QuickMedia.cpp21
2 files changed, 41 insertions, 5 deletions
diff --git a/src/Body.cpp b/src/Body.cpp
index 2134a44..260e90d 100644
--- a/src/Body.cpp
+++ b/src/Body.cpp
@@ -186,6 +186,14 @@ namespace QuickMedia {
//page_scroll = 0.0f;
}
+ int Body::get_index_by_body_item(BodyItem *body_item) {
+ for(int i = 0; i < (int)items.size(); ++i) {
+ if(items[i].get() == body_item)
+ return i;
+ }
+ return -1;
+ }
+
void Body::select_first_item() {
selected_item = 0;
prev_selected_item = selected_item;
@@ -225,6 +233,23 @@ namespace QuickMedia {
}
}
+ void Body::insert_item_by_timestamp(std::shared_ptr<BodyItem> body_item) {
+ for(size_t i = 0; i < items.size(); ++i) {
+ if(body_item->get_timestamp() < items[i]->get_timestamp()) {
+ items.insert(items.begin() + i, std::move(body_item));
+ return;
+ }
+ }
+ items.push_back(std::move(body_item));
+ }
+
+ // TODO: Optimize by resizing |items| before insert
+ void Body::insert_items_by_timestamps(BodyItems new_items) {
+ for(auto &new_item : new_items) {
+ insert_item_by_timestamp(std::move(new_item));
+ }
+ }
+
void Body::clear_thumbnails() {
item_thumbnail_textures.clear();
}
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index f18c0ce..776093d 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -4071,9 +4071,16 @@ namespace QuickMedia {
if(sync_future_room_id == current_room_id || !synced) {
int num_items = tabs[MESSAGES_TAB_INDEX].body->items.size();
bool scroll_to_end = (num_items == 0 || (num_items > 0 && tabs[MESSAGES_TAB_INDEX].body->get_selected_item() == num_items - 1));
- tabs[MESSAGES_TAB_INDEX].body->append_items(std::move(sync_result.body_items));
- if(scroll_to_end)
+
+ BodyItem *selected_item = tabs[MESSAGES_TAB_INDEX].body->get_selected();
+ tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(std::move(sync_result.body_items));
+ if(selected_item && !scroll_to_end) {
+ int selected_item_index = tabs[MESSAGES_TAB_INDEX].body->get_index_by_body_item(selected_item);
+ if(selected_item_index != -1)
+ tabs[MESSAGES_TAB_INDEX].body->set_selected_item(selected_item_index);
+ } else if(scroll_to_end) {
tabs[MESSAGES_TAB_INDEX].body->select_last_item();
+ }
}
// Initial sync
@@ -4112,9 +4119,13 @@ namespace QuickMedia {
// Ignore finished fetch of messages if it happened in another room. When we navigate back to the room we will get the messages again
size_t num_new_messages = new_body_items.size();
if(previous_messages_future_room_id == current_room_id && num_new_messages > 0) {
- int selected_item_index = tabs[MESSAGES_TAB_INDEX].body->get_selected_item();
- tabs[MESSAGES_TAB_INDEX].body->prepend_items(std::move(new_body_items));
- tabs[MESSAGES_TAB_INDEX].body->set_selected_item(selected_item_index + num_new_messages);
+ BodyItem *selected_item = tabs[MESSAGES_TAB_INDEX].body->get_selected();
+ tabs[MESSAGES_TAB_INDEX].body->insert_items_by_timestamps(std::move(new_body_items));
+ if(selected_item) {
+ int selected_item_index = tabs[MESSAGES_TAB_INDEX].body->get_index_by_body_item(selected_item);
+ if(selected_item_index != -1)
+ tabs[MESSAGES_TAB_INDEX].body->set_selected_item(selected_item_index);
+ }
}
fetching_previous_messages_running = false;
}