Ejemplo n.º 1
0
        //Get Messages Local Or Api
        private void Get_Messages()
        {
            try
            {
                BeforeMessageId = 0;
                MAdapter.MessageList.Clear();
                MAdapter.NotifyDataSetChanged();

                SqLiteDatabase dbDatabase = new SqLiteDatabase();
                var            localList  = dbDatabase.GetMessagesList(UserDetails.UserId, Userid, BeforeMessageId);
                if (localList == "1") //Database.. Get Messages Local
                {
                    MAdapter.NotifyDataSetChanged();

                    //Scroll Down >>
                    ChatBoxRecyclerView.ScrollToPosition(MAdapter.MessageList.Count - 1);
                    SwipeRefreshLayout.Refreshing = false;
                    SwipeRefreshLayout.Enabled    = false;
                }
                else //Or server.. Get Messages Api
                {
                    SwipeRefreshLayout.Refreshing = true;
                    SwipeRefreshLayout.Enabled    = true;
                    GetMessages_API();
                }

                //Set Event Scroll
                XamarinRecyclerViewOnScrollListener onScrollListener = new XamarinRecyclerViewOnScrollListener(MLayoutManager, SwipeRefreshLayout);
                onScrollListener.LoadMoreEvent += Messages_OnScroll_OnLoadMoreEvent;
                ChatBoxRecyclerView.AddOnScrollListener(onScrollListener);
                TaskWork = "Working";

                //Run timer
                Timer = new Timer {
                    Interval = AppSettings.MessageRequestSpeed, Enabled = true
                };
                Timer.Elapsed += TimerOnElapsed_MessageUpdater;
                Timer.Start();

                dbDatabase.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Ejemplo n.º 2
0
        protected override void OnDestroy()
        {
            try
            {
                if (Timer != null)
                {
                    Timer.Enabled = false;
                    Timer.Stop();
                    Timer = null;
                }

                MAdapter?.MessageList.Clear();
                MAdapter?.NotifyDataSetChanged();

                RewardedVideoAd?.OnDestroy(this);

                base.OnDestroy();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
Ejemplo n.º 3
0
        private async void MessageUpdater()
        {
            try
            {
                if (TaskWork == "Working")
                {
                    TaskWork = "Stop";

                    if (!Methods.CheckConnectivity())
                    {
                        SwipeRefreshLayout.Refreshing = false;
                        Toast.MakeText(this, Resource.String.Lbl_CheckYourInternetConnection, ToastLength.Long).Show();
                    }
                    else
                    {
                        int    countList = MAdapter.MessageList.Count;
                        string afterId   = MAdapter.MessageList.LastOrDefault()?.Id ?? "";
                        var(apiStatus, respond) = await RequestsAsync.Messages.GetUserMessages(Userid, "30", "", afterId);

                        if (apiStatus == 200)
                        {
                            if (respond is GetUserMessagesObject result)
                            {
                                int responseList = result.data.Messages.Count;
                                if (responseList > 0)
                                {
                                    if (countList > 0)
                                    {
                                        foreach (var item in result.data.Messages)
                                        {
                                            var check = MAdapter.MessageList.FirstOrDefault(a => a.Id == item.Id);
                                            if (check == null)
                                            {
                                                MAdapter.Add(item);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        MAdapter.MessageList = new ObservableCollection <GetUserMessagesObject.Message>(result.data.Messages);
                                    }

                                    RunOnUiThread(() =>
                                    {
                                        try
                                        {
                                            var lastCountItem = MAdapter.ItemCount;
                                            if (countList > 0)
                                            {
                                                MAdapter.NotifyItemRangeInserted(lastCountItem, MAdapter.MessageList.Count - 1);
                                            }
                                            else
                                            {
                                                MAdapter.NotifyDataSetChanged();
                                            }

                                            //Insert to DataBase
                                            SqLiteDatabase dbDatabase = new SqLiteDatabase();
                                            dbDatabase.InsertOrReplaceMessages(MAdapter.MessageList);
                                            dbDatabase.Dispose();

                                            //Scroll Down >>
                                            ChatBoxRecylerView.ScrollToPosition(MAdapter.MessageList.Count - 1);

                                            var lastMessage = MAdapter.MessageList.LastOrDefault();
                                            if (lastMessage != null)
                                            {
                                                var dataUser = LastChatActivity.MAdapter?.UserList?.FirstOrDefault(a => a.User.Id == lastMessage.FromId);
                                                if (dataUser != null)
                                                {
                                                    dataUser.GetLastMessage.Text = lastMessage.Text;

                                                    LastChatActivity.MAdapter.Move(dataUser);
                                                    LastChatActivity.MAdapter.Update(dataUser);
                                                }
                                                if (AppSettings.RunSoundControl)
                                                {
                                                    Methods.AudioRecorderAndPlayer.PlayAudioFromAsset("Popup_GetMesseges.mp3");
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e);
                                        }
                                    });
                                }
                            }
                        }
                        else
                        {
                            Methods.DisplayReportResult(this, respond);
                        }
                    }
                    TaskWork = "Working";
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }