Load messages for conversation view from cache

This commit is contained in:
fiaxh 2017-12-06 18:08:20 +01:00
parent bbbfdf0551
commit fc0d7feb5a
3 changed files with 38 additions and 10 deletions

View file

@ -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();
if (messages[conversation].size > 0) {
do {
ret.insert(0, iter.get());
iter.previous();
} while (iter.has_previous() && ret.size < count);
}
return ret;
}
@ -49,10 +51,24 @@ 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);
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) {
init_conversation(conversation);

View file

@ -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) {

View file

@ -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);