示例#1
0
        //Get General Data Using Api >> notifications , pro_users , promoted_pages , trending_hashtag
        public async Task <(string, string, string)> Get_GeneralData_Api(bool seenNotifications)
        {
            try
            {
                if (!IMethods.CheckConnectivity())
                {
                    swipeRefreshLayout.Refreshing = false;
                    Toast.MakeText(Context, Context.GetString(Resource.String.Lbl_CheckYourInternetConnection),
                                   ToastLength.Short).Show();
                }
                else
                {
                    // Get General Data : notifications , pro_users , promoted_pages , trending_hashtag
                    //==========================================
                    var(api_status, respond) = await Client.Global.Get_General_Data(seenNotifications, UserDetails.Device_ID);

                    if (api_status == 200)
                    {
                        if (respond is Get_General_Data_Object result)
                        {
                            this.Activity.RunOnUiThread(() =>
                            {
                                // Notifications
                                if (result.notifications.Length > 0)
                                {
                                    if (NotifyAdapter.mNotificationsList.Count > 0)
                                    {
                                        //Bring new users
                                        var listnew = result.notifications.Where(c => !NotifyAdapter.mNotificationsList.Select(fc => fc.notifier_id).Contains(c.notifier_id)).ToList();
                                        if (listnew.Count > 0)
                                        {
                                            //Results differ
                                            Classes.AddRange(NotifyAdapter.mNotificationsList, listnew);
                                            NotifyAdapter.BindEnd();
                                        }
                                    }
                                    else
                                    {
                                        NotifyAdapter.mNotificationsList = new ObservableCollection <Get_General_Data_Object.Notification>(result.notifications);
                                        NotifyAdapter.BindEnd();
                                    }
                                }

                                // Friend Requests
                                if (result.friend_requests.Length > 0)
                                {
                                    FriendRequestsAdapter.mFriendRequestsList = new ObservableCollection <Get_General_Data_Object.Friend_Requests>(result.friend_requests);

                                    ContextTab.Trending_Tab.layout_FriendRequest.Visibility = ViewStates.Visible;
                                    try
                                    {
                                        for (var i = 0; i < 4; i++)
                                        {
                                            if (i == 0)
                                            {
                                                ImageServiceLoader.Load_Image(ContextTab.Trending_Tab.FriendRequestimage1,
                                                                              "no_profile_image.png",
                                                                              FriendRequestsAdapter.mFriendRequestsList[i].avatar, 1);
                                            }
                                            else if (i == 1)
                                            {
                                                ImageServiceLoader.Load_Image(ContextTab.Trending_Tab.FriendRequestimage2,
                                                                              "no_profile_image.png",
                                                                              FriendRequestsAdapter.mFriendRequestsList[i].avatar, 1);
                                            }
                                            else if (i == 2)
                                            {
                                                ImageServiceLoader.Load_Image(ContextTab.Trending_Tab.FriendRequestimage3,
                                                                              "no_profile_image.png",
                                                                              FriendRequestsAdapter.mFriendRequestsList[i].avatar, 1);
                                            }
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        Crashes.TrackError(e);
                                    }
                                }
                                else
                                {
                                    ContextTab.Trending_Tab.layout_FriendRequest.Visibility = ViewStates.Gone;
                                }

                                if (Settings.Show_ProUsers_Members)
                                {
                                    // Pro Users
                                    if (result.pro_users.Length > 0)
                                    {
                                        int countList = ContextTab.ProUsersAdapter.mProUsersList.Count();

                                        if (countList > 0)
                                        {
                                            foreach (var item in result.pro_users)
                                            {
                                                var check = ContextTab.ProUsersAdapter.mProUsersList.FirstOrDefault(a => a.user_id == item.user_id);
                                                if (check == null)
                                                {
                                                    ContextTab.ProUsersAdapter.mProUsersList.Add(item);
                                                }
                                            }

                                            ContextTab.ProUsersAdapter.NotifyItemRangeChanged(countList, ContextTab.ProUsersAdapter.mProUsersList.Count - 1);
                                            ContextTab.Trending_Tab.ProRecylerView.ScrollToPosition(ContextTab.ProUsersAdapter.mProUsersList.Count - 1);
                                            if (ContextTab.ProUsersAdapter.mProUsersList.Count() >= 40)
                                            {
                                                try
                                                {
                                                    ContextTab.ProUsersAdapter.mProUsersList.RemoveAt(32);
                                                    ContextTab.ProUsersAdapter.mProUsersList.RemoveAt(33);
                                                    ContextTab.ProUsersAdapter.mProUsersList.RemoveAt(34);
                                                    ContextTab.ProUsersAdapter.mProUsersList.RemoveAt(35);
                                                    ContextTab.ProUsersAdapter.mProUsersList.RemoveAt(36);
                                                    ContextTab.ProUsersAdapter.mProUsersList.RemoveAt(37);
                                                    ContextTab.ProUsersAdapter.mProUsersList.RemoveAt(38);
                                                    ContextTab.ProUsersAdapter.mProUsersList.RemoveAt(39);
                                                    ContextTab.ProUsersAdapter.mProUsersList.RemoveAt(40);

                                                    ContextTab.ProUsersAdapter.NotifyItemRangeRemoved(0, 8);
                                                }
                                                catch (Exception e)
                                                {
                                                    Console.WriteLine(e);
                                                    throw;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            ContextTab.ProUsersAdapter.mProUsersList = new ObservableCollection <Get_General_Data_Object.Pro_Users>(result.pro_users);
                                        }


                                        if (ContextTab.Trending_Tab.layout_SuggestionProUsers.Visibility != ViewStates.Visible)
                                        {
                                            ContextTab.Trending_Tab.layout_SuggestionProUsers.Visibility = ViewStates.Visible;
                                        }
                                    }
                                    else
                                    {
                                        if (ContextTab.Trending_Tab.layout_SuggestionProUsers.Visibility != ViewStates.Gone)
                                        {
                                            ContextTab.Trending_Tab.layout_SuggestionProUsers.Visibility = ViewStates.Gone;
                                        }
                                    }
                                }
                                else
                                {
                                    ContextTab.Trending_Tab.layout_SuggestionProUsers.Visibility = ViewStates.Gone;
                                }


                                if (Settings.Show_Promoted_Pages)
                                {
                                    // Pro Pages
                                    if (result.promoted_pages.Length > 0)
                                    {
                                        ProPagesAdapter.mProPagesList =
                                            new ObservableCollection <Get_General_Data_Object.Promoted_Pages>(
                                                result.promoted_pages);
                                        ProPagesAdapter.BindEnd();
                                        ContextTab.Trending_Tab.PageRecylerView.SetLayoutManager(
                                            new LinearLayoutManager(Activity, LinearLayoutManager.Horizontal, false));
                                        ContextTab.Trending_Tab.PageRecylerView.SetAdapter(ProPagesAdapter);

                                        ContextTab.Trending_Tab.layout_SuggestionPromotedPage.Visibility = ViewStates.Visible;
                                    }
                                    else
                                    {
                                        ContextTab.Trending_Tab.layout_SuggestionPromotedPage.Visibility = ViewStates.Gone;
                                    }
                                }
                                else
                                {
                                    ContextTab.Trending_Tab.layout_SuggestionPromotedPage.Visibility = ViewStates.Gone;
                                }

                                if (Settings.Show_Trending_Hashtags)
                                {
                                    if (result.trending_hashtag.Length > 0)
                                    {
                                        HashtagUserAdapter.mHashtagList =
                                            new ObservableCollection <Get_General_Data_Object.Trending_Hashtag>(
                                                result.trending_hashtag);
                                    }
                                }

                                if (swipeRefreshLayout != null && swipeRefreshLayout.Refreshing)
                                {
                                    swipeRefreshLayout.Refreshing = false;
                                }

                                //Show Empty Page
                                if (NotifyAdapter.mNotificationsList.Count > 0)
                                {
                                    Notifcation_Recyler.Visibility = ViewStates.Visible;
                                    notifications_Empty.Visibility = ViewStates.Gone;
                                }
                                else
                                {
                                    Notifcation_Recyler.Visibility = ViewStates.Gone;
                                    notifications_Empty.Visibility = ViewStates.Visible;
                                }
                            });
                            return(result.new_notifications_count, result.new_friend_requests_count, result.count_new_messages);
                        }
                    }
                    else if (api_status == 400)
                    {
                        if (respond is Error_Object error)
                        {
                            var errortext = error._errors.Error_text;
                            //Toast.MakeText(this.Context, errortext, ToastLength.Short).Show();

                            if (errortext.Contains("Invalid or expired access_token"))
                            {
                                API_Request.Logout(Activity);
                            }
                        }

                        if (swipeRefreshLayout != null && swipeRefreshLayout.Refreshing)
                        {
                            swipeRefreshLayout.Refreshing = false;
                        }


                        return("", "", "");
                    }
                    else if (api_status == 404)
                    {
                        var error = respond.ToString();
                        //Toast.MakeText(this.Context, error, ToastLength.Short).Show();

                        if (swipeRefreshLayout != null && swipeRefreshLayout.Refreshing)
                        {
                            swipeRefreshLayout.Refreshing = false;
                        }

                        return("", "", "");
                    }
                }

                this.Activity.RunOnUiThread(() =>
                {
                    //Show Empty Page
                    if (NotifyAdapter.mNotificationsList.Count > 0)
                    {
                        Notifcation_Recyler.Visibility = ViewStates.Visible;
                        notifications_Empty.Visibility = ViewStates.Gone;
                    }
                    else
                    {
                        Notifcation_Recyler.Visibility = ViewStates.Gone;
                        notifications_Empty.Visibility = ViewStates.Visible;
                    }

                    if (swipeRefreshLayout != null && swipeRefreshLayout.Refreshing)
                    {
                        swipeRefreshLayout.Refreshing = false;
                    }
                });


                return("", "", "");
            }
            catch (Exception e)
            {
                Crashes.TrackError(e);
                await Get_GeneralData_Api(seenNotifications);

                return("", "", "");
            }
        }