private async void GetDialogsInfo() { try { var unreadDialogs = await _messageService.GetDialogs(true); foreach (var dialog in unreadDialogs) { if (dialog.Message.ChatId != 0) { continue; } var friend = FoundFriends.FirstOrDefault(f => f.Id == dialog.Message.UserId); if (friend != null) { friend.NumberOfNewMessages = dialog.UnreadMessagesAmount; //UnreadDialogs.Add(friend); } } var newDialogFriends = FoundFriends.Where(f => f.NumberOfNewMessages != null).ToList(); var readDialofFriends = FoundFriends.Where(f => f.NumberOfNewMessages == null).ToList(); newDialogFriends.AddRange(readDialofFriends); FoundFriends = newDialogFriends; OnPropertyChanged("FoundFriends"); } catch (WebException) { } }
private async Task GetMessages(User user) { var previousSelected = SelectedUser; try { SelectedUser = user; var query = (await _messageService.GetDialog(SelectedUser.Id)).ToList(); PraseMessages(query); var messages = query.OrderBy(m => m.Date).ToList(); foreach (var message in messages.ToArray()) { message.Body = _cryptTool.SplitAndUnpackReceivedMessage(message.Body); TakeFileNamesFromBody(message); if (message.Attachments != null && message.Attachments.Where(a => a.File == null).ToList().Count != 0) { message.HasUndefinedContent = true; message.Attachments = new ObservableCollection <Attachment>(message.Attachments.Where(a => a.File != null)); } } if (messages.Count > 0 && messages[0].UserId != SelectedUser.Id) { var inMessage = messages.FirstOrDefault(m => !m.Out); SelectedUser = inMessage != null ? inMessage.User : await _userService.GetUser(SelectedUser.Id); } Messages = messages; } catch (NullReferenceException ex) { SelectedUser = previousSelected; return; } catch (WebException ex) { Messages = null; ShowWebErrorMessage(); return; } finally { ScrollToEnd = true; } SelectedUser.NumberOfNewMessages = null; var sortedUnreadList = FoundFriends.Where(f => f.NumberOfNewMessages != null).ToList(); sortedUnreadList.AddRange(FoundFriends.Where(f => f.NumberOfNewMessages == null).OrderBy(f => f.FullName).ToList()); FoundFriends = new List <User>(sortedUnreadList); if (Messages.Count != 0 && !Messages[0].Out) { _messageService.MarkMessagesAsRead(new List <int> { Messages[0].Id }, SelectedUser.Id); } }