Example #1
0
 private async void _objChatSignalRService_OnGroupMessageReceived(object sender, GroupMessageResponseViewModel e)
 {
     try
     {
         if (e.MessageId != 0)
         {
             var chatmessage = GroupRepository.CheckGroupChatMessage(e.MessageId, e.GroupId);
             if (chatmessage == null)
             {
                 GroupRepository.SaveGroupMessage(e, "ChatList");
                 var chatConversation = ChatConversationRepository.GetConversationbyGroupId(e.GroupId);
                 if (e.Attachments.Count > 0)
                 {
                     e.Message = (string.IsNullOrEmpty(e.Message)) ? e.Attachments[e.Attachments.Count - 1].Type : e.Message;
                 }
                 chatConversation = ChatConversationRepository.UpdateChatLastMessage(chatConversation.id, e.Message, e.SenderProfileName);
                 if (chatConversation != null)
                 {
                     Activity.RunOnUiThread(() =>
                     {
                         MessageReceived(chatConversation, "OldChat");
                     });
                 }
                 //else
                 //{
                 //    Activity.RunOnUiThread(() =>
                 //    {
                 //        //chatConversation = new ChatConversation();
                 //        //chatConversation.ChatId = e.ChatId;
                 //        //chatConversation.ContactId = e.ContactId;
                 //        //chatConversation.CreatedDate = e.MessageTime;
                 //        //chatConversation.
                 //        //MessageReceived(chatConversation, "NewChat");
                 //    });
                 //}
                 GroupMessageStatusViewModel _model = new GroupMessageStatusViewModel();
                 _model.GroupMessageId = e.MessageId;
                 await _objChatSignalRService.SendGroupMessageUpdate(e.SenderId.ToString(), _model, "ChatList");
             }
         }
     }
     catch (Java.Lang.Exception ex)
     {
         Crashes.TrackError(ex);
     }
 }
        private async void _objChatSignalRService_OnGroupMessageReceived(object sender, GroupMessageResponseViewModel e)
        {
            try
            {
                if (e.MessageId != 0)
                {
                    var chatmessage = GroupRepository.CheckGroupChatMessage(e.MessageId, e.GroupId);
                    if (chatmessage == null)
                    {
                        GroupRepository.SaveGroupMessage(e, "ChatList");
                        var chatConversationResponse = ChatConversationRepository.GetConversationbyGroupId(e.GroupId);
                        if (chatConversationResponse.success)
                        {
                            if (e.Attachments.Count > 0)
                            {
                                e.Message = (string.IsNullOrEmpty(e.Message)) ? e.Attachments[e.Attachments.Count - 1].Type : e.Message;
                            }
                            chatConversationResponse.chatConversation = ChatConversationRepository.UpdateChatLastMessage(chatConversationResponse.chatConversation.id, e.Message, e.SenderProfileName);
                            if (chatConversationResponse.chatConversation != null)
                            {
                                InvokeOnMainThread(() =>
                                {
                                    MessageReceived(chatConversationResponse.chatConversation, "OldChat");
                                });
                            }
                        }

                        GroupMessageStatusViewModel _model = new GroupMessageStatusViewModel();
                        _model.GroupMessageId = e.MessageId;
                        await _objChatSignalRService.SendGroupMessageUpdate(e.SenderId.ToString(), _model, "ChatList");
                    }
                }
            }
            catch (Exception ex)
            {
                //Crashes.TrackError(ex);
            }
        }
        public async void ButtonSendChatMessage(string sMessageText)
        {
            try
            {
                if (!string.IsNullOrEmpty(sMessageText))
                {
                    var GroupTempSaveMessage = new GroupMessageResponseViewModel
                    {
                        GroupId     = contactViewModel.GroupId,
                        Message     = sMessageText.Trim(' ', '\n'),
                        Attachments = lstAttachments ?? new List <AttachmentViewModel>(),
                        SenderId    = CommonHelper.GetUserId(),
                        MessageTime = DateTime.Now.ToUniversalTime()
                    };

                    var message   = GroupRepository.SaveGroupMessage(GroupTempSaveMessage, "Private");
                    var GroupSend = new GroupMessageRequestViewModel
                    {
                        GroupId          = contactViewModel.GroupId,
                        Message          = sMessageText.Trim(' ', '\n'),
                        Attachments      = lstAttachments ?? new List <AttachmentViewModel>(),
                        MobileDatabaseId = message.id
                    };


                    lstAttachments = new List <AttachmentViewModel>();
                    try
                    {
                        var result = await new GroupChatService().PostGroupMessageService(GroupSend);

                        if (result.Status == 1)
                        {
                            var GroupMessagelist = JsonConvert.DeserializeObject <GroupMessageResponseViewModel>(result.Response.ToString());
                            Console.WriteLine("posted successfully in grp");

                            var chatConversationResponce = ChatConversationRepository.GetConversationbyGroupId(GroupMessagelist.GroupId);
                            if (chatConversationResponce.success)
                            {
                                ChatConversationRepository.UpdateChatLastMessage(chatConversationResponce.chatConversation.id, GroupMessagelist.Message, GroupMessagelist.SenderProfileName);
                            }
                            var savedItem = GroupRepository.UpdateGroupMessage(GroupMessagelist);
                            await _objChatSignalRService.SendGroup(GroupMessagelist, GroupMessagelist.GroupId);

                            LoadLocalLatestMessages();
                        }
                        else
                        {
                            Console.WriteLine("post failed in grp");
                        }
                    }

                    catch (Exception ex)
                    {
                        Crashes.TrackError(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
            }
        }