Exemplo n.º 1
0
        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);
            }
        }