public CoordinatesForAdvertisementsModel GetCoordinatesModel(int maxDistance = -1)
        {
            CoordinatesForAdvertisementsModel coordinatesModel = new CoordinatesForAdvertisementsModel();
            var location      = GetLocation();
            var settingsMOdel = (AppSettingsModel)sharedPreferencesHelper.GetSharedPreference <AppSettingsModel>(SharedPreferencesKeys.APP_SETTINGS);

            if (location == null || latitude == 0 || longitude == 0)
            {
                AlertsService.ShowLongToast(this.mContext, "Nie mogê ustaliæ aktualnej lokalizacji. W³¹cz lokalizacjê w ustawieniach telefonu.");
                throw new Exception("Brak lokalizacji");
            }
            else
            {
                coordinatesModel.Latitude  = latitude;
                coordinatesModel.Longitude = longitude;
                if (maxDistance > -1)
                {
                    coordinatesModel.MaxDistance = maxDistance;
                }
                else if (settingsMOdel != null)
                {
                    coordinatesModel.MaxDistance = settingsMOdel.LocationSettings.MaxDistance;
                }
                else
                {
                    coordinatesModel.MaxDistance = ValueConsts.MAX_DISTANCE_VALUE;
                }
            }

            return(coordinatesModel);
        }
        public async Task <IEnumerable <AdvertisementItemShort> > GetUserAdvertisements(string userId, int pageNumber, double lat, double lon)
        {
            var coordinatesModel = new CoordinatesForAdvertisementsModel {
                Latitude = lat, Longitude = lon
            };
            var advertisementsFromDb = this.advertisementItemDbService.GetUserAdvertisements(userId, pageNumber).ToList();
            IEnumerable <AdvertisementItemShort> advertisementsViewModels = await MapDbModelsToShortViewModels(advertisementsFromDb, coordinatesModel);

            return(advertisementsViewModels);
        }
        private async Task CheckNewAdvertisementsAroundUserHomeLocation(CoordinatesForAdvertisementsModel currentLocationCoordinates)
        {
            if (appsettings == null && appsettings.LocationSettings.Latitude == 0.00D)
            {
                //nic nie robie
                return;
            }
            SetSearchModel(appsettings);
            var areThereNewAdvertisements = await this.advertisementItemService.CheckForNewAdvertisementsAroundCurrentLocationSinceLastCheck(searchModelForNotifications);

            if (areThereNewAdvertisements)
            {
                if (!LocationsAreSimiliar(currentLocationCoordinates, appsettings.LocationSettings))
                {
                    NotifyUserAboutNewAdvertisements(AdvertisementsKind.AdvertisementsArounUserHomeLocation);
                }
            }
        }
Ejemplo n.º 4
0
        private bool LocationsAreSimiliar(CoordinatesForAdvertisementsModel currentLocationCoordinates, CoordinatesForAdvertisementsModel homeLocationCoordinates)
        {
            double positiveDifferenceKilometers = 0.0111 * 2;
            double negativeDifferenceKilometers = positiveDifferenceKilometers - (positiveDifferenceKilometers * 2);
            var    latDifference = currentLocationCoordinates.Latitude - homeLocationCoordinates.Latitude;
            var    lonDifference = currentLocationCoordinates.Longitude - homeLocationCoordinates.Longitude;

            if ((latDifference > 0 && (latDifference > positiveDifferenceKilometers)) ||
                (latDifference < 0 && (latDifference < negativeDifferenceKilometers)) ||
                (lonDifference > 0 && (lonDifference > positiveDifferenceKilometers)) ||
                (lonDifference < 0 && (lonDifference < negativeDifferenceKilometers)))
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
        private async Task <IEnumerable <AdvertisementItemShort> > MapDbModelsToShortViewModels(IEnumerable <AdvertisementItem> advertisementsFromDb, CoordinatesForAdvertisementsModel coordinatesModel = null)
        {
            var viewModelsList = new List <AdvertisementItemShort>();

            foreach (var dbModel in advertisementsFromDb)
            {
                var viewModel = new AdvertisementItemShort();
                viewModel.Id   = dbModel.Id;
                viewModel.Size = dbModel.Size;
                viewModel.AdvertisementTitle = dbModel.Title;
                viewModel.AdvertisementPrice = dbModel.Price;
                viewModel.MainPhoto          = await this.photosService.GetAdvertisementMinPhotoInBytes(dbModel.AdvertisementPhotos.FirstOrDefault(p => p.IsMainPhoto).PhotoName);

                if (coordinatesModel != null)
                {
                    viewModel.Distance = this.coordinatesCalculator.GetDistanceBetweenTwoLocalizations(coordinatesModel.Latitude, coordinatesModel.Longitude, dbModel.Latitude, dbModel.Longitude);
                }
                else
                {
                    viewModel.Distance = 0.0D;
                }
                viewModel.IsSellerOnline = this.chatHubCacheService.IsUserConnected(dbModel.UserId);
                viewModel.IsOnlyForSell  = dbModel.IsOnlyForSell;
                viewModel.IsExpired      = dbModel.ExpirationDate < DateTime.Now;
                viewModelsList.Add(viewModel);
            }

            return(viewModelsList);
        }