private async void masterDetail_pnl_SelectionChanged(object sender, SelectionChangedEventArgs e) { // Send active chat state: foreach (var added in e.AddedItems) { if (added is ChatTemplate) { ChatTemplate c = added as ChatTemplate; if (shouldSendChatState(c.chat)) { await c.client.sendChatStateAsync(c.chat.chatJabberId, XMPP_API.Classes.Network.XML.Messages.XEP_0085.ChatState.ACTIVE); } } } // Send inactive chat state: foreach (var added in e.RemovedItems) { if (added is ChatTemplate) { ChatTemplate c = added as ChatTemplate; if (shouldSendChatState(c.chat)) { await c.client.sendChatStateAsync(c.chat.chatJabberId, XMPP_API.Classes.Network.XML.Messages.XEP_0085.ChatState.INACTIVE); } } } }
/// <summary> /// Loads all chats and inserts them into the chatsList. /// </summary> /// <param name="selectedChatId">The id of the chat which should get selected.</param> private void loadChats(string selectedChatId) { // Load all chats: Task.Run(() => { ChatTemplate selectedChat = null; List <ChatTemplate> chats = getFilterdChats(null); for (int i = 0; i < chats.Count; i++) { if (string.Equals(selectedChatId, chats[i].chat.id)) { selectedChat = chats[i]; } } // Show selected chat: Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Clear list: CHATS.Clear(); // Add chats CHATS.AddRange(chats); if (masterDetail_pnl.SelectedItem == null && selectedChat != null) { masterDetail_pnl.SelectedItem = selectedChat; } }).AsTask(); }); }
/// <summary> /// Sorts the given list based on the chats lastActive date. /// </summary> /// <param name="list">The list which should get sorted.</param> /*private void sortList(List<ChatTemplate> list) * { * list.Sort((a, b) => { return DateTime.Compare(b.chat.lastActive, a.chat.lastActive); }); * }*/ /// <summary> /// Returns a list of ChatTemplates which match the given filter. /// </summary> /// <param name="filter">The filter for filtering the chats.</param> private List <ChatTemplate> getFilterdChats(string filter) { List <ChatTemplate> list = new List <ChatTemplate>(); foreach (XMPPClient c in ConnectionHandler.INSTANCE.getClients()) { foreach (ChatTable chat in ChatDBManager.INSTANCE.getAllChatsForClient(c.getXMPPAccount().getIdAndDomain(), filter)) { ChatTemplate chatElement = new ChatTemplate { chat = chat, client = c }; if (chat.chatType == ChatType.MUC) { chatElement.mucInfo = MUCDBManager.INSTANCE.getMUCInfo(chat.id); } list.Add(chatElement); } } return(list); }
private void INSTANCE_ChatChanged(ChatDBManager handler, ChatChangedEventArgs args) { Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Backup selected chat: ChatTemplate selectedChat = null; if (masterDetail_pnl.SelectedItem != null && masterDetail_pnl.SelectedItem is ChatTemplate) { selectedChat = masterDetail_pnl.SelectedItem as ChatTemplate; } if (args.REMOVED) { CHATS.RemoveId(args.CHAT.id); args.Cancel = true; // Restore selected chat: if (selectedChat != null && !string.Equals(args.CHAT.id, selectedChat.chat.id)) { masterDetail_pnl.SelectedItem = selectedChat; } return; } else { if (CHATS.UpdateChat(args.CHAT)) { args.Cancel = true; // Restore selected chat: if (selectedChat != null) { masterDetail_pnl.SelectedItem = selectedChat; } return; } } Task.Run(() => { // Add the new chat to the list of chats: foreach (XMPPClient c in ConnectionHandler.INSTANCE.getClients()) { if (Equals(args.CHAT.userAccountId, c.getXMPPAccount().getIdAndDomain())) { ChatTemplate chatElement = new ChatTemplate { chat = args.CHAT, client = c }; if (args.CHAT.chatType == ChatType.MUC) { chatElement.mucInfo = MUCDBManager.INSTANCE.getMUCInfo(args.CHAT.id); } if (currentChatQuery == null || doesChatTemplateMatchesFilter(chatElement, currentChatQuery)) { Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { CHATS.Add(chatElement); // Restore selected chat: if (selectedChat != null) { masterDetail_pnl.SelectedItem = selectedChat; } }).AsTask(); } } } }); }).AsTask(); }
/// <summary> /// Check if the given ChatTemplate matches the given filter. /// </summary> /// <param name="template">The ChatTemplate which should get checked.</param> /// <param name="filter">The filter that should get applied.</param> private bool doesChatTemplateMatchesFilter(ChatTemplate template, string filter) { return(template.chat.chatJabberId.ToLower().Contains(filter) || (template.mucInfo != null && template.mucInfo.name != null && template.mucInfo.name.ToLower().Contains(filter))); }