diff options
author | fiaxh <git@mx.ax.lt> | 2017-12-06 18:08:20 +0100 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2017-12-13 21:11:33 +0100 |
commit | fc0d7feb5aef16e0024cfeb73286038998e926d2 (patch) | |
tree | 22f7c9a365b6323994dccc52725e936a6ff87f6b /libdino | |
parent | bbbfdf0551fdb03e793944243dcaaf811e8ac027 (diff) | |
download | dino-fc0d7feb5aef16e0024cfeb73286038998e926d2.tar.gz dino-fc0d7feb5aef16e0024cfeb73286038998e926d2.zip |
Load messages for conversation view from cache
Diffstat (limited to 'libdino')
-rw-r--r-- | libdino/src/service/message_storage.vala | 28 |
1 files changed, 22 insertions, 6 deletions
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<Message> ret = new ArrayList<Message>(Message.equals_func); BidirIterator<Message> 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<Message>? get_messages_before(Conversation? conversation, DateTime before, int count = 20) { - Gee.List<Message> db_messages = db.get_messages(conversation.counterpart, conversation.account, Util.get_message_type_for_conversation(conversation), count, before); - return db_messages; + public Gee.List<Message>? get_messages_before_message(Conversation? conversation, Message message, int count = 20) { + SortedSet<Message>? before = messages[conversation].head_set(message); + if (before != null && before.size >= count) { + Gee.List<Message> ret = new ArrayList<Message>(Message.equals_func); + Iterator<Message> 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<Message> 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) { |