private async Task LoadPokes() { if (Methods.CheckConnectivity()) { var countList = MAdapter.PokeList.Count; var(apiStatus, respond) = await RequestsAsync.Global.FetchPokeAsync(); switch (apiStatus) { case 200: { switch (respond) { case PokeObject result when countList > 0: { var list = result.Data.Where(a => a.UserData?.UserDataClass != null).ToList(); foreach (var item in from item in list let check = MAdapter.PokeList.FirstOrDefault(a => a.Id == item.Id) where check == null select item) { MAdapter.PokeList.Add(item); } RunOnUiThread(() => { MAdapter.NotifyItemRangeInserted(countList, MAdapter.PokeList.Count - countList); }); break; } case PokeObject result: { var list = result.Data.Where(a => a.UserData?.UserDataClass != null).ToList(); MAdapter.PokeList = new ObservableCollection <PokeObject.Datum>(list); RunOnUiThread(() => { MAdapter.NotifyDataSetChanged(); }); break; } } break; } default: Methods.DisplayReportResult(this, respond); break; } RunOnUiThread(ShowEmptyPage); } else { Inflated = EmptyStateLayout.Inflate(); EmptyStateInflater x = new EmptyStateInflater(); x.InflateLayout(Inflated, EmptyStateInflater.Type.NoConnection); switch (x.EmptyStateButton.HasOnClickListeners) { case false: x.EmptyStateButton.Click += null !; x.EmptyStateButton.Click += EmptyStateButtonOnClick; break; } Toast.MakeText(this, GetString(Resource.String.Lbl_CheckYourInternetConnection), ToastLength.Short)?.Show(); } }
private async Task LoadPokes() { try { if (Methods.CheckConnectivity()) { int countList = MAdapter.PokeList.Count; (int apiStatus, var respond) = await RequestsAsync.Global.FetchPoke(); if (apiStatus == 200) { if (respond is PokeObject result) { if (countList > 0) { var list = result.Data.Where(a => a.UserData?.UserDataClass != null).ToList(); foreach (var item in from item in list let check = MAdapter.PokeList.FirstOrDefault(a => a.Id == item.Id) where check == null select item) { MAdapter.PokeList.Add(item); } RunOnUiThread(() => { MAdapter.NotifyItemRangeInserted(countList, MAdapter.PokeList.Count - countList); }); } else { var list = result.Data.Where(a => a.UserData?.UserDataClass != null).ToList(); MAdapter.PokeList = new ObservableCollection <PokeObject.Datum>(list); RunOnUiThread(() => { MAdapter.NotifyDataSetChanged(); }); } } } else { Methods.DisplayReportResult(this, respond); } RunOnUiThread(ShowEmptyPage); } else { Inflated = EmptyStateLayout.Inflate(); EmptyStateInflater x = new EmptyStateInflater(); x.InflateLayout(Inflated, EmptyStateInflater.Type.NoConnection); if (!x.EmptyStateButton.HasOnClickListeners) { x.EmptyStateButton.Click += null; x.EmptyStateButton.Click += EmptyStateButtonOnClick; } Toast.MakeText(this, GetString(Resource.String.Lbl_CheckYourInternetConnection), ToastLength.Short).Show(); } } catch (Exception e) { await LoadPokes(); Console.WriteLine(e); } }