aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfiaxh <git@mx.ax.lt>2017-12-06 18:08:20 +0100
committerfiaxh <git@mx.ax.lt>2017-12-13 21:11:33 +0100
commitfc0d7feb5aef16e0024cfeb73286038998e926d2 (patch)
tree22f7c9a365b6323994dccc52725e936a6ff87f6b
parentbbbfdf0551fdb03e793944243dcaaf811e8ac027 (diff)
downloaddino-fc0d7feb5aef16e0024cfeb73286038998e926d2.tar.gz
dino-fc0d7feb5aef16e0024cfeb73286038998e926d2.zip
Load messages for conversation view from cache
-rw-r--r--libdino/src/service/message_storage.vala28
-rw-r--r--main/src/ui/conversation_summary/conversation_view.vala5
-rw-r--r--main/src/ui/conversation_summary/message_populator.vala15
3 files changed, 38 insertions, 10 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) {
diff --git a/main/src/ui/conversation_summary/conversation_view.vala b/main/src/ui/conversation_summary/conversation_view.vala
index 37412335..7924da57 100644
--- a/main/src/ui/conversation_summary/conversation_view.vala
+++ b/main/src/ui/conversation_summary/conversation_view.vala
@@ -74,7 +74,8 @@ public class ConversationView : Box, Plugins.ConversationItemCollection {
populator.init(conversation, this, Plugins.WidgetType.GTK);
}
message_item_populator.init(conversation, this);
- message_item_populator.populate_number(conversation, new DateTime.now_utc(), 50);
+ message_item_populator.populate_latest(conversation, 40);
+ Idle.add(() => { on_value_notify(); return false; });
stack.set_visible_child_name("main");
}
@@ -217,7 +218,7 @@ public class ConversationView : Box, Plugins.ConversationItemCollection {
private void load_earlier_messages() {
was_value = scrolled.vadjustment.value;
if (!reloading_mutex.trylock()) return;
- if (meta_items.size > 0) message_item_populator.populate_number(conversation, meta_items.first().sort_time, 20);
+ if (meta_items.size > 0) message_item_populator.populate_before(conversation, meta_items.first(), 20);
}
private static int sort_meta_items(Plugins.MetaConversationItem a, Plugins.MetaConversationItem b) {
diff --git a/main/src/ui/conversation_summary/message_populator.vala b/main/src/ui/conversation_summary/message_populator.vala
index df44a14b..b342306b 100644
--- a/main/src/ui/conversation_summary/message_populator.vala
+++ b/main/src/ui/conversation_summary/message_populator.vala
@@ -10,6 +10,7 @@ public class MessagePopulator : Object {
private StreamInteractor? stream_interactor;
private Conversation? current_conversation;
private Plugins.ConversationItemCollection? item_collection;
+ private HashMap<Plugins.MetaConversationItem, Message> meta_message = new HashMap<Plugins.MetaConversationItem, Message>();
public MessagePopulator(StreamInteractor stream_interactor) {
this.stream_interactor = stream_interactor;
@@ -30,8 +31,17 @@ public class MessagePopulator : Object {
public void close(Conversation conversation) { }
- public void populate_number(Conversation conversation, DateTime from, int n) {
- Gee.List<Entities.Message>? messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages_before(conversation, from, n);
+ public void populate_latest(Conversation conversation, int n) {
+ Gee.List<Entities.Message>? messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages(conversation, n);
+ if (messages != null) {
+ foreach (Entities.Message message in messages) {
+ handle_message(message, conversation);
+ }
+ }
+ }
+
+ public void populate_before(Conversation conversation, Plugins.MetaConversationItem item, int n) {
+ Gee.List<Entities.Message>? messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages_before_message(conversation, meta_message[item], n);
if (messages != null) {
foreach (Entities.Message message in messages) {
handle_message(message, conversation);
@@ -53,6 +63,7 @@ public class MessagePopulator : Object {
}
Plugins.MetaConversationItem? meta_item = best_provider.get_item(message, conversation);
if (meta_item == null) return;
+ meta_message[meta_item] = message;
meta_item.mark = message.marked;
WeakRef weak_meta_item = WeakRef(meta_item);