public void LoadLocalPreviousMessages()
 {
     try
     {
         loadList = false;
         paginationModel.SkipRecords += 30;
         if (chatConversation != null)
         {
             int PreviousListCount = ListChatsCon.Values.Sum(list => list.Count);
             ListChatsCon = ChatMessageRepository.GetChatMessagesForPageIndex(paginationModel, chatConversation.ChatId);
             int CurrentListCount = ListChatsCon.Values.Sum(list => list.Count);
             if (ListChatsCon != null && ListChatsCon.Count > 0)
             {
                 mAdapter = new PrivateMessgeAdapter(this, ListChatsCon);
                 mRecyclerView.SetAdapter(mAdapter);
                 mAdapter.NotifyDataSetChanged();
                 mRecyclerView.ScrollToPosition(CurrentListCount - PreviousListCount - 2);
                 loadList = true;
             }
         }
     }
     catch (Exception e)
     {
         Crashes.TrackError(e);
     }
 }
        public void LoadLocalLatestMessages()
        {
            try
            {
                loadList = false;

                if (chatConversation != null)
                {
                    ListChatsCon = ChatMessageRepository.GetChatMessagesForPageIndex(paginationModel, chatConversation.ChatId);

                    if (ListChatsCon != null && ListChatsCon.Count > 0)
                    {
                        mAdapter = new PrivateMessgeAdapter(this, ListChatsCon);
                        mRecyclerView.SetAdapter(mAdapter);
                        mAdapter.NotifyDataSetChanged();
                        mRecyclerView.ScrollToPosition(mAdapter.ItemCount - 1);
                        loadList = true;
                    }
                }
            }
            catch (Exception e)
            {
                Crashes.TrackError(e);
            }
        }
        public async void ButtonSendChatMessage()
        {
            try
            {
                if (!string.IsNullOrEmpty(txtChatSendMessage.Text) || lstAttachments.Count > 0)
                {
                    txtChatSendMessage.Text = txtChatSendMessage.Text.Trim(' ', '\n');

                    var chatModel = new ChatMessageViewModel
                    {
                        Attachments = lstAttachments ?? new List <AttachmentViewModel>(),
                        ContactId   = contactId,
                        Message     = txtChatSendMessage.Text,
                        IsRead      = false,
                        IsRecieved  = false,
                        IsSend      = false,
                        MessageTime = DateTime.Now.ToUniversalTime()
                    };


                    chatConversation = ChatConversationRepository.GetConversationbyContactId(contactId);
                    if (chatConversation != null)
                    {
                        var message = ChatMessageRepository.SaveChatMessage(chatModel, chatConversation.ChatId);
                        chatModel.MobiledatabaseId = message.id;
                        chatModel.ChatId           = chatConversation.ChatId;
                        chatModel.IsRead           = false;
                        chatModel.IsRecieved       = false;
                        chatModel.IsSend           = false;
                        mAdapter.add(message);
                        mAdapter.NotifyDataSetChanged();
                        mRecyclerView.ScrollToPosition(mAdapter.ItemCount - 1);
                        txtChatSendMessage.Text = "";
                    }
                    lstAttachments          = new List <AttachmentViewModel>();
                    txtChatSendMessage.Text = "";
                    try
                    {
                        var result = await new ChatService().PostChat(chatModel);

                        if (result.Status == 2 || result.Status == 1)
                        {
                            var ChatResponse = JsonConvert.DeserializeObject <PostChatResponseViewModel>(result.Response.ToString());

                            if (chatConversation == null)
                            {
                                ChatConversationRepository.SaveConverstionNewFromServer(ChatResponse.Chat);
                                chatConversation = ChatConversationRepository.GetConversationbyContactId(contactId);

                                ChatConversationRepository.UpdateChatLastMessage(chatConversation.id, ChatResponse.ChatMessage.Message, "");

                                var savedMessages = ChatMessageRepository.SaveChatMessage(ChatResponse.ChatMessage, chatConversation.ChatId);

                                LoadLocalLatestMessages();
                                txtChatSendMessage.Text = "";
                                Console.WriteLine("CHAT POSTED : " + result);
                            }
                            else
                            {
                                chatConversation = ChatConversationRepository.GetConversationbyContactId(contactId);

                                ChatConversationRepository.UpdateChatLastMessage(chatConversation.id, ChatResponse.ChatMessage.Message, "");

                                var savedMessages = ChatMessageRepository.updateChatMessage(ChatResponse.ChatMessage);
                                LoadLocalLatestMessages();
                                await _objChatSignalRService.Send(chatConversation.ContactId.ToString(), ChatResponse.ChatMessage);
                            }
                        }
                    }

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