Esempio n. 1
0
        private async Task <List <ServiceVisitViewModel> > CalculateDistanceToCurrentLocationAsync(ObservableCollection <ServiceVisitViewModel> visits, Boolean isAsc)
        {
            var currentLocation = await LocationHelper.CurrentPositionAsync();

            var currentLatitude  = currentLocation.Latitude;
            var currentLongitude = currentLocation.Longitude;

            var result = new List <ServiceVisitViewModel>();

            if (currentLatitude.Equals(0) && currentLongitude.Equals(0))
            {
                this.SortListViewModel.RemoveSortSetting();
                await Application.Current.MainPage.DisplayAlert("Location Service Alert", "You need to turn on or give permission to your location service to enable sorting by location", "Ok");

                result = visits.ToList();
            }
            else
            {
                foreach (ServiceVisitViewModel viewModel in visits)
                {
                    viewModel.DistanceToCurrentLoc = LocationHelper.getDistanceFromLatLonInKmd(currentLatitude, currentLongitude, viewModel.PositionLat, viewModel.PostionLong);
                }


                if (isAsc)
                {
                    result = visits.OrderBy(o => o.DistanceToCurrentLoc).ToList();
                }
                else
                {
                    result = visits.OrderByDescending(o => o.DistanceToCurrentLoc).ToList();
                }
            }
            this.EnableSortIcon();

            return(result);
        }