Beispiel #1
0
        public void getUserDetails()
        {
            var chatConversationResponce = ChatConversationRepository.GetConversationbyContactId(contactViewModel.ContactId);

            if (chatConversationResponce.success)
            {
                chatConversation = chatConversationResponce.chatConversation;
            }
            if (contactViewModel != null)
            {
                ContactName = ContactRepository.GetContactbyUserId(contactViewModel.ContactId).name;
                contactId   = contactViewModel.ContactId;
                Title       = ContactName;

                var titleView = new UILabel(new CGRect(0, 0, 100, 60));
                titleView.Text      = ContactName;
                titleView.TextColor = UIColor.White;
                var ShowUserProfileViewController = new UITapGestureRecognizer(() =>
                {
                    var viewController = (UserProfileViewController)Storyboard.InstantiateViewController("UserProfileViewController");
                    viewController.contactViewModel = contactViewModel;
                    NavigationController.PushViewController(viewController, true);
                });
                titleView.UserInteractionEnabled = true;
                titleView.AddGestureRecognizer(ShowUserProfileViewController);
                NavigationItem.TitleView = titleView;
            }
        }
        public async void ButtonSendChatMessage(string sMessageText)
        {
            try
            {
                if (!string.IsNullOrEmpty(sMessageText))
                {
                    var chatModel = new ChatMessageViewModel
                    {
                        Attachments = lstAttachments ?? new List <AttachmentViewModel>(),
                        ContactId   = contactId,
                        Message     = sMessageText.Trim(' ', '\n'),
                        IsRead      = false,
                        IsRecieved  = false,
                        IsSend      = false,
                        MessageTime = DateTime.Now.ToUniversalTime()
                    };


                    var chatConversationResponse = ChatConversationRepository.GetConversationbyContactId(contactId);
                    if (chatConversationResponse.success)
                    {
                        if (chatConversationResponse.chatConversation != null)
                        {
                            var message = ChatMessageRepository.SaveChatMessage(chatModel, chatConversationResponse.chatConversation.ChatId);
                            chatModel.MobiledatabaseId = message.id;
                            chatModel.ChatId           = chatConversationResponse.chatConversation.ChatId;
                            chatModel.IsRead           = false;
                            chatModel.IsRecieved       = false;
                            chatModel.IsSend           = false;
                            var msg = new Message
                            {
                                Type = MessageType.Outgoing,
                                Text = sMessageText.Trim()
                            };

                            messages.Add(msg);
                            tableView.InsertRows(new NSIndexPath[] { NSIndexPath.FromRowSection(messages.Count - 1, 0) }, UITableViewRowAnimation.None);
                            ScrollToBottom(true);
                        }
                    }
                    lstAttachments = new List <AttachmentViewModel>();
                    try
                    {
                        var result = await new ChatService().PostChat(chatModel);

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

                            if (chatConversationResponse.chatConversation == null)
                            {
                                ChatConversationRepository.SaveConverstionNewFromServer(ChatResponse.Chat);
                                chatConversationResponse = ChatConversationRepository.GetConversationbyContactId(contactId);
                                if (chatConversationResponse.success)
                                {
                                    ChatConversationRepository.UpdateChatLastMessage(chatConversationResponse.chatConversation.id, ChatResponse.ChatMessage.Message, "");

                                    var savedMessages = ChatMessageRepository.SaveChatMessage(ChatResponse.ChatMessage, chatConversationResponse.chatConversation.ChatId);
                                }
                                LoadLocalLatestMessages();
                                Console.WriteLine("CHAT POSTED : " + result);
                            }
                            else
                            {
                                chatConversationResponse = ChatConversationRepository.GetConversationbyContactId(contactId);
                                if (chatConversationResponse.success)
                                {
                                    ChatConversationRepository.UpdateChatLastMessage(chatConversationResponse.chatConversation.id, ChatResponse.ChatMessage.Message, "");
                                }
                                var savedMessages = ChatMessageRepository.updateChatMessage(ChatResponse.ChatMessage);
                                LoadLocalLatestMessages();
                                await _objChatSignalRService.Send(chatConversationResponse.chatConversation.ContactId.ToString(), ChatResponse.ChatMessage);
                            }
                        }
                    }

                    catch (Exception ex)
                    {
                        Crashes.TrackError(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
            }
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            try
            {
                base.OnCreate(savedInstanceState);

                AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
                TaskScheduler.UnobservedTaskException      += TaskSchedulerOnUnobservedTaskException;

                SetContentView(Resource.Layout.ChatMessagelayout);
                var requiredPermissions = new String[]
                {
                    Manifest.Permission.Internet,
                    Manifest.Permission.WriteExternalStorage,
                    Manifest.Permission.ReadExternalStorage,
                    Manifest.Permission.Camera,
                    Manifest.Permission.ReadContacts
                };
                ActivityCompat.RequestPermissions(this, requiredPermissions, REQUEST_LOCATION);

                var toolbar = FindViewById <Android.Support.V7.Widget.Toolbar>(Resource.Id.app_bar);
                SetSupportActionBar(toolbar);
                //SupportActionBar.SetDisplayHomeAsUpEnabled(true);
                //SupportActionBar.SetHomeButtonEnabled(true);
                //SupportActionBar.SetDisplayUseLogoEnabled(true);
                ////SupportActionBar.SetIcon(Resource.Drawable.logo512_512);
                LinearLayout    li_toolbarDetails   = FindViewById <LinearLayout>(Resource.Id.li_toolbarDetails);
                CircleImageView IV_Userlogo         = FindViewById <CircleImageView>(Resource.Id.iv_customforUserlogo);
                CircleImageView _IVarrow_back_white = FindViewById <CircleImageView>(Resource.Id.iv_arrow_back_white);
                TextView        toolbar_title       = FindViewById <TextView>(Resource.Id.toolbar_title);
                li_toolbarDetails.Click   += Li_toolbarDetails_Click;
                IV_Userlogo.Click         += IV_Userlogo_Click;
                toolbar_title.Click       += Toolbar_title_Click;
                _IVarrow_back_white.Click += _IVarrow_back_white_Click;



                // Create your application here
                ContactObject = JsonConvert.DeserializeObject <ContactViewModel>(Intent.GetStringExtra("ContactObject"));
                if (ContactObject != null)
                {
                    ContactName        = ContactRepository.GetContactbyUserId(ContactObject.ContactId).name;
                    contactId          = ContactObject.ContactId;
                    toolbar_title.Text = ContactName;
                }

                if (ContactObject.ProfileImageUrl != null && ContactObject.ProfileImageUrl != "")
                {
                    Picasso.With(this)
                    .Load(ContactObject.ProfileImageUrl)
                    .Resize(100, 100)
                    .CenterCrop().Placeholder(Resource.Drawable.default_profile)
                    .Into(IV_Userlogo);
                }
                else
                {
                    IV_Userlogo.SetBackgroundResource(Resource.Drawable.default_profile);
                }

                txtChatSendMessage              = FindViewById <EditText>(Resource.Id.txtSendMessage);
                btnAttachment                   = (Button)FindViewById(Resource.Id.btnextra_msg);
                btnSendButton                   = FindViewById <Button>(Resource.Id.btnsend_msg);
                ButtonLayout                    = FindViewById <LinearLayout>(Resource.Id.linearLayout3);
                txtChatSendMessage.TextChanged += TxtChatSendMessage_TextChanged;

                mySwipeRefreshLayout = FindViewById <SwipeRefreshLayout>(Resource.Id.swiperefresh);
                var refreshListEvent = new SwipeRefreshLayoutPrivateMessage(this);
                mySwipeRefreshLayout.SetOnRefreshListener(refreshListEvent);
                refreshListEvent.LoadMoreEvent += RefreshListEvent_LoadMoreEvent;

                mRecyclerView = FindViewById <RecyclerView>(Resource.Id.recyclerView);
                if (mRecyclerView != null)
                {
                    mRecyclerView.HasFixedSize = true;
                    var layoutManager = new LinearLayoutManager(this);
                    mRecyclerView.SetLayoutManager(layoutManager);
                }

                imgCamera_msg = FindViewById <ImageView>(Resource.Id.imgCamera_msg);

                chatConversation = ChatConversationRepository.GetConversationbyContactId(ContactObject.ContactId);

                btnSendButton.Click += btnSendButton_Click;
                btnAttachment.Click += BtnAttachment_Click;
                imgCamera_msg.Click += ImgCamera_msg_Click;
            }
            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);
            }
        }