From fc0d7feb5aef16e0024cfeb73286038998e926d2 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 6 Dec 2017 18:08:20 +0100 Subject: Load messages for conversation view from cache --- libdino/src/service/message_storage.vala | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'libdino') diff --git a/libdino/src/service/message_storage.vala b/libdino/src/service/message_storage.vala index f4691971..27d9504d 100644 --- a/libdino/src/service/message_storage.vala +++ b/libdino/src/service/message_storage.vala @@ -34,9 +34,11 @@ public class MessageStorage : StreamInteractionModule, Object { Gee.List ret = new ArrayList(Message.equals_func); BidirIterator iter = messages[conversation].bidir_iterator(); iter.last(); - while (iter.valid && ret.size < count) { - iter.previous(); - ret.insert(0, iter.get()); + if (messages[conversation].size > 0) { + do { + ret.insert(0, iter.get()); + iter.previous(); + } while (iter.has_previous() && ret.size < count); } return ret; } @@ -49,9 +51,23 @@ public class MessageStorage : StreamInteractionModule, Object { return null; } - public Gee.List? get_messages_before(Conversation? conversation, DateTime before, int count = 20) { - Gee.List db_messages = db.get_messages(conversation.counterpart, conversation.account, Util.get_message_type_for_conversation(conversation), count, before); - return db_messages; + public Gee.List? get_messages_before_message(Conversation? conversation, Message message, int count = 20) { + SortedSet? before = messages[conversation].head_set(message); + if (before != null && before.size >= count) { + Gee.List ret = new ArrayList(Message.equals_func); + Iterator iter = before.iterator(); + iter.next(); + for (int from_index = before.size - count; iter.has_next() && from_index > 0; from_index--) iter.next(); + while(iter.has_next()) { + Message m = iter.get(); + ret.add(m); + iter.next(); + } + return ret; + } else { + Gee.List db_messages = db.get_messages(conversation.counterpart, conversation.account, Util.get_message_type_for_conversation(conversation), count, message.local_time); + return db_messages; + } } public Message? get_message_by_id(string stanza_id, Conversation conversation) { -- cgit v1.2.3-70-g09d2