Ejemplo n.º 1
0
        private async void SendMessage()
        {
            if (string.IsNullOrWhiteSpace(MessageToSend) && AttachmentUploads.Count == 0)
            {
                return;
            }

            var message = MessageToSend?.Trim();

            MessageToSend = null;

            var newMessage = new Message(new VkMessage()
            {
                Body = message, IsOut = true, Date = DateTime.Now
            },
                                         ViewModelLocator.Main.CurrentUser);

            newMessage.IsNew = true;

            try
            {
                newMessage.IsSent = false;

                bool hasPendingUploads = AttachmentUploads.Any(a => a.IsUploading);
                bool isChat            = Dialog.Message.ChatId != 0;

                if (AttachmentUploads.Count > 0)
                {
                    if (hasPendingUploads)
                    {
                        await Task.Run(() => _uploadsEvent.Wait()); //ждем завершение загрузок
                    }
                    newMessage.MessageContent.Attachments = new List <VkAttachment>();

                    foreach (var attachment in AttachmentUploads)
                    {
                        if (attachment is PhotoAttachmentUpload)
                        {
                            var photo = (PhotoAttachmentUpload)attachment;

                            newMessage.MessageContent.Attachments.Add(new VkPhotoAttachment(photo.VkPhoto));
                        }
                        else if (attachment is DocumentAttachmentUpload)
                        {
                            var document = (DocumentAttachmentUpload)attachment;

                            newMessage.MessageContent.Attachments.Add(document.VkDocument);
                        }
                        else if (attachment is VideoAttachmentUpload)
                        {
                            var video = (VideoAttachmentUpload)attachment;

                            newMessage.MessageContent.Attachments.Add(new VkVideoAttachment(video.VkVideo));
                        }
                        else if (attachment is ForwardMessagesAttachmentUpload)
                        {
                            var forwardMessages = (ForwardMessagesAttachmentUpload)attachment;

                            newMessage.MessageContent.ForwardMessages = forwardMessages.Messages.Select(m => m.MessageContent).ToList();

                            long dialogId = isChat ? Dialog.Message.ChatId : Dialog.User.Profile.Id;
                            ForwardMessagesHelper.PendingForwardMessages[dialogId].Clear();
                            ForwardMessagesHelper.PendingForwardMessages[dialogId] = null;
                            ForwardMessagesHelper.PendingForwardMessages.Remove(dialogId);
                        }
                    }

                    AttachmentUploads.Clear();
                }

                Messages.Add(newMessage);
                _lastSentMessages.Add(newMessage);

                var newMessageId = await ServiceLocator.Vkontakte.Messages.Send(!isChat?Dialog.User.Profile.Id : 0, isChat?Dialog.Message.ChatId : 0, message, newMessage.MessageContent.Attachments, newMessage.MessageContent.ForwardMessages);

                newMessage.MessageContent.Id = newMessageId;
                newMessage.IsSent            = true;
                _lastSentMessages.Remove(newMessage);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Unable to send message");

                newMessage.IsFailed = true;
            }
        }