public override async Task UpdateBusinessesListExecute()
        {
            await base.UpdateBusinessesListExecute();

            var tempBusiness = new List <Business>(BusinessesList.Where(b => b.IsFeatured));

            BusinessesList.Clear();
            foreach (Business business in tempBusiness)
            {
                BusinessesList.Add(business);
            }
        }
Exemple #2
0
        private async Task UpdateBusinessesListExecute()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;
            try
            {
                // tentar ir buscar à cache primeiro
                SemaphoreSlim semaphore = (Application.Current as App).semaphore;
                await semaphore.WaitAsync();

                if (((App)Application.Current).CachedBusinesses == null)
                {
                    Debug.WriteLine("Getting from db");
                    // Get Businesses and set to cache, otherwise use cached
                    var businesses = await Task.Run(GetBusinesses);

                    ((App)Application.Current).CachedBusinesses = new List <Business>(businesses);
                }
                else
                {
                    Debug.WriteLine("Using cached");
                }
                semaphore.Release();
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);

                if (status == PermissionStatus.Granted)
                {
                    Location userLocation = await((App)Application.Current).GetLocationAsync();
                    if (((App)Application.Current).LocationEnabled)
                    {
                        foreach (Business business in ((App)Application.Current).CachedBusinesses)
                        {
                            business.Distance =
                                Location.CalculateDistance(business.Location, userLocation, DistanceUnits.Kilometers);
                        }
                        DisplayLocationError = false;
                    }
                }
                else
                {
                    DisplayLocationError = true;
                }

                // Needs to be in main, UI thread
                await Device.InvokeOnMainThreadAsync(() =>
                {
                    BusinessesList.Clear();
                    foreach (Business business in (Application.Current as App).CachedBusinesses)
                    {
                        BusinessesList.Add(business);
                    }
                });
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
            }
            finally
            {
                IsBusy = false;
            }
        }