public void CheckNewMessages() { if (IsChatsFill) { MessengerClient msClient = new MessengerClient(); bool isExist = false; List <ChatDTO> chats = msClient.GetChats(Account).ToList(); List <StackPanel> dialogPanels = null; Dispatcher.Invoke(() => dialogPanels = DialogPanels); foreach (var item in chats) { foreach (var panel in dialogPanels) { ChatDTO chatPanel = null; Dispatcher.Invoke(() => chatPanel = panel.Tag as ChatDTO); if (item.Participants == chatPanel.Participants) { isExist = true; if (item.MessageId > chatPanel.MessageId) { Dispatcher.Invoke(() => { chatPanel.MessageId = item.MessageId; AddMessage(panel, msClient.GetMessageById(item.MessageId.Value)); chatScroll.ScrollToVerticalOffset(Math.Pow(100, 100)); return; }); } } } if (!isExist) { bool isContact = false; Dispatcher.Invoke(() => { foreach (Chip acc in panelContacts.Children) { if (item.Participants.Contains((acc.Tag as AccountDTO).Id.ToString())) { isContact = true; } } AccountDTO account = messengerClient.GetUserById(messengerClient.GetMessageById(item.MessageId.Value).DeliverId.Value); if (!isContact & account.Id != Account.Id) { ms = new MemoryStream(account.Image); listMails.Items.Add(new SearchClass { Img = BitmapFrame.Create(ms), Text = account.Name, Tag = account }); } StackPanel panel = new StackPanel { Tag = item, Width = 907 }; AddMessage(panel, msClient.GetMessageById(item.MessageId.Value)); DialogPanels.Add(panel); }); } isExist = false; } } }
public void FillAllChats() { new Task(() => Loading()).Start(); MessengerClient msClient = new MessengerClient(); AccountDTO account = null; Dispatcher.Invoke(() => account = Account); List <ChatDTO> chats = msClient.GetAllMessagesInChats(account).ToList(); bool isExist = false; List <StackPanel> dialogPanels = new List <StackPanel>(); foreach (var item in chats) { foreach (var panel in dialogPanels) { ChatDTO chatPanel = null; Dispatcher.Invoke(() => chatPanel = panel.Tag as ChatDTO); if (item.Participants == chatPanel.Participants) { Dispatcher.Invoke(() => AddMessage(panel, msClient.GetMessageById(item.MessageId.Value))); isExist = true; Dispatcher.Invoke(() => panel.Tag = item); } } if (!isExist) { StackPanel panel = null; bool isContact = false; Dispatcher.Invoke(() => { foreach (Chip ac in panelContacts.Children) { if (item.Participants.Contains((ac.Tag as AccountDTO).Id.ToString())) { isContact = true; } } AccountDTO acc = messengerClient.GetUserById(messengerClient.GetMessageById(item.MessageId.Value).DeliverId.Value); if (!isContact & acc.Id != Account.Id) { ms = new MemoryStream(acc.Image); listMails.Items.Add(new SearchClass { Img = BitmapFrame.Create(ms), Text = acc.Name, Tag = acc }); } panel = new StackPanel { Tag = item, Width = 907 }; AddMessage(panel, msClient.GetMessageById(item.MessageId.Value)); }); dialogPanels.Add(panel); } isExist = false; } Dispatcher.Invoke(() => { DialogPanels.AddRange(dialogPanels.ToArray()); for (int i = 0; i < DialogPanels.Count; i++) { DialogPanels[i].Tag = dialogPanels[i].Tag as ChatDTO; } timer.Stop(); pnlLoading.Visibility = Visibility.Hidden; tbLoading.Visibility = Visibility.Hidden; }); IsChatsFill = true; }