//Api public async void Get_Contacts_APi() { try { if (!IMethods.CheckConnectivity()) { RunOnUiThread(() => { swipeRefreshLayout.Refreshing = false; }); Toast.MakeText(this, GetString(Resource.String.Lbl_CheckYourInternetConnection), ToastLength.Short) .Show(); } else { var lastIdUser = UserContactsList?.LastOrDefault()?.user_id ?? "0"; var(api_status, respond) = await API_Request.Get_users_friends_Async(lastIdUser); if (api_status == 200) { if (respond is Classes.UserContacts.Rootobject result) { RunOnUiThread(() => { if (result.users.Length <= 0) { } else if (result.users.Length > 0) { var listNew = result.users?.Where(c => !UserContactsList.Select(fc => fc.user_id).Contains(c.user_id)).ToList(); if (listNew.Count > 0) { Classes.AddRange(UserContactsList, listNew); var listOrder = new JavaList <Classes.UserContacts.User>(UserContactsList.OrderBy(a => a.name)); if (MyContactsAdapter == null) { //Results differ MyContactsAdapter = new MyContacts_Adapter(this, listOrder, ContactsRecyler); ContactsRecyler.SetAdapter(MyContactsAdapter); MyContactsAdapter.ItemClick += MyContactsAdapter_OnItemClick; var lastCountItem = MyContactsAdapter.ItemCount; MyContactsAdapter.NotifyItemRangeInserted(lastCountItem, listNew.Count); } else { MyContactsAdapter.mMyContactsList = new JavaList <Classes.UserContacts.User>(listOrder); var lastCountItem = MyContactsAdapter.ItemCount; MyContactsAdapter.NotifyItemRangeInserted(lastCountItem, listNew.Count); } //Insert Or Update All data UsersContact to database var dbDatabase = new SqLiteDatabase(); dbDatabase.Insert_Or_Replace_MyContactTable(UserContactsList); dbDatabase.Dispose(); } else { if (ShowSnackbarNoMore) { Snackbar.Make(ContactsRecyler, GetText(Resource.String.Lbl_No_have_more_users), Snackbar.LengthLong).Show(); ShowSnackbarNoMore = false; } } if (swipeRefreshLayout != null) { swipeRefreshLayout.Refreshing = false; } } }); } } else if (api_status == 400) { if (respond is Error_Object error) { var errortext = error._errors.Error_text; //Toast.MakeText(this, errortext, ToastLength.Short).Show(); if (errortext.Contains("Invalid or expired access_token")) { API_Request.Logout(this); } } } else if (api_status == 404) { var error = respond.ToString(); //Toast.MakeText(this, error, ToastLength.Short).Show(); } } //Show Empty Page >> //=============================================================== RunOnUiThread(() => { if (UserContactsList?.Count > 0) { Contacts_Empty.Visibility = ViewStates.Gone; ContactsRecyler.Visibility = ViewStates.Visible; } else { Contacts_Empty.Visibility = ViewStates.Visible; ContactsRecyler.Visibility = ViewStates.Gone; } swipeRefreshLayout.Refreshing = false; //Set Event Scroll if (OnMainScrolEvent == null) { var xamarinRecyclerViewOnScrollListener = new XamarinRecyclerViewOnScrollListener(ContactsLayoutManager, swipeRefreshLayout); OnMainScrolEvent = xamarinRecyclerViewOnScrollListener; OnMainScrolEvent.LoadMoreEvent += MyContact_OnScroll_OnLoadMoreEvent; ContactsRecyler.AddOnScrollListener(OnMainScrolEvent); ContactsRecyler.AddOnScrollListener(new ScrollDownDetector()); } else { OnMainScrolEvent.IsLoading = false; } }); } catch (Exception e) { Crashes.TrackError(e); Get_Contacts_APi(); } }
public void Get_MyContact(int lastId = 0) { try { RunOnUiThread(() => { if (Settings.ConnectivitySystem == "1") // Following { SupportActionBar.Title = GetText(Resource.String.Lbl_Following); } else // Friend { SupportActionBar.Title = GetText(Resource.String.Lbl_Friends); } }); //Get All User From Database var dbDatabase = new SqLiteDatabase(); var localList = dbDatabase.Get_MyContact(lastId, 25); if (localList != null) { RunOnUiThread(() => { var list = new JavaList <Classes.UserContacts.User>(localList); if (list.Count > 0) { var listNew = list?.Where(c => !UserContactsList.Select(fc => fc.user_id).Contains(c.user_id)).ToList(); if (listNew.Count > 0) { Classes.AddRange(UserContactsList, listNew); var listOrder = new JavaList <Classes.UserContacts.User>(UserContactsList.OrderBy(a => a.name)); if (MyContactsAdapter == null) { //Results differ MyContactsAdapter = new MyContacts_Adapter(this, listOrder, ContactsRecyler); ContactsRecyler.SetAdapter(MyContactsAdapter); MyContactsAdapter.ItemClick += MyContactsAdapter_OnItemClick; var lastCountItem = MyContactsAdapter.ItemCount; MyContactsAdapter.NotifyItemRangeInserted(lastCountItem, listNew.Count); } else { MyContactsAdapter.mMyContactsList = new JavaList <Classes.UserContacts.User>(listOrder); var lastCountItem = MyContactsAdapter.ItemCount; MyContactsAdapter.NotifyItemRangeInserted(lastCountItem, listNew.Count); } } else { if (ShowSnackbar) { Snackbar.Make(ContactsRecyler, GetText(Resource.String.Lbl_Loading_From_Server), Snackbar.LengthLong).Show(); ShowSnackbar = false; } Get_Contacts_APi(); } if (swipeRefreshLayout != null) { swipeRefreshLayout.Refreshing = false; } } else { if (ShowSnackbar) { Snackbar.Make(ContactsRecyler, GetText(Resource.String.Lbl_Loading_From_Server), Snackbar.LengthLong).Show(); ShowSnackbar = false; } Get_Contacts_APi(); } //Set Event Scroll if (OnMainScrolEvent == null) { var xamarinRecyclerViewOnScrollListener = new XamarinRecyclerViewOnScrollListener(ContactsLayoutManager, swipeRefreshLayout); OnMainScrolEvent = xamarinRecyclerViewOnScrollListener; OnMainScrolEvent.LoadMoreEvent += MyContact_OnScroll_OnLoadMoreEvent; ContactsRecyler.AddOnScrollListener(OnMainScrolEvent); ContactsRecyler.AddOnScrollListener(new ScrollDownDetector()); } else { OnMainScrolEvent.IsLoading = false; } }); } else { if (ShowSnackbar) { Snackbar.Make(ContactsRecyler, GetText(Resource.String.Lbl_Loading_From_Server), Snackbar.LengthLong).Show(); ShowSnackbar = false; } Get_Contacts_APi(); } dbDatabase.Dispose(); if (UserContactsList?.Count <= 24 || UserContactsList?.Count == 0) { swipeRefreshLayout.Refreshing = true; Get_Contacts_APi(); } } catch (Exception e) { Crashes.TrackError(e); } }