From 6f3eee1430b1e3bdb22b8c532f8b609015d01bfb Mon Sep 17 00:00:00 2001 From: fiaxh Date: Thu, 16 Mar 2017 16:28:32 +0100 Subject: Improve when to send chat state notifications --- libdino/src/service/chat_interaction.vala | 80 ++++++++++++++++++------------- 1 file changed, 47 insertions(+), 33 deletions(-) (limited to 'libdino/src') diff --git a/libdino/src/service/chat_interaction.vala b/libdino/src/service/chat_interaction.vala index 9d5c19e7..88ddbd19 100644 --- a/libdino/src/service/chat_interaction.vala +++ b/libdino/src/service/chat_interaction.vala @@ -4,6 +4,7 @@ using Xmpp; using Dino.Entities; namespace Dino { + public class ChatInteraction : StreamInteractionModule, Object { private const string id = "chat_interaction"; @@ -37,40 +38,33 @@ public class ChatInteraction : StreamInteractionModule, Object { } } - public void window_focus_in(Conversation? conversation) { - on_conversation_selected(selected_conversation); + public void on_window_focus_in(Conversation? conversation) { + on_conversation_focused(conversation); } - public void window_focus_out(Conversation? conversation) { - focus_in = false; + public void on_window_focus_out(Conversation? conversation) { + on_conversation_unfocused(conversation); } - public void on_message_entered(Conversation conversation) { - if (Settings.instance().send_read) { - if (!last_input_interaction.has_key(conversation) && conversation.type_ != Conversation.Type.GROUPCHAT) { - send_chat_state_notification(conversation, Xep.ChatStateNotifications.STATE_COMPOSING); - } + public void on_message_entered(Conversation? conversation) { + if (!last_input_interaction.has_key(conversation)) { + send_chat_state_notification(conversation, Xep.ChatStateNotifications.STATE_COMPOSING); } last_input_interaction[conversation] = new DateTime.now_utc(); last_interface_interaction[conversation] = new DateTime.now_utc(); } - public void on_message_cleared(Conversation conversation) { + public void on_message_cleared(Conversation? conversation) { if (last_input_interaction.has_key(conversation)) { last_input_interaction.unset(conversation); - last_interface_interaction.unset(conversation); send_chat_state_notification(conversation, Xep.ChatStateNotifications.STATE_ACTIVE); } } public void on_conversation_selected(Conversation? conversation) { + on_conversation_unfocused(selected_conversation); selected_conversation = conversation; - focus_in = true; - if (conversation != null) { - conversation_read(selected_conversation); - check_send_read(); - selected_conversation.read_up_to = MessageManager.get_instance(stream_interactor).get_last_message(conversation); - } + on_conversation_focused(conversation); } internal string get_id() { @@ -81,6 +75,29 @@ public class ChatInteraction : StreamInteractionModule, Object { return (ChatInteraction) stream_interactor.get_module(id); } + private void on_message_sent(Entities.Message message, Conversation conversation) { + last_input_interaction.unset(conversation); + last_interface_interaction.unset(conversation); + conversation.read_up_to = message; + } + + private void on_conversation_focused(Conversation? conversation) { + focus_in = true; + if (conversation == null) return; + conversation_read(selected_conversation); + check_send_read(); + selected_conversation.read_up_to = MessageManager.get_instance(stream_interactor).get_last_message(conversation); + } + + private void on_conversation_unfocused(Conversation? conversation) { + focus_in = false; + if (conversation == null) return; + if (last_input_interaction.has_key(conversation)) { + send_chat_state_notification(conversation, Xep.ChatStateNotifications.STATE_PAUSED); + last_input_interaction.unset(conversation); + } + } + private void check_send_read() { if (selected_conversation == null || selected_conversation.type_ == Conversation.Type.GROUPCHAT) return; Entities.Message? message = MessageManager.get_instance(stream_interactor).get_last_message(selected_conversation); @@ -92,24 +109,24 @@ public class ChatInteraction : StreamInteractionModule, Object { } private bool update_interactions() { - ArrayList remove_input = new ArrayList(Conversation.equals_func); - ArrayList remove_interface = new ArrayList(Conversation.equals_func); - foreach (Conversation conversation in last_input_interaction.keys) { + for (MapIterator iter = last_input_interaction.map_iterator(); iter.has_next(); iter.next()) { + if (!iter.valid && iter.has_next()) iter.next(); + Conversation conversation = iter.get_key(); if (last_input_interaction.has_key(conversation) && (new DateTime.now_utc()).difference(last_input_interaction[conversation]) >= 15 * TimeSpan.SECOND) { - remove_input.add(conversation); + iter.unset(); send_chat_state_notification(conversation, Xep.ChatStateNotifications.STATE_PAUSED); } } - foreach (Conversation conversation in last_interface_interaction.keys) { + for (MapIterator iter = last_interface_interaction.map_iterator(); iter.has_next(); iter.next()) { + if (!iter.valid && iter.has_next()) iter.next(); + Conversation conversation = iter.get_key(); if (last_interface_interaction.has_key(conversation) && (new DateTime.now_utc()).difference(last_interface_interaction[conversation]) >= 1.5 * TimeSpan.MINUTE) { - remove_interface.add(conversation); + iter.unset(); send_chat_state_notification(conversation, Xep.ChatStateNotifications.STATE_GONE); } } - foreach (Conversation conversation in remove_input) last_input_interaction.unset(conversation); - foreach (Conversation conversation in remove_interface) last_interface_interaction.unset(conversation); return true; } @@ -123,24 +140,21 @@ public class ChatInteraction : StreamInteractionModule, Object { } } - private void on_message_sent(Entities.Message message, Conversation conversation) { - last_input_interaction.unset(conversation); - last_interface_interaction.unset(conversation); - conversation.read_up_to = message; - } - private void send_chat_marker(Conversation conversation, Entities.Message message, string marker) { Core.XmppStream stream = stream_interactor.get_stream(conversation.account); - if (stream != null && Settings.instance().send_read && Xep.ChatMarkers.Module.requests_marking(message.stanza)) { + if (stream != null && Settings.instance().send_read && + Xep.ChatMarkers.Module.requests_marking(message.stanza)) { stream.get_module(Xep.ChatMarkers.Module.IDENTITY).send_marker(stream, message.stanza.from, message.stanza_id, message.get_type_string(), marker); } } private void send_chat_state_notification(Conversation conversation, string state) { Core.XmppStream stream = stream_interactor.get_stream(conversation.account); - if (stream != null && Settings.instance().send_read) { + if (stream != null && Settings.instance().send_read && + conversation.type_ != Conversation.Type.GROUPCHAT) { stream.get_module(Xep.ChatStateNotifications.Module.IDENTITY).send_state(stream, conversation.counterpart.to_string(), state); } } } + } \ No newline at end of file -- cgit v1.2.3-70-g09d2